人人商城

refund.mod.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. function refund_order_can_refund($module, $tid) {
  8. global $_W;
  9. $paylog = pdo_get('core_paylog', array('uniacid' => $_W['uniacid'], 'tid' => $tid, 'module' => $module));
  10. if (empty($paylog)) {
  11. return error(1, '订单不存在');
  12. }
  13. if ($paylog['status'] != 1) {
  14. return error(1, '此订单还未支付成功不可退款');
  15. }
  16. $refund_amount = pdo_getcolumn('core_refundlog', array('uniacid' => $_W['uniacid'], 'status' => 1, 'uniontid' => $paylog['uniontid']), 'SUM(fee)');
  17. if ($refund_amount >= $paylog['card_fee']) {
  18. return error(1, '订单已退款成功');
  19. }
  20. return true;
  21. }
  22. function refund_create_order($tid, $module, $fee = 0, $reason = '') {
  23. load()->classs('pay');
  24. load()->model('module');
  25. global $_W;
  26. $order_can_refund = refund_order_can_refund($module, $tid);
  27. if (is_error($order_can_refund)) {
  28. return $order_can_refund;
  29. }
  30. $module_info = module_fetch($module);
  31. $moduleid = empty($module_info['mid']) ? '000000' : sprintf("%06d", $module_info['mid']);
  32. $refund_uniontid = date('YmdHis') . $moduleid . random(8,1);
  33. $paylog = pdo_get('core_paylog', array('uniacid' => $_W['uniacid'], 'tid' => $tid, 'module' => $module));
  34. $refund = array (
  35. 'uniacid' => $_W['uniacid'],
  36. 'uniontid' => $paylog['uniontid'],
  37. 'fee' => empty($fee) ? $paylog['card_fee'] : $fee,
  38. 'status' => 0,
  39. 'refund_uniontid' => $refund_uniontid,
  40. 'reason' => $reason
  41. );
  42. pdo_insert('core_refundlog', $refund);
  43. return pdo_insertid();
  44. }
  45. function refund($refund_id) {
  46. load()->classs('pay');
  47. global $_W;
  48. $refundlog = pdo_get('core_refundlog', array('id' => $refund_id));
  49. $paylog = pdo_get('core_paylog', array('uniacid' => $_W['uniacid'], 'uniontid' => $refundlog['uniontid']));
  50. if ($paylog['type'] == 'wechat') {
  51. $refund_param = reufnd_wechat_build($refund_id);
  52. $wechat = Pay::create('wechat');
  53. $response = $wechat->refund($refund_param);
  54. unlink(ATTACHMENT_ROOT . $_W['uniacid'] . '_wechat_refund_all.pem');
  55. if (is_error($response)) {
  56. pdo_update('core_refundlog', array('status' => '-1'), array('id' => $refund_id));
  57. return $response;
  58. } else {
  59. return $response;
  60. }
  61. } elseif ($paylog['type'] == 'alipay') {
  62. $refund_param = reufnd_ali_build($refund_id);
  63. $ali = Pay::create('alipay');
  64. $response = $ali->refund($refund_param, $refund_id);
  65. if (is_error($response)) {
  66. pdo_update('core_refundlog', array('status' => '-1'), array('id' => $refund_id));
  67. return $response;
  68. } else {
  69. return $response;
  70. }
  71. }
  72. return error(1, '此订单退款方式不存在');
  73. }
  74. function reufnd_ali_build($refund_id) {
  75. global $_W;
  76. $setting = uni_setting_load('payment', $_W['uniacid']);
  77. $refund_setting = $setting['payment']['ali_refund'];
  78. if ($refund_setting['switch'] != 1) {
  79. return error(1, '未开启支付宝退款功能!');
  80. }
  81. if (empty($refund_setting['private_key'])) {
  82. return error(1, '缺少支付宝秘钥证书!');
  83. }
  84. $refundlog = pdo_get('core_refundlog', array('id' => $refund_id));
  85. $paylog = pdo_get('core_paylog', array('uniacid' => $_W['uniacid'], 'uniontid' => $refundlog['uniontid']));
  86. $refund_param = array(
  87. 'app_id' => $refund_setting['app_id'],
  88. 'method' => 'alipay.trade.refund',
  89. 'charset' => 'utf-8',
  90. 'sign_type' => 'RSA2',
  91. 'timestamp' => date('Y-m-d H:i:s'),
  92. 'version' => '1.0',
  93. 'biz_content' => array(
  94. 'out_trade_no' => $paylog['tid'],
  95. 'refund_amount' => $refundlog['fee'],
  96. 'refund_reason' => $refundlog['reason'],
  97. )
  98. );
  99. $refund_param['biz_content'] = json_encode($refund_param['biz_content']);
  100. return $refund_param;
  101. }
  102. function reufnd_wechat_build($refund_id) {
  103. global $_W;
  104. $setting = uni_setting_load('payment', $_W['uniacid']);
  105. $refund_setting = $setting['payment']['wechat_refund'];
  106. if ($refund_setting['switch'] != 1) {
  107. return error(1, '未开启微信退款功能!');
  108. }
  109. if (empty($refund_setting['key']) || empty($refund_setting['cert'])) {
  110. return error(1, '缺少微信证书!');
  111. }
  112. $refundlog = pdo_get('core_refundlog', array('id' => $refund_id));
  113. $paylog = pdo_get('core_paylog', array('uniacid' => $_W['uniacid'], 'uniontid' => $refundlog['uniontid']));
  114. $account = uni_fetch($_W['uniacid']);
  115. $refund_param = array(
  116. 'appid' => $account['key'],
  117. 'mch_id' => $setting['payment']['wechat']['mchid'],
  118. 'out_trade_no' => $refundlog['uniontid'],
  119. 'out_refund_no' => $refundlog['refund_uniontid'],
  120. 'total_fee' => $paylog['card_fee'] * 100,
  121. 'refund_fee' => $refundlog['fee'] * 100,
  122. 'nonce_str' => random(8),
  123. 'refund_desc' => $refundlog['reason']
  124. );
  125. if ($setting['payment']['wechat']['switch'] == PAYMENT_WECHAT_TYPE_SERVICE) {
  126. $refund_param['sub_mch_id'] = $setting['payment']['wechat']['sub_mch_id'];
  127. $refund_param['sub_appid'] = $account['key'];
  128. $proxy_account = uni_fetch($setting['payment']['wechat']['service']);
  129. $refund_param['appid'] = $proxy_account['key'];
  130. $refund_param['mch_id'] = $proxy_account['setting']['payment']['wechat_facilitator']['mchid'];
  131. }
  132. $cert = authcode($refund_setting['cert'], 'DECODE');
  133. $key = authcode($refund_setting['key'], 'DECODE');
  134. file_put_contents(ATTACHMENT_ROOT . $_W['uniacid'] . '_wechat_refund_all.pem', $cert . $key);
  135. return $refund_param;
  136. }