|
@@ -0,0 +1,104 @@
|
|
1
|
+<?php
|
|
2
|
+
|
|
3
|
+namespace App\Http\Controllers;
|
|
4
|
+
|
|
5
|
+use Illuminate\Http\Request;
|
|
6
|
+
|
|
7
|
+class PaymentController extends Controller
|
|
8
|
+{
|
|
9
|
+ /**
|
|
10
|
+ * 生成支付订单
|
|
11
|
+ * @param Request $request
|
|
12
|
+ * @return \Illuminate\Http\JsonResponse
|
|
13
|
+ */
|
|
14
|
+ public function buildOrder(Request $request)
|
|
15
|
+ {
|
|
16
|
+
|
|
17
|
+ $wechat_id = $request->header("openid");
|
|
18
|
+ if (!$wechat_id) {
|
|
19
|
+ $wechat_id = $request->input("openid");
|
|
20
|
+ }
|
|
21
|
+ $order_id = $request->input("order_id");
|
|
22
|
+ $order_type = $request->input("order_type");
|
|
23
|
+ $price = $request->input("price");
|
|
24
|
+
|
|
25
|
+ if ($wechat_id && $order_id && $order_type && $price) {
|
|
26
|
+ $order_id = $order_type . "_" . $order_id;
|
|
27
|
+ $app = EasyWeChat::payment();
|
|
28
|
+ $result = $app->order->unify([
|
|
29
|
+ 'body' => '玥子轩家政',
|
|
30
|
+ 'out_trade_no' => $order_id,
|
|
31
|
+ 'total_fee' => $price,
|
|
32
|
+ 'trade_type' => 'JSAPI',
|
|
33
|
+ 'openid' => $wechat_id,
|
|
34
|
+ ]);
|
|
35
|
+
|
|
36
|
+ if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
|
|
37
|
+ //第二次签名
|
|
38
|
+ $result = $app->jssdk->appConfig($result['prepay_id']);
|
|
39
|
+ return response()->json([
|
|
40
|
+ "status" => 0,
|
|
41
|
+ "code" => "success",
|
|
42
|
+ "msg" => $result
|
|
43
|
+ ]);
|
|
44
|
+ } else {
|
|
45
|
+ return response()->json([
|
|
46
|
+ "status" => -1,
|
|
47
|
+ "message" => '微信支付签名失败:' . var_export($result, 1)
|
|
48
|
+ ]);
|
|
49
|
+ }
|
|
50
|
+
|
|
51
|
+ }
|
|
52
|
+ }
|
|
53
|
+
|
|
54
|
+ /**
|
|
55
|
+ * 支付成功回调函数
|
|
56
|
+ * @param Request $request
|
|
57
|
+ * @return mixed
|
|
58
|
+ */
|
|
59
|
+ public function notify(Request $request)
|
|
60
|
+ {
|
|
61
|
+ $app = app('wechat.payment');
|
|
62
|
+ $response = $app->handlePaidNotify(function($message, $fail){
|
|
63
|
+ // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
|
|
64
|
+ list($order_type, $order_id) = spilt("_", $message['out_trade_no']);
|
|
65
|
+ $order = CommonController::getRecord($order_type, $order_id);
|
|
66
|
+
|
|
67
|
+ if (!$order || $order->paid_at) { // 如果订单不存在 或者 订单已经支付过了
|
|
68
|
+ return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
|
|
69
|
+ }
|
|
70
|
+
|
|
71
|
+ ///////////// <- 建议在这里调用微信的【订单查询】接口查一下该笔订单的情况,确认是已经支付 /////////////
|
|
72
|
+
|
|
73
|
+ /*$app = app('wechat.payment');
|
|
74
|
+ $order_result = $app->order->queryByOutTradeNumber($message['out_trade_no']);
|
|
75
|
+ if($order_result && $order_result['return_code'] === 'SUCCESS') {
|
|
76
|
+ // 如果支付成功
|
|
77
|
+ if($order_result["trade_state"] != "SUCCESS") {
|
|
78
|
+ $order->status = -1;
|
|
79
|
+ } else {
|
|
80
|
+
|
|
81
|
+ }
|
|
82
|
+ }*/
|
|
83
|
+
|
|
84
|
+ if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
|
|
85
|
+ // 用户是否支付成功
|
|
86
|
+ if (array_get($message, 'result_code') === 'SUCCESS') {
|
|
87
|
+ $order->paid_at = time(); // 更新支付时间为当前时间
|
|
88
|
+ $order->status = 2;
|
|
89
|
+
|
|
90
|
+ // 用户支付失败
|
|
91
|
+ } elseif (array_get($message, 'result_code') === 'FAIL') {
|
|
92
|
+ $order->status = -1;
|
|
93
|
+ }
|
|
94
|
+ } else {
|
|
95
|
+ return $fail('通信失败,请稍后再通知我');
|
|
96
|
+ }
|
|
97
|
+
|
|
98
|
+ $order->save(); // 保存订单
|
|
99
|
+
|
|
100
|
+ return true; // 返回处理完成
|
|
101
|
+ });
|
|
102
|
+ return $response;
|
|
103
|
+ }
|
|
104
|
+}
|