easywechat4.x升级到6.x

This commit is contained in:
Dev
2026-09-21 14:57:50 +08:00
parent 39882c6cf2
commit 5f4c8147ca
11 changed files with 263 additions and 185 deletions
@@ -385,77 +385,55 @@ class OrderService
/**
* 微信支付回调
* @param
* @return string
* 微信支付回调(EasyWeChat 6.x / 支付 v3: 自动验签+解密)
* @return \Psr\Http\Message\ResponseInterface
* @throws \Exception
*/
public static function payNotify()
{
$xmlData = file_get_contents('php://input');
$data = ToolService::FromXml($xmlData);
Log::info('微信回调信息_request_xml:' . json_encode(['data' => $data, 'xml' => $xmlData]));
$order_no = $data['out_trade_no'] ?? '';
//更新订单状态
$orderInfo = OrderService::getOne(['order_no' => $order_no]);
self::checkPaySuccess($data, $orderInfo);
$res = [
'return_code' => 'SUCCESS',
'return_msg' => 'OK'
];
$xml = ToolService::ToXml($res);
Log::info('微信回调信息信息_result:' . json_encode(['res' => $res, 'xml' => $xml]));
//如果为待支付,才可更新为已支付。防止重复更新状态,导致重复发货。
if ($orderInfo->order_status == self::ORDER_STATUS_WAIT_PAY) {
$paymentData = [
'order_no' => $order_no,
'pay_status' => $data['result_code'],
'amount' => $data['total_fee'] / 100,
'currency' => $data['fee_type'],
'pay_type' => '1', // 1微信 2支付宝
'pay_way' => $data['trade_type'],
'trace_no' => $data['transaction_id'],
'pay_time' => time(),
'pay_success_time' => strtotime($data['time_end']),
];
Db::transaction(function () use ($order_no, $paymentData, $orderInfo) {
self::where('order_no', $order_no)->update(['order_status' => self::ORDER_STATUS_WAIT_SHIP]);
OrderPaymentService::create($paymentData);
if ($orderInfo->order_type == 1) {
DemandService::customerContractFinishedDeal($orderInfo->parent_order_no);
} elseif ($orderInfo->order_type == 2) {
DemandService::customerWorkAcceptFinishedDeal($orderInfo->parent_order_no);
}
});
}
$service = new EasyWeChatService();
return $xml;
}
/**
* @param $data
* @param $info
* @return void
* @throws \Exception
*/
public static function checkPaySuccess($data, $info)
{
if ($info) {
$checkSign = ToolService::CheckSign($data, $data['sign']);
$checkMoney = sprintf("%.1f", $data['total_fee'] / 100) == sprintf("%.1f", $info->payment_amount); //分
$checkResult = $data['result_code'] == 'SUCCESS';
if (!$checkSign) {
throw new \Exception('签名错误');
return $service->handlePayNotify(function (PayMessage $message) {
Log::info('微信回调信息_request:' . json_encode($message->getOriginalAttributes() ?? [], JSON_UNESCAPED_UNICODE));
$order_no = $message->out_trade_no ?? '';
$orderInfo = OrderService::getOne(['order_no' => $order_no]);
if (!$orderInfo) {
throw new \Exception('获取订单数据失败');
}
//金额校验(v3 通知金额单位为分)
$total = $message->amount['total'] ?? 0;
$checkMoney = sprintf('%.1f', $total / 100) == sprintf('%.1f', $orderInfo->payment_amount);
if (!$checkMoney) {
throw new \Exception('金额错误:' . sprintf("%.2f", $data['total_fee'] / 100) . '----' . sprintf("%.2f", $info->payment_amount));
throw new \Exception('金额错误:' . sprintf('%.2f', $total / 100) . '----' . sprintf('%.2f', $orderInfo->payment_amount));
}
if (!$checkResult) {
throw new \Exception('支付失败');
if (($message->trade_state ?? '') != 'SUCCESS') {
throw new \Exception('支付失败:' . ($message->trade_state ?? ''));
}
} else {
throw new \Exception('获取订单数据失败');
}
Log::info('微信回调信息_result:' . json_encode(['order_no' => $order_no, 'trade_state' => $message->trade_state], JSON_UNESCAPED_UNICODE));
//如果为待支付,才可更新为已支付。防止重复更新状态,导致重复发货。
if ($orderInfo->order_status == self::ORDER_STATUS_WAIT_PAY) {
$paymentData = [
'order_no' => $order_no,
'pay_status' => 'SUCCESS',
'amount' => $total / 100,
'currency' => $message->amount['currency'] ?? 'CNY',
'pay_type' => '1', // 1微信 2支付宝
'pay_way' => $message->trade_type ?? 'JSAPI',
'trace_no' => $message->transaction_id ?? '',
'pay_time' => time(),
'pay_success_time' => strtotime((string)($message->success_time ?? '')) ?: time(),
];
Db::transaction(function () use ($order_no, $paymentData, $orderInfo) {
self::where('order_no', $order_no)->update(['order_status' => self::ORDER_STATUS_WAIT_SHIP]);
OrderPaymentService::create($paymentData);
if ($orderInfo->order_type == 1) {
DemandService::customerContractFinishedDeal($orderInfo->parent_order_no);
} elseif ($orderInfo->order_type == 2) {
DemandService::customerWorkAcceptFinishedDeal($orderInfo->parent_order_no);
}
});
}
});
}
/**
@@ -487,8 +465,8 @@ class OrderService
$EasyWeChatService = new EasyWeChatService();
$payRes = $EasyWeChatService->unify($body, $order_sn, $total_fee, $openid);
if (!($payRes && $payRes['return_code'] == 'SUCCESS' && $payRes['return_msg'] == 'OK')) {
throw new \Exception('统一下单接口调用失败');
if (!($payRes && !empty($payRes['prepay_id']))) {
throw new \Exception('统一下单接口调用失败:' . ($payRes['message'] ?? ($payRes['errmsg'] ?? '未知错误')));
}
$payData = $EasyWeChatService->sdkConfig($payRes['prepay_id']);