/
PaymentController.php
101 lines (78 loc) · 3.2 KB
/
PaymentController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
/*
* This file is part of ibrand/EC-Open-Server.
*
* (c) iBrand <https://www.ibrand.cc>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace iBrand\EC\Open\Server\Http\Controllers;
use Carbon\Carbon;
use iBrand\Component\Order\Models\Order;
use iBrand\Component\Order\Repositories\OrderRepository;
use iBrand\Component\Pay\Facades\Charge;
use iBrand\Component\Pay\Facades\PayNotify;
use iBrand\Component\Payment\Models\Payment;
use iBrand\Component\Payment\Services\PaymentService;
use EasyWeChat;
class PaymentController extends Controller
{
private $payment;
private $orderRepository;
public function __construct(PaymentService $paymentService,
OrderRepository $orderRepository
)
{
$this->payment = $paymentService;
$this->orderRepository = $orderRepository;
}
public function paidSuccess()
{
$user = request()->user();
$order_no = request('order_no');
if (!$order_no || !$order = $this->orderRepository->getOrderByNo($order_no)) {
return $this->failed('订单不存在');
}
if ($user->cant('update', $order)) {
return $this->failed('无权操作.');
}
//在pay_debug=true 状态下,可以调用此接口直接更改订单支付状态
if (config('ibrand.app.pay_debug')) {
$charge = \iBrand\Component\Pay\Models\Charge::where('order_no', $order_no)->orderBy('created_at', 'desc')->first();
$charge->transaction_no = '';
$charge->time_paid = Carbon::now();
$charge->paid = 1;
$charge->channel = 'test';
$charge->amount = $order->total;
$charge->save();
$order = PayNotify::success($charge->type, $charge);
} else {
//同步查询微信订单状态,防止异步通信失败导致订单状态更新失败
$charge = Charge::find(request('charge_id'));
$order = PayNotify::success($charge->type, $charge);
/*$payment = EasyWeChat::payment();
$result = $payment->order->queryByOutTradeNumber($order_no);
if ('FAIL' == $result['return_code']) {
return $this->failed($result['return_msg']);
}
if ('FAIL' == $result['result_code']) {
return $this->failed($result['err_code_des']);
}
if ('SUCCESS' != $result['trade_state']) {
return $this->failed($result['trade_state_desc']);
}
$charge['metadata']['order_no'] = $result['out_trade_no'];
$charge['amount'] = $result['total_fee'];
$charge['transaction_no'] = $result['transaction_id'];
$charge['time_paid'] = strtotime($result['time_end']);
$charge['details'] = json_encode($result);
$charge['channel'] = 'wx_lite';
$order = $this->payment->paySuccess($charge);*/
}
if (Order::STATUS_PAY == $order->status) {
return $this->success(['order' => $order, 'payment' => '微信支付']);
}
return $this->failed('支付失败');
}
}