refund.mod.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. $params = array('tid' => $tid, 'module' => $module);
  10. $params['uniacid'] = $_W['uniacid'];
  11. $paylog = pdo_get('core_paylog', $params);
  12. if (empty($paylog)) {
  13. return error(1, '订单不存在');
  14. }
  15. if ($paylog['status'] != 1) {
  16. return error(1, '此订单还未支付成功不可退款');
  17. }
  18. $refund_params = array('status' => 1, 'uniontid' => $paylog['uniontid']);
  19. $refund_params['uniacid'] = $_W['uniacid'];
  20. $refund_amount = pdo_getcolumn('core_refundlog', $refund_params, 'SUM(fee)');
  21. if ($refund_amount >= $paylog['card_fee']) {
  22. return error(1, '订单已退款成功');
  23. }
  24. return true;
  25. }
  26. function refund_create_order($tid, $module, $fee = 0, $reason = '') {
  27. global $_W;
  28. load()->model('module');
  29. $order_can_refund = refund_order_can_refund($module, $tid);
  30. if (is_error($order_can_refund)) {
  31. return $order_can_refund;
  32. }
  33. $module_info = module_fetch($module);
  34. $moduleid = empty($module_info['mid']) ? '000000' : sprintf("%06d", $module_info['mid']);
  35. $refund_uniontid = date('YmdHis') . $moduleid . random(8, 1);
  36. $params = array('tid' => $tid, 'module' => $module);
  37. $params['uniacid'] = $_W['uniacid'];
  38. $paylog = pdo_get('core_paylog', $params);
  39. $uniacid = $_W['uniacid'];
  40. $refund = array(
  41. 'uniacid' => $uniacid,
  42. 'uniontid' => $paylog['uniontid'],
  43. 'fee' => empty($fee) ? $paylog['card_fee'] : number_format($fee, 2, '.', ''),
  44. 'status' => 0,
  45. 'refund_uniontid' => $refund_uniontid,
  46. 'reason' => safe_gpc_string($reason),
  47. );
  48. pdo_insert('core_refundlog', $refund);
  49. return pdo_insertid();
  50. }
  51. function refund($refund_id) {
  52. load()->classs('pay');
  53. global $_W;
  54. $refundlog = pdo_get('core_refundlog', array('id' => $refund_id));
  55. $params = array('uniontid' => $refundlog['uniontid']);
  56. $params['uniacid'] = $_W['uniacid'];
  57. $paylog = pdo_get('core_paylog', $params);
  58. if ($paylog['type'] == 'wechat' || $paylog['type'] == 'wxapp') {
  59. $refund_param = reufnd_wechat_build($refund_id);
  60. if (is_error($refund_param)) {
  61. return $refund_param;
  62. }
  63. $module = '';
  64. $cert_file = ATTACHMENT_ROOT . $_W['uniacid'] . md5(complex_authkey()) . '_wechat_refund_all.pem';
  65. $wechat = Pay::create('wechat', $module);
  66. $response = $wechat->refund($refund_param, $module);
  67. unlink($cert_file);
  68. if (is_error($response)) {
  69. pdo_update('core_refundlog', array('status' => '-1'), array('id' => $refund_id));
  70. return $response;
  71. } else {
  72. return $response;
  73. }
  74. } elseif ($paylog['type'] == 'alipay') {
  75. $refund_param = reufnd_ali_build($refund_id);
  76. if (is_error($refund_param)) {
  77. return $refund_param;
  78. }
  79. $module = '';
  80. $ali = Pay::create('alipay', $module);
  81. $response = $ali->refund($refund_param, $refund_id);
  82. if (is_error($response)) {
  83. pdo_update('core_refundlog', array('status' => '-1'), array('id' => $refund_id));
  84. return $response;
  85. } else {
  86. return $response;
  87. }
  88. }
  89. return error(1, '此订单退款方式不存在');
  90. }
  91. function reufnd_ali_build($refund_id) {
  92. global $_W;
  93. $setting = uni_setting_load('payment', $_W['uniacid']);
  94. $refund_setting = $setting['payment']['ali_refund'];
  95. if ($refund_setting['switch'] != 1) {
  96. return error(1, '未开启支付宝退款功能!');
  97. }
  98. if (empty($refund_setting['private_key'])) {
  99. return error(1, '缺少支付宝密钥证书!');
  100. }
  101. $refundlog = pdo_get('core_refundlog', array('id' => $refund_id));
  102. $uniacid = $_W['uniacid'];
  103. $paylog = pdo_get('core_paylog', array('uniacid' => $uniacid, 'uniontid' => $refundlog['uniontid']));
  104. $refund_param = array(
  105. 'app_id' => $refund_setting['app_id'],
  106. 'method' => 'alipay.trade.refund',
  107. 'charset' => 'utf-8',
  108. 'sign_type' => 'RSA2',
  109. 'timestamp' => date('Y-m-d H:i:s'),
  110. 'version' => '1.0',
  111. 'biz_content' => array(
  112. 'out_trade_no' => $refundlog['uniontid'],
  113. 'refund_amount' => $refundlog['fee'],
  114. 'refund_reason' => $refundlog['reason'],
  115. )
  116. );
  117. $refund_param['biz_content'] = json_encode($refund_param['biz_content']);
  118. return $refund_param;
  119. }
  120. function reufnd_wechat_build($refund_id) {
  121. global $_W;
  122. $setting = uni_setting_load('payment', $_W['uniacid']);
  123. $pay_setting = $setting['payment'];
  124. $refund_setting = $setting['payment']['wechat_refund'];
  125. if ($refund_setting['switch'] != 1) {
  126. return error(1, '未开启微信退款功能!');
  127. }
  128. if (empty($refund_setting['key']) || empty($refund_setting['cert'])) {
  129. return error(1, '缺少微信证书!');
  130. }
  131. $refundlog = pdo_get('core_refundlog', array('id' => $refund_id));
  132. $uniacid = $_W['uniacid'];
  133. $paylog = pdo_get('core_paylog', array('uniacid' => $uniacid, 'uniontid' => $refundlog['uniontid']));
  134. $account = uni_fetch($uniacid);
  135. $refund_param = array(
  136. 'appid' => $account['key'],
  137. 'mch_id' => $pay_setting['wechat']['mchid'],
  138. 'out_trade_no' => $refundlog['uniontid'],
  139. 'out_refund_no' => $refundlog['refund_uniontid'],
  140. 'total_fee' => $paylog['card_fee'] * 100,
  141. 'refund_fee' => $refundlog['fee'] * 100,
  142. 'nonce_str' => random(8),
  143. 'refund_desc' => $refundlog['reason']
  144. );
  145. if ($pay_setting['wechat']['switch'] === PAYMENT_WECHAT_TYPE_SERVICE || !empty($pay_setting['wechat_facilitator']['switch'])) {
  146. $refund_param['sub_mch_id'] = $pay_setting['wechat']['sub_mch_id'];
  147. $refund_param['sub_appid'] = $account['key'];
  148. $proxy_account = uni_fetch($pay_setting['wechat']['service']);
  149. $refund_param['appid'] = $proxy_account['key'];
  150. $refund_param['mch_id'] = $proxy_account['setting']['payment']['wechat_facilitator']['mchid'];
  151. }
  152. $cert = authcode($refund_setting['cert'], 'DECODE');
  153. $key = authcode($refund_setting['key'], 'DECODE');
  154. $cert_file = ATTACHMENT_ROOT . $_W['uniacid'] . md5(complex_authkey()) . '_wechat_refund_all.pem';
  155. file_put_contents($cert_file, $cert . $key);
  156. return $refund_param;
  157. }