add 支付回调逻辑

This commit is contained in:
chenbo 2023-11-21 10:44:21 +08:00
parent c08163543c
commit 97534ec292
2 changed files with 47 additions and 4 deletions

View File

@ -21,6 +21,7 @@ use app\common\model\pay\PayWay;
use app\common\model\recharge\RechargeOrder; use app\common\model\recharge\RechargeOrder;
use app\common\model\user\User; use app\common\model\user\User;
use app\common\service\pay\WeChatPayService; use app\common\service\pay\WeChatPayService;
use Symfony\Component\HttpClient\HttpClient;
/** /**
@ -231,4 +232,17 @@ class PaymentLogic extends BaseLogic
return $orderSn . $terminal . $suffix; return $orderSn . $terminal . $suffix;
} }
public static function callBusiness($notifyUrl,$param)
{
try {
$requestResponse = HttpClient::create()->request('POST', $notifyUrl, [
'body' => $param
]);
return json_decode($requestResponse->getContent(), true);
} catch (Exception $e) {
self::setError($e->getMessage());
return false;
}
}
} }

View File

@ -19,6 +19,7 @@ namespace app\common\service\pay;
use app\common\enum\PayEnum; use app\common\enum\PayEnum;
use app\common\enum\PayOrderEnum; use app\common\enum\PayOrderEnum;
use app\common\enum\user\UserTerminalEnum; use app\common\enum\user\UserTerminalEnum;
use app\common\logic\PaymentLogic;
use app\common\logic\PayNotifyLogic; use app\common\logic\PayNotifyLogic;
use app\common\model\pay\PayConfig; use app\common\model\pay\PayConfig;
use app\common\model\pay\PayOrder; use app\common\model\pay\PayOrder;
@ -28,6 +29,7 @@ use app\common\service\wechat\WeChatConfigService;
use app\common\service\wechat\WeChatPayMerchantConfigService; use app\common\service\wechat\WeChatPayMerchantConfigService;
use EasyWeChat\Pay\Application; use EasyWeChat\Pay\Application;
use EasyWeChat\Pay\Message; use EasyWeChat\Pay\Message;
use think\Exception;
use think\Log; use think\Log;
@ -199,16 +201,43 @@ class WeChatPayMerchantService extends BasePayService
$server->handlePaid(function (Message $message) { $server->handlePaid(function (Message $message) {
Log::info(['支付回调信息', $message]); Log::info(['支付回调信息', $message]);
if ($message['trade_state'] === 'SUCCESS') { if ($message['trade_state'] === 'SUCCESS') {
$transaction_id = $message['transaction_id']; $this->handlePaid($message);
$attach = $message['attach']; } else {
$out_trade_no = $message['out_trade_no']; Log::info('支付失败', $message);
} }
return true; return true;
}); });
return $server->serve(); return $server->serve();
} }
/**
* @return void
* 修改支付状态,回写回调信息。
* 通知业务系统
*/
public function handlePaid($message)
{
try {
$transaction_id = $message['transaction_id'];
$out_trade_no = $message['out_trade_no'];
$order = PayOrder::where('order_no', $out_trade_no)->find();
if (empty($order)) {
throw new Exception('回调订单不存在');
}
$order->transaction_id = $transaction_id;
$order->pay_status = PayOrderEnum::PAY_STATUS_ISPAID;
$order->finish_time = time();
$order->save();
// 回调业务系统
PaymentLogic::callBusiness($order['business_callback_url'], ['out_trade_no'=>$order['business_order_no'], 'transaction_id'=>$transaction_id, 'pay_status'=>1]);
} catch (\Exception $e) {
Log::error(['支付回调信息处理失败', $e->getMessage(),$message]);
return;
}
}
public function refundNotify() public function refundNotify()
{ {
$server = $this->app->getServer(); $server = $this->app->getServer();