chuweiqiang 1 年之前
父节点
当前提交
213ca542fb
共有 3 个文件被更改,包括 135 次插入2 次删除
  1. 20 0
      application/api/controller/Common.php
  2. 28 1
      application/api/controller/Order.php
  3. 87 1
      application/api/controller/Pay.php

+ 20 - 0
application/api/controller/Common.php

@@ -7,6 +7,7 @@ use app\common\exception\UploadException;
 use app\common\library\Upload;
 use app\common\model\Area;
 use app\common\model\Version;
+use app\admin\model\books\Certificate; //体验卡
 use fast\Random;
 use think\captcha\Captcha;
 use think\Config;
@@ -177,4 +178,23 @@ class Common extends Api
 //        ];
         $this->success('操作成功',$list);
     }
+
+    /**
+     * 体验卡自动过期
+     * @param $id
+     * @return \think\Response
+     */
+    public function expireCertificate(){
+        $Certificate = new Certificate();
+        $date = date('Y-m-d H:i:s');
+        $arr = [
+            'is_expire' => 0
+        ];
+        $Certificate->where('status',1)
+            ->where('is_deleted',1)
+            ->where('free_end_time','<',$date)
+            ->update($arr);
+        $this->success('执行成功');
+    }
+
 }

+ 28 - 1
application/api/controller/Order.php

@@ -11,7 +11,7 @@ use app\admin\model\order\Order as OrderModel; //订单
 use app\admin\model\order\OrderGoods; //订单商品
 use app\admin\model\books\Books; //教材
 use app\admin\model\video\Video; //教材
-
+use app\api\controller\Pay; //教材
 use app\common\controller\Api;
 use think\Db;
 
@@ -134,6 +134,33 @@ class Order extends Api
         $this->success('操作成功',$lists);
     }
 
+    /**
+     * 订单支付
+     *
+     */
+    public function orderPay(){
+        $data = input();
+        $OrderModel = new OrderModel();
+        $order = $OrderModel->where('status',0)->where('id',$data['order_id'])->find();
+        if(!$order){
+            $this->error('订单不存在');
+        }
+        $Pay = new Pay();
+        if($data['pay_type'] == 0){
+            //微信
+            $arr = [
+                'total_fee' => $order['total_price']*100,
+                'out_trade_no' => $order['order_on'],
+                'trade_type' => $data['trade_type'],
+            ];
+            return $Pay->wxpay($arr);
+        }else if($data['pay_type'] == 1){
+            //支付宝
+
+        }
+
+    }
+
 
     /**
      * 生成订单号

+ 87 - 1
application/api/controller/Pay.php

@@ -7,12 +7,19 @@
  */
 
 namespace app\api\controller;
+use app\admin\model\order\Order;
+use app\admin\model\order\OrderGoods;
+use app\admin\model\order\UserGoods;
 use app\common\controller\Api;
 use EasyWeChat\Factory;
 
 class Pay extends Api
 {
-    public function wxpay(){
+    /**
+     * 微信支付
+     *
+     */
+    public function wxpay($arr){
         $config = [
             // 必要配置
             'app_id'             => 'xxxx',
@@ -27,5 +34,84 @@ class Pay extends Api
         ];
 
         $app = Factory::payment($config);
+        $result = $app->order->unify([
+            'body' => '购买教材',
+            'out_trade_no' => $arr['out_trade_no'],
+            'total_fee' => $arr['total_fee'],
+            'notify_url' => 'https://'.$_SERVER['HTTP_HOST'].'/api/pay/notify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
+            'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
+            'openid' => 'oUpF8uMuAJO_M2pxb1Q9zNjWeS6o',
+        ]);
+
+//如trade_type = APP
+//需要进行二次签名
+        (new \EasyWeChat\Payment\Jssdk\Client($app))->appConfig($result['prepay_id']);
+
+        return $result;
+    }
+
+    public function notify(){
+        $config = [
+            // 必要配置
+            'app_id'             => 'xxxx',
+            'mch_id'             => 'your-mch-id',
+            'key'                => 'key-for-signature',   // API v2 密钥 (注意: 是v2密钥 是v2密钥 是v2密钥)
+
+            // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
+            'cert_path'          => 'path/to/your/cert.pem', // XXX: 绝对路径!!!!
+            'key_path'           => 'path/to/your/key',      // XXX: 绝对路径!!!!
+
+            'notify_url'         => '默认的订单回调地址',     // 你也可以在下单时单独设置来想覆盖它
+        ];
+        $app = Factory::payment($config);
+        $response = $app->handlePaidNotify(function($message, $fail){
+            // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
+            $OrderModel = new Order();
+            $order = $OrderModel->where('order_on',$message['out_trade_no'])->find();
+
+            if (!$order || $order->paid_at) { // 如果订单不存在 或者 订单已经支付过了
+                return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
+            }
+
+            ///////////// <- 建议在这里调用微信的【订单查询】接口查一下该笔订单的情况,确认是已经支付 /////////////
+
+            if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
+                // 用户是否支付成功
+
+                if ($message['result_code'] === 'SUCCESS') {
+                    $OrderGoods = new OrderGoods();
+                    $UserGoods = new UserGoods();
+                    $OrderModel = new Order();
+                    $order = $OrderModel->where('order_on',$message['out_trade_no'])->find();
+                    $o_arr = [
+                        'status' => 1
+                    ];
+                    $OrderModel->where('id',$order['id'])->update($o_arr);
+
+                    $goods = $OrderGoods->where('order_id',$order['id'])->select();
+                    foreach ($goods as $k => $v){
+                        $g_arr = [
+                            'goods_type' => $v['goods_type'],
+                            'goods_id' => $v['goods_id'],
+                            'order_id' => $order['id'],
+                            'user_id' => $order['user_id'],
+                        ];
+                        $UserGoods->save($g_arr);
+                    }
+
+                    // 用户支付失败
+                } else{
+                    $order->status = 'paid_fail';
+                }
+            } else {
+                return $fail('通信失败,请稍后再通知我');
+            }
+
+            $order->save(); // 保存订单
+
+            return true; // 返回处理完成
+        });
+
+        $response->send();
     }
 }