Authored by Rock Zhang

修复微信支付的bug

Code Review By Rock Zhang
... ... @@ -2,12 +2,36 @@
namespace WebPlugin\Pay\weixin;
use LibModels\Web\Home\OrderData;
use LibModels\Web\Product\PayData;
use WebPlugin\Pay\weixin\lib\WxPayApi;
use WebPlugin\Pay\weixin\lib\WxPayNotify;
use WebPlugin\Pay\weixin\lib\WxPayOrderQuery;
use WebPlugin\PhpLog;
class PayNotifyCallBack extends WxPayNotify
{
// 向ERP提交订单状态有关接口调用日志和更新订单状态有关接口调用日志
const ORDER_STATUS_LOG = '/Data/logs/pc_pay/order_status';
// 日志等级,2表示记录信息等级的日志
const LOG_LEVEL = 2;
// 获取微信订单中的订单号需要截取的开始位置
const ORDER_CODE_START = 8;
private $log = null;
private $_uid;
/**
* PayNotifyCallBack constructor.
* @param int $uid
*/
public function __construct($uid)
{
// 初始化日志
$this->log = new PhpLog(self::ORDER_STATUS_LOG, 'PRC', self::LOG_LEVEL);
$this->_uid = $uid;
}
//查询订单
public function Queryorder($transaction_id)
... ... @@ -16,8 +40,44 @@ class PayNotifyCallBack extends WxPayNotify
$input->SetTransaction_id($transaction_id);
$result = WxPayApi::orderQuery($input);
if (array_key_exists("return_code", $result) && array_key_exists("result_code", $result) && $result["return_code"] == "SUCCESS" && $result["result_code"] == "SUCCESS") {
$orderCode = substr($result['out_trade_no'], self::ORDER_CODE_START);
$payment = 21; // 微信扫码支付
$bankName = 'WX';
$bankCode = '';
$amount = $result['total_fee'] / 100;
$tradeNo = $result['out_trade_no'];
$bankBillNo = $result['bank_type'];
$this->log->LogInfo('===[' . $orderCode . ']===');
$this->log->LogInfo('==[' . $orderCode . ']=开始调用ERP提交订单状态接口,接口方法为web.SpaceOrders.submitOrderStatus===');
$this->log->LogInfo('==[' . $orderCode . ']=请求参数为orderCode=' . $orderCode . '===');
// ERP提交订单状态
$erpReturn = PayData::submitOrderStatus($orderCode, $payment, $bankName, $bankCode, $amount, $orderCode, $tradeNo, $bankBillNo);
$this->log->LogInfo('==[' . $orderCode . ']=结束调用ERP提交订单状态接口,返回结果为===');
$this->log->LogInfo(var_export($erpReturn, true));
$this->log->LogInfo('==[' . $orderCode . ']=开始调用获取订单详情方式接口,接口方法为app.SpaceOrders.detail===');
$orderInfo = OrderData::getOrderDetail('', $orderCode);
$this->log->LogInfo('==[' . $orderCode . ']=结束调用获取订单详情方式接口,返回结果为===');
$this->log->LogInfo(var_export($orderInfo, true));
if (!isset($orderInfo['data']) || empty($orderInfo['data'])) {
$this->log->LogInfo('==[' . $orderCode . ']=微信支付状态同步中未获取到订单详情信息===');
return false;
}
$this->log->LogInfo('==[' . $orderCode . ']=开始调用更新订单状态接口,接口方法为web.SpaceOrders.updatePaymentStatus===');
$this->log->LogInfo('==[' . $orderCode . ']=请求参数为orderId=' . $orderInfo['data']['order_id'] . 'uid=' . $this->_uid . '===');
// 更新订单的状态
$updateOrderStatus = PayData::updateOrderStatus($orderInfo['data']['order_id'], $this->_uid, $payment, 'Y', $bankCode);
$this->log->LogInfo('==[' . $orderCode . ']=结束调用更新订单状态接口,返回结果为===');
$this->log->LogInfo(var_export($updateOrderStatus, true));
$this->log->LogInfo('==[' . $orderCode . ']=微信扫码订单支付成功===');
return true;
}
return false;
}
... ... @@ -33,6 +93,9 @@ class PayNotifyCallBack extends WxPayNotify
$msg = "订单查询失败";
return false;
}
$this->log->LogInfo('=====微信扫码支付通知结果为:' . $msg . '=====');
return true;
}
... ...
... ... @@ -476,22 +476,24 @@ class PayModel
// 初始化日志
$log = new PhpLog(self::ORDER_STATUS_LOG, 'PRC', self::LOG_LEVEL);
$log->LogInfo('========[' . $orderCode . ']========');
$log->LogInfo('========开始查询微信扫码支付状态========');
$log->LogInfo('=====[' . $orderCode . ']===开始查询微信扫码支付状态========');
// 组装微信支付的订单号
$tradeNo = 'YOHOBuy_' . $orderCode;
$input = new WxPayOrderQuery();
$input->SetOut_trade_no($tradeNo);
$payResult = WxPayApi::orderQuery($input);
$log->LogInfo(var_export($payResult, true));
if (isset($payResult['trade_state']) && $payResult['trade_state'] === 'SUCCESS') {
$log->LogInfo('========微信扫码支付成功========');
$log->LogInfo('====[' . $orderCode . ']====微信扫码支付成功========');
$result = array(
'code' => '200',
'message' => 'success'
);
}
$log->LogInfo('====[' . $orderCode . ']====微信扫码支付失败========');
} while (false);
return $result;
... ... @@ -518,13 +520,13 @@ class PayModel
$log->LogInfo('========[' . $payResult->orderCode . ']========');
$log->LogInfo(var_export($payResult, true));
$log->LogInfo('===开始调用获取订单详情方式接口,接口方法为app.SpaceOrders.detail===');
$log->LogInfo('==[' . $payResult->orderCode . ']=开始调用获取订单详情方式接口,接口方法为app.SpaceOrders.detail===');
$orderInfo = OrderData::getOrderDetail($uid, $payResult->orderCode);
$log->LogInfo('===结束调用获取订单详情方式接口,返回结果为===');
$log->LogInfo('==[' . $payResult->orderCode . ']=结束调用获取订单详情方式接口,返回结果为===');
$log->LogInfo(var_export($orderInfo, true));
if (!isset($orderInfo['data']) || empty($orderInfo['data'])) {
$log->LogInfo('===订单支付失败,未获取到订单详情信息===');
$log->LogInfo('==[' . $payResult->orderCode . ']=订单支付失败,未获取到订单详情信息===');
break;
}
... ... @@ -539,9 +541,9 @@ class PayModel
// 初始化日志
$log = new PhpLog(self::SEND_MESSAGE_LOG, 'PRC', self::LOG_LEVEL);
$log->LogInfo('===[' . $orderCode . ']===');
$log->LogInfo('===开始调用订单取消时给用户发短信接口,接口方法为app.message.sendMsg===');
$log->LogInfo('==[' . $orderCode . ']=开始调用订单取消时给用户发短信接口,接口方法为app.message.sendMsg===');
$messageReturn = PayData::sendMessage($orderData['mobile'], 'error_sms', '支付成功,但订单已取消,订单号为' . $orderCode);
$log->LogInfo('===结束调用给用户发短信接口,返回结果为===');
$log->LogInfo('==[' . $orderCode . ']=结束调用给用户发短信接口,返回结果为===');
$log->LogInfo(var_export($messageReturn, true));
$result['code'] = 417;
... ... @@ -552,7 +554,7 @@ class PayModel
// 支付金额与订单金额不一致
if (round($amount, 2) != round($payResult->totalFee, 2)) {
$log = new PhpLog(self::ORDER_STATUS_LOG, 'PRC', self::LOG_LEVEL);
$log->LogInfo('===订单支付失败,支付金额与订单金额不一致===');
$log->LogInfo('==[' . $orderCode . ']=订单支付失败,支付金额与订单金额不一致===');
$result['code'] = 415;
$result['message'] = '支付金额与订单金额不一致';
break;
... ... @@ -569,20 +571,20 @@ class PayModel
$log = new PhpLog(self::ORDER_STATUS_LOG, 'PRC', self::LOG_LEVEL);
$log->LogInfo('===[' . $orderCode . ']===');
$log->LogInfo('===开始调用ERP提交订单状态接口,接口方法为web.SpaceOrders.submitOrderStatus===');
$log->LogInfo('===请求参数为orderCode=' . $orderCode . '===');
$log->LogInfo('==[' . $orderCode . ']=开始调用ERP提交订单状态接口,接口方法为web.SpaceOrders.submitOrderStatus===');
$log->LogInfo('==[' . $orderCode . ']=请求参数为orderCode=' . $orderCode . '===');
// ERP提交订单状态
$erpReturn = PayData::submitOrderStatus($orderCode, $payment, $bankName, $bankCode, $amount, $payOrderCode, $tradeNo, $bankBillNo);
$log->LogInfo('===结束调用ERP提交订单状态接口,返回结果为===');
$log->LogInfo('==[' . $orderCode . ']=结束调用ERP提交订单状态接口,返回结果为===');
$log->LogInfo(var_export($erpReturn, true));
$log->LogInfo('===开始调用更新订单状态接口,接口方法为web.SpaceOrders.updatePaymentStatus===');
$log->LogInfo('===请求参数为orderId=' . $orderData['order_id'] . 'uid=' . $uid . '===');
$log->LogInfo('==[' . $orderCode . ']=开始调用更新订单状态接口,接口方法为web.SpaceOrders.updatePaymentStatus===');
$log->LogInfo('==[' . $orderCode . ']=请求参数为orderId=' . $orderData['order_id'] . 'uid=' . $uid . '===');
// 更新订单的状态
$updateOrderStatus = PayData::updateOrderStatus($orderData['order_id'], $uid, $payment, 'Y', $bankCode);
$log->LogInfo('===结束调用更新订单状态接口,返回结果为===');
$log->LogInfo('==[' . $orderCode . ']=结束调用更新订单状态接口,返回结果为===');
$log->LogInfo(var_export($updateOrderStatus, true));
$log->LogInfo('===订单支付成功,请等待发货===');
$log->LogInfo('==[' . $orderCode . ']=订单支付成功,请等待发货===');
$result['code'] = 200;
$result['message'] = '支付成功,请等待发货';
... ...
... ... @@ -278,7 +278,13 @@ class NoticeController extends WebAction
public function wechatqrcodenotifyAction()
{
$this->log->LogInfo("begin wechatqrcodenotifyAction");
$notify = new PayNotifyCallBack();
$uid = $this->getUid();
if (empty($uid)) { // cookie中获取为空时再从之前保存的session中获取
$uid = $this->getSession(self::SESSION_UID_KEY);
$this->setSession(self::SESSION_UID_KEY, null);
}
$notify = new PayNotifyCallBack($uid);
$notify->handle(false);
}
... ...