1 Star 0 Fork 0

叶天 / luckin-guzzle-middleware

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

luckin-guzzle-middleware

概览

Guzzle HttpClient中间件Middleware,实现了请求签名的生成和应答签名的验证。

如果你是使用Guzzle的商户开发者,可以在构造GuzzleHttp\Client时将WechatPayGuzzleMiddleware传入,得到的GuzzleHttp\Client实例在执行请求时将自动携带身份认证信息,并检查应答的签名。

项目状态

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

环境要求

我们开发和测试使用的环境如下:

  • PHP 5.5+ / PHP 7.0+
  • guzzlehttp/guzzle 6.0+

安装

可以使用PHP包管理工具composer引入SDK到项目中:

Composer

方式一:在项目目录中,通过composer命令行添加:

composer require luckin/luckin-guzzle-middleware

方式二:在项目的composer.json中加入以下配置:

    "require": {
        "wechatpay/LuckIn-guzzle-middleware": "^0.2.0"
    }

添加配置后,执行安装

composer install

开始

首先,通过LuckInMiddlewareBuilder构建一个LuckInMiddleware,然后将其加入GuzzleHttp\ClientHandlerStack中。我们提供相应的方法,可以方便的传入商户私钥和平台证书等信息。

use GuzzleHttp\Exception\RequestException;
use LuckIn\GuzzleMiddleware\LuckInMiddleware;
use LuckIn\GuzzleMiddleware\Util\PemUtil;

// 商户相关配置
$merchantId = '1000100'; // 商户号
$merchantSerialNumber = 'XXXXXXXXXX'; // 商户API证书序列号
$merchantPrivateKey = PemUtil::loadPrivateKey('/path/to/mch/private/key.pem'); // 商户私钥
// 平台配置
$LuckInCertificate = PemUtil::loadCertificate('/path/to/LuckIn/cert.pem'); // 平台证书

// 构造一个LuckInMiddleware
$LuckInMiddleware = LuckInMiddleware::builder()
    ->withMerchant($merchantId, $merchantSerialNumber, $merchantPrivateKey) // 传入商户相关配置
    ->withLuckIn([ $LuckInCertificate ]) // 可传入多个平台证书,参数类型为array
    ->build();

// 将LuckInMiddleware添加到Guzzle的HandlerStack中
$stack = GuzzleHttp\HandlerStack::create();
$stack->push($LuckInMiddleware, 'LuckIn');

// 创建Guzzle HTTP Client时,将HandlerStack传入
$client = new GuzzleHttp\Client(['handler' => $stack]);


// 接下来,正常使用Guzzle发起API请求,LuckInMiddleware会自动地处理签名和验签
try {
    $resp = $client->request('GET', 'https://api.mch.weixin.qq.com/v3/...', [ // 注意替换为实际URL
        'headers' => [ 'Accept' => 'application/json' ]
    ]);

    echo $resp->getStatusCode().' '.$resp->getReasonPhrase()."\n";
    echo $resp->getBody()."\n";

    $resp = $client->request('POST', 'https://api.mch.weixin.qq.com/v3/...', [
        'json' => [ // JSON请求体
            'field1' => 'value1',
            'field2' => 'value2'
        ],
        'headers' => [ 'Accept' => 'application/json' ]
    ]);

    echo $resp->getStatusCode().' '.$resp->getReasonPhrase()."\n";
    echo $resp->getBody()."\n";
} catch (RequestException $e) {
    // 进行错误处理
    echo $e->getMessage()."\n";
    if ($e->hasResponse()) {
        echo $e->getResponse()->getStatusCode().' '.$e->getResponse()->getReasonPhrase()."\n";
        echo $e->getResponse()->getBody();
    }
    return;
}

上传媒体文件

// 参考上述指引说明,并引入 `MediaUtil` 正常初始化,无额外条件
use LuckIn\GuzzleMiddleware\Util\MediaUtil;
// 实例化一个媒体文件流,注意文件后缀名需符合接口要求
$media = new MediaUtil('/your/file/path/with.extension');

// 正常使用Guzzle发起API请求
try {
    $resp = $client->request('POST', 'https://api.mch.weixin.qq.com/v3/[merchant/media/video_upload|marketing/favor/media/image-upload]', [
        'body'    => $media->getStream(),
        'headers' => [
            'Accept'       => 'application/json',
            'content-type' => $media->getContentType(),
        ]
    ]);
    // POST 语法糖
    $resp = $client->post('merchant/media/upload', [
        'body'    => $media->getStream(),
        'headers' => [
            'Accept'       => 'application/json',
            'content-type' => $media->getContentType(),
        ]
    ]);
    echo $resp->getStatusCode().' '.$resp->getReasonPhrase()."\n";
    echo $resp->getBody()."\n";
} catch (Exception $e) {
    echo $e->getMessage()."\n";
    if ($e->hasResponse()) {
        echo $e->getResponse()->getStatusCode().' '.$e->getResponse()->getReasonPhrase()."\n";
        echo $e->getResponse()->getBody();
    }
    return;
}

