|
@@ -65,7 +65,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);
|
|
|
+ /*$pay = Service::checkNotify($paytype);
|
|
|
if (!$pay) {
|
|
|
echo '签名错误';
|
|
|
exit;
|
|
@@ -86,10 +86,10 @@ class Pay extends Api
|
|
|
{
|
|
|
return $pay->success()->send();
|
|
|
exit;
|
|
|
- }
|
|
|
- //$out_trade_no = 'R230605144624823142892';
|
|
|
+ }*/
|
|
|
+ $out_trade_no = 'O230606102813244205462';
|
|
|
//你可以在此编写订单逻辑
|
|
|
- $rs = $this->$func($out_trade_no);
|
|
|
+ $rs = $this->$func($out_trade_no);echo '<pre>';var_dump($rs);exit;
|
|
|
if($rs === false){
|
|
|
//不论结果都应返回success
|
|
|
return $pay->success()->send();
|
|
@@ -243,7 +243,7 @@ class Pay extends Api
|
|
|
$data['table_name'] = 'recharge_config';
|
|
|
$data['table_id'] = $id;
|
|
|
|
|
|
- //$orderid = Db::name('pay_order')->insertGetId($data);
|
|
|
+ $orderid = Db::name('pay_order')->insertGetId($data);
|
|
|
|
|
|
$openid = $this->auth->mini_openid;
|
|
|
$httpStr = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'];
|
|
@@ -255,7 +255,7 @@ class Pay extends Api
|
|
|
'amount' => $data['order_amount'],
|
|
|
'method' => $platform,
|
|
|
'openid' => $openid,
|
|
|
- 'notifyurl' => $this->httpStr.'/api/pay/order_notify_base/paytype/'.$pay_type.'/func/recharge',
|
|
|
+ 'notifyurl' => $httpStr.'/api/pay/order_notify_base/paytype/'.$pay_type.'/func/recharge',
|
|
|
'returnurl' => '',
|
|
|
];
|
|
|
|
|
@@ -368,4 +368,183 @@ class Pay extends Api
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 购买套餐
|
|
|
+ * @return void
|
|
|
+ * @throws \Exception
|
|
|
+ */
|
|
|
+ public function package()
|
|
|
+ {
|
|
|
+ Db::startTrans();
|
|
|
+ try {
|
|
|
+ $pay_type = input('pay_type','wechat');
|
|
|
+ $platform = input('platform','miniapp');
|
|
|
+ $id = input('package_id','0');
|
|
|
+ $carId = input('car_id','0');
|
|
|
+ $uid = $this->auth->id;
|
|
|
+ $companyId = $this->auth->company_id;
|
|
|
+ if (empty($id)) {
|
|
|
+ throw new Exception('请选择套餐');
|
|
|
+ }
|
|
|
+ if (empty($carId)) {
|
|
|
+ throw new Exception('请选择车辆');
|
|
|
+ }
|
|
|
+ $userCarWhere['user_id'] = $uid;
|
|
|
+ $userCarWhere['id'] = $carId;
|
|
|
+ $userCar = Db::name('user_car')->where($userCarWhere)->find();
|
|
|
+ if (empty($userCar)) {
|
|
|
+ throw new Exception('未找到车辆信息');
|
|
|
+ }
|
|
|
+ $packageWhere['id'] = $id;
|
|
|
+ $packageWhere['status'] = 1;
|
|
|
+ $package = Db::name('package')->where($packageWhere)->find();
|
|
|
+ if (empty($package)) {
|
|
|
+ throw new Exception('未找到套餐信息');
|
|
|
+ }
|
|
|
+ if ($package['company_id'] != $companyId) {
|
|
|
+ throw new Exception('您绑定的门店和购买套餐门店不一致');
|
|
|
+ }
|
|
|
+ $gift_amount = 0.00;
|
|
|
+ $order_amount = $package['price'];
|
|
|
+ $extData = ['car_id'=>$carId];
|
|
|
+ //创建订单
|
|
|
+ $data['company_id'] = $companyId;
|
|
|
+ $data['user_id'] = $uid;
|
|
|
+ $data['out_trade_no'] = createUniqueNo('O',$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'] = 'package';
|
|
|
+ $data['table_id'] = $id;
|
|
|
+ $data['ext_data'] = json_encode($extData);
|
|
|
+
|
|
|
+ $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' => $httpStr.'/api/pay/order_notify_base/paytype/'.$pay_type.'/func/package_do',
|
|
|
+ '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 package_do($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;
|
|
|
+ }
|
|
|
+ //生成订单
|
|
|
+
|
|
|
+ //记录余额
|
|
|
+ //赠送优惠券
|
|
|
+ $rg = 'recharge_gift';
|
|
|
+ $c = 'coupons';
|
|
|
+ $field = $rg.'.*,'.$c.'.name,'.$c.'.info,'.$c.'.days';
|
|
|
+ $rechargeGiftWhere['config_id'] = $orderInfo['table_id'];
|
|
|
+ $rechargeGiftWhere[$c.'.status'] = 1;
|
|
|
+ $rechargeGift = Db::name($rg)->alias($rg)->field($field)
|
|
|
+ ->join($c,$c.'.id = '.$rg.'.coupon_id','LEFT')->where($rechargeGiftWhere)->select();
|
|
|
+ if (!empty($rechargeGift)) {
|
|
|
+ $time = time();
|
|
|
+ foreach ($rechargeGift as $key => $value) {
|
|
|
+ $endtime = $time + 86400 * $value['days'];
|
|
|
+ $userCouponsData[] = [
|
|
|
+ 'user_id' => $orderInfo['user_id'],
|
|
|
+ 'company_id' => $orderInfo['company_id'],
|
|
|
+ 'coupons_id' => $value['coupon_id'],
|
|
|
+ 'coupon_name' => $value['name'],
|
|
|
+ 'coupon_info' => $value['info'],
|
|
|
+ 'createtime' => $time,
|
|
|
+ 'endtime' => $endtime,
|
|
|
+ 'number' => $value['number'],
|
|
|
+ 'remain' => $value['number'],
|
|
|
+ 'payorder_id' => $orderInfo['id'],
|
|
|
+ 'getfrom' => '充值赠送',
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ $userCouponsRes = Db::name('user_coupons')->insertAll($userCouponsData);
|
|
|
+ if (!$userCouponsRes) {
|
|
|
+ throw new Exception('赠送优惠失败');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $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;
|
|
|
+ }
|
|
|
+
|
|
|
}
|