You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/app/Services/Payment/YipayClient.php

113 lines
4.4 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Services\Payment;
use GuzzleHttp\Client;
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';
$response = $this->client->post('mapi.php', [
'headers' => ['Accept' => 'application/json'],
'form_params' => $payload,
]);
$data = $this->decode($response->getStatusCode(), (string) $response->getBody(), '创建订单');
if (!isset($data['trade_no']) || trim((string) $data['trade_no']) === '') {
throw new \RuntimeException('易支付创建订单响应缺少平台订单号');
}
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)
{
if (strlen($body) > 65536) {
throw new \RuntimeException('易支付' . $operation . '响应过大');
}
$data = json_decode($body, true);
if (!is_array($data)) {
throw new \RuntimeException('易支付返回了无效 JSON(HTTP ' . (int) $httpStatus . ')');
}
if ((int) $httpStatus !== 200 || (int) ($data['code'] ?? 0) !== 1) {
$message = trim(strip_tags((string) ($data['msg'] ?? '')));
if ($message !== '') {
$message = mb_substr($message, 0, 300);
}
throw new \RuntimeException($message !== '' ? '易支付:' . $message : '易支付请求失败(HTTP ' . (int) $httpStatus . ')');
}
return $data;
}
}