WIN-2308041133\Administrator 6 mesi fa
parent
commit
41e99280c3

+ 94 - 0
application/api/controller/Lave.php

@@ -2,6 +2,7 @@
 
 namespace app\api\controller;
 
+use app\admin\model\WechatPlan;
 use app\common\controller\Api;
 use app\common\model\{Category, Lave as LaveModel};
 use liuniu\repositories\LaveRepository;
@@ -140,6 +141,99 @@ class Lave extends Api
         return $this->error('支付方式错误');
     }
 
+//    支付中签约
+    public function createSign(Request $request)
+    {
+        var_dump(123);die();
+        $where = UtilService::postMore(
+            [
+                ['cid', $this->cid],
+                ['user_id', $this->auth->getUserinfo()['id']],
+                ['order_name', ''],
+                ['category_id', 0],
+                ['amount', 0],
+                ['name', ''],
+                ['contact', ''],
+                ['tel', 0],
+                ['address', ''],
+                ['is_open', '0'],
+                ['is_ticket', '0'],
+                ['pay_type', '0'],
+                ['type', '0'],
+                ['help_id', 0],
+                ['from', 'weixin'],
+            ], $request
+        );
+        $where1 = $where;
+        unset($where1['from']);
+        $where1['order_id'] = LaveModel::getNewOrderId();
+        if (cache('lave_' . $where['user_id'])) $this->error('正在处理中');
+        @file_put_contents("lave.txt", json_encode($where1));
+        $order = LaveModel::create($where1);
+        cache('lave_' . $where['user_id'], '1', 10);
+        if (!$order) $this->error(LaveModel::getErrorInfo());
+        $orderId = $order['order_id'];
+        $info = compact('orderId');
+        $order['plan_id'] = WechatPlan::where('price', $where['amount'])->value('plan_id');
+        if ($orderId) {
+            $orderInfo = LaveModel::where('order_id', $orderId)->find();
+            if (!$orderInfo || !isset($orderInfo['paid'])) $this->error('支付订单不存在!');
+            if ($orderInfo['paid']) $this->error('支付已支付!');
+            try {
+                if ($where['from'] == 'routine') {
+                    $jsConfig = LaveRepository::jsPaySign($this->cid, $orderId); //创建订单jspay
+                } else if ($where['from'] == 'weixinh5') {
+                    $jsConfig = LaveRepository::h5PaySign($this->cid, $orderId);
+                } else {
+                    $jsConfig = LaveRepository::wxPaySign($this->cid, $orderId);
+                }
+            } catch (\Exception $e) {
+                return $this->error($e->getMessage());
+            }
+            $info['jsConfig'] = $jsConfig;
+
+            return $this->success('订单创建成功', $info);
+
+        } else $this->error(LaveModel::getErrorInfo());
+    }
+    /**
+     * 签约订单支付
+     * @param Request $request
+     * @return mixed
+     */
+    public function paySign(Request $request)
+    {
+        var_dump(456);die();
+        list($uni, $paytype, $from) = UtilService::postMore([
+            ['uni', ''],
+            ['paytype', '0'],
+            ['from', 'weixin']
+        ], $request, true);
+        if (!$uni) $this->error('参数错误!');
+        $order = LaveModel::where('cid', $this->cid)->where('order_id', $uni)->find();
+        if (!$order)
+            $this->error('订单不存在!');
+        if ($order['paid'])
+            $this->error('该订单已支付!');
+        $order['pay_type'] = $paytype; //重新支付选择支付方式
+        switch ($order['pay_type']) {
+            case '0':
+                try {
+                    if ($from == 'routine') {
+                        $jsConfig = LaveRepository::jsPaySign($this->cid, $order); //订单列表发起支付
+                    } else if ($from == 'weixinh5') {
+                        $jsConfig = LaveRepository::h5PaySign($this->cid, $order);
+                    } else {
+                        $jsConfig = LaveRepository::wxPaySign($this->cid, $order);
+                    }
+                } catch (\Exception $e) {
+                    $this->error($e->getMessage());
+                }
+                $this->success('获取成功', $jsConfig);
+                break;
+        }
+        return $this->error('支付方式错误');
+    }
 }
 
 ?>

