add 供销对接支付系统

This commit is contained in:
chenbo 2023-11-20 10:48:44 +08:00
parent b4c6f00ffe
commit 04d478d880
3 changed files with 68 additions and 32 deletions

View File

@ -16,8 +16,11 @@ namespace app\api\controller;
use app\api\validate\PayValidate; use app\api\validate\PayValidate;
use app\common\enum\PayEnum;
use app\common\enum\user\UserTerminalEnum; use app\common\enum\user\UserTerminalEnum;
use app\common\logic\PaymentLogic; use app\common\logic\PaymentLogic;
use app\common\logic\PayRequestLogic;
use app\common\model\Company;
use app\common\service\pay\WeChatPayService; use app\common\service\pay\WeChatPayService;
use think\facade\Log; use think\facade\Log;
@ -62,9 +65,19 @@ class PayController extends BaseApiController
if (false === $order) { if (false === $order) {
return $this->fail(PaymentLogic::getError(), $params); return $this->fail(PaymentLogic::getError(), $params);
} }
//支付流程 // 请求支付系统
$redirectUrl = $params['redirect'] ?? '/pages/payment/payment'; $companyInfo = Company::where(['company_id' => $this->userInfo['company_id']])->find();
$result = PaymentLogic::pay($params['pay_way'], $params['from'], $order, $this->userInfo['terminal'], $redirectUrl); $requestData = [
'street' => $companyInfo['street'],
'order_from' => 12,
'order_type' => 101,
'pay_user_role' => $this->userInfo['group_id'],
'pay_user_info' => $this->userInfo,
'business_order_no' => $order['order_no'],
'total_fee' => $order['order_amount'],
'business_callback_url' => (string)url('pay/notifyApp', [], false, true)
];
$result = PayRequestLogic::getPrePayId($requestData);
if (false === $result) { if (false === $result) {
return $this->fail(PaymentLogic::getError(), $params); return $this->fail(PaymentLogic::getError(), $params);
} }
@ -132,7 +145,8 @@ class PayController extends BaseApiController
*/ */
public function notifyApp() public function notifyApp()
{ {
return (new WeChatPayService(UserTerminalEnum::ANDROID))->notify(); $param = $this->request->param();
return (new WeChatPayService(UserTerminalEnum::ANDROID))->notify($param);
} }

View File

@ -0,0 +1,21 @@
<?php
namespace app\common\logic;
use Symfony\Component\HttpClient\HttpClient;
class PayRequestLogic extends BaseLogic
{
public static function getPrePayId($param)
{
try {
$requestResponse = HttpClient::create()->request('POST', env('url.pay_prefix'). '/api/wechat_pay_service_merchant_pay/appPrePay', [
'body' => $param
]);
return json_decode($requestResponse->getContent(), true);
} catch (Exception $e) {
self::setError($e->getMessage());
return false;
}
}
}

View File

@ -362,35 +362,36 @@ class WeChatPayService extends BasePayService
* @author 段誉 * @author 段誉
* @date 2023/2/28 14:20 * @date 2023/2/28 14:20
*/ */
public function notify() public function notify($param)
{ {
$server = $this->app->getServer(); Log::info(['支付回调', $param]);
// 支付通知 // $server = $this->app->getServer();
$server->handlePaid(function (Message $message) { // // 支付通知
$data = ['trade_state' => $message['trade_state'] ?? '', 'out_trade_no' => $message['out_trade_no'] ?? '', 'transaction_id' => $message['transaction_id'] ?? '', 'attach' => $message['attach'] ?? '']; // $server->handlePaid(function (Message $message) {
Log::info('wechat pay notify: ' . var_export($data, true)); // $data = ['trade_state' => $message['trade_state'] ?? '', 'out_trade_no' => $message['out_trade_no'] ?? '', 'transaction_id' => $message['transaction_id'] ?? '', 'attach' => $message['attach'] ?? ''];
if ($message['trade_state'] === 'SUCCESS') { // Log::info('wechat pay notify: ' . var_export($data, true));
$extra['transaction_id'] = $message['transaction_id']; // if ($message['trade_state'] === 'SUCCESS') {
$attach = $message['attach']; // $extra['transaction_id'] = $message['transaction_id'];
$message['out_trade_no'] = mb_substr($message['out_trade_no'], 0, 18); // $attach = $message['attach'];
switch ($attach) { // $message['out_trade_no'] = mb_substr($message['out_trade_no'], 0, 18);
case 'recharge': // switch ($attach) {
$order = RechargeOrder::where(['sn' => $message['out_trade_no']])->findOrEmpty(); // case 'recharge':
if($order->isEmpty() || $order->pay_status == PayEnum::ISPAID) { // $order = RechargeOrder::where(['sn' => $message['out_trade_no']])->findOrEmpty();
return true; // if($order->isEmpty() || $order->pay_status == PayEnum::ISPAID) {
} // return true;
PayNotifyLogic::handle('recharge', $message['out_trade_no'], $extra); // }
break; // PayNotifyLogic::handle('recharge', $message['out_trade_no'], $extra);
} // break;
} // }
return true; // }
}); // return true;
// });
// 退款通知 //
$server->handleRefunded(function (Message $message) { // // 退款通知
return true; // $server->handleRefunded(function (Message $message) {
}); // return true;
return $server->serve(); // });
// return $server->serve();
} }
public function configForPayment($prepayId, $appId) public function configForPayment($prepayId, $appId)