Skip to content

Commit

Permalink
Merge pull request #32 from TheNorthMemory/v1.1
Browse files Browse the repository at this point in the history
bump to v1.1.4
  • Loading branch information
xy-peng authored Aug 26, 2021
2 parents 23088ed + 76ad4c3 commit 3c0a8eb
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 46 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# 变更历史

## 1.1.4 - 2021-08-26

[变更细节](../../compare/v1.1.3...v1.1.4)

- 优化`平台证书下载工具`使用说明,增加`composer exec`执行方法说明;
- 优化了一点点代码结构,使逻辑更清晰了一些;

## 1.1.3 - 2021-08-22

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

## 项目状态

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

**版本说明:** `开发版`指: `类库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.1.3"
"wechatpay/wechatpay": "^1.1.4"
}
```

Expand Down
50 changes: 26 additions & 24 deletions bin/CertificateDownloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
$possibleFiles = [__DIR__.'/../vendor/autoload.php', __DIR__.'/../../../autoload.php', __DIR__.'/../../autoload.php'];
$file = null;
foreach ($possibleFiles as $possibleFile) {
if (file_exists($possibleFile)) {
if (\file_exists($possibleFile)) {
$file = $possibleFile;
break;
}
}
if (null === $file) {
throw new RuntimeException('Unable to locate autoload.php file.');
throw new \RuntimeException('Unable to locate autoload.php file.');
}

require_once $file;
Expand All @@ -30,6 +30,8 @@
*/
class CertificateDownloader
{
private const DEFAULT_BASE_URI = 'https://api.mch.weixin.qq.com/';

public function run(): void
{
$opts = $this->parseOpts();
Expand All @@ -39,7 +41,7 @@ public function run(): void
return;
}
if (isset($opts['version'])) {
echo ClientDecoratorInterface::VERSION, PHP_EOL;
static::prompt(ClientDecoratorInterface::VERSION);
return;
}
$this->job($opts);
Expand Down Expand Up @@ -85,7 +87,7 @@ private function job(array $opts): void
'serial' => $opts['serialno'],
'privateKey' => \file_get_contents((string)$opts['privatekey']),
'certs' => &$certs,
'base_uri' => (string)($opts['baseuri'] ?? 'https://api.mch.weixin.qq.com/'),
'base_uri' => (string)($opts['baseuri'] ?? self::DEFAULT_BASE_URI),
]);

/** @var \GuzzleHttp\HandlerStack $stack */
Expand All @@ -97,13 +99,13 @@ private function job(array $opts): void
$instance->chain('v3/certificates')->getAsync(
['debug' => true]
)->otherwise(static function($exception) {
echo $exception->getMessage(), PHP_EOL;
static::prompt($exception->getMessage());
if ($exception instanceof RequestException && $exception->hasResponse()) {
/** @var ResponseInterface $body */
$body = $exception->getResponse();
echo $body->getBody(), PHP_EOL, PHP_EOL, PHP_EOL;
/** @var ResponseInterface $response */
$response = $exception->getResponse();
static::prompt((string) $response->getBody(), '', '');
}
echo $exception->getTraceAsString(), PHP_EOL;
static::prompt($exception->getTraceAsString());
})->wait();
}

Expand All @@ -123,19 +125,19 @@ private static function certsRecorder(string $outputDir, array &$certs): callabl
$data = \is_object($json) && isset($json->data) && \is_array($json->data) ? $json->data : [];
\array_walk($data, static function($row, $index, $certs) use ($outputDir) {
$serialNo = $row->serial_no;
$outpath = $outputDir . DIRECTORY_SEPARATOR . 'wechatpay_' . $serialNo . '.pem';
$outpath = $outputDir . \DIRECTORY_SEPARATOR . 'wechatpay_' . $serialNo . '.pem';

static::prompt([
static::prompt(
'Certificate #' . $index . ' {',
' Serial Number: ' . static::highlight($serialNo),
' Not Before: ' . (new \DateTime($row->effective_time))->format(\DateTime::W3C),
' Not After: ' . (new \DateTime($row->expire_time))->format(\DateTime::W3C),
' Saved to: ' . static::highlight($outpath),
' You may confirm the above infos again even if this library already did(by Crypto\Rsa::verify):',
' ' . static::highlight(sprintf('openssl x509 -in %s -noout -serial -dates', $outpath)),
' ' . static::highlight(\sprintf('openssl x509 -in %s -noout -serial -dates', $outpath)),
' Content: ', '', $certs[$serialNo], '',
'}',
]);
'}'
);

\file_put_contents($outpath, $certs[$serialNo]);
}, $certs);
Expand All @@ -149,15 +151,15 @@ private static function certsRecorder(string $outputDir, array &$certs): callabl
*/
private static function highlight(string $thing): string
{
return sprintf("\x1B[1;32m%s\x1B[0m", $thing);
return \sprintf("\x1B[1;32m%s\x1B[0m", $thing);
}