+ 2 - 0
application/route.php

@@ -79,10 +79,12 @@ Route::group('api', function () {
     //爱心捐款
     Route::group('lave', function () {
         Route::post('create', 'api/lave/create');
+        Route::post('create_sign', 'api/lave/createSign');  //支付签约
         Route::get('ify', 'api/lave/ify');
         Route::get('lst', 'api/lave/lst');
         Route::get('mylst', 'api/lave/mylst');
         Route::get('pay', 'api/lave/pay');
+        Route::get('paySign', 'api/lave/paySign');
     });
     //帮扶
     Route::group('help', function () {

+ 24 - 2
extend/liuniu/MiniProgramService.php

@@ -111,7 +111,7 @@ class MiniProgramService
     public static function paymentOrder($openid, $out_trade_no, $total_fee, $attach, $body, $detail = '', $trade_type = 'JSAPI', $options = [],$cid=0)
     {
         $total_fee = bcmul($total_fee, 100, 0);
-        $order = array_merge(compact('out_trade_no', 'total_fee', 'attach', 'body', 'detail', 'trade_type'), $options);
+        $order = array_merge(compact('out_trade_no', 'total_ fee', 'attach', 'body', 'detail', 'trade_type'), $options);
         if (!is_null($openid)) $order['openid'] = $openid;
         if ($order['detail'] == '') unset($order['detail']);
         $result = self::payment(false,$cid)->order->unify($order);
@@ -120,7 +120,6 @@ class MiniProgramService
 
 
 
-
     /**
      * 使用商户订单号退款
      * @param $orderNo
@@ -177,5 +176,28 @@ class MiniProgramService
         return $token;
 
     }
+    /**
+     * 生成支付签约订单对象
+     * @param $openid
+     * @param $out_trade_no
+     * @param $total_fee
+     * @param $attach
+     * @param $body
+     * @param string $detail
+     * @param string $trade_type
+     * @param array $options
+     * @return Order
+     */
+    public static function paysignedOrder($openid, $out_trade_no, $total_fee, $attach, $body, $detail = '', $trade_type = 'JSAPI', $options = [],$cid=0,$plan_id=0,$contract_display_account='')
+    {
+        $total_fee = bcmul($total_fee, 100, 0);
+        $order = array_merge(compact('out_trade_no', 'total_fee', 'attach', 'body', 'detail', 'trade_type'), $options);
+        if (!is_null($openid)) $order['openid'] = $openid;
+        if ($order['detail'] == '') unset($order['detail']);
+        $order['contract_notify_url']=Request::instance()->domain() . "/api/wechat/notify/" . $cid;
+        $order['contract_display_account']=$contract_display_account;
+        $result = self::payment(false,$cid)->order->unify($order,true);
+        return $result;
+    }
 
 }

+ 24 - 1
extend/liuniu/WechatService.php

@@ -299,7 +299,7 @@ class WechatService
         $total_fee = bcmul($total_fee, 100, 0);
         $order = array_merge(compact('out_trade_no', 'total_fee', 'attach', 'body', 'detail', 'trade_type', 'openid'), $options);
         if ($order['detail'] == '') unset($order['detail']);
-
+        @file_put_contents("quanju.txt", json_encode($order)."-微信支付传值\r\n", 8);
         $result = self::payment(false, $cid)->order->unify(
             $order
         );
@@ -509,4 +509,27 @@ class WechatService
         $userInfo = is_array($openid) ? $userService->select($openid) : $userService->get($openid);
         return $userInfo;
     }
+    /**
+     * 生成支付签约订单对象
+     * @param $openid
+     * @param $out_trade_no
+     * @param $total_fee
+     * @param $attach
+     * @param $body
+     * @param string $detail
+     * @param string $trade_type
+     * @param array $options
+     * @return Order
+     */
+    public static function paysignedOrder($openid, $out_trade_no, $total_fee, $attach, $body, $detail = '', $trade_type = 'JSAPI', $options = [], $cid = 0,$plan_id=0,$contract_display_account='')
+    {
+        $total_fee = bcmul($total_fee, 100, 0);
+        $order = array_merge(compact('out_trade_no', 'total_fee', 'attach', 'body', 'detail', 'trade_type', 'openid'), $options);
+        if ($order['detail'] == '') unset($order['detail']);
+        $order['contract_notify_url']=Request::instance()->domain() . "/api/wechat/notify/" . $cid;
+        $result = self::payment(false, $cid)->order->unify(
+            $order,true
+        );
+        return $result;
+    }
 }

+ 70 - 0
extend/liuniu/repositories/LaveRepository.php

@@ -79,4 +79,74 @@ class LaveRepository
         if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
         return WechatService::payment(false, $cid)->paymentOrder(null, $orderInfo['order_id'], $orderInfo['amount'], "lave", $orderInfo['order_name'], '', 'MWEB', [], $cid);
     }
+    /**
+     * 微信公众号JS支付签约
+     * @param $orderId
+     * @param string $field
+     * @return array|string
+     * @throws Exception
+     */
+    public static function wxpaySign($cid, $orderId, $field = "order_id")
+    {
+        if (is_string($orderId))
+            $orderInfo = LaveModel::where($field, $orderId)->find();
+        else
+            $orderInfo = $orderId;
+        if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
+        if ($orderInfo['paid']) exception('支付已支付!');
+        if ($orderInfo['amount'] <= 0) exception('该支付无需支付!');
+        $openid = UserRelation::userIdToOpenId($orderInfo['user_id']);
+        $rs = WechatService::paysignedOrder($openid, $orderInfo['order_id'], $orderInfo['amount'], "lave", $orderInfo['order_name'], '', 'JSAPI', [], $cid);
+        return WechatService::jspay($cid, $rs['prepay_id']);
+
+    }
+
+    /**
+     * 小程序JS支付签约
+     * @param $orderId
+     * @param string $field
+     * @return array|string
+     * @throws DataNotFoundException
+     * @throws ModelNotFoundException
+     * @throws DbException
+     * @throws Exception
+     */
+    public static function jsPaySign($cid, $orderId, $field = 'order_id')
+    {
+        if (is_string($orderId))
+            $orderInfo = LaveModel::where($field, $orderId)->find();
+        else
+            $orderInfo = $orderId;
+        if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
+        if ($orderInfo['paid']) exception('支付已支付!');
+        if ($orderInfo['amount'] <= 0) exception('该支付无需支付!');
+        $openid = UserRelation::userIdToOpenId($orderInfo['user_id'], 'routine_openid');
+        $bodyContent = $orderInfo['intention'];
+        $site_name = sys_config('site_name');
+        if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
+        return MiniProgramService::paysignedOrder($openid, $orderInfo['order_id'], $orderInfo['amount'], 'lave', $orderInfo['order_name'], '', '', [], $cid);
+
+    }
+
+    /**
+     * 微信h5支付签约
+     * @param $orderId
+     * @param string $field
+     * @return array|string
+     * @throws Exception
+     */
+    public static function h5PaySign($cid, $orderId, $field = 'order_id')
+    {
+        if (is_string($orderId))
+            $orderInfo = Donate::where($field, $orderId)->find();
+        else
+            $orderInfo = $orderId;
+        if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
+        if ($orderInfo['paid']) exception('支付已支付!');
+        if ($orderInfo['amount'] <= 0) exception('该支付无需支付!');
+        $bodyContent = $orderInfo['intention'];
+        $site_name = sys_config('site_name');
+        if (!$bodyContent && !$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
+        return WechatService::payment(false, $cid)->paysignedOrder(null, $orderInfo['order_id'], $orderInfo['amount'], "lave", $orderInfo['order_name'], '', 'MWEB', [], $cid);
+    }
 }