|
|
<?php
|
|
|
|
|
|
namespace App\Services\Payment;
|
|
|
|
|
|
use GuzzleHttp\Client;
|
|
|
use GuzzleHttp\Exception\ConnectException;
|
|
|
|
|
|
class YipayClient
|
|
|
{
|
|
|
private $settings;
|
|
|
private $client;
|
|
|
|
|
|
public function __construct(array $settings)
|
|
|
{
|
|
|
$this->settings = $settings;
|
|
|
$timeout = max(3, min(60, (int) $settings['yipay_request_timeout_seconds']));
|
|
|
$this->client = new Client([
|
|
|
'base_uri' => rtrim((string) $settings['yipay_gateway_base_url'], '/') . '/',
|
|
|
'timeout' => $timeout,
|
|
|
'connect_timeout' => min(10, $timeout),
|
|
|
'verify' => true,
|
|
|
'http_errors' => false,
|
|
|
'allow_redirects' => false,
|
|
|
]);
|
|
|
}
|
|
|
|
|
|
public function createOrder(array $payload)
|
|
|
{
|
|
|
$payload['sign'] = YipaySigner::sign($payload, $this->settings[YipayConfig::SECRET_KEY]);
|
|
|
$payload['sign_type'] = 'MD5';
|
|
|
try {
|
|
|
$response = $this->client->post('mapi.php', [
|
|
|
'headers' => ['Accept' => 'application/json'],
|
|
|
'form_params' => $payload,
|
|
|
]);
|
|
|
} catch (ConnectException $exception) {
|
|
|
$errno = (int) ($exception->getHandlerContext()['errno'] ?? 0);
|
|
|
if (in_array($errno, [6, 7], true)) {
|
|
|
throw PaymentOrderCreationException::notDispatched(
|
|
|
'易支付连接失败,订单没有发送到网关'
|
|
|
);
|
|
|
}
|
|
|
throw PaymentOrderCreationException::unknown(
|
|
|
'易支付创建订单响应未确认'
|
|
|
);
|
|
|
} catch (\Throwable $exception) {
|
|
|
throw PaymentOrderCreationException::unknown(
|
|
|
'易支付创建订单响应未确认'
|
|
|
);
|
|
|
}
|
|
|
$data = $this->decode($response->getStatusCode(),
|
|
|
(string) $response->getBody(), '创建订单', true);
|
|
|
if (!isset($data['trade_no']) || trim((string) $data['trade_no']) === '') {
|
|
|
throw PaymentOrderCreationException::unknown(
|
|
|
'易支付创建订单响应缺少平台订单号'
|
|
|
);
|
|
|
}
|
|
|
return $data;
|
|
|
}
|
|
|
|
|
|
public function queryMerchant()
|
|
|
{
|
|
|
try {
|
|
|
$response = $this->client->get('api.php', [
|
|
|
'headers' => ['Accept' => 'application/json'],
|
|
|
'query' => [
|
|
|
'act' => 'query',
|
|
|
'pid' => (string) $this->settings['yipay_pid'],
|
|
|
'key' => (string) $this->settings[YipayConfig::SECRET_KEY],
|
|
|
],
|
|
|
]);
|
|
|
} catch (\Throwable $exception) {
|
|
|
// The documented query endpoint puts the key in the URL, so never expose the client exception.
|
|
|
throw new \RuntimeException('易支付商户查询连接失败');
|
|
|
}
|
|
|
return $this->decode($response->getStatusCode(), (string) $response->getBody(), '查询商户');
|
|
|
}
|
|
|
|
|
|
public function queryOrder($orderNo)
|
|
|
{
|
|
|
try {
|
|
|
$response = $this->client->get('api.php', [
|
|
|
'headers' => ['Accept' => 'application/json'],
|
|
|
'query' => [
|
|
|
'act' => 'order',
|
|
|
'pid' => (string) $this->settings['yipay_pid'],
|
|
|
'key' => (string) $this->settings[YipayConfig::SECRET_KEY],
|
|
|
'out_trade_no' => (string) $orderNo,
|
|
|
],
|
|
|
]);
|
|
|
} catch (\Throwable $exception) {
|
|
|
// Never include the request exception because the documented URL contains the merchant key.
|
|
|
throw new \RuntimeException('易支付订单查询连接失败');
|
|
|
}
|
|
|
return $this->decode($response->getStatusCode(), (string) $response->getBody(), '查询订单');
|
|
|
}
|
|
|
|
|
|
public function refund($tradeNo, $orderNo, $amount)
|
|
|
{
|
|
|
$response = $this->client->post('api.php', [
|
|
|
'headers' => ['Accept' => 'application/json'],
|
|
|
'query' => ['act' => 'refund'],
|
|
|
'form_params' => [
|
|
|
'pid' => (string) $this->settings['yipay_pid'],
|
|
|
'key' => (string) $this->settings[YipayConfig::SECRET_KEY],
|
|
|
'trade_no' => (string) $tradeNo,
|
|
|
'out_trade_no' => (string) $orderNo,
|
|
|
'money' => PaymentAmount::normalize($amount),
|
|
|
],
|
|
|
]);
|
|
|
return $this->decode($response->getStatusCode(), (string) $response->getBody(), '提交退款');
|
|
|
}
|
|
|
|
|
|
private function decode($httpStatus, $body, $operation,
|
|
|
bool $creatingOrder = false)
|
|
|
{
|
|
|
if (strlen($body) > 65536) {
|
|
|
if ($creatingOrder) {
|
|
|
throw PaymentOrderCreationException::unknown(
|
|
|
'易支付创建订单响应过大'
|
|
|
);
|
|
|
}
|
|
|
throw new \RuntimeException('易支付' . $operation . '响应过大');
|
|
|
}
|
|
|
$data = json_decode($body, true);
|
|
|
if (!is_array($data)) {
|
|
|
if ($creatingOrder) {
|
|
|
throw PaymentOrderCreationException::unknown(
|
|
|
'易支付返回了无效的创建订单响应'
|
|
|
);
|
|
|
}
|
|
|
throw new \RuntimeException('易支付返回了无效 JSON(HTTP ' . (int) $httpStatus . ')');
|
|
|
}
|
|
|
if ((int) $httpStatus !== 200) {
|
|
|
if ($creatingOrder) {
|
|
|
throw PaymentOrderCreationException::unknown(
|
|
|
'易支付创建订单返回 HTTP ' . (int) $httpStatus
|
|
|
);
|
|
|
}
|
|
|
throw new \RuntimeException(
|
|
|
'易支付请求失败(HTTP ' . (int) $httpStatus . ')'
|
|
|
);
|
|
|
}
|
|
|
if ((int) ($data['code'] ?? 0) !== 1) {
|
|
|
$message = trim(strip_tags((string) ($data['msg'] ?? '')));
|
|
|
if ($message !== '') {
|
|
|
$message = mb_substr($message, 0, 300);
|
|
|
}
|
|
|
$failure = $message !== '' ? '易支付:' . $message
|
|
|
: '易支付拒绝创建订单';
|
|
|
if ($creatingOrder) {
|
|
|
throw PaymentOrderCreationException::rejected($failure);
|
|
|
}
|
|
|
throw new \RuntimeException($failure);
|
|
|
}
|
|
|
return $data;
|
|
|
}
|
|
|
}
|