Skip to content

Commit

Permalink
Merge pull request #37 from TheNorthMemory/v1.2
Browse files Browse the repository at this point in the history
bump to v1.2.1
  • Loading branch information
xy-peng authored Sep 6, 2021
2 parents 25cab50 + 6ea2279 commit b7398ef
Show file tree
Hide file tree
Showing 15 changed files with 271 additions and 53 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# 变更历史

## 1.2.1 - 2021-09-06

[变更细节](../../compare/v1.2.0...v1.2.1)

- 增加`加密RSA私钥`的测试用例覆盖;
- 优化文档样例及升级指南,修正错别字;
- 优化内部`withDefaults`函数,使用变长参数合并初始化参数;
- 优化`Rsa::encrypt``Rsa::decrpt`方法,增加第三可选参数,以支持`OPENSSL_PKCS1_PADDING`填充模式的加解密;

## 1.2.0 - 2021-09-02

[变更细节](../../compare/v1.1.4...v1.2.0)
Expand Down
59 changes: 52 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ APIv3已内置 `请求签名` 和 `应答验签` 两个middleware中间件,创

## 项目状态

当前版本为`1.2.0`测试版本。
当前版本为`1.2.1`测试版本。
请商户的专业技术人员在使用时注意系统和软件的正确性和兼容性,以及带来的风险。

