| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <?php
- /**
- * Created by PhpStorm.
- * User: shenyang
- * Date: 2018/8/15
- * Time: 下午1:49
- */
- namespace app\backend\modules\order\fix;
- use app\backend\modules\orderPay\fix\DoublePaymentRepair;
- use app\common\events\payment\ChargeComplatedEvent;
- use app\common\models\Order;
- use app\common\models\OrderPay;
- use app\common\models\PayRequestDataLog;
- use app\common\models\PayResponseDataLog;
- use app\common\services\PayFactory;
- use app\frontend\modules\order\services\OrderService;
- class OrderPayFailRepair
- {
- /**
- * @var Order
- */
- private $order;
- public $message = [];
- public function __construct(Order $order)
- {
- $this->order = $order;
- }
- /**
- * @return bool
- * @throws \app\common\exceptions\AppException
- */
- public function handle()
- {
- if (!$this->check()) {
- $this->message[] = '不满足修复条件';
- return false;
- }
- /**
- * @var OrderPay $orderPay
- */
- $orderPay = $this->order->orderPays->where('status', 1)->sort(function ($orderPay){
- return $orderPay->updated_at;
- })->first();
- if (!$orderPay) {
- $this->message[] = $this->order->order_sn.'未存在状态为已支付的支付流水记录';
- return false;
- }
- $orderPay->pay();
- $this->message[] = $this->order->order_sn.'已修复';
- // todo 剩余的记录执行退款
- $this->order->orderPays->where('status', 1)->where('id', '!=', $orderPay->id)->each(
- function (OrderPay $orderPay) {
- $this->message[] = $orderPay->pay_sn . '已退款';
- (new DoublePaymentRepair($orderPay))->handle();
- }
- );
- }
- private function check()
- {
- // 待支付
- if ($this->order->status != Order::WAIT_PAY) {
- $this->message[] = '订单已支付';
- return false;
- }
- // 已付款
- if (count($this->order->orderPays->where('status', 1)) > 0) {
- return true;
- }
- $paySns = $this->order->orderPays->pluck('pay_sn');
- $payResponse = PayResponseDataLog::whereIn('out_order_no',$paySns)->get();
- $this->message[] = "共{$payResponse->count()}条支付回调记录,需要联系技术人工处理";
- return false;
- //todo 以下代码有问题,原因每种支付请求和回调存的参数不一致所以无法准确获得到支付金额
- $payResult = PayRequestDataLog::whereIn('out_order_no',$paySns)->get();
- if($payResult->count()>0){
- $this->message[] = "共{$payResult->count()}条支付记录";
- $data = json_decode($payResult->first()->params,true);
- $orderPay = OrderPay::where('pay_sn', $data['out_trade_no'])->orderBy('id', 'desc')->first();
- if ($data['unit'] == 'fen') {
- $orderPay->amount = $orderPay->amount * 100;
- }
- if (bccomp($orderPay->amount, $data['total_fee'], 2) == 0) {
- \Log::debug('更新订单状态');
- OrderService::ordersPay(['order_pay_id' => $orderPay->id, 'pay_type_id' => $data['pay_type_id']]);
- event(new ChargeComplatedEvent([
- 'order_sn' => $data['out_trade_no'],
- 'pay_sn' => $data['trade_no'],
- 'order_pay_id' => $orderPay->id
- ]));
- }
- return true;
- }
- return false;
- }
- }
|