Browse Source

微信支付

牟新芬 3 years ago
parent
commit
c1c001af2b

+ 1 - 0
app/api/controller/v1/Order.php

@@ -276,6 +276,7 @@ class Order extends BaseController
             OrderModel::where('order_id', $orderId)->save([
                 'status'   => 1,
                 'is_pay'   => 1,
+                'pay_type' => 'yue',
                 'pay_time' => time()
             ]);
             //改子订单状态

+ 47 - 72
app/api/controller/v1/Weixin.php

@@ -5,7 +5,9 @@ namespace app\api\controller\v1;
 use app\BaseController;
 use app\model\api\Member;
 use app\model\api\Order;
+use app\model\api\OrderInfo;
 use app\model\api\PayTrade;
+use app\model\api\Product;
 use app\model\api\Recharge;
 use app\Request;
 use EasyWeChat\Factory;
@@ -18,21 +20,6 @@ use think\facade\Db;
 
 class Weixin extends BaseController
 {
-    public function test()
-    {
-        $user1 = Db::name("member")->where('i_uid',1655)
-            ->where('regtime', 'between', [1614528000, 1617206400])
-            ->where('last_con_time','<>',0)
-            ->column('uid');
-        $user2 = Db::name("member")->where('i_uid',1655)
-            ->where('regtime', 'between', [1617206400, 1619798400])
-            ->where('last_con_time','<>',0)
-            ->column('uid');
-        $user = array_merge($user1,$user2);
-        $order = Db::name("order_info")->alias('s')->join('order o',"o.id = s.o_id")->where('s.uid', 'in', $user)
-            ->where('o.pay_time', 'between', [1617206400, 1619798400])->count();
-        dump($order);
-    }
 
     public function getInfo(Request $request)
     {
@@ -96,22 +83,20 @@ class Weixin extends BaseController
         if (!empty($order['is_pay'])) {
             return app('json')->fail('订单已经支付成功');
         }
-        $config = [
-            // 必要配置
-            'app_id' => 'wxa9130b2090dfb130',
-            'mch_id' => '1582297191',
-            'key' => 'fgwhdf323gewdsd83ewewhd56wrr333g',   // API 密钥
-            'notify_url' => '',
-        ];
-        $app = Factory::payment($config);
+        $data['out_trade_no'] = $order['order_id'];
+        $data['money'] = $order['all_price'];
+        $data['type'] = 'order';
+        $data['time'] = time();
+        Db::name('wx_notify')->insert($data);
+        $app = Factory::payment(config('weixin')['wxPay']);
         if ($from == 'weixin') {
             $result = $app->order->unify([
                 'body' => '支付订单',
                 'out_trade_no' => $order['order_id'],
                 'total_fee' => $order['all_price'] * 100,
-                'notify_url' => 'https://api.lipinwawa.com/api/weixin/notify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
+                'notify_url' => 'https://www.boofly.cn/api/weixin/notify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
                 'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
-                'openid' => 'olkUP6fDbDSQKjidMQyeQ_0TO3XE',
+                'openid' => $request->user['openid']
             ]);
             $jssdk = $app->jssdk;
             $jsConfig = $jssdk->bridgeConfig($result['prepay_id'], false);
@@ -122,7 +107,7 @@ class Weixin extends BaseController
                 'body' => '支付订单',
                 'out_trade_no' => $order['order_id'],
                 'total_fee' => $order['all_price'] * 100,
-                'notify_url' => 'https://api.lipinwawa.com/api/weixin/notify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
+                'notify_url' => 'https://www.boofly.cn/api/weixin/notify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
                 'trade_type' => 'MWEB' // 请对应换成你的支付方式对应的值类型
             ]);
             $jssdk = $app->jssdk;
@@ -135,10 +120,9 @@ class Weixin extends BaseController
 
     public function recharge(Request $request)
     {
-        [$money, $from, $code] = UtilService::getMore([
+        [$money, $from] = UtilService::getMore([
             ['money', '', 'empty', '请选择充值金额'],
-            ['from', '', 'empty', '参数错误'],
-            ['code', '']
+            ['from', '', 'empty', '参数错误']
         ], $request, true);
         try {
             Recharge::beginTrans();
@@ -146,46 +130,25 @@ class Weixin extends BaseController
             $d = [];
             $d['order_id'] = 'RE' . time() . sprintf('%04d', rand(0, 1000)) . $request->user['uid'];
             $d['v'] = $money;
-            $d['status'] = '0';
             $d['time'] = time();
-            $d['sassid'] = $request->site['sassid'];
             $d['uid'] = $request->user['uid'];
             $recharge->insert($d);
             //生成支付凭证
-            (new PayTrade)->mkTrade([
-                'out_trade_no' => $d['order_id'],
-                'uid' => $request->user['uid'],
-                'tag' => 'wxpay',
-                'type' => 'recharge',
-                'money' => $money,
-                'content' => '会员充值' . $d['v'] . '元',
-                'sassid' => $request->site['sassid'],
-            ]);
+            $data['out_trade_no'] = $d['order_id'];
+            $data['money'] = $money;
+            $data['type'] = 'recharge';
+            $data['time'] = time();
+            Db::name('wx_notify')->insert($data);
             Recharge::commitTrans();
-            $config = [
-                // 必要配置
-                'app_id' => 'wxa9130b2090dfb130',
-                'mch_id' => '1582297191',
-                'key' => 'fgwhdf323gewdsd83ewewhd56wrr333g',   // API 密钥
-                'notify_url' => '',
-            ];
-            $app = Factory::payment($config);
+            $app = Factory::payment(config('weixin')['wxPay']);
             if ($from == 'weixin') {
-                if ($code) {
-                    $weixinA = new weixina;
-                    $token = $weixinA->oauth_reuslt($code);
-                    $openid = $token['openid'];
-                    (new Member)->where('uid', $request->user['uid'])->save(['openid' => $openid]);
-                } else {
-                    $openid = $request->user['openid'];
-                }
                 $result = $app->order->unify([
                     'body' => '充值余额',
                     'out_trade_no' => $d['order_id'],
                     'total_fee' => $money * 100,
-                    'notify_url' => 'https://api.lipinwawa.com/api/weixin/notify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
+                    'notify_url' => 'https://www.boofly.cn/api/weixin/notify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
                     'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
-                    'openid' => $openid,
+                    'openid' => $request->user['openid'],
                 ]);
                 $jssdk = $app->jssdk;
                 $jsConfig = $jssdk->bridgeConfig($result['prepay_id'], false);
@@ -196,7 +159,7 @@ class Weixin extends BaseController
                     'body' => '充值余额',
                     'out_trade_no' => $d['order_id'],
                     'total_fee' => $money * 100,
-                    'notify_url' => 'https://api.lipinwawa.com/api/weixin/notify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
+                    'notify_url' => 'https://www.boofly.cn/api/weixin/notify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
                     'trade_type' => 'MWEB' // 请对应换成你的支付方式对应的值类型
                 ]);
                 $json['result'] = $result;
@@ -230,7 +193,6 @@ class Weixin extends BaseController
                 $data['mch_id'] = $xmlObj['mch_id'];
                 $data['nonce_str'] = $xmlObj['nonce_str'];
                 $data['openid'] = $xmlObj['openid'];
-                $data['out_trade_no'] = $xmlObj['out_trade_no'];
                 $data['result_code'] = $xmlObj['result_code'];
                 $data['return_code'] = $xmlObj['return_code'];
                 $data['sign'] = $xmlObj['sign'];
@@ -238,23 +200,36 @@ class Weixin extends BaseController
                 $data['total_fee'] = $xmlObj['total_fee'];
                 $data['trade_type'] = $xmlObj['trade_type'];
                 $data['transaction_id'] = $xmlObj['transaction_id'];
-                $res = Db::name('wx_notify')->insertGetId($data);
-                if ($res) {
-                    $payTrade = new PayTrade();
-                    $data2 = $payTrade->where('out_trade_no', $xmlObj['out_trade_no'])->find();
-                    if (empty($data2)) {
-                        echo 'SUCCESS';
-                        exit;
-                    }
-                    if ($data2['status'] == 1) {
-                        echo 'SUCCESS';
-                        exit;
+                $data2 = Db::name('wx_notify')->where('out_trade_no', $xmlObj['out_trade_no'])->find();
+                if (empty($data2)) {
+                    echo 'SUCCESS';
+                    exit;
+                }
+                if ($data2['status'] == 1) {
+                    echo 'SUCCESS';
+                    exit;
+                }
+                $res = Db::name('wx_notify')->where('out_trade_no',$xmlObj['out_trade_no'])->save($data);
+                if($res){
+                    if ($data2['type'] == 'order') {
+                        $order = Order::where('order_id', $xmlObj['out_trade_no'])->find();
+                        //减库存加销量
+                        Product::where('id', $order['pro_id'])->dec('stock', $order['num'])->inc('sales', $order['num'])->update();
+                        Db::name('ProductAttrValue')->where('product_id', $order['pro_id'])->where('unique', $order['unique'])->dec('stock', $order['num'])->inc('sales', $order['num'])->update();
+                        //改订单状态
+                        Order::where('order_id', $xmlObj['out_trade_no'])->save([
+                            'status'   => 1,
+                            'is_pay'   => 1,
+                            'pay_type' => 'weixin',
+                            'pay_time' => time()
+                        ]);
+                        //改子订单状态
+                        OrderInfo::where('o_id', $order['id'])->save(['status' => 1,]);
                     }
                     if ($data2['type'] == 'recharge') {
                         $recharge = new Recharge();
                         $recharge->rechargeSuccess($xmlObj['out_trade_no']);
                     }
-                    $payTrade->where('id', $data2['id'])->save(['status' => 1, 'pay_time' => time()]);
                     echo 'SUCCESS';
                     exit;
                 }

+ 0 - 2
app/api/route/weixin.php

@@ -17,8 +17,6 @@ Route::group('weixin',function () {
     Route::rule('wxPay', 'v1.weixin/pay');
     //微信支付回调
     Route::rule('notify', 'v1.weixin/notify');
-    //统计业绩
-    Route::rule('test', 'v1.weixin/test');
 
 })->middleware([
     AllowOriginMiddleware::class

+ 0 - 1
app/model/api/PayTrade.php

@@ -25,7 +25,6 @@ class PayTrade extends BaseModel
        $d['content'] = empty($data['content']) ? '' : $data['content'];
        $d['d_json'] = empty($data['d_json']) ? '[]' : json_encode($data['d_json']);
        $d['time'] = time();
-       $d['sassid'] = empty($data['sassid']) ? 0 : $data['sassid'];
        $this->insert($d);
     }
 

+ 9 - 3
config/weixin.php

@@ -10,7 +10,13 @@
 // +----------------------------------------------------------------------
 
 return [
-  'APPID'       => 'wx21448716e2a0b7df',
-  'APPSECRET'   => '196324da0b037154c00ed3d29827eef7',
-  'REDIRECT'    => 'http://www.boofly.cn/api/weixin/result'
+    'APPID'       => 'wx21448716e2a0b7df',
+    'APPSECRET'   => '196324da0b037154c00ed3d29827eef7',
+    'REDIRECT'    => 'http://www.boofly.cn/api/weixin/result',
+    'wxPay' => [
+        'app_id' => 'wx21448716e2a0b7df',
+        'mch_id' => '1611790333',
+        'key' => 'W2GAkgcny9CkjIRpWviHuZwfmGP5Wk4f',
+        'notify_url' => ''
+    ]
 ];