This commit is contained in:
chenbo 2023-11-21 16:06:27 +08:00
parent e1e599a4cb
commit 0bba4e2aa6
3 changed files with 62 additions and 8 deletions

View File

@ -2,9 +2,12 @@
namespace app\api\controller;
use app\common\enum\PayOrderEnum;
use app\common\enum\user\UserTerminalEnum;
use app\common\logic\PaymentLogic;
use app\common\logic\WechatPayServiceMerchantPaymentLogic;
use app\common\model\pay\PayOrder;
use app\common\model\pay\RefundOrder;
use app\common\service\pay\WeChatPayMerchantService;
use app\common\service\pay\WeChatPayService;
use think\Log;
@ -36,19 +39,37 @@ class WechatPayServiceMerchantPayController extends BaseApiController
/**
* 申请退款
* 先查询有没有支付订单
* 在查询微信支付有没有支付记录
* 最后再发起退款申请
*/
public function refund()
{
$params = $this->request->param(); // business_order_no order_type
$params = $this->request->param(); // business_order_no business_callback_url
$payOrder = PayOrder::where(['business_order_no' => $params['business_order_no'], 'pay_status'=>PayOrderEnum::PAY_STATUS_ISPAID])->find();
if (empty($payOrder)) {
return $this->fail('系统未找到该订单的支付记录');
}
$wechatPayOrder = (new WeChatPayMerchantService())->queryPayOrder($payOrder);
if ($wechatPayOrder['trade_state'] === 'REFUND') {
return $this->fail('该订单已申请退款,请勿重复申请');
}
if ($wechatPayOrder['trade_state'] !== 'SUCCESS') {
return $this->fail('交易平台查询到该笔订单未支付成功,无法发起退款');
}
$refundOrder = WechatPayServiceMerchantPaymentLogic::createRefundOrder($params);
if (false === $refundOrder) {
return $this->fail('退款订单创建失败', $params);
}
//支付流程
// 发起退款申请
$result = (new WeChatPayMerchantService())->refund($refundOrder);
if (false === $result) {
return $this->fail((new WeChatPayMerchantService())->getError(), $params);
}
return $this->success('成功', $result);
}
/**

View File

@ -69,12 +69,13 @@ class WechatPayServiceMerchantPaymentLogic extends BaseLogic
$data = [
'refund_sn' => 'refund'.generate_sn(RefundOrder::class, 'refund_sn'),
'order_sn' => $payOrder['order_no'],
'business_order_sn' => $payOrder['business_order_sn'],
'business_order_sn' => $payOrder['business_order_no'],
'order_from' => $payOrder['order_from'],
'order_amount' => $payOrder['total_fee'],
'refund_amount' => $payOrder['total_fee'],
'refund_type' => 1,
'refund_status' => 0,
'business_callback_url' => $params['business_callback_url'],
'create_time' => time(),
'update_time' => time(),
];

View File

@ -119,12 +119,13 @@ class WeChatPayMerchantService extends BasePayService
*/
public function refund($refundOrder)
{
$payOrder = PayOrder::where(['order_no' => $refundOrder['order_sn'], 'pay_status'=>PayOrderEnum::PAY_STATUS_ISPAID])->find();
$response = $this->app->getClient()->postJson('/v3/refund/domestic/refunds', [
'sub_mchid' => $payOrder['collection_account'],
'out_refund_no' => $payOrder['order_no'],
'notify_url' => (string)url('wechat_pay_service_merchant_pay/notifyApp', [], false, true),
'out_trade_no' => $payOrder['order_no'],
'out_refund_no' => $refundOrder['refund_sn'],
'notify_url' => (string)url('wechat_pay_service_merchant_pay/refundNotify', [], false, true),
'amount' => [
'refund' => $refundOrder['refund_amount'],
'total' => $refundOrder['refund_amount'],
@ -136,6 +137,26 @@ class WeChatPayMerchantService extends BasePayService
return $result;
}
/**
* @notes 查询退款
* @param $refundSn
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @author 段誉
* @date 2023/3/1 11:16
*/
public function queryPayOrder($order)
{
$response = $this->app->getClient()->get("/v3/pay/partner/transactions/out-trade-no/{$order['order_no']}", [
'query' => [
'sp_mchid' => $this->config['mch_id'],
'sub_mchid' => $order['collection_account']
]
]);
$result = $response->toArray(false);
$this->checkResultFail($result);
return $result;
}
/**
* @notes 查询退款
@ -148,7 +169,9 @@ class WeChatPayMerchantService extends BasePayService
public function queryRefund($refundSn)
{
$response = $this->app->getClient()->get("v3/refund/domestic/refunds/{$refundSn}");
return $response->toArray(false);
$result = $response->toArray(false);
$this->checkResultFail($result);
return $result;
}
@ -203,7 +226,7 @@ class WeChatPayMerchantService extends BasePayService
if ($message['trade_state'] === 'SUCCESS') {
return $this->handlePaid($message);
} else {
\think\facade\Log::info('支付失败', $message);
\think\facade\Log::info('支付失败', (array)$message);
}
return true;
});
@ -241,6 +264,15 @@ class WeChatPayMerchantService extends BasePayService
}
/**
* @return \Psr\Http\Message\ResponseInterface
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
* @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
* @throws \ReflectionException
* @throws \Throwable
* 修改状态,回写信息
* 通知业务系统
*/
public function refundNotify()
{
$server = $this->app->getServer();