xiaogang 4 jaren geleden
bovenliggende
commit
93dd8af0f3

+ 33 - 0
app/Http/Controllers/Api/PayNotifyController.php

xqd
@@ -0,0 +1,33 @@
+<?php
+
+
+namespace App\Http\Controllers\Api;
+
+
+use App\Services\PayService;
+use Illuminate\Support\Facades\Log;
+use Yansongda\Pay\Pay;
+
+class PayNotifyController extends Controller
+{
+    /**
+     * 微信支付回调
+     */
+    public function wx_notify(){
+        $wxpay = Pay::wechat(PayService::wx_config());
+        $data = $wxpay->verify();
+        Log::info($data);
+
+       return $wxpay->success();
+    }
+
+    /**
+     * 支付宝支付回调
+     */
+    public function ali_notify(){
+        $alipay = Pay::alipay(PayService::ali_config());
+        $data = $alipay->verify();
+        Log::info($data);
+        return $alipay->success();
+    }
+}

+ 2 - 1
app/Http/Controllers/Api/UserController.php

xqd
@@ -200,8 +200,9 @@ class UserController extends Controller
     public function buy_vip(Request $request){
         try {
             $user = auth('api')->user();
-            $param['id'] = $request->id;
+            $param['id'] = $request->id;  //会员id
             $param['user_id'] = $user->id;
+            $param['payment'] = $request->payment; //支付方式 1微信  2支付宝
             $res = $this->userService->buy_vip($param);
         }catch (\Exception $exception){
             return $this->response->errorForbidden($exception->getMessage());

+ 62 - 0
app/Services/PayService.php

xqd
@@ -4,7 +4,69 @@
 namespace App\Services;
 
 
+use Yansongda\Pay\Pay;
+
 class PayService
 {
     //public function
+
+    public static function ali_config(){
+        $config = [
+            'app_id' => '2016082000295641',
+            'notify_url' => 'https://'.$_SERVER['HTTP_HOST'].'/api/ali_notify.php',
+            'return_url' => 'https://'.$_SERVER['HTTP_HOST'].'/api/ali_return.php',
+            'ali_public_key' => '',//支付宝公钥
+            'private_key' => '',//应用私钥
+            'log' => [ // optional
+                'file' => './logs/alipay.log',
+                'level' => 'info', // 建议生产环境等级调整为 info,开发环境为 debug
+                'type' => 'single', // optional, 可选 daily.
+                'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天
+            ],
+            'http' => [ // optional
+                'timeout' => 5.0,
+                'connect_timeout' => 5.0,
+                // 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
+            ],
+            // 'mode' => 'dev', // optional,设置此参数,将进入沙箱模式
+        ];
+        return $config;
+    }
+
+    public static function wx_config(){
+        $config = [
+            'appid' => 'wxb3fxxxxxxxxxxx', // APP APPID
+            'app_id' => 'wxb3fxxxxxxxxxxx', // 公众号 APPID
+            //'miniapp_id' => 'wxb3fxxxxxxxxxxx', // 小程序 APPID
+            'mch_id' => '145776xxxx',
+            'key' => 'mF2suE9sU6Mk1CxxxxIxxxxx',
+            'notify_url' => 'https://'.$_SERVER['HTTP_HOST'].'/api/wx_notify.php',
+            //'cert_client' => './cert/apiclient_cert.pem', // optional, 退款,红包等情况时需要用到
+            //'cert_key' => './cert/apiclient_key.pem',// optional, 退款,红包等情况时需要用到
+            'log' => [ // optional
+                'file' => './logs/wechat.log',
+                'level' => 'info', // 建议生产环境等级调整为 info,开发环境为 debug
+                'type' => 'single', // optional, 可选 daily.
+                'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天
+            ],
+            'http' => [ // optional
+                'timeout' => 5.0,
+                'connect_timeout' => 5.0,
+                // 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
+            ],
+            // 'mode' => 'dev',
+        ];
+        return $config;
+    }
+
+
+    public static function pay($param){
+        if($param['payment'] == 1){
+            $wechat = Pay::wechat(self::wx_config());
+            return $wechat->app($param)->send();
+        }else{
+            $alipay = Pay::alipay(self::ali_config());
+            return $alipay->app($param)->send();
+        }
+    }
 }

+ 10 - 2
app/Services/UserService.php

xqd
@@ -59,9 +59,17 @@ class UserService
         $ins['status'] = 0;
         $ins['content'] = json_encode($vip_info);
         $ins['type'] = 1;
-        if(!$order_id = PaymentLogModel::query()->insertGetId($ins)){
+        $ins['payment'] = $param['payment'];
+        if(!PaymentLogModel::query()->insertGetId($ins)){
             throw new Exception("插入订单失败");
         }
-        return true;
+
+        $pay_param = [
+            'out_trade_no' => $ins['order_no'],
+            'body' => '购买VIP',
+            'total_fee' => $ins['price'],
+            'payment' => $ins['payment'],
+        ];
+        return PayService::pay($pay_param);
     }
 }

+ 8 - 0
routes/api.php

xqd
@@ -117,5 +117,13 @@ $api->version('v1', [
         $api->put('/logout', 'AuthorizationsController@logout')->name('logout');
     });
 
+    /*
+        |--------------------------------------------------------------
+        |  支付回调
+        |--------------------------------------------------------------
+        */
+    $api->any('/wx_notify', 'PayNotifyController@wx_notify')->name('wx_notify');
+    $api->any('/ali_notify', 'PayNotifyController@ali_notify')->name('ali_notify');
+
 
 });