Skip to content

Commit ccf0856

Browse files
committed
🆕Alipay Single Payment Notice
1 parent 5759811 commit ccf0856

File tree

6 files changed

+197
-47
lines changed

6 files changed

+197
-47
lines changed

example/checkout.php

+38-4
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,38 @@
77
'alipayPublicKey' => 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAihzSL26iayp+mj1ipXa7zdQoNDPhTBaxwJ08KZn3ja+G1eFJP445AmbZwGtASGJtbnctuav+ztElJvEU+NvNW3db+EvJXsb9QIj1Elgnt5WCvMDIhUQyDcp/b7WMRZlAyAWbO52sgA9ioAwaNS/jBPtb+8lx0s0bloAVleG7st8Wy7VTXrhOgpMZqsbQfE6dM4PiX7oeU+8NWGWR+pihLYTUsjaY2l+McusfQkBqKvp1bILljbVxBtT66dldCoEPxoCUN4kihwovXhkUzDbVhKFQ8fwrwOTWi2UgNnnMNrtH+cPcJCMz3WMcUaFy0cbQlyQmUbapI3moyPx20m+7jwIDAQAB',
88
'is_sandbox' => true,
99
));
10-
$type = $_GET['type'] ?? 'request';
11-
route($type === 'request', function () use (&$alipayGlobal) {
10+
$type = $_GET['type'] ?? '';
11+
route($type === 'cashier', function () use (&$alipayGlobal) {
1212
try {
1313
$currentUrl = getCurrentUrl();
14-
$result = $alipayGlobal->checkout(array(
14+
$result = $alipayGlobal->cashier(array(
15+
'notify_url' => setQueryParams($currentUrl, array('type' => 'notify')),
16+
'return_url' => setQueryParams($currentUrl, array('type' => 'return')),
17+
'amount' => array(
18+
'currency' => 'USD',
19+
'value' => '1',
20+
),
21+
'order' => array(
22+
'id' => null,
23+
'desc' => 'Order Desc',
24+
'extend_info' => array(
25+
'china_extra_trans_info' => array(
26+
'business_type' => 'MEMBERSHIP',
27+
),
28+
),
29+
),
30+
'payment_request_id' => null,
31+
));
32+
header('Location: ' . $result->normalUrl);
33+
} catch (Exception $e) {
34+
echo $e->getMessage();
35+
}
36+
});
37+
38+
route($type === 'agreement', function () use (&$alipayGlobal) {
39+
try {
40+
$currentUrl = getCurrentUrl();
41+
$result = $alipayGlobal->agreement(array(
1542
'notify_url' => setQueryParams($currentUrl, array('type' => 'notify')),
1643
'return_url' => setQueryParams($currentUrl, array('type' => 'return')),
1744
'amount' => array(
@@ -36,7 +63,14 @@
3663
});
3764

3865
route($type === 'notify', function () use (&$alipayGlobal) {
39-
$alipayGlobal->notify();
66+
try {
67+
$notify = $alipayGlobal->getNotify();
68+
// do something
69+
70+
$alipayGlobal->sendNotifyResponseWithRSA();
71+
} catch (Exception $e) {
72+
echo $e->getMesssage();
73+
}
4074
});
4175

4276
route($type === 'return', function () {

src/AliPayGlobal.php

+102-14
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
use Mantoufan\model\ProductCodeType;
1414
use Mantoufan\model\SettlementStrategy;
1515
use Mantoufan\model\TerminalType;
16+
use Mantoufan\request\notify\AlipayAcNotify;
1617
use Mantoufan\request\pay\AlipayPayRequest;
17-
use Mantoufan\response\NotifyResponse;
1818
use Mantoufan\SignatureTool;
19+
use \Exception;
1920

2021
class AliPayGlobal
2122
{
@@ -26,6 +27,7 @@ class AliPayGlobal
2627
private $client_id;
2728
private $is_sandbox;
2829
private $alipayPublicKey;
30+
private $merchantPrivateKey;
2931

3032
function __construct($params)
3133
{
@@ -37,11 +39,12 @@ function __construct($params)
3739
'is_sandbox' => false,
3840
), $params);
3941
$this->alipayPublicKey = $params['alipayPublicKey'];
42+
$this->merchantPrivateKey = $params['merchantPrivateKey'];
4043
$this->client_id = $params['client_id'];
4144
$this->is_sandbox = $params['is_sandbox'];
4245
$this->alipayClient = new DefaultAlipayClient(
4346
constant(Endpoint::class . '::' . $params['endpoint_area']),
44-
$params['merchantPrivateKey'],
47+
$this->merchantPrivateKey,
4548
$this->alipayPublicKey
4649
);
4750
}
@@ -63,7 +66,7 @@ function getPath($key)
6366
return str_replace('{sandbox}', $this->is_sandbox ? 'sandbox/' : '', self::PATHS[$key]);
6467
}
6568

66-
function checkout($params)
69+
function cashier($params)
6770
{
6871
$params = array_merge(array(
6972
'notify_url' => '',
@@ -132,20 +135,105 @@ function checkout($params)
132135
}
133136
}
134137

135-
function notify()
138+
function getNotify()
136139
{
137-
$notifyResponse = new NotifyResponse();
138-
$response = $notifyResponse->getResponse();
139-
var_dump($response);
140+
$alipayAcNotify = new AlipayAcNotify();
141+
$notifyPaymentRequest = $alipayAcNotify->getNotifyPaymentRequest();
140142
$result = SignatureTool::verify(
141-
$response->getHttpMethod(),
142-
$this->getPath('payments/pay'),
143-
$response->getClientId(),
144-
$response->getRspTime(),
145-
$response->getRspBody(),
146-
$response->getSignature(),
143+
$notifyPaymentRequest->getHttpMethod(),
144+
$_SERVER['PHP_SELF'],
145+
$notifyPaymentRequest->getClientId(),
146+
$notifyPaymentRequest->getRsqTime(),
147+
$notifyPaymentRequest->getRsqBody(),
148+
$notifyPaymentRequest->getSignature(),
147149
$this->alipayPublicKey
148150
);
149-
var_dump($result);
151+
if ($result === 0) {
152+
throw Exception('Invalid Signature');
153+
}
154+
return $notifyPaymentRequest;
155+
}
156+
157+
function sendNotifyResponse()
158+
{
159+
$alipayAcNotify = new AlipayAcNotify();
160+
$alipayAcNotify->sendNotifyResponse();
161+
}
162+
163+
function sendNotifyResponseWithRSA()
164+
{
165+
$alipayAcNotify = new AlipayAcNotify();
166+
$alipayAcNotify->sendNotifyResponseWithRSA(array(
167+
'merchantPrivateKey' => $this->merchantPrivateKey,
168+
));
169+
}
170+
171+
function agreement($params)
172+
{
173+
$params = array_merge(array(
174+
'notify_url' => '',
175+
'return_url' => '',
176+
'amount' => array(
177+
'currency' => 'USD',
178+
'value' => '',
179+
),
180+
'order' => array(
181+
'id' => null,
182+
'desc' => '',
183+
'extend_info' => array(
184+
'china_extra_trans_info' => array(
185+
'business_type' => '',
186+
),
187+
),
188+
),
189+
'payment_request_id' => null,
190+
), $params);
191+
192+
$alipayPayRequest = new AlipayPayRequest();
193+
$alipayPayRequest->setPath($this->getPath('payments/pay'));
194+
$alipayPayRequest->setClientId($this->client_id);
195+
196+
$alipayPayRequest->setProductCode(ProductCodeType::AGREEMENT_PAYMENT);
197+
$alipayPayRequest->setPaymentNotifyUrl($params['notify_url']);
198+
$alipayPayRequest->setPaymentRedirectUrl($params['return_url']);
199+
$alipayPayRequest->setPaymentRequestId($params['payment_request_id'] ?? self::CreatePaymentRequestId());
200+
201+
$paymentMethod = new PaymentMethod();
202+
$paymentMethod->setPaymentMethodType(CustomerBelongsTo::ALIPAY_CN);
203+
$alipayPayRequest->setPaymentMethod($paymentMethod);
204+
205+
$amount = new Amount();
206+
$amount->setCurrency($params['amount']['currency']);
207+
$amount->setValue($params['amount']['value']);
208+
209+
$order = new Order();
210+
$order->setOrderDescription($params['order']['desc']);
211+
$order->setReferenceOrderId($params['order']['id'] ?? self::CreateOredrId());
212+
$order->setOrderAmount($amount);
213+
214+
$chinaExtraTransInfo = new ChinaExtraTransInfo();
215+
$chinaExtraTransInfo->setBusinessType($params['order']['extend_info']['china_extra_trans_info']['business_type']);
216+
$extendInfo = $chinaExtraTransInfo;
217+
218+
$extendInfo = new ExtendInfo();
219+
$extendInfo->setChinaExtraTransInfo($chinaExtraTransInfo);
220+
$order->setExtendInfo($extendInfo . '');
221+
222+
$env = new Env();
223+
$env->setTerminalType(TerminalType::WEB);
224+
$order->setEnv($env);
225+
226+
$alipayPayRequest->setPaymentAmount($amount);
227+
$alipayPayRequest->setOrder($order);
228+
229+
$settlementStrategy = new SettlementStrategy();
230+
$settlementStrategy->setSettlementCurrency($params['amount']['currency']);
231+
$alipayPayRequest->setSettlementStrategy($settlementStrategy);
232+
233+
try {
234+
return $this->alipayClient->execute($alipayPayRequest);
235+
} catch (Exception $e) {
236+
throw $e;
237+
}
150238
}
151239
}

src/SignatureTool.php

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ private static function verifySignatureWithSHA256RSA($rspContent, $rspSignValue,
4545
} else {
4646
$originalRspSignValue = base64_decode(urldecode($rspSignValue));
4747
}
48+
4849
$verifyResult = openssl_verify($rspContent, $originalRspSignValue, $pubKey, OPENSSL_ALGO_SHA256);
4950
return $verifyResult;
5051
}

src/model/Response.php src/model/NotifyPaymentRequest.php

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?php
22
namespace Mantoufan\model;
33

4-
class Response
4+
class NotifyPaymentRequest
55
{
66
public $httpMethod;
77
public $clientId;
8-
public $rspTime;
9-
public $rspBody;
8+
public $rsqTime;
9+
public $rsqBody;
1010
public $signature;
1111

1212
public function getHttpMethod()
@@ -29,24 +29,24 @@ public function setClientId($clientId)
2929
$this->clientId = $clientId;
3030
}
3131

32-
public function getRspTime()
32+
public function getRsqTime()
3333
{
34-
return $this->rspTime;
34+
return $this->rsqTime;
3535
}
3636

37-
public function setRspTime($rspTime)
37+
public function setRsqTime($rsqTime)
3838
{
39-
$this->rspTime = $rspTime;
39+
$this->rsqTime = $rsqTime;
4040
}
4141

42-
public function getRspBody()
42+
public function getRsqBody()
4343
{
44-
return $this->rspBody;
44+
return $this->rsqBody;
4545
}
4646

47-
public function setRspBody($rspBody)
47+
public function setRsqBody($rsqBody)
4848
{
49-
$this->rspBody = $rspBody;
49+
$this->rsqBody = $rsqBody;
5050
}
5151

5252
public function getSignature()

src/request/notify/AlipayAcNotify.php

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
namespace Mantoufan\request\notify;
3+
4+
use Mantoufan\model\NotifyPaymentRequest;
5+
use Mantoufan\SignatureTool;
6+
7+
class AlipayAcNotify
8+
{
9+
public function getNotifyPaymentRequest()
10+
{
11+
$notifyPaymentRequest = new NotifyPaymentRequest();
12+
$notifyPaymentRequest->setHttpMethod($_SERVER['REQUEST_METHOD']);
13+
$notifyPaymentRequest->setClientId($_SERVER['HTTP_CLIENT_ID']);
14+
$notifyPaymentRequest->setRsqTime($_SERVER['HTTP_REQUEST_TIME']);
15+
$notifyPaymentRequest->setRsqBody(file_get_contents('php://input'));
16+
if (preg_match('/signature=(?<signature>.*?)(?:$|,)/', $_SERVER['HTTP_SIGNATURE'], $matches)) {
17+
$notifyPaymentRequest->setSignature($matches['signature']);
18+
}
19+
return $notifyPaymentRequest;
20+
}
21+
22+
public function sendNotifyResponse()
23+
{
24+
echo '{"result":{"resultCode":"SUCCESS","resultMessage":"success","resultStatus":"S"}}';
25+
}
26+
27+
public function sendNotifyResponseWithRSA($params = array(
28+
'merchantPrivateKey' => '',
29+
)) {
30+
$reqTime = date('c', time());
31+
$content = '{"result":{"resultCode":"SUCCESS","resultMessage":"success","resultStatus":"S"}}';
32+
header('Content-Type:application/json; charset=UTF-8');
33+
header('Response-Time:' . $reqTime);
34+
header('Client-Id:' . $_SERVER['HTTP_CLIENT_ID']);
35+
header('Signature:' . 'algorithm=RSA256,keyVersion=1,signature=' . SignatureTool::sign(
36+
$_SERVER['REQUEST_METHOD'],
37+
$_SERVER['PHP_SELF'],
38+
$_SERVER['HTTP_CLIENT_ID'],
39+
$reqTime,
40+
$content,
41+
$params['merchantPrivateKey']
42+
));
43+
echo $content;
44+
}
45+
}

src/response/NotifyResponse.php

-18
This file was deleted.

0 commit comments

Comments
 (0)