forked from alipay/global-open-sdk-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseAlipayClient.php
159 lines (121 loc) · 5.24 KB
/
BaseAlipayClient.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
require_once 'SignatureTool.php';
require_once 'model/ResultStatusType.php';
abstract class BaseAlipayClient{
const DEFULT_KEY_VERSION = 1;
private $gatewayUrl;
private $merchantPrivateKey;
private $alipayPublicKey;
function __construct($gatewayUrl, $merchantPrivateKey, $alipayPublicKey) {
$this->gatewayUrl = $gatewayUrl;
$this->merchantPrivateKey = $merchantPrivateKey;
$this->alipayPublicKey = $alipayPublicKey;
}
public function execute($request){
$this->checkRequestParam($request);
$clientId = $request->getClientId();
$httpMethod = $request->getHttpMethod();
$path = $request->getPath();
$keyVersion = $request->getKeyVersion();
$reqTime = date(DATE_ISO8601);
$reqBody = json_encode($request);
$signValue = $this->genSignValue($httpMethod, $path, $clientId, $reqTime, $reqBody);
$baseHeaders = $this->buildBaseHeader($reqTime, $clientId, $keyVersion, $signValue);
$customHeaders = $this->buildCustomHeader();
if(isset($customHeaders) && count($customHeaders) > 0){
$headers = array_merge($baseHeaders, $customHeaders);
} else {
$headers = $baseHeaders;
}
$requestUrl = $this->genRequestUrl($path);
$rsp = $this->sendRequest($requestUrl, $httpMethod, $headers, $reqBody);
if(!isset($rsp) || $rsp == null){
throw new Exception("HttpRpcResult is null.");
}
$rspBody = $rsp->getRspBody();
$rspSignValue = $rsp->getRspSign();
$rspTime = $rsp->getRspTime();
$alipayRsp = json_decode($rspBody);
$result = $alipayRsp->result;
if(!isset($result)){
throw new Exception("Response data error,result field is null,rspBody:" . $rspBody);
}
if (!isset($rspSignValue) || trim($rspSignValue) === "" || !isset($rspTime) || trim($rspTime) === ""){
return $alipayRsp;
}
$isVerifyPass = $this->checkRspSign($httpMethod, $path, $clientId, $rspTime, $rspBody, $rspSignValue);
if(!$isVerifyPass){
throw new Exception("Response signature verify fail.");
}
return $alipayRsp;
}
private function checkRequestParam($request){
if(!isset($request)){
throw new Exception("alipayRequest can't null");
}
$clientId = $request->getClientId();
$httpMehod = $request->getHttpMethod();
$path = $request->getPath();
$keyVersion = $request->getKeyVersion();
if (!isset($this->gatewayUrl) || trim($this->gatewayUrl) === ""){
throw new Exception("clientId can't null");
}
if (!isset($clientId) || trim($clientId) === ""){
throw new Exception("clientId can't null");
}
if (!isset($httpMehod) || trim($httpMehod) === ""){
throw new Exception("httpMehod can't null");
}
if (!isset($path) || trim($path) === ""){
throw new Exception("path can't null");
}
if(strpos($path,'/') != 0){
throw new Exception("path must start with /");
}
if(isset($keyVersion) && !is_numeric($keyVersion)){
throw new Exception("keyVersion must be numeric");
}
}
private function genSignValue($httpMethod, $path, $clientId, $reqTime, $reqBody){
try {
$signValue = SignatureTool::sign($httpMethod, $path, $clientId, $reqTime, $reqBody, $this->merchantPrivateKey);
} catch(Exception $e){
throw new Exception($e);
}
return $signValue;
}
private function checkRspSign($httpMethod, $path, $clientId, $rspTime, $rspBody, $rspSignValue){
try {
$isVerify = SignatureTool::verify($httpMethod, $path, $clientId, $rspTime, $rspBody, $rspSignValue, $this->alipayPublicKey);
} catch(Exception $e){
throw new Exception($e);
}
return $isVerify;
}
private function buildBaseHeader($requestTime, $clientId, $keyVersion, $signValue){
$baseHeader = array();
$baseHeader[] = "Content-Type:application/json; charset=UTF-8";
$baseHeader[] = "User-Agent:global-alipay-sdk-php";
$baseHeader[] = "Request-Time:" . $requestTime;
$baseHeader[] = "client-id:" . $clientId;
if (!isset($keyVersion)){
$keyVersion = self::DEFULT_KEY_VERSION;
}
$signatureHeader = "algorithm=RSA256,keyVersion=". $keyVersion. ",signature=" . $signValue;
$baseHeader[] = "Signature:" . $signatureHeader;
return $baseHeader;
}
private function genRequestUrl($path){
if(strpos($this->gatewayUrl,"https://") != 0){
$this->gatewayUrl = "https://" . $this->gatewayUrl;
}
if(substr_compare($this->gatewayUrl, '/', -strlen('/')) === 0){
$len = strlen($this->gatewayUrl);
$this->gatewayUrl = substr($this->gatewayUrl ,0,$len - 1);
}
$requestUrl = $this->gatewayUrl . $path;
return $requestUrl;
}
abstract protected function buildCustomHeader();
abstract protected function sendRequest($requestUrl, $httpMethod, $headers, $reqBody);
}