汇付宝支付商户入驻资料国密SM2签名和验签PHP扩展
$ composer require vochina/heepay-customer -vvv
在使用本扩展之前,请自己生成国密SM2私钥和公钥,可以使用支付宝开放平台开发助手一键生成,生成的格式Tests文件夹有示例
use Vochina\HeepayCustomer\HeepayCustomer;
$sm2PrivateKeyPath = __DIR__ . '/test_sm2_private_key.pem';
$sm2publicKeyPath = __DIR__ . '/test_sm2_public_key.pem';
$handler = new HeepayCustomer($sm2PrivateKeyPath, $sm2publicKeyPath);
$result = $handler->sign('This is the string that needs to be signed!');
示例:
签名结果: nUjieZmUOhuEc6+rjy5EqetXRSlM9Nk3nZgv1Dz6P6CLrEt72TsOSLrWT/HIvFwBmsxgc5tHydUyEXXqeSobjA==
$verify_result = $handler->verifySign($sign_content, $sign);
示例:
验证签名结果: bool(true)
$data = [
"app_id" => "100000000000000",
"method" => "customer.entry.apply",
"version" => "1.0",
"charset" => "utf-8",
"format" => "json",
"timestamp" => date('Y-m-d H:i:s'),
"notify_url" => "https://www.google.com",
"sign_type" => "SM2",
"biz_content" => [
"apply_no" => "1233231212212",
],
];
$sm2PrivateKeyPath = __DIR__ . '/test_sm2_private_key.pem';
$sm2publicKeyPath = __DIR__ . '/test_sm2_public_key.pem';
$handler = new HeepayCustomer($sm2PrivateKeyPath, $sm2publicKeyPath);
$sign_text = $handler->parameterText($data, '&', true, null);
var_dump($sign_text);
在 Laravel 中使用也是同样的安装方式,配置写在 config/services.php
中:
.
.
.
'heepay-customer' => [
'sm2PrivateKeyPath' => storage_path('cert/sm2_private_key.pem'),
'sm2publicKeyPath' => storage_path('cert/sm2_public_key.pem'),
],
然后把key文件复制到相应的文件夹下
可以用两种方式来获取 Vochina\HeepayCustomer\HeepayCustomer
实例:
.
.
.
public function edit(HeepayCustomer $HeepayCustomer)
{
$response = $HeepayCustomer->sign('This is the string that needs to be signed!');
}
.
.
.
.
.
.
public function edit()
{
$response = app('heepay-customer')->sign('This is the string that needs to be signed!');
}
.
.
.
./vendor/bin/phpunit --filter verifySign
MIT