微信v3支付签名php代码
时间: 2023-07-30 18:10:36 浏览: 208
PHP微信支付v3 完整实例
4星 · 用户满意度95%
以下是微信v3支付签名的PHP代码示例:
```php
<?php
function generateSign($method, $url, $timestamp, $nonceStr, $body) {
$merchantId = "YOUR_MERCHANT_ID"; // 商户号
$serialNo = "YOUR_SERIAL_NO"; // 商户API证书序列号
$privateKey = file_get_contents("path/to/your/private/key.pem"); // 商户API私钥
$message = $method . "\n" .
$url . "\n" .
$timestamp . "\n" .
$nonceStr . "\n" .
$body . "\n";
$signature = "";
openssl_sign($message, $signature, $privateKey, "sha256WithRSAEncryption");
$signature = base64_encode($signature);
return "WECHATPAY2-SHA256-RSA2048 " .
"mchid=\"" . $merchantId . "\", " .
"serial_no=\"" . $serialNo . "\", " .
"timestamp=\"" . $timestamp . "\", " .
"nonce_str=\"" . $nonceStr . "\", " .
"signature=\"" . $signature . "\"";
}
// 使用示例
$method = "POST";
$url = "https://api.mch.weixin.qq.com/v3/pay/transactions/native";
$timestamp = time();
$nonceStr = "YOUR_NONCE_STR";
$body = '{"mchid":"YOUR_MERCHANT_ID","out_trade_no":"YOUR_OUT_TRADE_NO","appid":"YOUR_APPID","description":"YOUR_DESCRIPTION","notify_url":"YOUR_NOTIFY_URL","amount":{"total":100,"currency":"CNY"}}';
$sign = generateSign($method, $url, $timestamp, $nonceStr, $body);
echo $sign;
?>
```
在代码中,需要填写自己的商户号、证书序列号、私钥以及请求的URL、请求体等信息。使用时,只需要调用 `generateSign` 函数即可生成签名字符串。
阅读全文