使用EasyWechat6 对接 微信V3支付 的过程

519 阅读1分钟

根据EasyWeChat6的文档中的微信支付,创建一个实例APP

注意这里的Application是EasyWeChat\Pay\Application,而不是其他的Application

<?php
use EasyWeChat\Pay\Application;

$config = [
    'mch_id' => 1360649000,
 
    // V3调用所有接口都需要API平台证书,配置下面2行 
    'private_key' => __DIR__ . '/certs/apiclient_key.pem',
    'certificate' => __DIR__ . '/certs/apiclient_cert.pem',

     // v3 API 秘钥
    'secret_key' => '43A03299A3C3FEB820Fxxxxx', 
    
    //商户端的证书需要下载工具转换,但这个证书很少用 
    'platform_certs' => [
    ],
    
    'http' => [
        'throw'  => true,
        'timeout' => 5.0,
    ],
];

$app = new Application($config);
$response=$app->getClient()->postJson("v3/pay/transactions/jsapi",$arr);
Log::error("预支付ID:".$response->toArray(false));
$res=$response->toArray(false);
if(!$res || !isset($res['prepay_id'])){
    err("调取支付失败");
} 
return $res['prepay_id'];

我使用的是thinkphp,记录日志的函数是Log::error('错误信息');

Log::error("预支付ID:".$response->toArray(false)); 这样会告诉你错误在哪里。 如果格式不对,就将$response->toArray(false)转下json

常见错误:

报错 openssl:就是证书错误,V3支付必须要有证书,就是微信商户平台下载的API证书。下载后有3个,需要放入两个

报错 400 Bad Request returned:就是参数错误,建议先直接写死商户号和金额1,成功后在用变量赋值给商户号和金额。打印$response->toArray(false)会告诉你具体错误

easywechat中介绍的用wechatpay-apiv3工具转换后的证书,是商户和服务商用的,普通支付不需要这个转换后的证书