Ver código fonte

支付签约

WIN-2308041133\Administrator 5 meses atrás
pai
commit
ff76d06688

+ 5 - 1
application/admin/lang/zh-cn/wechat/plan_record.php

@@ -3,8 +3,12 @@
 return [
     'Cid'        => '单位编号',
     'Plan_id'    => '模版id',
-    'Wechat_Plan_id'     => '微信模版id',
     'price'     => '签约扣款金额',
+    'contract_display_account'     => '用户展示名称',
+    'contract_id'     => '委托代扣协议id	',
+    'Is_open'     => '是否实名公开',
+    'Is_open 0'   => '不公开',
+    'Is_open 1'   => '公开',
     'Createtime' => '创建时间',
     'Updatetime' => '更新时间'
 ];

+ 9 - 0
application/admin/model/WechatPlan.php

@@ -13,4 +13,13 @@ class WechatPlan extends Model
     protected $createTime = 'createtime';
     protected $updateTime = 'updatetime';
 
+    public static function getNewCode()
+    {
+        do {
+            list($msec, $sec) = explode(' ', microtime());
+            $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
+            $orderId = 'qy' . $msectime . mt_rand(10000, 99999);
+        } while (self::where(['order_id' => $orderId])->find());
+        return $orderId;
+    }
 }

+ 4 - 0
application/admin/model/WechatPlanRecord.php

@@ -12,4 +12,8 @@ class WechatPlanRecord extends Model
     // 定义时间戳字段名
     protected $createTime = 'createtime';
     protected $updateTime = 'updatetime';
+    public function getIsOpenList()
+    {
+        return ['0' => __('Is_open 0'), '1' => __('Is_open 1')];
+    }
 }

+ 115 - 7
application/api/controller/Lave.php

@@ -2,11 +2,14 @@
 
 namespace app\api\controller;
 
+use app\admin\model\Company;
 use app\admin\model\WechatPlan;
+use app\admin\model\WechatPlanRecord;
 use app\common\controller\Api;
-use app\common\model\{Category, Lave as LaveModel};
+use app\common\model\{Category, Lave as LaveModel, User};
 use liuniu\repositories\LaveRepository;
 use liuniu\UtilService;
+use liuniu\WechatService;
 use think\Request;
 
 class Lave extends Api
