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\common\enum\PayEnum;
use app\common\enum\user\UserTerminalEnum;
use app\common\logic\PaymentLogic;
use app\common\logic\PayRequestLogic;
use app\common\model\Company;
use app\common\service\pay\WeChatPayService;
use think\facade\Log;
@ -62,9 +65,19 @@ class PayController extends BaseApiController
if (false === $order) {
return $this->fail(PaymentLogic::getError(), $params);
}
//支付流程
$redirectUrl = $params['redirect'] ?? '/pages/payment/payment';
$result = PaymentLogic::pay($params['pay_way'], $params['from'], $order, $this->userInfo['terminal'], $redirectUrl);
// 请求支付系统
$companyInfo = Company::where(['company_id' => $this->userInfo['company_id']])->find();
$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) {
return $this->fail(PaymentLogic::getError(), $params);
}
@ -132,7 +145,8 @@ class PayController extends BaseApiController
*/
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 段誉
* @date 2023/2/28 14:20
*/
public function notify()
public function notify($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'] ?? ''];
Log::info('wechat pay notify: ' . var_export($data, true));
if ($message['trade_state'] === 'SUCCESS') {
$extra['transaction_id'] = $message['transaction_id'];
$attach = $message['attach'];
$message['out_trade_no'] = mb_substr($message['out_trade_no'], 0, 18);
switch ($attach) {
case 'recharge':
$order = RechargeOrder::where(['sn' => $message['out_trade_no']])->findOrEmpty();
if($order->isEmpty() || $order->pay_status == PayEnum::ISPAID) {
return true;
}
PayNotifyLogic::handle('recharge', $message['out_trade_no'], $extra);
break;
}
}
return true;
});
// 退款通知
$server->handleRefunded(function (Message $message) {
return true;
});
return $server->serve();
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'] ?? ''];
// Log::info('wechat pay notify: ' . var_export($data, true));
// if ($message['trade_state'] === 'SUCCESS') {
// $extra['transaction_id'] = $message['transaction_id'];
// $attach = $message['attach'];
// $message['out_trade_no'] = mb_substr($message['out_trade_no'], 0, 18);
// switch ($attach) {
// case 'recharge':
// $order = RechargeOrder::where(['sn' => $message['out_trade_no']])->findOrEmpty();
// if($order->isEmpty() || $order->pay_status == PayEnum::ISPAID) {
// return true;
// }
// PayNotifyLogic::handle('recharge', $message['out_trade_no'], $extra);
// break;
// }
// }
// return true;
// });
//
// // 退款通知
// $server->handleRefunded(function (Message $message) {
// return true;
// });
// return $server->serve();
}
public function configForPayment($prepayId, $appId)