Laravel 对接微信支付ApiV3接口踩坑
安装
composer require wechatpay/wechatpay
调用
use App\Events\ApiLogEvent;
use App\Jobs\UpdateLimit;
use App\Models\refundModel;
class wechatPay{
/**
* 微信支付V3
* @param $oid
* @param $price
* @param $des
* @param $openid
* @return array
*/
private function wechatPayV3($oid,$price,$des,$openid){
$config = config('wechat');
$merchantId = $config['mchid'];
$merchantPrivateKeyFilePath = $config['KEY_PATH'];
$merchantPrivateKeyInstance = Rsa::from('file://'.$merchantPrivateKeyFilePath, Rsa::KEY_TYPE_PRIVATE);
$merchantCertificateSerial = $config['serial'];
$platformCertificateFilePath = $config['CERT_PATH'];
$platformPublicKeyInstance = Rsa::from('file://'.$platformCertificateFilePath, Rsa::KEY_TYPE_PUBLIC);
$platformCertificateSerial = PemUtil::parseCertificateSerialNo('file://'.$platformCertificateFilePath);
$instance = Builder::factory([
'mchid' => $merchantId,
'serial' => $merchantCertificateSerial,
'privateKey' => $merchantPrivateKeyInstance,
'certs' => [
$platformCertificateSerial => $platformPublicKeyInstance,
],
]);
try {
$resp = $instance
->chain('v3/pay/transactions/jsapi')
->post(['json' => [
'mchid' => $config['mchid'],
'out_trade_no' => $oid,
'appid' => $config['appid'],
'description' => $des,
'notify_url' => $config['notify_url'],
'profit_sharing'=> true,
'amount' => [
'total' => $price,
'currency' => 'CNY'
],
'payer' => [
'openid' => $openid,
],
]]);
\Log::alert($resp->getStatusCode());
\Log::alert($resp->getBody());
return $resp->getBody();
} catch (\Exception $e) {
\Log::alert($e->getMessage());
return false;
}
}
}
坑1:cert.pem并非API安全申请API证书的apiclient_cert.pem,需手动生成,命令执行参考:
php vendor/wechatpay/wechatpay/bin/CertificateDownloader.php
坑2:执行会报错:cURL error 60: SSL certificate problem: unable to get local issuer certificate
下载cacert.pem放在D:\phpEnv\soft\php\php-7.3\extras\ssl内
修改php.ini
curl.cainfo=‘D:\phpEnv\soft\php\php-7.3\extras\ssl\cacert.pem’
openssl.cafile=‘D:\phpEnv\soft\php\php-7.3\extras\ssl\cacert.pem’