|
|
@@ -735,7 +735,17 @@ class OrderController extends BaseController
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- //微信和支付宝付款码支付 ssh 2021.4.11
|
|
|
+ /**
|
|
|
+ * 微信和支付宝付款码支付
|
|
|
+ * 职责:处理商户扫描客户付款码(被扫支付)的请求,调用拉卡拉支付接口并更新本地订单状态
|
|
|
+ * 入参:GET 传参 id (订单ID), authCode (付款码)
|
|
|
+ * 返回:JSON 格式的支付结果(SUCCESS/FAILURE)
|
|
|
+ * 副作用:更新订单及采购单的支付状态、支付方式,记录支付日志,触发云打印、跑腿呼叫及语音播报
|
|
|
+ * 关键边界:
|
|
|
+ * 1. 验证订单及采购单的有效性与时效性
|
|
|
+ * 2. 校验付款码格式(微信/支付宝)
|
|
|
+ * 3. 优化:将拉卡拉外部网络请求移出数据库事务,避免因网络延迟长时间占用数据库连接和行锁
|
|
|
+ */
|
|
|
public function actionCodePay()
|
|
|
{
|
|
|
ini_set('date.timezone', 'Asia/Shanghai');
|
|
|
@@ -817,8 +827,6 @@ class OrderController extends BaseController
|
|
|
}
|
|
|
$totalFee = $cg->actPrice ?? 0;
|
|
|
|
|
|
- $connection = Yii::$app->db;
|
|
|
- $transaction = $connection->beginTransaction();
|
|
|
try {
|
|
|
$subject = '购买花材 ' . $orderSn;
|
|
|
$capitalType = dict::getDict('capitalType', 'xhPurchase', 'id');
|
|
|
@@ -842,39 +850,49 @@ class OrderController extends BaseController
|
|
|
'subject' => $subject,
|
|
|
'authCode' => $authCode,
|
|
|
];
|
|
|
+
|
|
|
+ // 复杂分支/关键逻辑:在调用外部支付接口前不开启事务,防止网络延迟卡死数据库连接
|
|
|
$response = $laResource->scanPay($scanParams);
|
|
|
if (isset($response['code']) && $response['code'] == 'BBS00000') {
|
|
|
|
|
|
- $account_type = $response['resp_data']['account_type'] ?? '';
|
|
|
- $payWayType = dict::getDict('payWay', 'wxPay');
|
|
|
- if ($account_type == 'WECHAT') {
|
|
|
+ // 复杂分支/关键逻辑:支付成功后,开启数据库事务,确保本地订单状态更新的原子性
|
|
|
+ $connection = Yii::$app->db;
|
|
|
+ $transaction = $connection->beginTransaction();
|
|
|
+ try {
|
|
|
+ $account_type = $response['resp_data']['account_type'] ?? '';
|
|
|
$payWayType = dict::getDict('payWay', 'wxPay');
|
|
|
- }
|
|
|
- if ($account_type == 'ALIPAY') {
|
|
|
- $payWayType = dict::getDict('payWay', 'alipay');
|
|
|
- }
|
|
|
- $order->payWay = $payWayType;
|
|
|
- $order->save();
|
|
|
- $cg->payWay = $payWayType;
|
|
|
- $cg->save();
|
|
|
+ if ($account_type == 'WECHAT') {
|
|
|
+ $payWayType = dict::getDict('payWay', 'wxPay');
|
|
|
+ }
|
|
|
+ if ($account_type == 'ALIPAY') {
|
|
|
+ $payWayType = dict::getDict('payWay', 'alipay');
|
|
|
+ }
|
|
|
+ $order->payWay = $payWayType;
|
|
|
+ $order->save();
|
|
|
+ $cg->payWay = $payWayType;
|
|
|
+ $cg->save();
|
|
|
|
|
|
- $transactionId = $response['resp_data']['trade_no'] ?? '';
|
|
|
- $openId = '';
|
|
|
- $aliUserId = '';
|
|
|
+ $transactionId = $response['resp_data']['trade_no'] ?? '';
|
|
|
+ $openId = '';
|
|
|
+ $aliUserId = '';
|
|
|
|
|
|
- $cg->onlinePay = dict::getDict('onlinePay', 'yes');
|
|
|
- $cg->codePay = 1;
|
|
|
- $cg->payOpenId = $openId;
|
|
|
- $cg->aliUserId = $aliUserId;
|
|
|
- $cg->save();
|
|
|
+ $cg->onlinePay = dict::getDict('onlinePay', 'yes');
|
|
|
+ $cg->codePay = 1;
|
|
|
+ $cg->payOpenId = $openId;
|
|
|
+ $cg->aliUserId = $aliUserId;
|
|
|
+ $cg->save();
|
|
|
|
|
|
- $order->onlinePay = dict::getDict('onlinePay', 'yes');
|
|
|
- $order->codePay = 1;
|
|
|
- $order->save();
|
|
|
+ $order->onlinePay = dict::getDict('onlinePay', 'yes');
|
|
|
+ $order->codePay = 1;
|
|
|
+ $order->save();
|
|
|
|
|
|
- $attach = '';
|
|
|
- payUtil::thirdPay($payWayType, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
|
|
|
- $transaction->commit();
|
|
|
+ $attach = '';
|
|
|
+ payUtil::thirdPay($payWayType, $capitalType, $orderSn, $totalFee, $attach, $transactionId);
|
|
|
+ $transaction->commit();
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ $transaction->rollBack();
|
|
|
+ throw $e;
|
|
|
+ }
|
|
|
|
|
|
$saleId = $order->id ?? 0;
|
|
|
$order = OrderClass::getById($saleId, true);
|
|
|
@@ -922,7 +940,6 @@ class OrderController extends BaseController
|
|
|
util::success(['returnStatus' => 'FAILURE', 'returnCode' => $retCode], $msg);
|
|
|
}
|
|
|
} catch (\Exception $e) {
|
|
|
- $transaction->rollBack();
|
|
|
Yii::error("支付失败原因:" . $e->getMessage());
|
|
|
util::fail('支付失败');
|
|
|
}
|