Refund.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\utils;
  8. use app\models\Order;
  9. use app\models\OrderUnion;
  10. use app\models\PtActivityOrder;
  11. use app\utils\Alipay\Alipay;
  12. use app\utils\Alipay\AlipayProfit;
  13. use app\utils\Wechat\WechatNewPay;
  14. use app\utils\Wechat\WechatPay;
  15. use app\utils\Adapay\Adapay;
  16. use app\utils\Allinpay\Allinpay;
  17. use app\models\UserPayMonth;
  18. use app\models\Store;
  19. use app\models\AccountLog;
  20. use app\utils\yunst\src\OrderYunst;
  21. class Refund
  22. {
  23. /**
  24. * @param $order Order|IntegralOrder|MsOrder|OrderUnion|PtOrder|YyOrder 订单
  25. * @param $order_type string
  26. * @param $refundFee integer 退款金额
  27. * @param $orderRefundNo string 退款单号
  28. * @return array|bool|\Yansongda\Supports\Collection
  29. */
  30. public static function refund_old($order, $order_type, $orderRefundNo, $refundFee)
  31. {
  32. $user = $order->user;
  33. if ($user->platform == 0 || $user->platform == 2) {
  34. $res = WechatPay::orderRefund($order, $order_type, $refundFee, $orderRefundNo);
  35. if (!$res || $res['code'] == 1) {
  36. return $res;
  37. }
  38. return true;
  39. } else if ($user->platform == 1) {
  40. $res = Alipay::refund($order, $refundFee);
  41. if (!$res || $res['code'] == 1) {
  42. return $res;
  43. }
  44. return true;
  45. } else {
  46. return [
  47. 'code' => 1,
  48. 'msg' => '退款失败'
  49. ];
  50. }
  51. }
  52. /**
  53. * @param $order Order | PtActivityOrder
  54. * @param $order_type string
  55. * @param $refundFee integer 退款金额
  56. * @param $orderRefundNo string 退款单号
  57. * @return array|bool|\Yansongda\Supports\Collection
  58. */
  59. public static function refund($order, $order_type, $orderRefundNo, $refundFee)
  60. {
  61. if(isset($order['order_type']) && $order['order_type'] == Order::ORDER_TYPE_GIVING_GIFTS && $refundFee && $order['giving_gifts_received_user_id']){
  62. AccountLog::saveLog($order['giving_gifts_received_user_id'], $refundFee, AccountLog::TYPE_BALANCE, AccountLog::LOG_TYPE_INCOME, AccountLog::TYPE_PLATFORM_REFUND_ORDER, $order->id, "送礼订单退款至余额:退款订单号:{$orderRefundNo}");
  63. return true;
  64. }
  65. if ($order->pay_type == Order::PAY_TYPE_YUNST_WECHAT_PAY && $order->transaction_id) {
  66. debug_log('我是退款111');
  67. $orderYunst = new OrderYunst();
  68. $store = Store::findOne($order->store_id);
  69. $data["cusid"] = $store->cusid;//商户号
  70. // $data["trxamt"] = $refundFee *100;//退款金额
  71. $data["trxamt"] = 1;//退款金额
  72. $data["reqsn"] = $order->order_no .time();//商户退款订单号
  73. $data["oldreqsn"] = $order->order_no;//原交易订单号
  74. $data["oldtrxid"] = $order->transaction_id;//原交易流水
  75. // $data["oldtrxid"] = '240124119500033224';//原交易流水
  76. $res_info = $orderYunst->YunstRefund($data);
  77. debug_log($data);
  78. debug_log('走到通联退款');
  79. debug_log($res_info);
  80. if ($res_info['subCode'] != 'SUCCESS' || $res_info['data']['trxstatus'] != '0000') {
  81. $res = [
  82. 'code' => 1,
  83. 'msg' => '订单取消失败,退款失败,' . $res_info['data']['errmsg'],
  84. 'res' => $res_info,
  85. ];
  86. }else{
  87. $res = [
  88. 'code' => 0,
  89. 'msg' => 'success',
  90. 'data' => true
  91. ];
  92. return true;
  93. }
  94. return $res;
  95. debug_log($res_info);
  96. }
  97. if ($order->pay_type == 1 && $order->transaction_id) {
  98. $self_mini = intval(\app\models\Option::get('self_mini', $order->store_id, 'store', 0)['value']);
  99. if (\Yii::$app->prod_is_dandianpu() && (!Store::hasIncoming($order->store_id) && !$self_mini)) {
  100. $res = WechatPay::orderRefund($order, $order_type, $refundFee, $orderRefundNo);
  101. } else {
  102. if (is_profit_pay()) {
  103. $res = WechatNewPay::orderRefund($order, $order_type, $refundFee, $orderRefundNo);
  104. } else {
  105. $res = WechatPay::orderRefund($order, $order_type, $refundFee, $orderRefundNo);
  106. }
  107. }
  108. if (!$res || $res['code'] == 1) {
  109. return $res;
  110. }
  111. return true;
  112. } else if (in_array($order->pay_type, [Order::PAY_TYPE_WX_B2B]) && $order->transaction_id) {
  113. $res = Wechat\B2b\B2bPay::orderRefund($order, $order_type, $refundFee, $orderRefundNo);
  114. if (!$res || $res['code']) {
  115. //处理23.0的支付类型存为b2b的bug,
  116. if($order->version == 23.0 && $res['res'] && $res['res']['errcode'] == 48001){
  117. $order->pay_type = 1;
  118. if(!$order->save()){
  119. return [
  120. 'code' => 1,
  121. 'msg' => $order->getFirstErrors(),
  122. ];
  123. }
  124. return self::refund($order, $order_type, $orderRefundNo, $refundFee);
  125. }
  126. return $res;
  127. }
  128. return true;
  129. } else if (in_array($order->pay_type, [Order::PAY_TYPE_ADAPAY_WX, Order::PAY_TYPE_ADAPAY_ALIPAY, Order::PAY_TYPE_ADAPAY_QUICKPAY_FRONTPAY, Order::PAY_TYPE_HUIFU_V2_JSPAY_WX]) && $order->adapay_payment_id) {
  130. $res = Adapay::orderRefund($order, $order_type, $refundFee, $orderRefundNo);
  131. if (!$res || $res['code']) {
  132. return $res;
  133. }
  134. return true;
  135. } else if (in_array($order->pay_type, [Order::PAY_TYPE_ALLINPAY_WX]) && $order->allinpay_payment_id) {
  136. $res = Allinpay::orderRefund($order, $order_type, $refundFee, $orderRefundNo);
  137. if (!$res || $res['code'] == 1) {
  138. return $res;
  139. }
  140. return true;
  141. }else if ($order->pay_type == 4 && $order->alipay_trade_no) {
  142. if (is_profit_pay()) {
  143. $res = AlipayProfit::refund($order, $refundFee);
  144. } else {
  145. $res = Alipay::refund($order, $refundFee);
  146. }
  147. if (!$res || $res['code'] == 1) {
  148. return $res;
  149. }
  150. return true;
  151. } else if ($order->pay_type == Order::PAY_TYPE_MONTH) {
  152. $res = UserPayMonth::refund($order->user_id, $order, $refundFee);
  153. if (!$res || $res['code']) {
  154. return $res;
  155. }
  156. return true;
  157. } else {
  158. return [
  159. 'code' => 1,
  160. 'msg' => '退款失败'
  161. ];
  162. }
  163. }
  164. }