refund.mod.php 6.8 KB

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