Skip to content

Commit

Permalink
优化订单支付的代码流程
Browse files Browse the repository at this point in the history
  • Loading branch information
fancyecommerce committed Apr 27, 2018
1 parent 06ae048 commit bdc719f
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 14 deletions.
20 changes: 10 additions & 10 deletions services/Order.php
Expand Up @@ -103,16 +103,16 @@ public function getOrderPaymentedStatusArr(){
*/
protected function actionGetStatusArr(){
return [
$this->payment_status_pending => $this->payment_status_pending,
$this->payment_status_processing => $this->payment_status_processing,
$this->payment_status_confirmed => $this->payment_status_confirmed,
$this->payment_status_canceled => $this->payment_status_canceled,
$this->payment_status_suspected_fraud => $this->payment_status_suspected_fraud,
$this->status_holded => $this->status_holded,
$this->status_processing => $this->status_processing,
$this->status_dispatched => $this->status_dispatched,
$this->status_refunded => $this->status_refunded,
$this->status_completed => $this->status_completed,
$this->payment_status_pending => $this->payment_status_pending,
$this->payment_status_processing => $this->payment_status_processing,
$this->payment_status_confirmed => $this->payment_status_confirmed,
$this->payment_status_canceled => $this->payment_status_canceled,
$this->payment_status_suspected_fraud => $this->payment_status_suspected_fraud,
$this->status_holded => $this->status_holded,
$this->status_processing => $this->status_processing,
$this->status_dispatched => $this->status_dispatched,
$this->status_refunded => $this->status_refunded,
$this->status_completed => $this->status_completed,
];

}
Expand Down
32 changes: 30 additions & 2 deletions services/payment/Alipay.php
Expand Up @@ -54,6 +54,9 @@ class Alipay extends Service
protected $_ipnMessageModelName = '\fecshop\models\mysqldb\IpnMessage';
protected $_ipnMessageModel;

// 允许更改的订单状态,不存在这里面的订单状态不允许修改
protected $_allowChangOrderStatus;

/**
* 引入 支付宝支付的SDK文件。
*/
Expand All @@ -63,6 +66,10 @@ public function init()
$AopSdkFile = Yii::getAlias('@fecshop/lib/alipay/AopSdk.php');
require($AopSdkFile);
list($this->_ipnMessageModelName,$this->_ipnMessageModel) = \Yii::mapGet($this->_ipnMessageModelName);
$this->_allowChangOrderStatus = [
Yii::$service->order->payment_status_pending,
Yii::$service->order->payment_status_processing,
];
}
/**
* 初始化 $this->_AopClient
Expand Down Expand Up @@ -236,15 +243,34 @@ protected function paymentSuccess($increment_id,$trade_no,$sendEmail = true)
$this->_order = Yii::$service->order->getByIncrementId($increment_id);
Yii::$service->payment->setPaymentMethod($this->_order['payment_method']);
}
// 【优化后的代码 ##】
$orderstatus = Yii::$service->order->payment_status_confirmed;
$updateArr['order_status'] = $orderstatus;
$updateArr['txn_id'] = $trade_no; // 支付宝的交易号
$updateColumn = $this->_order->updateAll(
$updateArr,
[
'and',
['order_id' => $this->_order['order_id']],
['in','order_status',$this->_allowChangOrderStatus]
]
);
if (!empty($updateColumn)) {
// 发送邮件,以及其他的一些操作(订单支付成功后的操作)
Yii::$service->order->orderPaymentCompleteEvent($this->_order['increment_id']);
}
// 【优化后的代码 ##】

/* 注释掉的原来代码,上面进行了优化,保证更改只有一次,这样发邮件也就只有一次了
// 如果订单状态已经是processing,那么,不需要更改订单状态了。
if ($this->_order['order_status'] == Yii::$service->order->payment_status_processing){
if ($this->_order['order_status'] == Yii::$service->order->payment_status_confirmed){
return true;
}
$order = $this->_order;
if (isset($order['increment_id']) && $order['increment_id']) {
// 如果支付成功,则更改订单状态为支付成功
$order->order_status = Yii::$service->order->payment_status_processing;
$order->order_status = Yii::$service->order->payment_status_confirmed;
$order->txn_id = $trade_no; // 支付宝的交易号
// 更新订单信息
$order->save();
Expand All @@ -257,6 +283,8 @@ protected function paymentSuccess($increment_id,$trade_no,$sendEmail = true)
return true;
}
*/
return true;
}


Expand Down
33 changes: 31 additions & 2 deletions services/payment/Wxpay.php
Expand Up @@ -31,6 +31,9 @@ class Wxpay extends Service
public $expireTime = 600;
protected $_order;

// 允许更改的订单状态,不存在这里面的订单状态不允许修改
protected $_allowChangOrderStatus;

public function init()
{
ini_set('date.timezone','Asia/Shanghai');
Expand Down Expand Up @@ -67,6 +70,10 @@ public function init()
throw new InvalidConfigException('you must config param [devide] in payment wxpay service');
return ;
}
$this->_allowChangOrderStatus = [
Yii::$service->order->payment_status_pending,
Yii::$service->order->payment_status_processing,
];
}
/**
* 接收IPN消息的url,接收微信支付的异步消息,进而更改订单状态。
Expand Down Expand Up @@ -369,15 +376,35 @@ protected function paymentSuccess($increment_id,$trade_no,$sendEmail = true)
$this->_order = Yii::$service->order->getByIncrementId($increment_id);
Yii::$service->payment->setPaymentMethod($this->_order['payment_method']);
}

// 【优化后的代码 ##】
$orderstatus = Yii::$service->order->payment_status_confirmed;
$updateArr['order_status'] = $orderstatus;
$updateArr['txn_id'] = $trade_no; // 微信的交易号
$updateColumn = $this->_order->updateAll(
$updateArr,
[
'and',
['order_id' => $this->_order['order_id']],
['in','order_status',$this->_allowChangOrderStatus]
]
);
if (!empty($updateColumn)) {
// 发送邮件,以及其他的一些操作(订单支付成功后的操作)
Yii::$service->order->orderPaymentCompleteEvent($this->_order['increment_id']);
}
// 【优化后的代码 ##】

/* 注释掉的原来代码,上面进行了优化,保证更改只有一次,这样发邮件也就只有一次了
// 如果订单状态已经是processing,那么,不需要更改订单状态了。
if ($this->_order['order_status'] == Yii::$service->order->payment_status_processing){
if ($this->_order['order_status'] == Yii::$service->order->payment_status_confirmed){
return true;
}
$order = $this->_order;
if (isset($order['increment_id']) && $order['increment_id']) {
// 如果支付成功,则更改订单状态为支付成功
$order->order_status = Yii::$service->order->payment_status_processing;
$order->order_status = Yii::$service->order->payment_status_confirmed;
$order->txn_id = $trade_no; // 微信的交易号
// 更新订单信息
$order->save();
Expand All @@ -388,6 +415,8 @@ protected function paymentSuccess($increment_id,$trade_no,$sendEmail = true)
// Yii::$service->email->order->sendCreateEmail($orderInfo);
return true;
}
*/
return true;
}


Expand Down

0 comments on commit bdc719f

Please sign in to comment.