/**
* @param string[] $messages
* @param string $messages
*/
private static function prompt(array $messages): void
private static function prompt(...$messages): void
{
array_walk($messages, static function (string $message): void { printf('%s%s', $message, PHP_EOL); });
\array_walk($messages, static function (string $message): void { \printf('%s%s', $message, \PHP_EOL); });
}

/**
Expand All @@ -178,7 +180,7 @@ private function parseOpts(): ?array
$shortopts = 'hV';
$longopts = [ 'help', 'version' ];
foreach ($opts as $opt) {
list($key, $alias) = $opt;
[$key, $alias] = $opt;
$shortopts .= $alias . ':';
$longopts[] = $key . ':';
}
Expand All @@ -190,7 +192,7 @@ private function parseOpts(): ?array

$args = [];
foreach ($opts as $opt) {
list($key, $alias, $mandatory) = $opt;
[$key, $alias, $mandatory] = $opt;
if (isset($parsed[$key]) || isset($parsed[$alias])) {
$possiable = $parsed[$key] ?? $parsed[$alias] ?? '';
$args[$key] = (string) (\is_array($possiable) ? $possiable[0] : $possiable);
Expand All @@ -210,7 +212,7 @@ private function parseOpts(): ?array

private function printHelp(): void
{
static::prompt([
static::prompt(
'Usage: 微信支付平台证书下载工具 [-hV]',
' -f=<privateKeyFilePath> -k=<apiv3Key> -m=<merchantId>',
' -s=<serialNo> -o=[outputFilePath] -u=[baseUri]',
Expand All @@ -222,10 +224,10 @@ private function printHelp(): void
' -k, --key=<apiv3Key> APIv3密钥',
' -o, --output=[outputFilePath]',
' 下载成功后保存证书的路径,可选,默认为临时文件目录夹',
' -u, --baseuri=[baseUri] 接入点,可选,默认为 https://api.mch.weixin.qq.com/',
' -u, --baseuri=[baseUri] 接入点,可选,默认为 ' . self::DEFAULT_BASE_URI,
' -V, --version Print version information and exit.',
' -h, --help Show this help message and exit.', '',
]);
' -h, --help Show this help message and exit.', ''
);
}
}

Expand Down
17 changes: 16 additions & 1 deletion bin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ Certificate Downloader 是 PHP版 微信支付 APIv3 平台证书的命令行下

Usage: 微信支付平台证书下载工具 [-hV]
-f=<privateKeyFilePath> -k=<apiV3key> -m=<merchantId>
-o=[outputFilePath] -s=<serialNo>
-s=<serialNo> -o=[outputFilePath] -u=[baseUri]
Options:
-m, --mchid=<merchantId> 商户号
-s, --serialno=<serialNo> 商户证书的序列号
-f, --privatekey=<privateKeyFilePath>
Expand All @@ -29,12 +30,24 @@ Usage: 微信支付平台证书下载工具 [-hV]
./bin/CertificateDownloader.php -k ${apiV3key} -m ${mchId} -f ${mchPrivateKeyFilePath} -s ${mchSerialNo} -o ${outputFilePath}
```


```shell
php -f ./bin/CertificateDownloader.php -k ${apiV3key} -m ${mchId} -f ${mchPrivateKeyFilePath} -s ${mchSerialNo} -o ${outputFilePath}
```

使用`composer`安装的软件包,可以通过如下命令下载:

```shell
vendor/bin/CertificateDownloader.php -k ${apiV3key} -m ${mchId} -f ${mchPrivateKeyFilePath} -s ${mchSerialNo} -o ${outputFilePath}
```


```shell
composer exec CertificateDownloader.php -- -k ${apiV3key} -m ${mchId} -f ${mchPrivateKeyFilePath} -s ${mchSerialNo} -o ${outputFilePath}
```

使用源码克隆版本,也可以使用`composer`通过以下命令下载:

```shell
Expand All @@ -47,6 +60,8 @@ composer v3-certificates -k ${apiV3key} -m ${mchId} -f ${mchPrivateKeyFilePath}
composer v3-certificates -k ${apiV3key} -m ${mchId} -f ${mchPrivateKeyFilePath} -s ${mchSerialNo} -o ${outputFilePath} -u https://apihk.mch.weixin.qq.com/
```

**注:** 示例命令行上的`${}`是变量表达方法,运行时请替换(包括`${}`)为对应的实际值。

## 常见问题

### 如何保证证书正确
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.1.3",
"version": "1.1.4",
"description": "[A]Sync Chainable WeChatPay v2&v3's OpenAPI SDK for PHP",
"type": "library",
"keywords": [
Expand Down
10 changes: 10 additions & 0 deletions src/ClientDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ final class ClientDecorator implements ClientDecoratorInterface
use ClientXmlTrait;
use ClientJsonTrait;

/**
* @var ClientInterface - The APIv2's `\GuzzleHttp\Client`
*/
protected $v2;