@@ -144,7 +147,7 @@ class Lave extends Api
 //    支付中签约
     public function createSign(Request $request)
     {
-        var_dump(123);die();
+//        var_dump(123);die();
         $where = UtilService::postMore(
             [
                 ['cid', $this->cid],
@@ -167,6 +170,12 @@ class Lave extends Api
         $where1 = $where;
         unset($where1['from']);
         $where1['order_id'] = LaveModel::getNewOrderId();
+        $where1['contract_code'] = WechatPlan::getNewCode();
+        $contract_display_account = User::where('id', $where1['user_id'])->value('nickname');
+        if (empty($contract_display_account)){
+            $this->error('用户不存在!');
+        }
+        $where1['contract_display_account']=$contract_display_account;
         if (cache('lave_' . $where['user_id'])) $this->error('正在处理中');
         @file_put_contents("lave.txt", json_encode($where1));
         $order = LaveModel::create($where1);
@@ -174,18 +183,32 @@ class Lave extends Api
         if (!$order) $this->error(LaveModel::getErrorInfo());
         $orderId = $order['order_id'];
         $info = compact('orderId');
-        $order['plan_id'] = WechatPlan::where('price', $where['amount'])->value('plan_id');
+        $order['plan_id'] = WechatPlan::where('cid', $where['cid'])->value('plan_id');
         if ($orderId) {
             $orderInfo = LaveModel::where('order_id', $orderId)->find();
             if (!$orderInfo || !isset($orderInfo['paid'])) $this->error('支付订单不存在!');
             if ($orderInfo['paid']) $this->error('支付已支付!');
+//          创建签约
+            $plan_record=[
+                'plan_id'=>WechatPlan::where('price', $where['amount'])->value('plan_id'),
+                'cid'=>$where['cid'],
+                'uid'=>$where['user_id'],
+                'price'=>$where['amount'],
+                'is_signing'=>0,
+                'contract_code'=>$where1['contract_code'],
+                'contract_display_account'=>$where1['contract_display_account'],
+                'is_open'=>$where1['is_open'],
+            ];
+            $record=WechatPlanRecord::create($plan_record);
+            $plan_record['spbill_create_ip']=User::where('id', $where1['user_id'])->value('loginip');
+            $plan_record['contract_notify_url']=Request::instance()->domain() . "/api/wechat/notify/" . $where['cid'];
             try {
                 if ($where['from'] == 'routine') {
-                    $jsConfig = LaveRepository::jsPaySign($this->cid, $orderId); //创建订单jspay
+                    $jsConfig = LaveRepository::jsPaySign($this->cid, $orderId,$plan_record); //创建订单jspay
                 } else if ($where['from'] == 'weixinh5') {
-                    $jsConfig = LaveRepository::h5PaySign($this->cid, $orderId);
+                    $jsConfig = LaveRepository::h5PaySign($this->cid, $orderId,$plan_record);
                 } else {
-                    $jsConfig = LaveRepository::wxPaySign($this->cid, $orderId);
+                    $jsConfig = LaveRepository::wxPaySign($this->cid, $orderId,$plan_record);
                 }
             } catch (\Exception $e) {
                 return $this->error($e->getMessage());
@@ -203,7 +226,7 @@ class Lave extends Api
      */
     public function paySign(Request $request)
     {
-        var_dump(456);die();
+//        var_dump(456);die();
         list($uni, $paytype, $from) = UtilService::postMore([
             ['uni', ''],
             ['paytype', '0'],
@@ -234,6 +257,91 @@ class Lave extends Api
         }
         return $this->error('支付方式错误');
     }
+    //    申请扣款
+    public function payPap(Request $request)
+    {
+        $list=WechatPlanRecord::where('is_signing',0)->select();
+        $cid=$this->cid;
+
+        $arr=[];
+
+        $arr['body']='月捐款';
+
+        foreach ($list as $k => $v){
+            $cid=$v['cid'];
+            $arr['mch_id'] =Company::where('id', $cid)->value('mch_id');
+            $arr['out_trade_no']=LaveModel::getNewOrderId();
+            $arr['total_fee'] = $v['price'];
+//            $arr['trade_type'] = 'PAP';
+            $arr['contract_id'] = Company::where('id', $cid)->value('contract_id');
+//            $rs = WechatService::paymentOrder($openid, $orderInfo['order_id'], $orderInfo['amount'], "lave", $orderInfo['order_name'], '', 'JSAPI', [], $cid);
+            $rs = WechatService::papPayApply($arr['out_trade_no'], $arr['total_fee'], "lave", '月捐款', 'PAP', [], $cid,$arr['mch_id'],$arr['contract_id']);
+            $where1['cid']=$cid;
+            $where1['user_id']=$this->auth->getUserinfo()['id'];
+            $where1['order_name']='月捐款';
+            $where1['category_id']=81;
+            $where1['amount']=$arr['total_fee'];
+            $where1['name']=$this->auth->getUserinfo()['username'];
+            $where1['contact']='';
+            $where1['tel']=$this->auth->getUserinfo()['mobile'];
+            $where1['address']='用户未填写联系地址';
+            $where1['is_open']=$v['is_open'];
+            $where1['is_ticket']='0';
+            $where1['type']='0';
+            $where1['help_id']='0';
+            $order = LaveModel::create($where1);
+        }
+
+
+        $where = UtilService::postMore(
+            [
+                ['cid', $this->cid],
+                ['user_id', $this->auth->getUserinfo()['id']],
+                ['order_name', 'order_name'],
+                ['category_id', 0],
+                ['amount', 0],
+                ['name', ''],
+                ['contact', ''],
+                ['tel', 0],
+                ['address', ''],
+                ['is_open', '0'],
+                ['is_ticket', '0'],
+                ['type', '0'],
+                ['help_id', 0],
+            ], $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());
+    }
 }
 
 ?>

+ 2 - 3
application/api/controller/Wechat.php

@@ -178,7 +178,7 @@ class Wechat extends Api
 
             WechatPlanRecord::create([
                 'uid' => $uid,
-                'plan_id' => $p_id,
+                'plan_id' => $plan_id,
                 'price' => $price,
                 'is_signing' => 0,
                 'request_serial' => $request_serial,
@@ -186,7 +186,6 @@ class Wechat extends Api
                 'contract_display_account' => $contract_display_account,
                 'createtime' => time(),
                 'cid' => 12,
-                'wechat_plan_id' => $plan_id,
             ]);
 //            die();
             $this->success('获取成功', $response);
@@ -216,7 +215,7 @@ class Wechat extends Api
             $app_id = Company::where('id', $cid)->value('wechat_appid');
             $mch_id = Company::where('id', $cid)->value('pay_weixin_mchid');
             $contract_code = WechatPlanRecord::where('id', $record_id)->value('contract_code');
-            $plan_id = WechatPlanRecord::where('id', $record_id)->value('wechat_plan_id');
+            $plan_id = WechatPlanRecord::where('id', $record_id)->value('plan_id');
             $contract_termination_remark = '解约备注';
             $version = '1.0';
             $array = array(

+ 4 - 6
extend/liuniu/MiniProgramService.php

@@ -111,8 +111,8 @@ 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);
-        if (!is_null($openid)) $order['openid'] = $openid;
+        $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);
         return $result;
@@ -188,14 +188,12 @@ class MiniProgramService
      * @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='')
+    public static function paysignedOrder($openid, $out_trade_no, $total_fee, $attach, $body,$contract_code,$plan_id,$spbill_create_ip, $detail = '', $trade_type = 'JSAPI', $options = [],$cid=0,$contract_display_account='',$contract_notify_url='')
     {
         $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','contract_code','spbill_create_ip','plan_id','contract_display_account','contract_notify_url'), $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;
     }

+ 26 - 2
extend/liuniu/WechatService.php

@@ -521,10 +521,10 @@ class WechatService
      * @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='')
+    public static function paysignedOrder($openid, $out_trade_no, $total_fee, $attach, $body,$contract_code, $plan_id,$spbill_create_ip,$detail = '', $trade_type = 'JSAPI', $options = [], $cid = 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);
+        $order = array_merge(compact('out_trade_no', 'total_fee', 'attach', 'body', 'detail', 'trade_type', 'openid','contract_code','plan_id','spbill_create_ip','contract_display_account'), $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(
@@ -532,4 +532,28 @@ class WechatService
         );
         return $result;
     }
+    /**
+     * 签约申请扣款
+     * @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 papPayApply($out_trade_no, $total_fee, $attach, $detail = '', $trade_type = 'PAP', $options = [], $cid = 0,$mch_id='',$contract_id='')
+    {
+        $total_fee = bcmul($total_fee, 100, 0);
+        $order = array_merge(compact('out_trade_no', 'total_fee', 'attach', 'detail', 'trade_type','mch_id','contract_id'), $options);
+        if ($order['detail'] == '') unset($order['detail']);
+        $order['notify_url']=Request::instance()->domain() . "/api/wechat/notify/" . $cid;
+        $result = self::payment(false, $cid)->contract->apply(
+            $order
+        );
+        return $result;
+    }
+
 }

+ 9 - 6
extend/liuniu/repositories/LaveRepository.php

@@ -86,7 +86,7 @@ class LaveRepository
      * @return array|string
      * @throws Exception
      */
-    public static function wxpaySign($cid, $orderId, $field = "order_id")
+    public static function wxpaySign($cid, $orderId,$plan_record, $field = "order_id")
     {
         if (is_string($orderId))
             $orderInfo = LaveModel::where($field, $orderId)->find();
@@ -96,7 +96,8 @@ class LaveRepository
         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);
+//                                          ($openid, $out_trade_no,         $total_fee,                  $attach,  $body,                   $contract_code,              $plan_id,              $spbill_create_ip                   ,$detail = '', $trade_type = 'JSAPI', $options = [], $cid = 0,$contract_display_account='')
+        $rs = WechatService::paysignedOrder($openid, $orderInfo['order_id'], $orderInfo['amount'], "lave", $orderInfo['order_name'], $plan_record['contract_code'],$plan_record['plan_id'],$plan_record['spbill_create_ip'],'', 'JSAPI', [], $cid,$plan_record['contract_display_account']);
         return WechatService::jspay($cid, $rs['prepay_id']);
 
     }
@@ -111,7 +112,7 @@ class LaveRepository
      * @throws DbException
      * @throws Exception
      */
-    public static function jsPaySign($cid, $orderId, $field = 'order_id')
+    public static function jsPaySign($cid, $orderId,$plan_record, $field = 'order_id')
     {
         if (is_string($orderId))
             $orderInfo = LaveModel::where($field, $orderId)->find();
@@ -124,7 +125,8 @@ class LaveRepository
         $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);
+//                                              ($openid, $out_trade_no,         $total_fee,                     $attach, $body,                  $contract_code,             $plan_id,$spbill_create_ip, $detail = '', $trade_type = 'JSAPI', $options = [],        $cid=0,$contract_display_account='',            $contract_notify_url='')
+        return MiniProgramService::paysignedOrder($openid, $orderInfo['order_id'], $orderInfo['amount'], 'lave', $orderInfo['order_name'], $plan_record['contract_code'],$plan_record['plan_id'],$plan_record['spbill_create_ip'],'', '', [], $cid,$plan_record['contract_display_account'],$plan_record['contract_notify_url']);
 
     }
 
