QueryRefund.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\common\command;
  15. use app\common\enum\PayEnum;
  16. use app\common\enum\RefundEnum;
  17. use app\common\model\recharge\RechargeOrder;
  18. use app\common\model\refund\RefundLog;
  19. use app\common\model\refund\RefundRecord;
  20. use app\common\service\pay\WeChatPayService;
  21. use think\console\Command;
  22. use think\console\Input;
  23. use think\console\Output;
  24. use think\facade\Log;
  25. class QueryRefund extends Command
  26. {
  27. protected function configure()
  28. {
  29. $this->setName('query_refund')
  30. ->setDescription('订单退款状态处理');
  31. }
  32. protected function execute(Input $input, Output $output)
  33. {
  34. try {
  35. // 查找退款中的退款记录(微信,支付宝支付)
  36. $refundRecords = (new RefundLog())->alias('l')
  37. ->join('refund_record r', 'r.id = l.record_id')
  38. ->field([
  39. 'l.id' => 'log_id', 'l.sn' => 'log_sn',
  40. 'r.id' => 'record_id', 'r.order_id', 'r.sn' => 'record_sn', 'r.order_type'
  41. ])
  42. ->where(['l.refund_status' => RefundEnum::REFUND_ING])
  43. ->select()->toArray();
  44. if (empty($refundRecords)) {
  45. return false;
  46. }
  47. // 分别处理各个类型订单
  48. $rechargeRecords = array_filter($refundRecords, function ($item) {
  49. return $item['order_type'] == RefundEnum::ORDER_TYPE_RECHARGE;
  50. });
  51. if (!empty($rechargeRecords)) {
  52. $this->handleRechargeOrder($rechargeRecords);
  53. }
  54. return true;
  55. } catch (\Exception $e) {
  56. Log::write('订单退款状态查询失败,失败原因:' . $e->getMessage());
  57. return false;
  58. }
  59. }
  60. /**
  61. * @notes 处理充值订单
  62. * @param $refundRecords
  63. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  64. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  65. * @author 段誉
  66. * @date 2023/3/1 15:55
  67. */
  68. public function handleRechargeOrder($refundRecords)
  69. {
  70. $orderIds = array_unique(array_column($refundRecords, 'order_id'));
  71. $Orders = RechargeOrder::whereIn('id', $orderIds)->column('*', 'id');
  72. foreach ($refundRecords as $record) {
  73. if (!isset($Orders[$record['order_id']])) {
  74. continue;
  75. }
  76. $order = $Orders[$record['order_id']];
  77. if (!in_array($order['pay_way'], [PayEnum::WECHAT_PAY, PayEnum::ALI_PAY])) {
  78. continue;
  79. }
  80. $this->checkReFundStatus([
  81. 'record_id' => $record['record_id'],
  82. 'log_id' => $record['log_id'],
  83. 'log_sn' => $record['log_sn'],
  84. 'pay_way' => $order['pay_way'],
  85. 'order_terminal' => $order['order_terminal'],
  86. ]);
  87. }
  88. }
  89. /**
  90. * @notes 校验退款状态
  91. * @param $refundData
  92. * @return bool
  93. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  94. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  95. * @author 段誉
  96. * @date 2023/3/1 15:54
  97. */
  98. public function checkReFundStatus($refundData)
  99. {
  100. $result = null;
  101. switch ($refundData['pay_way']) {
  102. case PayEnum::WECHAT_PAY:
  103. $result = self::checkWechatRefund($refundData['order_terminal'], $refundData['log_sn']);
  104. break;
  105. }
  106. if (is_null($result)) {
  107. return false;
  108. }
  109. if (true === $result) {
  110. $this->updateRefundSuccess($refundData['log_id'], $refundData['record_id']);
  111. } else {
  112. $this->updateRefundMsg($refundData['log_id'], $result);
  113. }
  114. return true;
  115. }
  116. /**
  117. * @notes 查询微信支付退款状态
  118. * @param $orderTerminal
  119. * @param $refundLogSn
  120. * @return bool|string|null
  121. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  122. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  123. * @author 段誉
  124. * @date 2023/3/1 15:47
  125. */
  126. public function checkWechatRefund($orderTerminal, $refundLogSn)
  127. {
  128. // 根据商户退款单号查询退款
  129. $result = (new WeChatPayService($orderTerminal))->queryRefund($refundLogSn);
  130. if (!empty($result['status']) && $result['status'] == 'SUCCESS') {
  131. return true;
  132. }
  133. if (!empty($result['code']) || !empty($result['message'])) {
  134. return '微信:' . $result['code'] . '-' . $result['message'];
  135. }
  136. return null;
  137. }
  138. /**
  139. * @notes 更新记录为成功
  140. * @param $logId
  141. * @param $recordId
  142. * @author 段誉
  143. * @date 2023/3/1 15:38
  144. */
  145. public function updateRefundSuccess($logId, $recordId)
  146. {
  147. // 更新日志
  148. RefundLog::update([
  149. 'id' => $logId,
  150. 'refund_status' => RefundEnum::REFUND_SUCCESS,
  151. ]);
  152. // 更新记录
  153. RefundRecord::update([
  154. 'id' => $recordId,
  155. 'refund_status' => RefundEnum::REFUND_SUCCESS,
  156. ]);
  157. }
  158. /**
  159. * @notes 更新退款信息
  160. * @param $logId
  161. * @param $msg
  162. * @author 段誉
  163. * @date 2023/3/1 15:47
  164. */
  165. public function updateRefundMsg($logId, $msg)
  166. {
  167. // 更新日志
  168. RefundLog::update([
  169. 'id' => $logId,
  170. 'refund_msg' => $msg,
  171. ]);
  172. }
  173. }