refund.mod.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 ($refundlog['is_wish'] == 1) {
  68. $module = 'store';
  69. $cert_file = ATTACHMENT_ROOT . 'store_wechat_refund_all.pem';
  70. } else {
  71. $module = '';
  72. $cert_file = ATTACHMENT_ROOT . $_W['uniacid'] . '_wechat_refund_all.pem';
  73. }
  74. $wechat = Pay::create('wechat', $module);
  75. $response = $wechat->refund($refund_param, $module);
  76. unlink($cert_file);
  77. if (is_error($response)) {
  78. pdo_update('core_refundlog', array('status' => '-1'), array('id' => $refund_id));
  79. return $response;
  80. } else {
  81. return $response;
  82. }
  83. } elseif ($paylog['type'] == 'alipay') {
  84. $refund_param = reufnd_ali_build($refund_id, $refundlog['is_wish']);
  85. $module = $refundlog['is_wish'] == 1 ? 'store' : '';
  86. $ali = Pay::create('alipay', $module);
  87. $response = $ali->refund($refund_param, $refund_id);
  88. if (is_error($response)) {
  89. pdo_update('core_refundlog', array('status' => '-1'), array('id' => $refund_id));
  90. return $response;
  91. } else {
  92. return $response;
  93. }
  94. }
  95. return error(1, '此订单退款方式不存在');
  96. }
  97. function reufnd_ali_build($refund_id, $is_wish = 0) {
  98. global $_W;
  99. if ($is_wish == 1) {
  100. $setting = setting_load('store_pay');
  101. $refund_setting = $setting['store_pay']['ali_refund'];
  102. } else {
  103. $setting = uni_setting_load('payment', $_W['uniacid']);
  104. $refund_setting = $setting['payment']['ali_refund'];
  105. }
  106. if ($refund_setting['switch'] != 1) {
  107. return error(1, '未开启支付宝退款功能!');
  108. }
  109. if (empty($refund_setting['private_key'])) {
  110. return error(1, '缺少支付宝密钥证书!');
  111. }
  112. $refundlog = pdo_get('core_refundlog', array('id' => $refund_id));
  113. $uniacid = $is_wish == 1 ? $refundlog['uniacid'] : $_W['uniacid'];
  114. $paylog = pdo_get('core_paylog', array('uniacid' => $uniacid, 'uniontid' => $refundlog['uniontid']));
  115. $refund_param = array(
  116. 'app_id' => $refund_setting['app_id'],
  117. 'method' => 'alipay.trade.refund',
  118. 'charset' => 'utf-8',
  119. 'sign_type' => 'RSA2',
  120. 'timestamp' => date('Y-m-d H:i:s'),
  121. 'version' => '1.0',
  122. 'biz_content' => array(
  123. 'out_trade_no' => $refundlog['uniontid'],
  124. 'refund_amount' => $refundlog['fee'],
  125. 'refund_reason' => $refundlog['reason'],
  126. )
  127. );
  128. $refund_param['biz_content'] = json_encode($refund_param['biz_content']);
  129. return $refund_param;
  130. }
  131. function reufnd_wechat_build($refund_id, $is_wish = 0) {
  132. global $_W;
  133. if ($is_wish == 1) {
  134. $setting = setting_load('store_pay');
  135. $pay_setting = $setting['store_pay'];
  136. $refund_setting = $setting['store_pay']['wechat_refund'];
  137. } else {
  138. $setting = uni_setting_load('payment', $_W['uniacid']);
  139. $pay_setting = $setting['payment'];
  140. $refund_setting = $setting['payment']['wechat_refund'];
  141. }
  142. if ($refund_setting['switch'] != 1) {
  143. return error(1, '未开启微信退款功能!');
  144. }
  145. if (empty($refund_setting['key']) || empty($refund_setting['cert'])) {
  146. return error(1, '缺少微信证书!');
  147. }
  148. $refundlog = pdo_get('core_refundlog', array('id' => $refund_id));
  149. $uniacid = $is_wish == 1 ? $refundlog['uniacid'] : $_W['uniacid'];
  150. $paylog = pdo_get('core_paylog', array('uniacid' => $uniacid, 'uniontid' => $refundlog['uniontid']));
  151. $account = uni_fetch($uniacid);
  152. $refund_param = array(
  153. 'appid' => $is_wish == 1 ? $pay_setting['wechat']['appid'] : $account['key'],
  154. 'mch_id' => $pay_setting['wechat']['mchid'],
  155. 'out_trade_no' => $refundlog['uniontid'],
  156. 'out_refund_no' => $refundlog['refund_uniontid'],
  157. 'total_fee' => $paylog['card_fee'] * 100,
  158. 'refund_fee' => $refundlog['fee'] * 100,
  159. 'nonce_str' => random(8),
  160. 'refund_desc' => $refundlog['reason']
  161. );
  162. if ($pay_setting['wechat']['switch'] == PAYMENT_WECHAT_TYPE_SERVICE) {
  163. $refund_param['sub_mch_id'] = $pay_setting['wechat']['sub_mch_id'];
  164. $refund_param['sub_appid'] = $account['key'];
  165. $proxy_account = uni_fetch($pay_setting['wechat']['service']);
  166. $refund_param['appid'] = $proxy_account['key'];
  167. $refund_param['mch_id'] = $proxy_account['setting']['payment']['wechat_facilitator']['mchid'];
  168. }
  169. $cert = authcode($refund_setting['cert'], 'DECODE');
  170. $key = authcode($refund_setting['key'], 'DECODE');
  171. $cert_file = $is_wish == 1 ? 'store_wechat_refund_all.pem' : $_W['uniacid'] . '_wechat_refund_all.pem';
  172. file_put_contents(ATTACHMENT_ROOT . $cert_file, $cert . $key);
  173. return $refund_param;
  174. }