|
@@ -2,6 +2,7 @@
|
|
|
|
|
|
namespace app\common\service;
|
|
|
|
|
|
+use app\common\model\GreenScoreLogModel;
|
|
|
use app\common\model\PaymentModel;
|
|
|
use app\common\model\ScoreLogModel;
|
|
|
use app\common\model\ServicesOrderModel;
|
|
@@ -23,19 +24,20 @@ class PaymentService
|
|
|
protected static $instance = null;
|
|
|
protected $cacheTime = 7200;
|
|
|
protected $payWayArr = [
|
|
|
- 1=>'wxpay',
|
|
|
- 2=>'alipay',
|
|
|
- 3=>'balancePay',
|
|
|
- 14=>'huifuPay',
|
|
|
- 15=>'sqzpay',
|
|
|
- 16=>'sqzAliPay',
|
|
|
- 17=>'sqzWxPay',
|
|
|
- 18=>'sqzYljk',
|
|
|
- 19=>'usdtPay',
|
|
|
- 20=>'yswkPay',
|
|
|
- 22=>'dkpay',
|
|
|
- 56=>'ysftyPay',
|
|
|
- 66=>'dkysf',
|
|
|
+ 1 => 'wxpay',
|
|
|
+ 2 => 'alipay',
|
|
|
+ 3 => 'balancePay',
|
|
|
+ 6 => 'greenScorePay',
|
|
|
+ 14 => 'huifuPay',
|
|
|
+ 15 => 'sqzpay',
|
|
|
+ 16 => 'sqzAliPay',
|
|
|
+ 17 => 'sqzWxPay',
|
|
|
+ 18 => 'sqzYljk',
|
|
|
+ 19 => 'usdtPay',
|
|
|
+ 20 => 'yswkPay',
|
|
|
+ 22 => 'dkpay',
|
|
|
+ 56 => 'ysftyPay',
|
|
|
+ 66 => 'dkysf',
|
|
|
];
|
|
|
protected $model = null;
|
|
|
|
|
@@ -50,7 +52,7 @@ class PaymentService
|
|
|
*/
|
|
|
public static function make()
|
|
|
{
|
|
|
- if(!self::$instance){
|
|
|
+ if (!self::$instance) {
|
|
|
self::$instance = new static();
|
|
|
}
|
|
|
|
|
@@ -64,7 +66,7 @@ class PaymentService
|
|
|
*/
|
|
|
public function getPayCode($payType)
|
|
|
{
|
|
|
- return isset($this->payWayArr[$payType])? $this->payWayArr[$payType] : 'defpay';
|
|
|
+ return isset($this->payWayArr[$payType]) ? $this->payWayArr[$payType] : 'defpay';
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -78,32 +80,32 @@ class PaymentService
|
|
|
* @throws \think\db\exception\DbException
|
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
|
*/
|
|
|
- public function getPaymentCountByState($uid, $orderSn='', $orderType, $state, $time = 2)
|
|
|
+ public function getPaymentCountByState($uid, $orderSn = '', $orderType, $state, $time = 2)
|
|
|
{
|
|
|
$cacheKey = "caches:paymentCall:u{$uid}_ot{$orderType}_s{$state}_{$time}_{$orderSn}";
|
|
|
$data = RedisCache::get($cacheKey);
|
|
|
- if($data){
|
|
|
+ if ($data) {
|
|
|
return $data;
|
|
|
}
|
|
|
$where = [];
|
|
|
- if($orderType){
|
|
|
+ if ($orderType) {
|
|
|
$where['order_type'] = $orderType;
|
|
|
}
|
|
|
|
|
|
- if($state){
|
|
|
+ if ($state) {
|
|
|
$where['state'] = $state;
|
|
|
}
|
|
|
|
|
|
- $data = $this->model->where($where)->where(function ($query) use($time,$orderSn){
|
|
|
- if($time>0){
|
|
|
- $query->where('creat_at','>=', time() - $time * 3600);
|
|
|
+ $data = $this->model->where($where)->where(function ($query) use ($time, $orderSn) {
|
|
|
+ if ($time > 0) {
|
|
|
+ $query->where('creat_at', '>=', time() - $time * 3600);
|
|
|
}
|
|
|
- if($orderSn){
|
|
|
- $query->where('remarks',$orderSn);
|
|
|
+ if ($orderSn) {
|
|
|
+ $query->where('remarks', $orderSn);
|
|
|
}
|
|
|
})->count('id');
|
|
|
- if($data){
|
|
|
- RedisCache::set($cacheKey, $data, rand(3,5));
|
|
|
+ if ($data) {
|
|
|
+ RedisCache::set($cacheKey, $data, rand(3, 5));
|
|
|
}
|
|
|
|
|
|
return $data;
|
|
@@ -122,16 +124,16 @@ class PaymentService
|
|
|
{
|
|
|
$cacheKey = "caches:paymentState:u{$uid}_sn{$orderSn}";
|
|
|
$data = RedisCache::get($cacheKey);
|
|
|
- if($data){
|
|
|
+ if ($data) {
|
|
|
return $data;
|
|
|
}
|
|
|
- $data = $this->model->where(['state'=> 6])->where(function ($query) use($orderSn){
|
|
|
- if($orderSn){
|
|
|
+ $data = $this->model->where(['state' => 6])->where(function ($query) use ($orderSn) {
|
|
|
+ if ($orderSn) {
|
|
|
$query->where('remarks', $orderSn);
|
|
|
}
|
|
|
})->value('id');
|
|
|
- if($data){
|
|
|
- RedisCache::set($cacheKey, $data, rand(2,3));
|
|
|
+ if ($data) {
|
|
|
+ RedisCache::set($cacheKey, $data, rand(2, 3));
|
|
|
}
|
|
|
|
|
|
return $data;
|
|
@@ -145,24 +147,24 @@ class PaymentService
|
|
|
* @param false $cache
|
|
|
* @return array|mixed
|
|
|
*/
|
|
|
- public function getCacheInfo($outTradeNo, $state=7, $field='', $cache = false)
|
|
|
+ public function getCacheInfo($outTradeNo, $state = 7, $field = '', $cache = false)
|
|
|
{
|
|
|
- $cacheKey = "caches:payment:info:otn{$outTradeNo}_{$state}".($field? '_'.md5($field):'');
|
|
|
+ $cacheKey = "caches:payment:info:otn{$outTradeNo}_{$state}" . ($field ? '_' . md5($field) : '');
|
|
|
$data = RedisCache::get($cacheKey);
|
|
|
- if($data && $cache){
|
|
|
+ if ($data && $cache) {
|
|
|
return $data;
|
|
|
}
|
|
|
- $where = ['out_trade_no'=> $outTradeNo];
|
|
|
- if($state){
|
|
|
+ $where = ['out_trade_no' => $outTradeNo];
|
|
|
+ if ($state) {
|
|
|
$where['state'] = $state;
|
|
|
}
|
|
|
- $field = $field? $field : 'id,out_trade_no,uid,total_fee,state,trade_type,out_trade_no1,hy_token_id,syl_sureorderid,hy_bill_no,is_retreat,pay_way,order_type,sid,remarks,trade_no';
|
|
|
+ $field = $field ? $field : 'id,out_trade_no,uid,total_fee,state,trade_type,out_trade_no1,hy_token_id,syl_sureorderid,hy_bill_no,is_retreat,pay_way,order_type,sid,remarks,trade_no';
|
|
|
$data = $this->model->where($where)
|
|
|
->field($field)
|
|
|
->order('creat_at desc')
|
|
|
->findOrEmpty();
|
|
|
- if($data && $cache){
|
|
|
- RedisCache::set($cacheKey, $data, rand(5,10));
|
|
|
+ if ($data && $cache) {
|
|
|
+ RedisCache::set($cacheKey, $data, rand(5, 10));
|
|
|
}
|
|
|
|
|
|
return $data;
|
|
@@ -170,7 +172,7 @@ class PaymentService
|
|
|
|
|
|
|
|
|
/**
|
|
|
- * 支付回调处理
|
|
|
+ * 线上支付回调处理
|
|
|
* @param $outTradeNo
|
|
|
* @param $payMoney
|
|
|
* @param $payType
|
|
@@ -183,37 +185,42 @@ class PaymentService
|
|
|
public function payBack($outTradeNo, $payMoney, $payType, $content)
|
|
|
{
|
|
|
$nowTime = date('Y-m-d H:i:s');
|
|
|
- $params = $content? json_decode($content, true):[];
|
|
|
- $payCode = PaymentService::make()->getPayCode($payType);
|
|
|
+ $params = $content ? json_decode($content, true) : [];
|
|
|
+ $payConfig = PayConfigService::make()->getInfoByChannel($payType, 1, 1);
|
|
|
+ $payCode = isset($payConfig['pay_code']) && $payConfig['pay_code']? $payConfig['pay_code'] : '';
|
|
|
+ if(empty($payConfig) || empty($payCode)){
|
|
|
+ sr_throw('该支付暂未开放');
|
|
|
+ }
|
|
|
+
|
|
|
$cacheKey = "caches:payNotify:{$payCode}:otn_{$outTradeNo}:";
|
|
|
- RedisCache::set($cacheKey.'catch', ['params'=>$params,'date'=> $nowTime], $this->cacheTime);
|
|
|
+ RedisCache::set($cacheKey . 'catch', ['params' => $params, 'date' => $nowTime], $this->cacheTime);
|
|
|
|
|
|
// 验证支付请求信息
|
|
|
$payInfo = PaymentService::make()->getCacheInfo($outTradeNo, 0);
|
|
|
- $payId = isset($payInfo['id'])? $payInfo['id']:0;
|
|
|
- $payUid = isset($payInfo['uid'])? $payInfo['uid']:0;
|
|
|
- $payState = isset($payInfo['state'])? intval($payInfo['state']):0;
|
|
|
- $totalFee = isset($payInfo['total_fee'])? floatval($payInfo['total_fee']):0;
|
|
|
- $orderSn = isset($payInfo['remarks'])? trim($payInfo['remarks']):'';
|
|
|
- $orderType = isset($payInfo['order_type'])? intval($payInfo['order_type']) : 0;
|
|
|
- if (!$payInfo || $payUid<=0 || empty($orderSn) || $payState!= 7) {
|
|
|
- $error = empty($payInfo) || !$payId || empty($orderSn)? "单号{$orderSn}支付信息不存在或参数错误":"单号{$orderSn}已经回调支付";
|
|
|
- $logData = ['params'=>$params,'payInfo'=>$payInfo,'msg'=> $error,'date'=> $nowTime];
|
|
|
- RedisCache::set($cacheKey.'error', $logData, $this->cacheTime);
|
|
|
+ $payId = isset($payInfo['id']) ? $payInfo['id'] : 0;
|
|
|
+ $payUid = isset($payInfo['uid']) ? $payInfo['uid'] : 0;
|
|
|
+ $payState = isset($payInfo['state']) ? intval($payInfo['state']) : 0;
|
|
|
+ $totalFee = isset($payInfo['total_fee']) ? floatval($payInfo['total_fee']) : 0;
|
|
|
+ $orderSn = isset($payInfo['remarks']) ? trim($payInfo['remarks']) : '';
|
|
|
+ $orderType = isset($payInfo['order_type']) ? intval($payInfo['order_type']) : 0;
|
|
|
+ if (!$payInfo || $payUid <= 0 || empty($orderSn) || $payState != 7) {
|
|
|
+ $error = empty($payInfo) || !$payId || empty($orderSn) ? "单号{$orderSn}支付信息不存在或参数错误" : "单号{$orderSn}已经回调支付";
|
|
|
+ $logData = ['params' => $params, 'payInfo' => $payInfo, 'msg' => $error, 'date' => $nowTime];
|
|
|
+ RedisCache::set($cacheKey . 'error', $logData, $this->cacheTime);
|
|
|
sr_throw($error);
|
|
|
}
|
|
|
|
|
|
// 验证支付金额
|
|
|
- if($payMoney < $totalFee || $totalFee<=0){
|
|
|
- $logData = ['params'=>$params,'payInfo'=>$payInfo,'msg'=> "单号{$orderSn}支付金额错误",'date'=> $nowTime];
|
|
|
- RedisCache::set($cacheKey.'error', $logData, $this->cacheTime);
|
|
|
+ if ($payMoney < $totalFee || $totalFee <= 0) {
|
|
|
+ $logData = ['params' => $params, 'payInfo' => $payInfo, 'msg' => "单号{$orderSn}支付金额错误", 'date' => $nowTime];
|
|
|
+ RedisCache::set($cacheKey . 'error', $logData, $this->cacheTime);
|
|
|
sr_throw("单号{$orderSn}支付金额错误");
|
|
|
}
|
|
|
|
|
|
// 更新支付状态
|
|
|
- if(!PaymentModel::where('id', $payId)->update(['state' => 6, 'pay_at' => $nowTime])){
|
|
|
- $logData = ['params'=>$params,'payInfo'=>$payInfo,'msg'=> "单号{$orderSn}更新支付状态失败",'date'=> $nowTime];
|
|
|
- RedisCache::set($cacheKey.'_error', $logData, $this->cacheTime);
|
|
|
+ if (!PaymentModel::where('id', $payId)->update(['state' => 6, 'pay_at' => $nowTime])) {
|
|
|
+ $logData = ['params' => $params, 'payInfo' => $payInfo, 'msg' => "单号{$orderSn}更新支付状态失败", 'date' => $nowTime];
|
|
|
+ RedisCache::set($cacheKey . '_error', $logData, $this->cacheTime);
|
|
|
sr_throw("单号{$orderSn}更新支付状态失败");
|
|
|
}
|
|
|
|
|
@@ -226,9 +233,9 @@ class PaymentService
|
|
|
'uid' => $payUid,
|
|
|
'money' => $payMoney
|
|
|
];
|
|
|
- if(!ThirdpayBackModel::insertGetid($data)){
|
|
|
- $logData = ['params'=>$params,'payInfo'=>$payInfo,'third'=>$data,'msg'=> "单号{$orderSn}处理第三方回调数据错误",'date'=> $nowTime];
|
|
|
- RedisCache::set($cacheKey.'_error', $logData, $this->cacheTime);
|
|
|
+ if (!ThirdpayBackModel::insertGetid($data)) {
|
|
|
+ $logData = ['params' => $params, 'payInfo' => $payInfo, 'third' => $data, 'msg' => "单号{$orderSn}处理第三方回调数据错误", 'date' => $nowTime];
|
|
|
+ RedisCache::set($cacheKey . '_error', $logData, $this->cacheTime);
|
|
|
sr_throw("单号{$orderSn}处理第三方回调数据错误");
|
|
|
}
|
|
|
|
|
@@ -236,67 +243,65 @@ class PaymentService
|
|
|
if ($orderType == 6) {
|
|
|
// 获取验证订单信息
|
|
|
$orderInfo = ServiceOrderService::make()->getInfoBySn($orderSn, $payUid);
|
|
|
- $orderId = isset($orderInfo['order_id'])? $orderInfo['order_id'] : 0;
|
|
|
- $orderMoney = isset($orderInfo['payment'])? floatval($orderInfo['payment']) : 0;
|
|
|
- $orderStatus = isset($orderInfo['status'])? intval($orderInfo['status']) : 0;
|
|
|
- if(empty($orderInfo) || $orderId<=0 || $orderMoney<=0){
|
|
|
- $logData = ['params'=>$params,'payInfo'=>$payInfo,'order'=>$orderInfo,'msg'=> "服务商订单{$orderSn}不存在或参数错误",'date'=> $nowTime];
|
|
|
- RedisCache::set($cacheKey.'_error', $logData, $this->cacheTime);
|
|
|
+ $orderId = isset($orderInfo['order_id']) ? $orderInfo['order_id'] : 0;
|
|
|
+ $orderMoney = isset($orderInfo['payment']) ? floatval($orderInfo['payment']) : 0;
|
|
|
+ $orderStatus = isset($orderInfo['status']) ? intval($orderInfo['status']) : 0;
|
|
|
+ if (empty($orderInfo) || $orderId <= 0 || $orderMoney <= 0) {
|
|
|
+ $logData = ['params' => $params, 'payInfo' => $payInfo, 'order' => $orderInfo, 'msg' => "服务商订单{$orderSn}不存在或参数错误", 'date' => $nowTime];
|
|
|
+ RedisCache::set($cacheKey . '_error', $logData, $this->cacheTime);
|
|
|
sr_throw("服务商订单{$orderSn}不存在或参数错误");
|
|
|
}
|
|
|
|
|
|
// 验证订单状态
|
|
|
- if($orderStatus != 1){
|
|
|
- $logData = ['params'=>$params,'payInfo'=>$payInfo,'order'=>$orderInfo,'msg'=> "服务商订单{$orderSn}订单已处理",'date'=> $nowTime];
|
|
|
- RedisCache::set($cacheKey.'_error', $logData, $this->cacheTime);
|
|
|
+ if ($orderStatus != 1) {
|
|
|
+ $logData = ['params' => $params, 'payInfo' => $payInfo, 'order' => $orderInfo, 'msg' => "服务商订单{$orderSn}订单已处理", 'date' => $nowTime];
|
|
|
+ RedisCache::set($cacheKey . '_error', $logData, $this->cacheTime);
|
|
|
sr_throw("服务商订单{$orderSn}订单已处理");
|
|
|
}
|
|
|
|
|
|
// 更新订单状态
|
|
|
- if(!ServicesOrderModel::where('order_id', $orderId)->update(['status' => 2, 'pay_type' => $payType, 'updated_time' => $nowTime])){
|
|
|
- $logData = ['params'=>$params,'payInfo'=>$payInfo,'order'=>$orderInfo,'msg'=> "服务商订单{$orderSn}更新状态错误",'date'=> $nowTime];
|
|
|
- RedisCache::set($cacheKey.'_error', $logData, $this->cacheTime);
|
|
|
+ if (!ServicesOrderModel::where('order_id', $orderId)->update(['status' => 2, 'pay_type' => $payType, 'updated_time' => $nowTime])) {
|
|
|
+ $logData = ['params' => $params, 'payInfo' => $payInfo, 'order' => $orderInfo, 'msg' => "服务商订单{$orderSn}更新状态错误", 'date' => $nowTime];
|
|
|
+ RedisCache::set($cacheKey . '_error', $logData, $this->cacheTime);
|
|
|
sr_throw("服务商订单{$orderSn}更新状态错误");
|
|
|
}
|
|
|
|
|
|
// 更新用户服务商有效期
|
|
|
$date = sr_getcurtime(time(), 'Y-m-d');
|
|
|
$expireDay = date('Y-m-d', strtotime("$date +1 month"));
|
|
|
- if(!UserModel::where('id', $payUid)->update(['store_type' => 1,'store_expire_time' => $expireDay,'update_time'=>time()])){
|
|
|
- $logData = ['params'=>$params,'payInfo'=>$payInfo,'order'=>$orderInfo,'storeExpire'=>$expireDay,'msg'=> "服务商订单{$orderSn}更新用户服务商有效期错误",'date'=> $nowTime];
|
|
|
- RedisCache::set($cacheKey.'_error', $logData, $this->cacheTime);
|
|
|
+ if (!UserModel::where('id', $payUid)->update(['store_type' => 1, 'store_expire_time' => $expireDay, 'update_time' => time()])) {
|
|
|
+ $logData = ['params' => $params, 'payInfo' => $payInfo, 'order' => $orderInfo, 'storeExpire' => $expireDay, 'msg' => "服务商订单{$orderSn}更新用户服务商有效期错误", 'date' => $nowTime];
|
|
|
+ RedisCache::set($cacheKey . '_error', $logData, $this->cacheTime);
|
|
|
sr_throw("服务商订单{$orderSn}更新用户服务商有效期错误");
|
|
|
}
|
|
|
|
|
|
- $logData = ['params'=>$params,'payInfo'=>$payInfo,'order'=>$orderInfo,'storeExpire'=>$expireDay,'msg'=> "服务商订单{$orderSn}支付回调处理成功",'date'=> $nowTime];
|
|
|
- RedisCache::set($cacheKey."success_{$orderSn}", $logData, $this->cacheTime);
|
|
|
+ $logData = ['params' => $params, 'payInfo' => $payInfo, 'order' => $orderInfo, 'storeExpire' => $expireDay, 'msg' => "服务商订单{$orderSn}支付回调处理成功", 'date' => $nowTime];
|
|
|
+ RedisCache::set($cacheKey . "success_{$orderSn}", $logData, $this->cacheTime);
|
|
|
|
|
|
- }
|
|
|
-
|
|
|
- /** TODO 商城订单处理 **/
|
|
|
+ } /** TODO 商城订单处理 **/
|
|
|
elseif ($orderType == 4) {
|
|
|
// 获取验证订单信息
|
|
|
$orderInfo = ShopOrderService::make()->getInfoBySn($orderSn, $payUid);
|
|
|
- $orderId = isset($orderInfo['order_id'])? $orderInfo['order_id'] : 0;
|
|
|
- $orderMoney = isset($orderInfo['payment'])? floatval($orderInfo['payment']) : 0;
|
|
|
- $orderStatus = isset($orderInfo['status'])? intval($orderInfo['status']) : -1;
|
|
|
- if(empty($orderInfo) || $orderId<=0 || $orderMoney<=0){
|
|
|
- $logData = ['params'=>$params,'payInfo'=>$payInfo,'order'=>$orderInfo,'msg'=> "商城订单{$orderSn}不存在或参数错误",'date'=> $nowTime];
|
|
|
- RedisCache::set($cacheKey."error_{$orderSn}", $logData, $this->cacheTime);
|
|
|
+ $orderId = isset($orderInfo['order_id']) ? $orderInfo['order_id'] : 0;
|
|
|
+ $orderMoney = isset($orderInfo['payment']) ? floatval($orderInfo['payment']) : 0;
|
|
|
+ $orderStatus = isset($orderInfo['status']) ? intval($orderInfo['status']) : -1;
|
|
|
+ if (empty($orderInfo) || $orderId <= 0 || $orderMoney <= 0) {
|
|
|
+ $logData = ['params' => $params, 'payInfo' => $payInfo, 'order' => $orderInfo, 'msg' => "商城订单{$orderSn}不存在或参数错误", 'date' => $nowTime];
|
|
|
+ RedisCache::set($cacheKey . "error_{$orderSn}", $logData, $this->cacheTime);
|
|
|
sr_throw("商城订单{$orderSn}不存在或参数错误");
|
|
|
}
|
|
|
|
|
|
// 验证订单状态
|
|
|
- if($orderStatus != 0){
|
|
|
- $logData = ['params'=>$params,'payInfo'=>$payInfo,'order'=>$orderInfo,'msg'=> "商城订单{$orderSn}订单已处理",'date'=> $nowTime];
|
|
|
- RedisCache::set($cacheKey."error_{$orderSn}", $logData, $this->cacheTime);
|
|
|
+ if ($orderStatus != 0) {
|
|
|
+ $logData = ['params' => $params, 'payInfo' => $payInfo, 'order' => $orderInfo, 'msg' => "商城订单{$orderSn}订单已处理", 'date' => $nowTime];
|
|
|
+ RedisCache::set($cacheKey . "error_{$orderSn}", $logData, $this->cacheTime);
|
|
|
sr_throw("商城订单{$orderSn}订单已处理");
|
|
|
}
|
|
|
|
|
|
// 更新订单状态
|
|
|
- if(!ShopOrderModel::where('order_id', $orderId)->update(['status' => 1, 'pay_type' => $payType, 'updated_time' => $nowTime])){
|
|
|
- $logData = ['params'=>$params,'payInfo'=>$payInfo,'order'=>$orderInfo,'msg'=> "服务商订单{$orderSn}更新状态错误",'date'=> $nowTime];
|
|
|
- RedisCache::set($cacheKey.'_error', $logData, $this->cacheTime);
|
|
|
+ if (!ShopOrderModel::where('order_id', $orderId)->update(['status' => 1, 'pay_type' => $payType, 'updated_time' => $nowTime])) {
|
|
|
+ $logData = ['params' => $params, 'payInfo' => $payInfo, 'order' => $orderInfo, 'msg' => "服务商订单{$orderSn}更新状态错误", 'date' => $nowTime];
|
|
|
+ RedisCache::set($cacheKey . '_error', $logData, $this->cacheTime);
|
|
|
sr_throw("服务商订单{$orderSn}更新状态错误");
|
|
|
}
|
|
|
|
|
@@ -310,25 +315,24 @@ class PaymentService
|
|
|
'g.real_sales_volume' => Db::raw('g.real_sales_volume + og.num'),
|
|
|
]);
|
|
|
if (!$updateSale) {
|
|
|
- Db::rollback();
|
|
|
- $logData = ['params' => $params, 'orderInfo' => $orderInfo, 'payInfo' => $payInfo, 'error' => "商城订单{$orderSn}更新商品销量失败", 'date' => $date];
|
|
|
+ $logData = ['params' => $params, 'orderInfo' => $orderInfo, 'payInfo' => $payInfo, 'error' => "商城订单{$orderSn}更新商品销量失败", 'date' => $nowTime];
|
|
|
RedisCache::set($cacheKey . "error_{$orderSn}", $logData, 7200);
|
|
|
sr_throw("商城订单{$orderSn}更新商品销量失败");
|
|
|
}
|
|
|
|
|
|
// 更新用户交易额(消费)
|
|
|
- $userInfo = UserService::make()->getCacheInfo($payUid,'id,mobile,score,money,path', false);
|
|
|
- $upperPath = isset($userInfo['path'])? $userInfo['path'] : '';
|
|
|
- if($userInfo && !UserModel::where('id', $payUid)->inc('total_income', $totalFee)->inc('total_team_income',$totalFee)->update()){
|
|
|
- $logData = ['params'=>$params,'payInfo'=>$payInfo,'user'=>$userInfo,'order'=>$orderInfo,'msg'=> "商城订单{$orderSn}更新用户交易数据错误",'date'=> $nowTime];
|
|
|
- RedisCache::set($cacheKey."error_{$orderSn}", $logData, $this->cacheTime);
|
|
|
+ $userInfo = UserService::make()->getCacheInfo($payUid, 'id,mobile,score,money,path', false);
|
|
|
+ $upperPath = isset($userInfo['path']) ? $userInfo['path'] : '';
|
|
|
+ if ($userInfo && !UserModel::where('id', $payUid)->inc('total_income', $totalFee)->inc('total_team_income', $totalFee)->update()) {
|
|
|
+ $logData = ['params' => $params, 'payInfo' => $payInfo, 'user' => $userInfo, 'order' => $orderInfo, 'msg' => "商城订单{$orderSn}更新用户交易数据错误", 'date' => $nowTime];
|
|
|
+ RedisCache::set($cacheKey . "error_{$orderSn}", $logData, $this->cacheTime);
|
|
|
sr_throw("商城订单{$orderSn}更新用户交易数据错误");
|
|
|
}
|
|
|
|
|
|
// 更新团队交易额数据
|
|
|
- if($upperPath && !UserModel::whereIn('id', explode(',', $upperPath))->inc('total_team_income',$totalFee)->update()){
|
|
|
- $logData = ['params'=>$params,'payInfo'=>$payInfo,'user'=>$userInfo,'order'=>$orderInfo,'msg'=> "商城订单{$orderSn}更新用户上级交易数据错误",'date'=> $nowTime];
|
|
|
- RedisCache::set($cacheKey."error_{$orderSn}", $logData, $this->cacheTime);
|
|
|
+ if ($upperPath && !UserModel::whereIn('id', explode(',', $upperPath))->inc('total_team_income', $totalFee)->update()) {
|
|
|
+ $logData = ['params' => $params, 'payInfo' => $payInfo, 'user' => $userInfo, 'order' => $orderInfo, 'msg' => "商城订单{$orderSn}更新用户上级交易数据错误", 'date' => $nowTime];
|
|
|
+ RedisCache::set($cacheKey . "error_{$orderSn}", $logData, $this->cacheTime);
|
|
|
sr_throw("商城订单{$orderSn}更新用户上级交易数据错误");
|
|
|
}
|
|
|
|
|
@@ -338,8 +342,8 @@ class PaymentService
|
|
|
if ($rebateScore > 0 && $userInfo) {
|
|
|
// 更新用户账户积分
|
|
|
if (!UserModel::where('id', $payUid)->inc('score', $rebateScore)->update()) {
|
|
|
- $logData = ['params'=>$params,'payInfo'=>$payInfo,'user'=>$userInfo,'order'=>$orderInfo,'msg'=> "商城订单{$orderSn}赠送积分处理错误",'date'=> $nowTime];
|
|
|
- RedisCache::set($cacheKey."error_{$orderSn}", $logData, $this->cacheTime);
|
|
|
+ $logData = ['params' => $params, 'payInfo' => $payInfo, 'user' => $userInfo, 'order' => $orderInfo, 'msg' => "商城订单{$orderSn}赠送积分处理错误", 'date' => $nowTime];
|
|
|
+ RedisCache::set($cacheKey . "error_{$orderSn}", $logData, $this->cacheTime);
|
|
|
sr_throw("商城订单{$orderSn}赠送积分处理错误");
|
|
|
}
|
|
|
|
|
@@ -350,22 +354,22 @@ class PaymentService
|
|
|
'type' => 3,
|
|
|
'score' => $rebateScore,
|
|
|
'create_at' => sr_getcurtime(time()),
|
|
|
- 'state' => 12,
|
|
|
+ 'state' => 1,
|
|
|
'before_score' => $userScore,
|
|
|
'after_score' => floatval($userScore + $rebateScore),
|
|
|
'from_id' => $payId,
|
|
|
'uid2' => 0
|
|
|
];
|
|
|
if (!ScoreLogModel::insertGetId($data)) {
|
|
|
- $logData = ['params'=>$params,'payInfo'=>$payInfo,'user'=>$userInfo,'order'=>$orderInfo,'msg'=> "商城订单{$orderSn}赠送积分明细处理错误",'date'=> $nowTime];
|
|
|
- RedisCache::set($cacheKey."error_{$orderSn}", $logData, $this->cacheTime);
|
|
|
+ $logData = ['params' => $params, 'payInfo' => $payInfo, 'user' => $userInfo, 'order' => $orderInfo, 'msg' => "商城订单{$orderSn}赠送积分明细处理错误", 'date' => $nowTime];
|
|
|
+ RedisCache::set($cacheKey . "error_{$orderSn}", $logData, $this->cacheTime);
|
|
|
sr_throw("商城订单{$orderSn}赠送积分明细处理错误");
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
- $logData = ['params'=>$params,'payInfo'=>$payInfo,'user'=>$userInfo,'order'=>$orderInfo,'msg'=> "商城订单{$orderSn}回调处理成功",'date'=> $nowTime];
|
|
|
- RedisCache::set($cacheKey."success_{$orderSn}", $logData, $this->cacheTime);
|
|
|
+ $logData = ['params' => $params, 'payInfo' => $payInfo, 'user' => $userInfo, 'order' => $orderInfo, 'msg' => "商城订单{$orderSn}回调处理成功", 'date' => $nowTime];
|
|
|
+ RedisCache::set($cacheKey . "success_{$orderSn}", $logData, $this->cacheTime);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -373,8 +377,241 @@ class PaymentService
|
|
|
}
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
+ * 账户支付处理
|
|
|
+ * @param $payUid
|
|
|
+ * @param $orderSn
|
|
|
+ * @param $payType
|
|
|
+ * @throws \think\Exception
|
|
|
+ * @throws \think\db\exception\DataNotFoundException
|
|
|
+ * @throws \think\db\exception\DbException
|
|
|
+ * @throws \think\db\exception\ModelNotFoundException
|
|
|
+ */
|
|
|
+ public function accountPayBack($payUid, $orderSn, $payType = 6)
|
|
|
+ {
|
|
|
+ $nowTime = date('Y-m-d H:i:s');
|
|
|
+ $params = ['uid' => $payUid, 'order_sn' => $orderSn, 'pay_type' => $payType];
|
|
|
+ $payConfig = PayConfigService::make()->getInfoByChannel($payType, 1, 1);
|
|
|
+ $payCode = isset($payConfig['pay_code']) && $payConfig['pay_code']? $payConfig['pay_code'] : '';
|
|
|
+ if(empty($payConfig) || empty($payCode)){
|
|
|
+ sr_throw('该支付暂未开放');
|
|
|
+ }
|
|
|
+
|
|
|
+ $cacheKey = "caches:payNotify:{$payCode}:otn_{$orderSn}:";
|
|
|
+ RedisCache::set($cacheKey . 'catch', ['params' => $params, 'date' => $nowTime], $this->cacheTime);
|
|
|
+
|
|
|
+ // 获取验证订单信息
|
|
|
+ $orderInfo = ShopOrderService::make()->getInfoBySn($orderSn, $payUid);
|
|
|
+ $orderId = isset($orderInfo['order_id']) ? $orderInfo['order_id'] : 0;
|
|
|
+ $totalFee = isset($orderInfo['payment']) ? floatval($orderInfo['payment']) : 0;
|
|
|
+ $orderStatus = isset($orderInfo['status']) ? intval($orderInfo['status']) : -1;
|
|
|
+ if (empty($orderInfo) || $orderId <= 0 || $totalFee <= 0) {
|
|
|
+ $logData = ['params' => $params, 'order' => $orderInfo, 'msg' => "商城订单{$orderSn}不存在或参数错误", 'date' => $nowTime];
|
|
|
+ RedisCache::set($cacheKey . "error_{$orderSn}", $logData, $this->cacheTime);
|
|
|
+ sr_throw("商城订单{$orderSn}不存在或参数错误");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证订单状态
|
|
|
+ if ($orderStatus != 0) {
|
|
|
+ $logData = ['params' => $params, 'order' => $orderInfo, 'msg' => "商城订单{$orderSn}订单已处理", 'date' => $nowTime];
|
|
|
+ RedisCache::set($cacheKey . "error_{$orderSn}", $logData, $this->cacheTime);
|
|
|
+ sr_throw("商城订单{$orderSn}订单已处理");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 用户信息
|
|
|
+ $userInfo = UserService::make()->getCacheInfo($payUid, 'id,mobile,green_score,score,money,path', false);
|
|
|
+ if(empty($userInfo)){
|
|
|
+ $logData = ['params' => $params, 'order' => $orderInfo, 'msg' => "商城订单{$orderSn}账户信息不寻找或已被冻结", 'date' => $nowTime];
|
|
|
+ RedisCache::set($cacheKey . "error_{$orderSn}", $logData, $this->cacheTime);
|
|
|
+ sr_throw('账户信息不寻找或已被冻结');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 支付处理
|
|
|
+ Db::startTrans();
|
|
|
+ try {
|
|
|
+ switch ($payType) {
|
|
|
+ case 3: // 余额支付
|
|
|
+ // 验证账户余额
|
|
|
+ $userMoney = isset($userInfo['money']) ? floatval($userInfo['money']) : 0;
|
|
|
+ if ($userMoney < $totalFee) {
|
|
|
+ $logData = ['params' => $params, 'order' => $orderInfo, 'userInfo' => $userInfo, 'msg' => "服务商订单{$orderSn}账户余额不足", 'date' => $nowTime];
|
|
|
+ RedisCache::set($cacheKey . '_error', $logData, $this->cacheTime);
|
|
|
+ sr_throw('账户余额不足');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 扣款
|
|
|
+ if (!UserModel::where('id', $payUid)->inc('money', $totalFee)->update()) {
|
|
|
+ $logData = ['params' => $params, 'order' => $orderInfo, 'userInfo' => $userInfo, 'msg' => "服务商订单{$orderSn}余额支付失败", 'date' => $nowTime];
|
|
|
+ RedisCache::set($cacheKey . '_error', $logData, $this->cacheTime);
|
|
|
+ sr_throw('余额支付失败');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 流水
|
|
|
+ $log = [
|
|
|
+ 'uid' => $payUid,
|
|
|
+ 'type' => 11,
|
|
|
+ 'score' => $totalFee,
|
|
|
+ 'create_at' => sr_getcurtime(time()),
|
|
|
+ 'state' => 2, // 1进账,2-出账
|
|
|
+ 'before_score' => $userMoney,
|
|
|
+ 'after_score' => max(0, $userMoney - $totalFee),
|
|
|
+ 'from_id' => $orderId,
|
|
|
+ 'uid2' => 0
|
|
|
+ ];
|
|
|
+
|
|
|
+ if (!GreenScoreLogModel::insertGetId($log)) {
|
|
|
+ $logData = ['params' => $params, 'order' => $orderInfo, 'userInfo' => $userInfo,'log'=>$log, 'msg' => "服务商订单{$orderSn}账户余额支付处理失败", 'date' => $nowTime];
|
|
|
+ RedisCache::set($cacheKey . '_error', $logData, $this->cacheTime);
|
|
|
+ sr_throw('账户余额支付处理失败');
|
|
|
+ }
|
|
|
+
|
|
|
+ break;
|
|
|
+ case 6: // 绿色积分
|
|
|
+ // 验证账户
|
|
|
+ $userGreenScore = isset($userInfo['green_score']) ? floatval($userInfo['green_score']) : 0;
|
|
|
+ if ($userGreenScore < $totalFee) {
|
|
|
+ $logData = ['params' => $params, 'order' => $orderInfo, 'userInfo' => $userInfo, 'msg' => "服务商订单{$orderSn}账户绿色积分不足", 'date' => $nowTime];
|
|
|
+ RedisCache::set($cacheKey . '_error', $logData, $this->cacheTime);
|
|
|
+ sr_throw('账户绿色积分不足');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 扣款
|
|
|
+ if (!UserModel::where('id', $payUid)->inc('green_score', $totalFee)->update()) {
|
|
|
+ $logData = ['params' => $params, 'order' => $orderInfo, 'userInfo' => $userInfo, 'msg' => "服务商订单{$orderSn}账户绿色积分支付失败", 'date' => $nowTime];
|
|
|
+ RedisCache::set($cacheKey . '_error', $logData, $this->cacheTime);
|
|
|
+ sr_throw('绿色积分支付失败');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 流水
|
|
|
+ $log = [
|
|
|
+ 'uid' => $payUid,
|
|
|
+ 'type' => 4,
|
|
|
+ 'score' => $totalFee,
|
|
|
+ 'create_at' => sr_getcurtime(time()),
|
|
|
+ 'state' => 2, // 1进账,2-出账
|
|
|
+ 'before_score' => $userGreenScore,
|
|
|
+ 'after_score' => max(0, $userGreenScore - $totalFee),
|
|
|
+ 'from_id' => $orderId,
|
|
|
+ 'uid2' => 0
|
|
|
+ ];
|
|
|
+
|
|
|
+ if (!GreenScoreLogModel::insertGetId($log)) {
|
|
|
+ $logData = ['params' => $params, 'order' => $orderInfo, 'userInfo' => $userInfo,'log'=>$log, 'msg' => "服务商订单{$orderSn}绿色积分支付处理失败", 'date' => $nowTime];
|
|
|
+ RedisCache::set($cacheKey . '_error', $logData, $this->cacheTime);
|
|
|
+ sr_throw('绿色积分支付处理失败');
|
|
|
+ }
|
|
|
+
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ sr_throw("该支付方式暂不支持");
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新订单状态
|
|
|
+ if (!ShopOrderModel::where('order_id', $orderId)->update(['status' => 1, 'pay_type' => $payType, 'updated_time' => $nowTime])) {
|
|
|
+ $logData = ['params' => $params, 'order' => $orderInfo, 'msg' => "服务商订单{$orderSn}更新状态错误", 'date' => $nowTime];
|
|
|
+ RedisCache::set($cacheKey . '_error', $logData, $this->cacheTime);
|
|
|
+ sr_throw("服务商订单{$orderSn}更新状态错误");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 写入支付信息
|
|
|
+ $outTradeNo = 'BA' . createdOrderSn();
|
|
|
+ $log = [
|
|
|
+ 'total_fee' => $totalFee,
|
|
|
+ 'trade_type' => 'app',
|
|
|
+ 'body' => '购买商品',
|
|
|
+ 'state' => 6,
|
|
|
+ 'out_trade_no' => $outTradeNo,
|
|
|
+ 'pay_way' => 3,
|
|
|
+ 'remarks' => $orderId,
|
|
|
+ 'order_type' => 4,
|
|
|
+ 'pay_at' => sr_getcurtime(time()),
|
|
|
+ 'uid' => $payUid,
|
|
|
+ 'voucher_img' => '',
|
|
|
+ 'out_trade_no1' => ''
|
|
|
+ ];
|
|
|
+ if (!PaymentModel::insertGetId($log)) {
|
|
|
+ $logData = ['params' => $params, 'orderInfo' => $orderInfo,'payLog'=>$log, 'error' => "商城订单{$orderSn}更新商品销量失败", 'date' => $nowTime];
|
|
|
+ RedisCache::set($cacheKey . "error_{$orderSn}", $logData, 7200);
|
|
|
+ sr_throw("商城订单{$orderSn}更新商品销量失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 增加订单商品销量
|
|
|
+ $updateSale = ShopOrderModel::alias('a')
|
|
|
+ ->leftJoin('shop_order_goods og', 'og.order_id=a.order_id')
|
|
|
+ ->leftJoin('shop_goods g', 'g.goods_id=og.goods_id')
|
|
|
+ ->where(['a.order_id' => $orderId, 'a.user_id' => $payUid])
|
|
|
+ ->update([
|
|
|
+ 'g.sales_volume' => Db::raw('g.sales_volume + og.num'),
|
|
|
+ 'g.real_sales_volume' => Db::raw('g.real_sales_volume + og.num'),
|
|
|
+ ]);
|
|
|
+ if (!$updateSale) {
|
|
|
+ $logData = ['params' => $params, 'orderInfo' => $orderInfo, 'error' => "商城订单{$orderSn}更新商品销量失败", 'date' => $nowTime];
|
|
|
+ RedisCache::set($cacheKey . "error_{$orderSn}", $logData, 7200);
|
|
|
+ sr_throw("商城订单{$orderSn}更新商品销量失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新用户交易额(消费)
|
|
|
+ $upperPath = isset($userInfo['path']) ? $userInfo['path'] : '';
|
|
|
+ if ($userInfo && !UserModel::where('id', $payUid)->inc('total_income', $totalFee)->inc('total_team_income', $totalFee)->update()) {
|
|
|
+ $logData = ['params' => $params, 'user' => $userInfo, 'order' => $orderInfo, 'msg' => "商城订单{$orderSn}更新用户交易数据错误", 'date' => $nowTime];
|
|
|
+ RedisCache::set($cacheKey . "error_{$orderSn}", $logData, $this->cacheTime);
|
|
|
+ sr_throw("商城订单{$orderSn}更新用户交易数据错误");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新团队交易额数据
|
|
|
+ if ($upperPath && !UserModel::whereIn('id', explode(',', $upperPath))->inc('total_team_income', $totalFee)->update()) {
|
|
|
+ $logData = ['params' => $params, 'user' => $userInfo, 'order' => $orderInfo, 'msg' => "商城订单{$orderSn}更新用户上级交易数据错误", 'date' => $nowTime];
|
|
|
+ RedisCache::set($cacheKey . "error_{$orderSn}", $logData, $this->cacheTime);
|
|
|
+ sr_throw("商城订单{$orderSn}更新用户上级交易数据错误");
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
+ // 赠送积分处理
|
|
|
+ $rebateScore = isset($orderInfo['rebate_score']) ? floatval($orderInfo['rebate_score']) : 0;
|
|
|
+ if ($rebateScore > 0 && $userInfo) {
|
|
|
+ // 更新用户账户积分
|
|
|
+ if (!UserModel::where('id', $payUid)->inc('score', $rebateScore)->update()) {
|
|
|
+ $logData = ['params' => $params, 'user' => $userInfo, 'order' => $orderInfo, 'msg' => "商城订单{$orderSn}赠送积分处理错误", 'date' => $nowTime];
|
|
|
+ RedisCache::set($cacheKey . "error_{$orderSn}", $logData, $this->cacheTime);
|
|
|
+ sr_throw("商城订单{$orderSn}赠送积分处理错误");
|
|
|
+ }
|
|
|
|
|
|
+ // 处理积分流水明细
|
|
|
+ $userScore = isset($userInfo['score']) ? floatval($userInfo['score']) : 0;
|
|
|
+ $data = [
|
|
|
+ 'uid' => $payUid,
|
|
|
+ 'type' => 3,
|
|
|
+ 'score' => $rebateScore,
|
|
|
+ 'create_at' => sr_getcurtime(time()),
|
|
|
+ 'state' => 1,
|
|
|
+ 'before_score' => $userScore,
|
|
|
+ 'after_score' => floatval($userScore + $rebateScore),
|
|
|
+ 'from_id' => $orderId,
|
|
|
+ 'uid2' => 0
|
|
|
+ ];
|
|
|
+ if (!ScoreLogModel::insertGetId($data)) {
|
|
|
+ $logData = ['params' => $params, 'user' => $userInfo, 'order' => $orderInfo, 'msg' => "商城订单{$orderSn}赠送积分明细处理错误", 'date' => $nowTime];
|
|
|
+ RedisCache::set($cacheKey . "error_{$orderSn}", $logData, $this->cacheTime);
|
|
|
+ sr_throw("商城订单{$orderSn}赠送积分明细处理错误");
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ Db::commit();
|
|
|
+ $logData = ['params' => $params, 'user' => $userInfo, 'order' => $orderInfo, 'msg' => "商城订单{$orderSn}回调处理成功", 'date' => $nowTime];
|
|
|
+ RedisCache::set($cacheKey . "success_{$orderSn}", $logData, $this->cacheTime);
|
|
|
+ return true;
|
|
|
+ }catch (\Exception $exception){
|
|
|
+ Db::rollback();
|
|
|
+ $msg = $exception->getMessage();
|
|
|
+ $logData = ['params' => $params, 'user' => $userInfo, 'order' => $orderInfo, 'msg' => $msg, 'trace'=>$exception->getTrace(), 'date' => $nowTime];
|
|
|
+ RedisCache::set($cacheKey . "error_{$orderSn}", $logData, $this->cacheTime);
|
|
|
+ sr_throw($msg? $msg:'支付失败');
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
|
|
|
}
|