Skip to content

Commit 289ab67

Browse files
committedDec 29, 2015
添加请求签名
1 parent 96c1854 commit 289ab67

10 files changed

+132
-12
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* 修改:
33
添加 PUT 方法
44
添加 JSON 序列化方法
5+
添加请求签名
56

67
# 2.1.0
78
* 增加:

‎README.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Pingpp PHP SDK
22
=================
33
## 简介
4-
lib 文件夹下是 PHP SDK 文件,<br>
4+
lib 文件夹下是 PHP SDK 文件,
55
example 文件夹里面是简单的接入示例,该示例仅供参考。
66

77
## 版本要求
@@ -36,6 +36,12 @@ require_once('/path/to/pingpp-php/init.php');
3636
\Pingpp\Pingpp::setApiKey('YOUR-KEY');
3737
```
3838

39+
### 设置请求签名密钥
40+
密钥需要你自己生成,公钥请填写到 [Ping++ Dashboard](https://dashboard.pingxx.com)
41+
```php
42+
\Pingpp\Pingpp::setPrivateKeyPath('/path/to/your_rsa_private_key.pem');
43+
```
44+
3945
### 支付
4046
```php
4147
$ch = \Pingpp\Charge::create(
@@ -128,7 +134,6 @@ $signature = \Pingpp\WxpubOauth::getSignature($charge, $ticket, $url);
128134
pingpp.createPayment(charge, callback, signature, false);
129135
```
130136

131-
132137
### event 查询
133138

134139
```php
@@ -141,7 +146,6 @@ pingpp.createPayment(charge, callback, signature, false);
141146
```
142147
**详细信息请参考 [API 文档](https://pingxx.com/document/api?php)**
143148

144-
145149
### 微信企业付款
146150
```php
147151
\Pingpp\Transfer::create(

‎example/pay.php

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222
$amount = $input_data['amount'];
2323
$orderNo = substr(md5(time()), 0, 12);
2424

25+
// 设置请求签名密钥,密钥对需要你自己用 openssl 工具生成,把公钥填写到 https://dashboard.pingxx.com
26+
\Pingpp\Pingpp::setPrivateKeyPath(__DIR__ . '/your_rsa_private_key.pem');
27+
// 也可以设置私钥内容
28+
// \Pingpp\Pingpp::setPrivateKey(file_get_contents(__DIR__ . '/your_rsa_private_key.pem'));
29+
2530
/**
2631
* $extra 在使用某些渠道的时候,需要填入相应的参数,其它渠道则是 array()。
2732
* 以下 channel 仅为部分示例,未列出的 channel 请查看文档 https://pingxx.com/document/api#api-c-new
File renamed without changes.

‎example/webhooks.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function verify_signature($raw_data, $signature, $pub_key_path) {
2626
// $signature = 'BX5sToHUzPSJvAfXqhtJicsuPjt3yvq804PguzLnMruCSvZ4C7xYS4trdg1blJPh26eeK/P2QfCCHpWKedsRS3bPKkjAvugnMKs+3Zs1k+PshAiZsET4sWPGNnf1E89Kh7/2XMa1mgbXtHt7zPNC4kamTqUL/QmEVI8LJNq7C9P3LR03kK2szJDhPzkWPgRyY2YpD2eq1aCJm0bkX9mBWTZdSYFhKt3vuM1Qjp5PWXk0tN5h9dNFqpisihK7XboB81poER2SmnZ8PIslzWu2iULM7VWxmEDA70JKBJFweqLCFBHRszA8Nt3AXF0z5qe61oH1oSUmtPwNhdQQ2G5X3g==';
2727

2828
// 请从 https://dashboard.pingxx.com 获取「Ping++ 公钥」
29-
$pub_key_path = __DIR__ . "/rsa_public_key.pem";
29+
$pub_key_path = __DIR__ . "/pingpp_rsa_public_key.pem";
3030

3131
$result = verify_signature($raw_data, $signature, $pub_key_path);
3232
if ($result === 1) {

‎example/your_rsa_private_key.pem

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
-----BEGIN RSA PRIVATE KEY-----
2+
MIIEowIBAAKCAQEAx2MktxcKBEqdYRi2IgYcupPQIN5cxgiBL5udCCBJBNBbXPaq
3+
uOE1qspfhB1KUzHXATnCONiSzubLcBTnwi2tz0ErRCeJZSERRCpbKx4eu6b1neUT
4+
Wkga7xpZxWONEvkmZo5Nlhf4fXRPUYnO/bdGCNGpQ/HSJfWLtzmhCqO1aJwVhcDm
5+
DMYz4bTkZavhFB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21+
RIz377sC1KafBjHHO/1ooQKBgDQqfJrZv2ppquVTKH9pF/pwMq68daL7JkOXERqT
22+
iGYbwQqozJ+q2Y3Iu2gi6o/rVl0SggAWoM0TitKP0+dCQcYx7+imAK3GFv1KexyP
23+
Xs3WzO8Dc7ti42fr3qPjJG7g7PSfzwoME5iSNjX0MFZdlT1Q2dJwS4uXEsJO3yIj
24+
XS/9AoGBALRApgtUA7Odw4tjCLGvxXuLFnyRkg6hFqoXAP2j8H9bJDOlSSVwQTFd
25+
ahbcIDtQJS57vXUGK2uspbFKLm1WCFzPVyuxDIW6oue/kO+YxxU3NA58zk8oaORq
26+
eA3YvHc7ZmRjVnVkxnXjKofrL6jF5A+lXSXnXchrv2ZYI+1pOsIV
27+
-----END RSA PRIVATE KEY-----

‎example/your_rsa_public_key.pem

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-----BEGIN PUBLIC KEY-----
2+
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAx2MktxcKBEqdYRi2IgYc
3+
upPQIN5cxgiBL5udCCBJBNBbXPaquOE1qspfhB1KUzHXATnCONiSzubLcBTnwi2t
4+
z0ErRCeJZSERRCpbKx4eu6b1neUTWkga7xpZxWONEvkmZo5Nlhf4fXRPUYnO/bdG
5+
CNGpQ/HSJfWLtzmhCqO1aJwVhcDmDMYz4bTkZavhFBdVyXf/8n7UKylk03eymlKJ
6+
1swQpeFcxaKfzsk1mJU7mc93mCWjaR+VWkNbw4AQHDyHgbzH+zYARzCluiy5hXdi
7+
xGEP+iO4ZBk48rEs1hKTvGz1k+jhLCdkdpBRjq0pK/htjA3Ce8pF2AJs+fgN6ZUu
8+
mQIDAQAB
9+
-----END PUBLIC KEY-----

‎lib/ApiRequestor.php

+33-6
Original file line numberDiff line numberDiff line change
@@ -226,18 +226,27 @@ private function _curlRequest($method, $absUrl, $headers, $params)
226226
$curl = curl_init();
227227
$method = strtolower($method);
228228
$opts = array();
229+
$requestSignature = NULL;
229230
if ($method == 'get') {
230231
$opts[CURLOPT_HTTPGET] = 1;
231232
if (count($params) > 0) {
232233
$encoded = self::encode($params);
233234
$absUrl = "$absUrl?$encoded";
234235
}
235-
} elseif ($method == 'post') {
236-
$opts[CURLOPT_POST] = 1;
237-
$opts[CURLOPT_POSTFIELDS] = json_encode($params);
238-
} elseif ($method == 'put') {
239-
$opts[CURLOPT_CUSTOMREQUEST] = 'PUT';
240-
$opts[CURLOPT_POSTFIELDS] = json_encode($params);
236+
} elseif ($method == 'post' || $method == 'put') {
237+
if ($method == 'post') {
238+
$opts[CURLOPT_POST] = 1;
239+
} else {
240+
$opts[CURLOPT_CUSTOMREQUEST] = 'PUT';
241+
}
242+
$rawRequestBody = json_encode($params);
243+
$opts[CURLOPT_POSTFIELDS] = $rawRequestBody;
244+
if ($this->privateKey()) {
245+
$signResult = openssl_sign($rawRequestBody, $requestSignature, $this->privateKey(), 'sha256');
246+
if (!$signResult) {
247+
throw new Error\Api("Generate signature failed");
248+
}
249+
}
241250
} elseif ($method == 'delete') {
242251
$opts[CURLOPT_CUSTOMREQUEST] = 'DELETE';
243252
if (count($params) > 0) {
@@ -248,6 +257,10 @@ private function _curlRequest($method, $absUrl, $headers, $params)
248257
throw new Error\Api("Unrecognized method $method");
249258
}
250259

260+
if ($requestSignature) {
261+
$headers[] = 'Pingplusplus-Signature: ' . base64_encode($requestSignature);
262+
}
263+
251264
$absUrl = Util\Util::utf8($absUrl);
252265
$opts[CURLOPT_URL] = $absUrl;
253266
$opts[CURLOPT_RETURNTRANSFER] = true;
@@ -329,4 +342,18 @@ private function caBundle()
329342
{
330343
return dirname(__FILE__) . '/../data/ca-certificates.crt';
331344
}
345+
346+
private function privateKey()
347+
{
348+
if (!Pingpp::$privateKey) {
349+
if (!Pingpp::$privateKeyPath) {
350+
return NULL;
351+
}
352+
if (!file_exists(Pingpp::$privateKeyPath)) {
353+
throw new Error\Api('Private key file not found at: ' . Pingpp::$privateKeyPath);
354+
}
355+
Pingpp::$privateKey = file_get_contents(Pingpp::$privateKeyPath);
356+
}
357+
return Pingpp::$privateKey;
358+
}
332359
}

‎lib/Pingpp.php

+43
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ class Pingpp
2323

2424
const VERSION = '2.1.1';
2525

26+
/**
27+
* @var string The private key path to be used for signing requests.
28+
*/
29+
public static $privateKeyPath;
30+
31+
/**
32+
* @var string The PEM formatted private key to be used for signing requests.
33+
*/
34+
public static $privateKey;
35+
2636
/**
2737
* @return string The API key used for requests.
2838
*/
@@ -73,4 +83,37 @@ public static function setVerifySslCerts($verify)
7383
{
7484
self::$verifySslCerts = $verify;
7585
}
86+
87+
/**
88+
* @return string
89+
*/
90+
public static function getPrivateKeyPath()
91+
{
92+
return self::$privateKeyPath;
93+
}
94+
95+
/**
96+
* @param string $path
97+
*/
98+
public static function setPrivateKeyPath($path)
99+
{
100+
self::$privateKeyPath = $path;
101+
}
102+
103+
104+
/**
105+
* @return string
106+
*/
107+
public static function getPrivateKey()
108+
{
109+
return self::$privateKey;
110+
}
111+
112+
/**
113+
* @param string $key
114+
*/
115+
public static function setPrivateKey($key)
116+
{
117+
self::$privateKey = $key;
118+
}
76119
}

‎lib/PingppObject.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -214,10 +214,14 @@ public function jsonSerialize()
214214

215215
public function __toJSON()
216216
{
217-
if (defined('JSON_PRETTY_PRINT'))
217+
if (defined('JSON_PRETTY_PRINT')) {
218+
if (defined('JSON_UNESCAPED_UNICODE')) {
219+
return json_encode($this->__toStdObject(), JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE);
220+
}
218221
return json_encode($this->__toStdObject(), JSON_PRETTY_PRINT);
219-
else
222+
} else {
220223
return json_encode($this->__toStdObject());
224+
}
221225
}
222226

223227
public function __toString()

0 commit comments

Comments
 (0)
Please sign in to comment.