Skip to content

Commit f7e816b

Browse files
committed
update example add namespace
1 parent 27e614b commit f7e816b

14 files changed

+322
-318
lines changed

BaseAlipayClient.php

Lines changed: 0 additions & 180 deletions
This file was deleted.

client/BaseAlipayClient.php

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

DefaultAlipayClient.php renamed to client/DefaultAlipayClient.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
<?php
22

3-
require_once 'BaseAlipayClient.php';
4-
require_once 'model/HttpRpcResult.php';
3+
namespace Client;
54

6-
class DefaultAlipayClient extends BaseAlipayClient
5+
class DefaultAlipayClient extends \Client\BaseAlipayClient
76
{
87

98
function __construct(){
@@ -51,7 +50,7 @@ protected function sendRequest($requestUrl, $httpMethod, $headers, $reqBody)
5150
$headerContent = substr($rspContent, 0, $headerSize);
5251
$rspBody = substr($rspContent, $headerSize);
5352

54-
$httpRpcResult = new HttpRpcResult();
53+
$httpRpcResult = new \Model\HttpRpcResult();
5554
$httpRpcResult->setRspBody($rspBody);
5655

5756
$headArr = explode("\r\n", $headerContent);

0 commit comments

Comments
 (0)