| 
					
				 | 
			
			
				@@ -5,6 +5,7 @@ namespace app\api\controller; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 use app\common\controller\Api; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 use think\Db; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 use addons\epay\library\Service; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+use think\Exception; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  * 充值配置与充值订单 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  */ 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -62,6 +63,7 @@ class Pay extends Api 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         //验签 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         $paytype = input('paytype','wechat'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        $func = input('func','order_notify_do'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         $notify_file = $this->notify_log_start($paytype); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         $pay = Service::checkNotify($paytype); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         if (!$pay) { 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -85,9 +87,9 @@ class Pay extends Api 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             return $pay->success()->send(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             exit; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				- 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        //$out_trade_no = 'R230605144624823142892'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         //你可以在此编写订单逻辑 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				-        $rs = $this->order_notify_do($out_trade_no); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        $rs = $this->$func($out_trade_no); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				         if($rs === false){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             //不论结果都应返回success 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				             return $pay->success()->send(); 
			 | 
		
	
	
		
			
				| 
					
				 | 
			
			
				@@ -195,6 +197,143 @@ class Pay extends Api 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    //微信小程序充值 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    public function pay_recharge() 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Db::startTrans(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        try { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            $pay_type = input('pay_type','wechat'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            $platform = input('platform','miniapp'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            $id = input('id','0'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            $amounts = input('amounts','0.00'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            $uid = $this->auth->id; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            $companyId = $this->auth->company_id; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (empty($id) && empty($amounts)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                throw new Exception('参数错误'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            $gift_amount = 0.00; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if (!empty($id)) {//验证充值配置 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                $where['status'] = 1;//上架 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                $orderinfo = Db::name('recharge_config')->where('id',$id)->where($where)->find(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                if (empty($orderinfo)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    throw new Exception('未获取到充值套餐信息'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                $order_amount = $orderinfo['price']; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                $gift_amount = $orderinfo['giftprice']; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } else { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                $isInt = is_int($amounts); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                if (!$isInt) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    throw new Exception('请输入整数'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                if ($amounts < 1) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                    throw new Exception('充值金额有误'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                $order_amount = $amounts; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            //创建订单 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            $data['company_id'] = $companyId; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            $data['user_id'] = $uid; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            $data['out_trade_no'] = createUniqueNo('R',$uid); // 数据库订单号加密 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            $data['order_amount'] = $order_amount; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            $data['gift_amount'] = $gift_amount; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            $data['createtime'] = time(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            $data['pay_type'] = $pay_type; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            $data['order_status'] = 0; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            $data['table_name'] = 'recharge_config'; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            $data['table_id'] = $id; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            //$orderid = Db::name('pay_order')->insertGetId($data); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            $openid = $this->auth->mini_openid; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            $httpStr = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST']; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            //下单 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            $params = [ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                'type'         => $pay_type, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                'orderid'      => $data['out_trade_no'], 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                'title'        => '充值', 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                'amount'       => $data['order_amount'], 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                'method'       => $platform, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                'openid'       => $openid, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                'notifyurl' => $this->httpStr.'/api/pay/order_notify_base/paytype/'.$pay_type.'/func/recharge', 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                'returnurl' => '', 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            ]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            $res = Service::submitOrder($params); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            Db::commit(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            if($pay_type == 'wechat'){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                $this->success('success',json_decode($res,true)); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            }else{ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+                $this->success('success',$res); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } catch (Exception $e) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            Db::rollback(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            $this->error($e->getMessage()); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    /** 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * 充值到账 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @param $out_trade_no 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     * @return bool 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+     */ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    private function recharge($out_trade_no) 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Db::startTrans(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        $orderInfo = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->lock(true)->find(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (empty($orderInfo)) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            Db::rollback(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if($orderInfo['order_status'] != 0){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            Db::rollback(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        $userWalletWhere['user_id'] = $orderInfo['user_id']; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        $userWalletWhere['company_id'] = $orderInfo['company_id']; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        $userWalletData = Db::name('user_wallet')->where($userWalletWhere)->find(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        $before = isset($userWalletData['money']) ? $userWalletData['money'] : 0.00; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        $changeValue = bcadd($orderInfo['order_amount'],$orderInfo['gift_amount'],2); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        $remain = bcadd($before,$changeValue,2); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        $time = time(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        //逻辑开始 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        $userMoneyLogData = [ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            'user_id'      => $orderInfo['user_id'], 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            'company_id'   => $orderInfo['company_id'], 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            'log_type'     => 104, //日志类型 104 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            'before'       => $before, //之前余额 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            'change_value' => $changeValue, //变动金额 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            'remain'       => $remain, //剩余金额 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            'table'        => 'pay_order', //数据来源 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            'table_id'     => $orderInfo['id'], //数据来源ID 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            'remark'       => '充值', //remark 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            'createtime'   => $time, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        ]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        $userMoneyLogRes = Db::name('user_money_log')->insertGetId($userMoneyLogData); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if (!$userMoneyLogRes) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            throw new Exception('充值记录失败'); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        $update = [ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            'money' => $remain, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            'updatetime' => $time, 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        ]; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        $rs_order = Db::name('user_wallet')->where($userWalletWhere)->update($update); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if(!$rs_order){ 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            Db::rollback(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        //逻辑结束 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        //状态 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        $ros = Db::name('pay_order')->where(['out_trade_no' => $out_trade_no])->update(['order_status'=>1,'notifytime'=>time()]); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        if($ros === false) { 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            Db::rollback(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+            return false; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        //默认提交 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        Db::commit(); 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+        return true; 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				+    } 
			 | 
		
	
		
			
				 | 
				 | 
			
			
				  
			 | 
		
	
		
			
				 | 
				 | 
			
			
				 } 
			 |