1.准备几个东西
appid: wx*******************
v3密钥:GM****************************
商户号:16******************
证书序列号:12***(验证商户身份的证书)
平台证书详见2
配置并获取商户appId、证书、v3密钥、证书序列号等官方有接入指引,按照要求申请即可顺利获取:https://pay.weixin.qq.com/index.php/core/cert/api_cert#/api-cert-manage
准备平台证书
composer安装wechatpay官方推荐sdk官方仓库:
https://github.com/wechatpay-apiv3/wechatpay-php/tree/main
1.并运行安装命令:cd path_to_project/项目根目录
composer require wechatpay/wechatpay
# 查看使用帮助
php vendor/bin/CertificateDownloader.php -V`
我用的thinkphp8,平台证书制作在根目录下;
php vendor/bin/CertificateDownloader.php -m 1678****** -f "cert/apiclient_key.pem" -k 9fcacaca***************** -s 454E14B4*********************97609 -o "cert"
3.状态运行
4.如果提示报错:如下操作,再执行上述命令;
Trying [240e:e1:aa00:4000::94]:443...
Connected to api.mch.weixin.qq.com (240e:e1:aa00:4000::94) port 443
ALPN: curl offers http/1.1
SSL certificate problem: unable to get local issuer certificate
Closing connection
cURL error 60: SSL certificate problem: unable to get local issuer certificate (seehttps://curl.haxx.se/libcurl/c/libcurl-errors.html) forhttps://api.mch.weixin.qq.com/v3/certificates
需要进入到vendor/guzzlehttp/guzzle/src/Client.php,修改配置:
private function configureDefaults(array $config): void {
$defaults = [
'allow_redirects' => RedirectMiddleware::$defaultSettings,
'http_errors' => true,
'decode_content' => true,
// verify = ture 修改成 false,等证书下载完成再改回来
'verify' => false,
'cookies' => false,
'idn_conversion' => false,
];
...
}
5.准备到位上代码
wxpay.php
<?php
// +----------------------------------------------------------------------
// | Author:laojiadehuiyi
// +----------------------------------------------------------------------
// | SystemName:项目管理系统
// +----------------------------------------------------------------------
// | Email:740453714@qq.com
// +----------------------------------------------------------------------
namespace app\common\service\wxpay;
use think\facade\Config;
use WeChatPay\Builder;
use WeChatPay\Crypto\Rsa;
use WeChatPay\Util\PemUtil;
class WxPay
{
private $appid; // 应用ID
private $weChatPay; // 微信支付对象
private $merchantId; // 商户号
private $merchantCertificateSerial; // 验证商户身份证的值证书序号
private $merchantPrivateKeyFilePath; // 商户私钥文件路径
private $platformCertPath; // 平台证书路径
private $apiV3Key; // API v3 密钥
public function __construct()
{
$settings = Config::get('wxconfig.wxpay'); // 获取微信支付配置
$this->appid = $settings['appid'];
$this->merchantId = $settings['mch_id'];
$this->merchantCertificateSerial = $settings['serial_no'];
$this->merchantPrivateKeyFilePath = $settings['private_key_path'];
$this->platformCertPath = $settings['cert_path'];
$this->apiV3Key = $settings['api_v3_key'];
}
public function Pay($order) {
// 商户号
$merchantId = $this->merchantId;
// 从本地文件中加载「商户API私钥」,「商户API私钥」会用来生成请求的签名 // 商户私钥,用来发送数据签名
$merchantPrivateKeyFilePath = $this->merchantPrivateKeyFilePath;
$merchantPrivateKeyInstance = Rsa::from($merchantPrivateKeyFilePath, Rsa::KEY_TYPE_PRIVATE);
// 「商户API证书」的「证书序列号」
$merchantCertificateSerial = $this->merchantCertificateSerial;
// 从本地文件中加载「微信支付平台证书」,用来验证微信支付应答的签名 // 微信公钥,用来验证返回数据的签名
$platformCertificateFilePath = $this->platformCertPath;
$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,
],
]);
try {
$resp = $instance
->chain('v3/pay/transactions/native') // 这里使用的native支付,其它接口参考官方文档,如jsapi
->post(['json' => [
'mchid' => $this->merchantId,
'out_trade_no' => $order['out_trade_no'],
'appid' => $order['appid'],
'description' => $order['description'],
'notify_url' => 'https://weixin.qq.com/',
'amount' => [
'total' => $order['amount_total'],
'currency' => 'CNY',
],
]]);
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;
}
}
}
config.php
'wxpay' => [
'appid' => 'wx71611a17676b9669', //公众号appid
'mch_id' => '1678525470', //商户号
'api_v3_key' => '9fcacaca8c5c511b6c544540b07860b5', //v3密钥key
// 'api_v3_key' => '9fcacaca8c5c511b6c544540b07860b4',还有1个什么东西是这个串,现在不知道了
// 'serial_no' => '11D051C074ED98E2587919B2907ACC441685B927',//验证商户身份证的值证书序号
'serial_no' => '454E14B4190FAD6934665C8A1D2B508057E97609',//api证书序列号(验证商户身份)
'cert_path' => 'file://D:/phpstudy_pro/WWW/yylAdmin/runtime/cert/wechatpaypingtaicert.pem', //apiclient_cert.pem证书路径'
'private_key_path' => 'file://D:/phpstudy_pro/WWW/yylAdmin/runtime/cert/apiclient_key.pem'
]
6.结果展示
本文主要要点转载自
简书:Thinkphp8 集成微信支付sdk
@感谢作者:非新生代菜鸟
如有侵权,实属抱歉,请发个消息马上处理。