chenxiqiang vor 6 Jahren
Ursprung
Commit
db2af7e380

+ 1
- 1
manage-server/app/Http/Controllers/CommonController.php Datei anzeigen

@@ -318,7 +318,7 @@ class CommonController extends Controller
318 318
      * @param $id
319 319
      * @return null
320 320
      */
321
-    public function getRecord($type, $id)
321
+    public static function getRecord($type, $id)
322 322
     {
323 323
         $record = null;
324 324
         switch ($type) {

+ 104
- 0
manage-server/app/Http/Controllers/PaymentController.php Datei anzeigen

@@ -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
+}

+ 1
- 0
manage-server/app/Models/Order.php Datei anzeigen

@@ -25,6 +25,7 @@ class Order extends Model
25 25
         'price', // 订单价格
26 26
         'photo', // 显示的图片
27 27
         'comment_id', // 评论id
28
+        'paid_at',  // 支付时间
28 29
     ];
29 30
 
30 31
     public function setFillable($fillable) {

+ 12
- 12
manage-server/config/wechat.php Datei anzeigen

@@ -105,18 +105,18 @@ return [
105 105
     /*
106 106
      * 微信支付
107 107
      */
108
-    // 'payment' => [
109
-    //     'default' => [
110
-    //         'sandbox'            => env('WECHAT_PAYMENT_SANDBOX', false),
111
-    //         'app_id'             => env('WECHAT_PAYMENT_APPID', ''),
112
-    //         'mch_id'             => env('WECHAT_PAYMENT_MCH_ID', 'your-mch-id'),
113
-    //         'key'                => env('WECHAT_PAYMENT_KEY', 'key-for-signature'),
114
-    //         'cert_path'          => env('WECHAT_PAYMENT_CERT_PATH', 'path/to/cert/apiclient_cert.pem'),    // XXX: 绝对路径!!!!
115
-    //         'key_path'           => env('WECHAT_PAYMENT_KEY_PATH', 'path/to/cert/apiclient_key.pem'),      // XXX: 绝对路径!!!!
116
-    //         'notify_url'         => 'http://example.com/payments/wechat-notify',                           // 默认支付结果通知地址
117
-    //     ],
118
-    //     // ...
119
-    // ],
108
+    'payment' => [
109
+        'default' => [
110
+            'sandbox'            => env('WECHAT_PAYMENT_SANDBOX', false),
111
+            'app_id'             => env('WECHAT_PAYMENT_APPID', 'wx14eb9c1640e3ecfe'),
112
+            'mch_id'             => env('WECHAT_PAYMENT_MCH_ID', '1513123211'),
113
+            'key'                => env('WECHAT_PAYMENT_KEY', 'king3430439854450499252258817008'),
114
+            //'cert_path'          => env('WECHAT_PAYMENT_CERT_PATH', 'path/to/cert/apiclient_cert.pem'),    // XXX: 绝对路径!!!!
115
+            //'key_path'           => env('WECHAT_PAYMENT_KEY_PATH', 'path/to/cert/apiclient_key.pem'),      // XXX: 绝对路径!!!!
116
+            'notify_url'         => 'https://wechat.sotype.com/payments/wechat-notify',                           // 默认支付结果通知地址
117
+        ],
118
+        // ...
119
+    ],
120 120
 
121 121
     /*
122 122
      * 企业微信

+ 1
- 0
manage-server/database/migrations/2018_08_10_074321_create_product_orders_table.php Datei anzeigen

@@ -47,6 +47,7 @@ class CreateProductOrdersTable extends Migration
47 47
             $table->string('phone');
48 48
             $table->string('photo');
49 49
             $table->unsignedInteger('comment_id');
50
+            $table->timestamp("paid_at");
50 51
         });
51 52
     }
52 53
 

+ 1
- 0
manage-server/database/migrations/2018_08_10_075128_create_maternity_matron_orders_table.php Datei anzeigen

@@ -41,6 +41,7 @@ class CreateMaternityMatronOrdersTable extends Migration
41 41
             $table->string('photo');
42 42
             $table->unsignedInteger('comment_id');
43 43
             $table->timestamps();
44
+            $table->timestamp("paid_at");
44 45
         });
45 46
     }
46 47
 

+ 1
- 0
manage-server/database/migrations/2018_08_10_075742_create_housekeeper_orders_table.php Datei anzeigen

@@ -43,6 +43,7 @@ class CreateHousekeeperOrdersTable extends Migration
43 43
             $table->double('price');
44 44
             $table->string('photo');
45 45
             $table->unsignedInteger('comment_id');
46
+            $table->timestamp("paid_at");
46 47
         });
47 48
     }
48 49
 

+ 1
- 0
manage-server/database/migrations/2018_08_10_080158_create_house_clean_orders_table.php Datei anzeigen

@@ -47,6 +47,7 @@ class CreateHouseCleanOrdersTable extends Migration
47 47
             $table->double('price');
48 48
             $table->string('photo');
49 49
             $table->unsignedInteger('comment_id');
50
+            $table->timestamp("paid_at");
50 51
         });
51 52
     }
52 53
 

+ 1
- 0
manage-server/database/migrations/2018_08_10_080546_create_house_applicance_clean_orders_table.php Datei anzeigen

@@ -35,6 +35,7 @@ class CreateHouseApplicanceCleanOrdersTable extends Migration
35 35
             $table->double('price');
36 36
             $table->string('photo');
37 37
             $table->unsignedInteger('comment_id');
38
+            $table->timestamp("paid_at");
38 39
         });
39 40
     }
40 41
 

+ 3
- 0
manage-server/routes/api.php Datei anzeigen

@@ -61,3 +61,6 @@ Route::post('/product_order/store', 'ProductOrderController@store');
61 61
 Route::post('/server_info/store', 'ServerInfoController@store');
62 62
 Route::post('/company_info/store', 'CompanyInfoController@store');
63 63
 
64
+Route::any('payments/wechat-notify', 'PaymentController@notify');
65
+
66
+Route::post('payments/pay', 'PaymentController@buildOrder');

Loading…
Abbrechen
Speichern