OrderPayFailRepair.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shenyang
  5. * Date: 2018/8/15
  6. * Time: 下午1:49
  7. */
  8. namespace app\backend\modules\order\fix;
  9. use app\backend\modules\orderPay\fix\DoublePaymentRepair;
  10. use app\common\events\payment\ChargeComplatedEvent;
  11. use app\common\models\Order;
  12. use app\common\models\OrderPay;
  13. use app\common\models\PayRequestDataLog;
  14. use app\common\models\PayResponseDataLog;
  15. use app\common\services\PayFactory;
  16. use app\frontend\modules\order\services\OrderService;
  17. class OrderPayFailRepair
  18. {
  19. /**
  20. * @var Order
  21. */
  22. private $order;
  23. public $message = [];
  24. public function __construct(Order $order)
  25. {
  26. $this->order = $order;
  27. }
  28. /**
  29. * @return bool
  30. * @throws \app\common\exceptions\AppException
  31. */
  32. public function handle()
  33. {
  34. if (!$this->check()) {
  35. $this->message[] = '不满足修复条件';
  36. return false;
  37. }
  38. /**
  39. * @var OrderPay $orderPay
  40. */
  41. $orderPay = $this->order->orderPays->where('status', 1)->sort(function ($orderPay){
  42. return $orderPay->updated_at;
  43. })->first();
  44. if (!$orderPay) {
  45. $this->message[] = $this->order->order_sn.'未存在状态为已支付的支付流水记录';
  46. return false;
  47. }
  48. $orderPay->pay();
  49. $this->message[] = $this->order->order_sn.'已修复';
  50. // todo 剩余的记录执行退款
  51. $this->order->orderPays->where('status', 1)->where('id', '!=', $orderPay->id)->each(
  52. function (OrderPay $orderPay) {
  53. $this->message[] = $orderPay->pay_sn . '已退款';
  54. (new DoublePaymentRepair($orderPay))->handle();
  55. }
  56. );
  57. }
  58. private function check()
  59. {
  60. // 待支付
  61. if ($this->order->status != Order::WAIT_PAY) {
  62. $this->message[] = '订单已支付';
  63. return false;
  64. }
  65. // 已付款
  66. if (count($this->order->orderPays->where('status', 1)) > 0) {
  67. return true;
  68. }
  69. $paySns = $this->order->orderPays->pluck('pay_sn');
  70. $payResponse = PayResponseDataLog::whereIn('out_order_no',$paySns)->get();
  71. $this->message[] = "共{$payResponse->count()}条支付回调记录,需要联系技术人工处理";
  72. return false;
  73. //todo 以下代码有问题,原因每种支付请求和回调存的参数不一致所以无法准确获得到支付金额
  74. $payResult = PayRequestDataLog::whereIn('out_order_no',$paySns)->get();
  75. if($payResult->count()>0){
  76. $this->message[] = "共{$payResult->count()}条支付记录";
  77. $data = json_decode($payResult->first()->params,true);
  78. $orderPay = OrderPay::where('pay_sn', $data['out_trade_no'])->orderBy('id', 'desc')->first();
  79. if ($data['unit'] == 'fen') {
  80. $orderPay->amount = $orderPay->amount * 100;
  81. }
  82. if (bccomp($orderPay->amount, $data['total_fee'], 2) == 0) {
  83. \Log::debug('更新订单状态');
  84. OrderService::ordersPay(['order_pay_id' => $orderPay->id, 'pay_type_id' => $data['pay_type_id']]);
  85. event(new ChargeComplatedEvent([
  86. 'order_sn' => $data['out_trade_no'],
  87. 'pay_sn' => $data['trade_no'],
  88. 'order_pay_id' => $orderPay->id
  89. ]));
  90. }
  91. return true;
  92. }
  93. return false;
  94. }
  95. }