$merchantId = $config['mch_id'];
// 从本地文件中加载「商户API私钥」,「商户API私钥」会用来生成请求的签名
$merchantPrivateKeyFilePath = $config['ssl_key'];
$merchantPrivateKeyInstance = Rsa::from($merchantPrivateKeyFilePath, Rsa::KEY_TYPE_PRIVATE);
// 「商户API证书」的「证书序列号」
$merchantCertificateSerial = $config['merchant_serial_number'];
// 从本地文件中加载「微信支付平台证书」,用来验证微信支付应答的签名
$platformCertificateFilePath = $config['ssl_cers'];
$platformPublicKeyInstance = Rsa::from($platformCertificateFilePath, Rsa::KEY_TYPE_PUBLIC);
// 从「微信支付平台证书」中获取「证书序列号」
$platformCertificateSerial = PemUtil::parseCertificateSerialNo($platformCertificateFilePath);
// 构造一个 APIv3 客户端实例
$instance = Builder::factory([
'mchid' => $merchantId,
'serial' => $merchantCertificateSerial,
'privateKey' => $merchantPrivateKeyInstance,
'certs' => [
$platformCertificateSerial => $platformPublicKeyInstance,
],
]);
/*$encryptor = static function(string $msg) use ($platformPublicKeyInstance): string {
return Rsa::encrypt($msg, $platformPublicKeyInstance);
};*/
try {
$resp = $instance
->chain('v3/transfer/batches/out-batch-no/T020220525104305ML0/details/out-detail-no/T020220525104305FRC')
->get(['json' => [
'out_batch_no' => 'T020220525104305ML0',
'out_detail_no' => 'T020220525104305FRC',
]
]);
// echo $resp->getStatusCode(), PHP_EOL;
echo $resp->getBody(), PHP_EOL;
} catch (\Exception $e) {
// 进行错误处理
// echo $e->getMessage(), PHP_EOL;
if ($e instanceof \GuzzleHttp\Exception\RequestException && $e->hasResponse()) {
$r = $e->getResponse();
// echo $r->getStatusCode() . ' ' . $r->getReasonPhrase(), PHP_EOL;
echo $r->getBody(), PHP_EOL, PHP_EOL, PHP_EOL;
}
// echo $e->getTraceAsString(), PHP_EOL;
}
运行返回结果:
{"code":"SIGN_ERROR","detail":{"detail":{"issue":"sign not match"},"field":"signature","location":"authorization","sign_information":{"method":"GET","sign_message_length":152,"truncated_sign_message":"GET\n/v3/transfer/batches/out-batch-no/-t020220525104305-m-l0/details/out-detail-no/-t020220525104305-f-r-c\n1653459825\nbqd0VEpAV4HVV3R8KSZcbpYY5mtY5DwE\n\n","url":"/v3/transfer/batches/out-batch-no/-t020220525104305-m-l0/details/out-detail-no/-t020220525104305-f-r-c"}},"message":"错误的签名,验签失败"}