**版本说明:** `开发版`指: `类库API`随时会变;`测试版`指: 少量`类库API`可能会变;`稳定版`指: `类库API`稳定持续;版本遵循[语义化版本号](https://semver.org/lang/zh-CN/)规则。
Expand Down Expand Up @@ -60,7 +60,7 @@ composer require wechatpay/wechatpay

```json
"require": {
"wechatpay/wechatpay": "^1.2.0"
"wechatpay/wechatpay": "^1.2.1"
}
```

Expand Down Expand Up @@ -139,7 +139,7 @@ $instance = Builder::factory([
- `mchid` 为你的`商户号`,一般是10字节纯数字
- `serial` 为你的`商户证书序列号`,一般是40字节字符串
- `privateKey` 为你的`商户API私钥`,一般是通过官方证书生成工具生成的文件名是`apiclient_key.pem`文件,支持纯字符串或者文件`resource`格式
- `certs[$serial_number => #resource]` 为通过下载工具下载的平台证书`key/value`键值对,键为`平台证书序列号`,值为`平台证书`pem格式的纯字符串或者文件`resource`格式
- `certs[$serial_number => #resource]` 为通过下载工具下载的`平台证书序列号``平台公钥`键值对,键为`平台证书序列号`,值为`平台证书`内置的`平台公钥`,推荐由`Rsa::from`函数加载后的`对象``资源`对象
- `secret` 为APIv2版的`密钥`,商户平台上设置的32字节字符串
- `merchant[cert => $path]` 为你的`商户证书`,一般是文件名为`apiclient_cert.pem`文件路径,接受`[$path, $passphrase]` 格式,其中`$passphrase`为证书密码
- `merchant[key => $path]` 为你的`商户API私钥`,一般是通过官方证书生成工具生成的文件名是`apiclient_key.pem`文件路径,接受`[$path, $passphrase]` 格式,其中`$passphrase`为私钥密码
Expand Down Expand Up @@ -462,7 +462,7 @@ $res = $instance
'desc' => '理赔',
'spbill_create_ip' => '192.168.0.1',
],
'security' => true,
'security' => true, //请求需要双向证书
'debug' => true //开启调试模式
])
->then(static function($response) {
Expand All @@ -488,13 +488,15 @@ print_r($res);
[官方开发文档地址](https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay_yhk.php?chapter=24_7&index=4)

```php
use WeChatPay\Transformer;
$res = $instance
->v2->risk->getpublickey
->postAsync([
'xml' => [
'mch_id' => '1900000109',
'sign_type' => 'MD5',
],
'security' => true, //请求需要双向证书
// 特殊接入点,仅对本次请求有效
'base_uri' => 'https://fraud.mch.weixin.qq.com/',
])
Expand All @@ -514,14 +516,15 @@ print_r($res);
[官方开发文档地址](https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay_yhk.php?chapter=24_2)

```php
use WeChatPay\Transformer;
use WeChatPay\Crypto\Rsa;
// 做一个匿名方法,供后续方便使用,$rsaPubKeyString 是`risk/getpublickey` 的返回值'pub_key'字符串
$rsaPublicKeyInstance = Rsa::from($rsaPubKeyString, Rsa::KEY_TYPE_PUBLIC);
$encryptor = static function(string $msg) use ($rsaPublicKeyInstance): string {
return Rsa::encrypt($msg, $rsaPublicKeyInstance);
};
$res = $instance
->mmpaysptrans->pay_bank
->v2->mmpaysptrans->pay_bank
->postAsync([
'xml' => [
'mch_id' => '1900000109',
Expand All @@ -532,8 +535,49 @@ $res = $instance
'amount' => '100000',
'desc' => '理财',
],
'security' => true,
'security' => true, //请求需要双向证书
])
->then(static function($response) {
return Transformer::toArray((string)$response->getBody());
})
->otherwise(static function($e) {
if ($e instanceof \GuzzleHttp\Promise\RejectionException) {
return Transformer::toArray((string)$e->getReason()->getBody());
}
return [];
})
->wait();
print_r($res);
```

### 刷脸支付-人脸识别-获取调用凭证

[官方开发文档地址](https://pay.weixin.qq.com/wiki/doc/wxfacepay/develop/android/faceuser.html)

```php
use WeChatPay\Formatter;
use WeChatPay\Transformer;

$res = $instance
->v2->face->get_wxpayface_authinfo
->postAsync([
'xml' => [
'store_id' => '1234567',
'store_name' => '云店(广州白云机场店)',
'device_id' => 'abcdef',
'rawdata' => '从客户端`getWxpayfaceRawdata`方法取得的数据',
'appid' => 'wx8888888888888888',
'mch_id' => '1900000109',
'now' => (string)Formatter::timestamp(),
'version' => '1',
'sign_type' => 'HMAC-SHA256',
],
// 特殊接入点,仅对本次请求有效
'base_uri' => 'https://payapp.weixin.qq.com/',
])
->then(static function($response) {
return Transformer::toArray((string)$response->getBody());
})
->otherwise(static function($e) {
if ($e instanceof \GuzzleHttp\Promise\RejectionException) {
return Transformer::toArray((string)$e->getReason()->getBody());
Expand All @@ -549,6 +593,7 @@ print_r($res);
[官方开发文档地址](https://pay.weixin.qq.com/wiki/doc/api/tools/sp_coupon.php?chapter=23_1&index=2)

```php
use WeChatPay\Transformer;
$res = $instance
->v2->sandboxnew->pay->getsignkey
->postAsync([
Expand Down Expand Up @@ -860,7 +905,7 @@ $stack->before('http_errors', static function (callable $handler) use ($remoteVe
}, 'verifier');

// 链式/同步/异步请求APIv3即可,例如:
$instance->V3->Certificates->getAsync()->then(static function($res) { return $res->getBody(); })->wait();
$instance->v3->certificates->getAsync()->then(static function($res) { return $res->getBody(); })->wait();
```

## 常见问题
Expand Down
2 changes: 2 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ v1.2 对 `RSA公/私钥`加载做了加强,释放出 `Rsa::from` 统一加载
APIv3相关「RSA数据签名」,变化如下:

```diff
-use WeChatPay\Util\PemUtil;
-$merchantPrivateKeyFilePath = '/path/to/merchant/apiclient_key.pem';
-$merchantPrivateKeyInstance = PemUtil::loadPrivateKey($merchantPrivateKeyFilePath);
+$merchantPrivateKeyFilePath = 'file:///path/to/merchant/apiclient_key.pem';
Expand All @@ -61,6 +62,7 @@ APIv3相关「RSA数据签名」,变化如下:
APIv3回调通知「验签」,变化如下:

```diff
-use WeChatPay\Util\PemUtil;
// 根据通知的平台证书序列号,查询本地平台证书文件,
// 假定为 `/path/to/wechatpay/inWechatpaySerial.pem`
-$certInstance = PemUtil::loadCertificate('/path/to/wechatpay/inWechatpaySerial.pem');
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wechatpay/wechatpay",
"version": "1.2.0",
"version": "1.2.1",
"description": "[A]Sync Chainable WeChatPay v2&v3's OpenAPI SDK for PHP",
"type": "library",
"keywords": [
Expand Down
4 changes: 2 additions & 2 deletions src/ClientDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ final class ClientDecorator implements ClientDecoratorInterface
*
* @return array<string, string|mixed> - With the built-in configuration.
*/
protected static function withDefaults(array $config = []): array
protected static function withDefaults(array ...$config): array
{
return array_replace_recursive(static::$defaults, ['headers' => static::userAgent()], $config);
return array_replace_recursive(static::$defaults, ['headers' => static::userAgent()], ...$config);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/ClientDecoratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ interface ClientDecoratorInterface
/**
* @var string - This library version
*/
public const VERSION = '1.2.0';
public const VERSION = '1.2.1';

/**
* @var string - The HTTP transfer `xml` based protocol
Expand Down
3 changes: 2 additions & 1 deletion src/ClientJsonTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use function is_resource;
use function is_object;
use function is_array;
use function implode;
use function count;
use function sprintf;
use function array_key_exists;
Expand Down Expand Up @@ -51,7 +52,7 @@ trait ClientJsonTrait

abstract protected static function body(MessageInterface $message): string;

abstract protected static function withDefaults(array $config = []): array;
abstract protected static function withDefaults(array ...$config): array;

/**
* APIv3's signer middleware stack
Expand Down
6 changes: 2 additions & 4 deletions src/ClientXmlTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace WeChatPay;

use function strlen;
use function array_replace_recursive;
use function trigger_error;
use function sprintf;

Expand Down Expand Up @@ -33,7 +32,7 @@ trait ClientXmlTrait

abstract protected static function body(MessageInterface $message): string;

abstract protected static function withDefaults(array $config = []): array;
abstract protected static function withDefaults(array ...$config): array;

/**
* APIv2's transformRequest, did the `datasign` and `array2xml` together
Expand Down Expand Up @@ -124,10 +123,9 @@ public static function xmlBased(array $config = []): Client
$stack->before('prepare_body', static::transformRequest($config['mchid'] ?? null, $config['secret'] ?? '', $config['merchant'] ?? []), 'transform_request');
$stack->before('http_errors', static::transformResponse($config['secret'] ?? ''), 'transform_response');
$config['handler'] = $stack;
$config['headers'] = array_replace_recursive(static::$headers, $config['headers'] ?? []);

unset($config['mchid'], $config['serial'], $config['privateKey'], $config['certs'], $config['secret'], $config['merchant']);

return new Client(static::withDefaults($config));
return new Client(static::withDefaults(['headers' => static::$headers], $config));
}
}
Loading

0 comments on commit b7398ef

Please sign in to comment.