/**
* @var ClientInterface - The APIv3's `\GuzzleHttp\Client`
*/
protected $v3;

/**
* Deep merge the input with the defaults
*
Expand Down
6 changes: 3 additions & 3 deletions src/ClientDecoratorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ interface ClientDecoratorInterface
/**
* @var string - This library version
*/
const VERSION = '1.1.3';
public const VERSION = '1.1.4';

/**
* @var string - The HTTP transfer `xml` based protocol
* @deprecated 1.0 - @see \WeChatPay\Exception\WeChatPayException::DEP_XML_PROTOCOL_IS_REACHABLE_EOL
*/
const XML_BASED = 'v2';
public const XML_BASED = 'v2';

/**
* @var string - The HTTP transfer `json` based protocol
*/
const JSON_BASED = 'v3';
public const JSON_BASED = 'v3';

/**
* Protocol selector
Expand Down
5 changes: 0 additions & 5 deletions src/ClientJsonTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@
*/
trait ClientJsonTrait
{
/**
* @var Client - The APIv3's `GuzzleHttp\Client`
*/
protected $v3;

/**
* @var array<string, string|array<string, string>> - The defaults configuration whose pased in `GuzzleHttp\Client`.
*/
Expand Down
5 changes: 0 additions & 5 deletions src/ClientXmlTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@
*/
trait ClientXmlTrait
{
/**
* @var Client - The APIv2's `GuzzleHttp\Client`
*/
protected $v2;

/**
* @var array<string, string> - The default headers whose passed in `GuzzleHttp\Client`.
*/
Expand Down
10 changes: 5 additions & 5 deletions src/Crypto/AesInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,29 @@ interface AesInterface
/**
* Bytes Length of the AES block
*/
const BLOCK_SIZE = 16;
public const BLOCK_SIZE = 16;

/**
* Bytes length of the AES secret key.
*/
const KEY_LENGTH_BYTE = 32;
public const KEY_LENGTH_BYTE = 32;

/**
* Bytes Length of the authentication tag in AEAD cipher mode
* @deprecated 1.0 - As of the OpenSSL described, the `auth_tag` length may be one of 16, 15, 14, 13, 12, 8 or 4.
* Keep it only compatible for the samples on the official documentation.
*/
const AUTH_TAG_LENGTH_BYTE = 16;
public const AUTH_TAG_LENGTH_BYTE = 16;

/**
* The `aes-256-gcm` algorithm string
*/
const ALGO_AES_256_GCM = 'aes-256-gcm';
public const ALGO_AES_256_GCM = 'aes-256-gcm';

/**
* The `aes-256-ecb` algorithm string
*/
const ALGO_AES_256_ECB = 'aes-256-ecb';
public const ALGO_AES_256_ECB = 'aes-256-ecb';

/**
* Encrypts given data with given key and iv, returns a base64 encoded string.
Expand Down

0 comments on commit 3c0a8eb

Please sign in to comment.