@@ -135,7 +137,7 @@ class LaveRepository
      * @return array|string
      * @throws Exception
      */
-    public static function h5PaySign($cid, $orderId, $field = 'order_id')
+    public static function h5PaySign($cid, $orderId,$plan_record, $field = 'order_id')
     {
         if (is_string($orderId))
             $orderInfo = Donate::where($field, $orderId)->find();
@@ -147,6 +149,7 @@ class LaveRepository
         $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);
+        //                                                            ($openid, $out_trade_no,         $total_fee,           $attach,  $body,                   $contract_code,              $plan_id,              $spbill_create_ip             ,$detail = '', $trade_type = 'JSAPI', $options = [], $cid = 0,$contract_display_account='')
+        return WechatService::payment(false, $cid)->paysignedOrder(null, $orderInfo['order_id'], $orderInfo['amount'], "lave", $orderInfo['order_name'],$plan_record['contract_code'],$plan_record['plan_id'],$plan_record['spbill_create_ip'], '', 'MWEB', [], $cid.$plan_record['contract_display_account']);
     }
 }

+ 0 - 1
public/assets/js/backend/wechat/plan_record.js

@@ -25,7 +25,6 @@ define(['jquery', 'bootstrap', 'backend', 'form', 'table'], function ($, undefin
                         {field: 'id', title: __('Id')},
                         {field: 'cid', title: __('Cid')},
                         {field: 'plan_id', title: __('Plan_id')},
-                        {field: 'wechat_plan_id', title: __('Wechat_Plan_id')},
                         // {field: 'text', title: __('Text')},
                         {field: 'price', title: __('price')},
                         {field: 'createtime', title: __('Create time'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},

+ 3 - 1
vendor/overtrue/wechat/src/Payment/Order/Client.php

@@ -41,11 +41,13 @@ class Client extends BaseClient
         $params['notify_url'] = $params['notify_url'] ?? $this->app['config']['notify_url'];
 
         if ($isContract) {
+            $params['mchid'] = $this->app['config']['mch_id'];
+//            $params['nonce_str'] = uniqid('micro');
             $params['contract_appid'] = $this->app['config']['app_id'];
             $params['contract_mchid'] = $this->app['config']['mch_id'];
             $params['request_serial'] = $params['request_serial'] ?? time();
             $params['contract_notify_url'] = $params['contract_notify_url'] ?? $this->app['config']['contract_notify_url'];
-
+            @file_put_contents("quanju.txt", json_encode($params) . "-传参\r\n", 8);
             return $this->request($this->wrap('pay/contractorder'), $params);
         }