Files
liche/common/libraries/WechatPayV3.php
T
2021-09-02 20:29:17 +08:00

129 lines
5.6 KiB
PHP

<?php
/**
* Created by Vim
* User: lcc
* Date: 2021-08-11
* Time: 18:54
* Desc: 微信支付V3
*/
use GuzzleHttp\Exception\RequestException;
use WechatPay\GuzzleMiddleware\WechatPayMiddleware;
use WechatPay\GuzzleMiddleware\Util\PemUtil;
class WechatPayV3{
private $merchantId;
private $merchantSerialNumber;
private $merchantPrivateKey;
public function __construct($config){
$this->merchantId = $config['merchantId']; //商户号
$this->merchantSerialNumber = $config['merchantSerialNumber']; //商户API证书序列号
$this->merchantPrivateKey = $config['merchantPrivateKey']; //商户证书路径
$this->wechatpayCertificate = $config['wechatpayCertificate']; //微信支付平台证书
}
public function unifiedOrder($json,$appid,$noncestr){
$url = 'https://api.mch.weixin.qq.com/v3/pay/partner/transactions/jsapi';
// 商户相关配置
//$merchantId = '1612096731'; // 商户号
//$merchantSerialNumber = '761590F1FF6DFC2466894F96E2DE1169CE644A74'; // 商户API证书序列号
$merchantPrivateKey = PemUtil::loadPrivateKey($this->merchantPrivateKey); // 商户私钥
// 微信支付平台配置
$wechatpayCertificate = PemUtil::loadCertificate($this->wechatpayCertificate); // 微信支付平台证书
// 构造一个WechatPayMiddleware
$wechatpayMiddleware = WechatPayMiddleware::builder()
->withMerchant($this->merchantId, $this->merchantSerialNumber, $merchantPrivateKey) // 传入商户相关配置
->withWechatPay([ $wechatpayCertificate ]) // 可传入多个微信支付平台证书,参数类型为array
->build();
// 将WechatPayMiddleware添加到Guzzle的HandlerStack中
$stack = GuzzleHttp\HandlerStack::create();
$stack->push($wechatpayMiddleware, 'wechatpay');
// 创建Guzzle HTTP Client时,将HandlerStack传入
$client = new GuzzleHttp\Client(['handler' => $stack]);
// 接下来,正常使用Guzzle发起API请求,WechatPayMiddleware会自动地处理签名和验签
try {
$resp = $client->request('POST', $url, [
'json' => $json,
'headers' => [ 'Accept' => 'application/json' ]
]);
$body = json_decode($resp->getBody(),true);
if(!$body['prepay_id']){
return ['code'=>0,'msg'=>'下单失败'];
}
//微信支付(小程序)签名
$timeStamp = time();
$str = $this->getWechartSign($appid,$timeStamp,$noncestr,'prepay_id='.$body['prepay_id']);
$data = array('appid'=>$appid,'timeStamp'=>"$timeStamp",'package'=>'prepay_id='.$body['prepay_id'],'paySign'=>$str,'nonceStr'=>$noncestr,'signType'=>'RSA');
return ['code'=>1,'data'=>$data];
} catch (RequestException $e) {
// 进行错误处理
//echo $e->getMessage()."\n";
$error_msg = $e->getMessage();
if ($e->hasResponse()) {
//echo $e->getResponse()->getStatusCode().' '.$e->getResponse()->getReasonPhrase()."\n";
//echo $e->getResponse()->getBody();
}
return ['code'=>0,'msg'=>$error_msg];
}
}
//解冻剩余资金API
public function unfreeze($json,$noncestr){
$url = 'https://api.mch.weixin.qq.com/v3/profitsharing/orders/unfreeze';
// 商户相关配置
$merchantPrivateKey = PemUtil::loadPrivateKey($this->merchantPrivateKey); // 商户私钥
$wechatpayCertificate = PemUtil::loadCertificate($this->wechatpayCertificate); // 微信支付平台证书
$wechatpayMiddleware = WechatPayMiddleware::builder()
->withMerchant($this->merchantId, $this->merchantSerialNumber, $merchantPrivateKey) // 传入商户相关配置
->withWechatPay([ $wechatpayCertificate ]) // 可传入多个微信支付平台证书,参数类型为array
->build();
$stack = GuzzleHttp\HandlerStack::create();
$stack->push($wechatpayMiddleware, 'wechatpay');
// 创建Guzzle HTTP Client时,将HandlerStack传入
$client = new GuzzleHttp\Client(['handler' => $stack]);
// 接下来,正常使用Guzzle发起API请求,WechatPayMiddleware会自动地处理签名和验签
try {
$resp = $client->request('POST', $url, [
'json' => $json,
'headers' => [ 'Accept' => 'application/json' ]
]);
$body = json_decode($resp->getBody(),true);
return ['code'=>1,'data'=>$body];
} catch (RequestException $e) {
// 进行错误处理
//echo $e->getMessage()."\n";
$error_msg = $e->getMessage();
if ($e->hasResponse()) {
//echo $e->getResponse()->getStatusCode().' '.$e->getResponse()->getReasonPhrase()."\n";
//echo $e->getResponse()->getBody();
}
return ['code'=>0,'msg'=>$error_msg];
}
}
//调起支付的签名
private function getWechartSign($appid,$timeStamp,$noncestr,$prepay_id){
$str = $appid."\n".$timeStamp."\n".$noncestr."\n".$prepay_id."\n";
$key = file_get_contents($this->merchantPrivateKey);
$str = $this->getSha256WithRSA($str,$key);
return $str;
}
private function getSha256WithRSA($content, $privateKey){
$binary_signature = "";
$algo = "SHA256";
openssl_sign($content, $binary_signature, $privateKey, $algo);
$sign = base64_encode($binary_signature);
return $sign;
}
}