207 lines
6.0 KiB
PHP
207 lines
6.0 KiB
PHP
<?php
|
|
|
|
namespace App\Service\Common\Pay\Wx;
|
|
|
|
use App\Exception\ErrException;
|
|
use App\Lib\Log;
|
|
use App\Service\Common\Pay\ThirdPayInterface;
|
|
use Exception;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
use Hyperf\HttpServer\Contract\RequestInterface;
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use Yansongda\Artful\Exception\InvalidParamsException;
|
|
use Yansongda\Artful\Rocket;
|
|
use Yansongda\HyperfPay\Pay;
|
|
use Yansongda\Supports\Collection;
|
|
use function Hyperf\Config\config;
|
|
|
|
abstract class WxJsRechargeBaseService implements ThirdPayInterface
|
|
{
|
|
/**
|
|
* @var string
|
|
*/
|
|
public string $openId;
|
|
|
|
/**
|
|
* 回调地址
|
|
* @var string
|
|
*/
|
|
protected string $notifyUrl = '';
|
|
|
|
/**
|
|
* @var Pay $ysdPay
|
|
*/
|
|
#[Inject]
|
|
protected Pay $ysdPay;
|
|
|
|
/**
|
|
* @var Log
|
|
*/
|
|
#[Inject]
|
|
protected Log $log;
|
|
|
|
/**
|
|
* 注入
|
|
* @var RequestInterface
|
|
*/
|
|
#[Inject]
|
|
protected RequestInterface $request;
|
|
|
|
/**
|
|
* 配置项
|
|
* @var array
|
|
*/
|
|
protected array $config = [];
|
|
|
|
/**
|
|
* 返回数据
|
|
* @var array
|
|
*/
|
|
protected array $callbackData = [];
|
|
|
|
/**
|
|
* 充值回调
|
|
*/
|
|
abstract public function callBackHandle();
|
|
|
|
/**
|
|
* 退款回调
|
|
*/
|
|
abstract public function refundCallbackHandle();
|
|
|
|
/**
|
|
* 设置回调地址
|
|
*/
|
|
abstract public function setNotify();
|
|
|
|
/**
|
|
* 设置退款回调地址
|
|
*/
|
|
abstract public function setRefundNotify();
|
|
|
|
// /**
|
|
// * 设置商户号id
|
|
// */
|
|
// abstract public function setMchId();
|
|
|
|
|
|
/**
|
|
* 获取返回数据
|
|
* @return void
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface|InvalidParamsException
|
|
*/
|
|
public function getWechatData(): void
|
|
{
|
|
$this->setConfig();
|
|
|
|
$this->callbackData = $this->request->all();
|
|
// try {
|
|
// $this->callbackData = $this->ysdPay->wechat($this->config)->callback($this->request)->toArray()['resource']['ciphertext'];
|
|
// }catch (Exception $e) {
|
|
// $this->log->debug(__CLASS__.'wxPay回调解密失败'.json_encode($e->getMessage()));
|
|
// throw new ErrException('wxPay回调解密失败');
|
|
// }
|
|
|
|
if (empty($this->callbackData)) {
|
|
$this->log->debug(__CLASS__.'wxPay获取回调失败'.json_encode($this->request));
|
|
throw new ErrException('wxPay获取回调失败');
|
|
}
|
|
|
|
$this->log->callbackLog(__CLASS__.'wxPay完成回调'.json_encode($this->callbackData));
|
|
}
|
|
|
|
|
|
/**
|
|
* 确认回调
|
|
* @return ResponseInterface
|
|
*/
|
|
protected function returnSuccess(): ResponseInterface
|
|
{
|
|
$this->setConfig();
|
|
return $this->ysdPay->wechat($this->config)->success();
|
|
}
|
|
|
|
/**
|
|
* @return void
|
|
*/
|
|
public function setConfig(): void
|
|
{
|
|
$this->config = config('ysdPay');
|
|
$this->config = array_merge($this->config, ['_force' => true]);
|
|
}
|
|
|
|
/**
|
|
* @param float $money
|
|
* @param int $orderId
|
|
* @param string $outTradeNo
|
|
* @param int $userId
|
|
* @return Collection|Rocket
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function pay(float $money, int $orderId, string $outTradeNo, int $userId): Collection|Rocket
|
|
{
|
|
if (empty($this->config)) throw new ErrException('调起支付失败-微信支付配置项不存在');
|
|
|
|
try {
|
|
$wxOrder = [
|
|
'out_trade_no' => $outTradeNo,
|
|
'description' => '测试订单',
|
|
'amount' => [
|
|
'total' => (int)bcmul((string)$money, "100"),
|
|
],
|
|
'payer' => [
|
|
'sub_openid' => $this->openId,
|
|
],
|
|
];
|
|
|
|
$result = $this->ysdPay->wechat($this->config)->mp($wxOrder);
|
|
// $result = $this->ysdPay->wechat(array_merge($this->config, ['_force' => true]))->mp($wxOrder);
|
|
|
|
$this->log->callbackLog(__CLASS__.'微信支付调起数据|回调地址:'. json_encode($this->config['wechat']['default']).'|回调数据:'.json_encode($result).'|请求数据:'.json_encode($wxOrder));
|
|
return $result;
|
|
} catch (Exception $e) {
|
|
$this->log->error(__CLASS__.'微信支付调起失败。reason:'.$e->getMessage());
|
|
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);
|
|
// $result = $this->ysdPay->wechat(array_merge($this->config, ['_force' => true]))->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());
|
|
}
|
|
}
|
|
} |