Skip to content

Commit e9d35dd

Browse files
committed
notify
1 parent 604e07c commit e9d35dd

File tree

5 files changed

+98
-24
lines changed

5 files changed

+98
-24
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
'yzh_public_key' => env('YZH_YZH_PUBLIC_KEY', storage_path('certs/yzh_public.pem')),
1616
'sign_type' => 'rsa',
1717
'timeout' => 30,
18+
1819
// 支付全局配置 调用 pay 方法指定的配置优先级高于全局配置
19-
'pay_remark' => 'remark', // 订单备注 [选填]
20-
'notify_url' => 'notify url', // 回调地址 长度不超过 200 个字符
20+
'pay_remark' => 'remark', // 订单备注 [选填] 银行卡支付时最大36个字符 支付宝支付时候最大40个字符 都不能包含特殊字符
21+
'notify_url' => 'notify url', // 回调地址 [选填] 长度不超过 200 个字符
2122
'project_id' => 'project id', // 项目ID [选填] 该字段由云账户分配,当接口指定项目时,会将订单关联指定项目
22-
];
2323
// ...
2424
```

src/Response.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
use Yzh\Model\Apiusersign\ApiUserSignReleaseResponseData;
88
use Yzh\Model\Apiusersign\ApiUserSignResponseData;
99
use Yzh\Model\BaseResponse;
10+
use Yzh\Model\Notify\NotifyResponse;
1011
use Yzh\Model\Payment\CreateAlipayOrderResponseData;
1112
use Yzh\Model\Payment\CreateBankpayOrderResponseData;
1213

1314
class Response
1415
{
1516
public function __construct(
1617
protected bool $status,
17-
protected ?BaseResponse $response = null,
18+
protected BaseResponse|NotifyResponse|null $response = null,
1819
protected string $error = ''
1920
) {
2021
}
@@ -27,6 +28,10 @@ public function ok(): bool
2728
public function data(): Collection
2829
{
2930
if ($response = $this->response) {
31+
if ($response instanceof NotifyResponse) {
32+
return collect(json_decode($response->getData(), true));
33+
}
34+
3035
$data = $response->getData();
3136

3237
if ($data instanceof ApiUserSignContractResponseData) {

src/ResponseAdapter.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Mitoop\Yzh;
4+
5+
use Yzh\Model\BaseResponse;
6+
use Yzh\Model\Notify\NotifyResponse;
7+
8+
class ResponseAdapter
9+
{
10+
public function __construct(protected BaseResponse|NotifyResponse $response)
11+
{
12+
}
13+
14+
public function isSuccess(): bool
15+
{
16+
if ($this->response instanceof BaseResponse) {
17+
return $this->response->isSuccess();
18+
}
19+
20+
if ($this->response instanceof NotifyResponse) {
21+
return $this->response->getSignRes() && $this->response->getDescryptRes();
22+
}
23+
}
24+
25+
public function getError(): string
26+
{
27+
if ($this->response instanceof BaseResponse) {
28+
return sprintf('%s:%s %s',
29+
$this->response->getCode(),
30+
$this->response->getMessage(),
31+
$this->response->getRequestID()
32+
);
33+
}
34+
35+
if ($this->response instanceof NotifyResponse) {
36+
return sprintf('验签失败 sign_res:%s descrypt_res%s',
37+
(int) $this->response->getSignRes(),
38+
(int) $this->response->getDescryptRes()
39+
);
40+
}
41+
42+
return '';
43+
}
44+
45+
public function getResponse(): BaseResponse|NotifyResponse
46+
{
47+
return $this->response;
48+
}
49+
}

src/Service.php

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,12 @@
66
use Yzh\ApiUserSignServiceClient;
77
use Yzh\Config;
88
use Yzh\Model\Apiusersign\ApiUserSignContractRequest;
9-
use Yzh\Model\Apiusersign\ApiUserSignContractResponse;
109
use Yzh\Model\Apiusersign\ApiUserSignReleaseRequest;
11-
use Yzh\Model\Apiusersign\ApiUserSignReleaseResponse;
1210
use Yzh\Model\Apiusersign\ApiUserSignRequest;
13-
use Yzh\Model\Apiusersign\ApiUserSignResponse;
11+
use Yzh\Model\Notify\NotifyRequest;
1412
use Yzh\Model\Payment\CreateAlipayOrderRequest;
15-
use Yzh\Model\Payment\CreateAlipayOrderResponse;
1613
use Yzh\Model\Payment\CreateBankpayOrderRequest;
17-
use Yzh\Model\Payment\CreateBankpayOrderResponse;
14+
use Yzh\NotifyClient;
1815
use Yzh\PaymentClient;
1916

2017
class Service
@@ -25,6 +22,8 @@ class Service
2522

2623
protected static string $projectId = '';
2724

25+
protected static bool $validateNotifyIP = false;
26+
2827
protected Config $config;
2928

3029
public function __construct(array $config)
@@ -35,7 +34,7 @@ public function __construct(array $config)
3534
$this->config = Config::newFromArray($config);
3635
}
3736

38-
public function getContract(): ApiUserSignContractResponse
37+
public function getContract(): ResponseAdapter
3938
{
4039
$client = new ApiUserSignServiceClient($this->config);
4140

@@ -46,10 +45,10 @@ public function getContract(): ApiUserSignContractResponse
4645

4746
$request->setRequestID((string) Str::orderedUuid());
4847

49-
return $client->apiUserSignContract($request);
48+
return new ResponseAdapter($client->apiUserSignContract($request));
5049
}
5150

52-
public function sign(string $name, string $idCard): ApiUserSignResponse
51+
public function sign(string $name, string $idCard): ResponseAdapter
5352
{
5453
$client = new ApiUserSignServiceClient($this->config);
5554

@@ -63,10 +62,10 @@ public function sign(string $name, string $idCard): ApiUserSignResponse
6362

6463
$request->setRequestID((string) Str::orderedUuid());
6564

66-
return $client->apiUserSign($request);
65+
return new ResponseAdapter($client->apiUserSign($request));
6766
}
6867

69-
public function unsign(string $name, string $idCard): ApiUserSignReleaseResponse
68+
public function unsign(string $name, string $idCard): ResponseAdapter
7069
{
7170
$client = new ApiUserSignServiceClient($this->config);
7271

@@ -80,10 +79,16 @@ public function unsign(string $name, string $idCard): ApiUserSignReleaseResponse
8079

8180
$request->setRequestID((string) Str::orderedUuid());
8281

83-
return $client->apiUserSignRelease($request);
82+
return new ResponseAdapter($client->apiUserSignRelease($request));
8483
}
8584

86-
protected function payWithBankCard(BankCard $bankCard, string $amount, string $orderId, string $payRemark, string $notifyUrl, string $projectId): CreateBankpayOrderResponse
85+
protected function payWithBankCard(
86+
BankCard $bankCard,
87+
string $amount,
88+
string $orderId,
89+
string $payRemark,
90+
string $notifyUrl,
91+
string $projectId): ResponseAdapter
8792
{
8893
$client = new PaymentClient($this->config);
8994

@@ -103,10 +108,16 @@ protected function payWithBankCard(BankCard $bankCard, string $amount, string $o
103108

104109
$request->setRequestID((string) Str::orderedUuid());
105110

106-
return $client->createBankpayOrder($request);
111+
return new ResponseAdapter($client->createBankpayOrder($request));
107112
}
108113

109-
protected function payWithAlipay(BankCard $bankCard, string $amount, string $orderId, string $payRemark, string $notifyUrl, string $projectId): CreateAlipayOrderResponse
114+
protected function payWithAlipay(
115+
BankCard $bankCard,
116+
string $amount,
117+
string $orderId,
118+
string $payRemark,
119+
string $notifyUrl,
120+
string $projectId): ResponseAdapter
110121
{
111122
$client = new PaymentClient($this->config);
112123

@@ -127,7 +138,7 @@ protected function payWithAlipay(BankCard $bankCard, string $amount, string $ord
127138

128139
$request->setRequestID((string) Str::orderedUuid());
129140

130-
return $client->createAlipayOrder($request);
141+
return new ResponseAdapter($client->createAlipayOrder($request));
131142
}
132143

133144
/**
@@ -144,7 +155,7 @@ public function pay(
144155
string $payRemark = '',
145156
string $notifyUrl = '',
146157
string $projectId = ''
147-
): CreateBankpayOrderResponse|CreateAlipayOrderResponse {
158+
): ResponseAdapter {
148159
$payRemark = ($payRemark !== '') ? $payRemark : static::$payRemark;
149160
$notifyUrl = ($notifyUrl !== '') ? $notifyUrl : static::$notifyUrl;
150161
$projectId = ($projectId !== '') ? $projectId : static::$projectId;
@@ -158,6 +169,15 @@ public function pay(
158169
}
159170
}
160171

172+
public function processCallback(): ResponseAdapter
173+
{
174+
$client = new NotifyClient($this->config);
175+
176+
$request = new NotifyRequest(request('data'), request('mess'), request('timestamp'), request('sign'));
177+
178+
return new ResponseAdapter($client->verifyAndDecrypt($request));
179+
}
180+
161181
public static function payRemarkUsing(string $payRemark): void
162182
{
163183
static::$payRemark = $payRemark;

src/Yzh.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
namespace Mitoop\Yzh;
44

55
use Throwable;
6-
use Yzh\Model\BaseResponse;
76

87
/**
98
* @method static Response getContract()
109
* @method static Response sign(string $name, string $idCard)
1110
* @method static Response unsign(string $name, string $idCard)
1211
* @method static Response pay(BankCard $bankCard, string $amount, string $orderId, string $payRemark = '',string $notifyUrl = '',string $projectId = '')
12+
* @method static Response processCallback()
1313
*
1414
* @see Service
1515
*/
@@ -18,16 +18,16 @@ class Yzh
1818
public static function __callStatic(string $method, array $args)
1919
{
2020
try {
21-
/** @var BaseResponse $response */
21+
/** @var ResponseAdapter $response */
2222
$response = app(Service::class)->$method(...$args);
2323

2424
if ($response->isSuccess()) {
25-
return new Response(true, response: $response);
25+
return new Response(true, response: $response->getResponse());
2626
}
2727

2828
return new Response(
2929
false,
30-
error: sprintf('%s:%s %s', $response->getCode(), $response->getMessage(), $response->getRequestID()));
30+
error: $response->getError());
3131
} catch (Throwable $e) {
3232
return new Response(
3333
false,

0 commit comments

Comments
 (0)