敏感信息加/解密

// 参考上上述说明,引入 `SensitiveInfoCrypto`
use LuckIn\GuzzleMiddleware\Util\SensitiveInfoCrypto;
// 上行加密API 多于 下行解密,默认为加密,实例后直接当方法用即可
$encryptor = new SensitiveInfoCrypto(PemUtil::loadCertificate('/path/to/LuckIn/cert.pem'));

// 正常使用Guzzle发起API请求
try {
    // POST 语法糖
    $resp = $client->post('/v3/applyment4sub/applyment/', [
        'json' => [
            'business_code' => 'APL_98761234',
            'contact_info'  => [
                'contact_name'      => $encryptor('value of `contact_name`'),
                'contact_id_number' => $encryptor('value of `contact_id_number'),
                'mobile_phone'      => $encryptor('value of `mobile_phone`'),
                'contact_email'     => $encryptor('value of `contact_email`'),
            ],
            //...
        ],
        'headers' => [
            // 命令行获取证书序列号
            // openssl x509 -in /path/to/LuckIn/cert.pem -noout -serial | awk -F= '{print $2}'
            // 或者使用工具类获取证书序列号 `PemUtil::parseCertificateSerialNo($certificate)`
            'LuckIn-Serial' => 'must be the serial number via the downloaded pem file of `/v3/certificates`',
            'Accept'           => 'application/json',
        ],
    ]);
    echo $resp->getStatusCode().' '.$resp->getReasonPhrase()."\n";
    echo $resp->getBody()."\n";
} catch (Exception $e) {
    echo $e->getMessage()."\n";
    if ($e->hasResponse()) {
        echo $e->getResponse()->getStatusCode().' '.$e->getResponse()->getReasonPhrase()."\n";
        echo $e->getResponse()->getBody();
    }
    return;
}

// 单例加解密示例如下
$crypto = new SensitiveInfoCrypto($LuckInCertificate, $merchantPrivateKey);
$encrypted = $crypto('Alice');
$decrypted = $crypto->setStage('decrypt')($encrypted);

定制

当默认的本地签名和验签方式不适合你的系统时,你可以通过实现Signer或者Verifier来定制签名和验签。比如,你的系统把商户私钥集中存储,业务系统需通过远程调用进行签名,你可以这样做。

use LuckIn\GuzzleMiddleware\Auth\Signer;
use LuckIn\GuzzleMiddleware\Auth\SignatureResult;
use LuckIn\GuzzleMiddleware\Auth\LuckIn2Credentials;

class CustomSigner implements Signer
{
    public function sign($message)
    {
        // 调用签名RPC服务,然后返回包含签名和证书序列号的SignatureResult
        return new SignatureResult('xxxx', 'yyyyy');
    }
}

$credentials = new LuckIn2Credentials($merchantId, new CustomSigner);

$LuckInMiddleware = LuckInMiddleware::builder()
    ->withCredentials($credentials)
    ->withLuckIn([ $LuckInCertificate ])
    ->build();

常见问题

如何下载平台证书?

use LuckIn\GuzzleMiddleware\Validator;

class NoopValidator implements Validator
{
    public function validate(\Psr\Http\Message\ResponseInterface $response)
    {
        return true;
    }
}

$wechatpayMiddleware = WechatPayMiddleware::builder()
    ->withMerchant($merchantId, $merchantSerialNumber, $merchantPrivateKey)
    ->withValidator(new NoopValidator) // NOTE: 设置一个空的应答签名验证器,**不要**用在业务请求
    ->build();

注意:业务请求请使用标准的初始化流程,务必验证应答签名。

证书和回调解密需要的AesGcm解密在哪里?

请参考AesUtil.php

联系我们

MIT License Copyright (c) 2020 叶天 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

暂无描述 展开 收起
PHP
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
PHP
1
https://gitee.com/liyloong/luckin-guzzle-middleware.git
git@gitee.com:liyloong/luckin-guzzle-middleware.git
liyloong
luckin-guzzle-middleware
luckin-guzzle-middleware
master

搜索帮助