feat : refund

This commit is contained in:
2025-02-24 15:06:26 +08:00
parent 6754d2969e
commit 27a6ad3e2e
15 changed files with 591 additions and 281 deletions

View File

@@ -66,11 +66,21 @@ abstract class WxJsRechargeBaseService implements ThirdPayInterface
*/
abstract public function callBackHandle();
/**
* 退款回调
*/
abstract public function refundCallbackHandle();
/**
* 设置回调地址
*/
abstract public function setNotify();
/**
* 设置退款回调地址
*/
abstract public function setRefundNotify();
// /**
// * 设置商户号id
// */
@@ -90,16 +100,16 @@ abstract class WxJsRechargeBaseService implements ThirdPayInterface
try {
$this->callbackData = $this->ysdPay->wechat($this->config)->callback($this->request)->toArray()['resource']['ciphertext'];
}catch (Exception $e) {
$this->log->debug(__CLASS__.'微信支付回调解密失败'.json_encode($e->getMessage()));
throw new ErrException('微信支付回调解密失败');
$this->log->debug(__CLASS__.'wxPay回调解密失败'.json_encode($e->getMessage()));
throw new ErrException('wxPay回调解密失败');
}
if (!empty($this->callbackData)) {
$this->log->debug(__CLASS__.'获取回调失败'.json_encode($this->request));
throw new ErrException('获取回调失败');
$this->log->debug(__CLASS__.'wxPay获取回调失败'.json_encode($this->request));
throw new ErrException('wxPay获取回调失败');
}
$this->log->info(__CLASS__.'微信支付完成回调'.json_encode($this->callbackData));
$this->log->info(__CLASS__.'wxPay完成回调'.json_encode($this->callbackData));
}
@@ -156,4 +166,39 @@ abstract class WxJsRechargeBaseService implements ThirdPayInterface
throw new ErrException($e->getMessage());
}
}
/**
* @param float $refundAmount
* @param float $totalAmount
* @param string $outTradeNo
* @param string $outRefundNo
* @return Collection|Rocket
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function refund(float $refundAmount,float $totalAmount, string $outTradeNo, string $outRefundNo): Collection|Rocket
{
if (empty($this->config)) throw new ErrException('调起支付失败-微信支付配置项不存在');
try {
$order = [
'out_trade_no' => $outTradeNo,
'out_refund_no' => $outRefundNo,
'amount' => [
'refund' => (int)bcmul((string)$refundAmount, "100"),
'total' => (int)bcmul((string)$totalAmount, "100"),
'currency' => 'CNY',
],
'_action' => 'mini', // jsapi 退款,默认
];
$result = $this->ysdPay->wechat($this->config)->refund($order);
$this->log->callbackLog(__CLASS__.'微信退款调起数据|回调地址:'. json_encode($this->config['wechat']['default']).'|回调数据:'.json_encode($result).'|请求数据:'.json_encode($order));
return $result;
} catch (Exception $e) {
$this->log->error(__CLASS__.'微信退款调起失败。reason:'.$e->getMessage());
throw new ErrException($e->getMessage());
}
}
}

View File

@@ -8,6 +8,7 @@ use App\Service\ServiceTrait\Api\CheckOrderCallBackTrait;
use App\Service\ServiceTrait\Api\GoodOrderTrait;
use App\Service\ServiceTrait\Api\OrderTrait;
use App\Service\ServiceTrait\Api\PayFinishTrait;
use App\Service\ServiceTrait\Api\RefundOrderTrait;
use Hyperf\DbConnection\Db;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
@@ -19,7 +20,9 @@ class WxJsRechargeOrderService extends WxJsRechargeBaseService
{
use CheckOrderCallBackTrait,
GoodOrderTrait,
PayFinishTrait;
PayFinishTrait,
RefundOrderTrait,
OrderTrait;
/**
*
@@ -47,15 +50,20 @@ class WxJsRechargeOrderService extends WxJsRechargeBaseService
*/
protected mixed $payInfo;
/**
* @var mixed
*/
protected mixed $refundInfo;
/**
* @return ResponseInterface
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws InvalidParamsException
*/
public function callBackHandle()
public function callBackHandle(): ResponseInterface
{
$this-> getWechatData();
$this->getWechatData();
$this->checkWxCallBackOrder();
@@ -79,6 +87,31 @@ class WxJsRechargeOrderService extends WxJsRechargeBaseService
return $this->returnSuccess();
}
/**
* @return ResponseInterface
* @throws ContainerExceptionInterface
* @throws InvalidParamsException
* @throws NotFoundExceptionInterface
*/
public function refundCallBackHandle(): ResponseInterface
{
$this->getWechatData();
$this->checkWxRefundCallBackOrder();
$this->checkRefundOrder();
Db::transaction(function (){
$this->manageRefundOrder();
$this->manageOrderByRefund();
});
$this->sendStockMq($this->orderInfo->id,$this->orderInfo->status);
return $this->returnSuccess();
}
/**
* @return void
*/
@@ -87,4 +120,13 @@ class WxJsRechargeOrderService extends WxJsRechargeBaseService
//返回的回调地址
$this->config['wechat']['default']['notify_url'] = config('system.api_url').'/common/wxPay/order/js/callBack';
}
/**
* @return void
*/
public function setRefundNotify(): void
{
//返回的退款回调地址
$this->config['wechat']['default']['notify_url'] = config('system.api_url').'/common/wxPay/order/js/refund/callBack';
}
}