use GuzzleHttp\Client;
use WechatPay\GuzzleMiddleware\WechatPayMiddleware;
use WechatPay\GuzzleMiddleware\Util\PemUtil;
protected $appid = ''; // 应⽤ID
protected $merchantId = ''; // 商户ID
protected $AppSecret = ''; // AppSecret
protected $v3_key = '';
protected $serial_no = ''; // api证书序列号
protected $merchantPrivateKey = './cert/apiclient_key.pem'; // 商户私钥
protected $wechatpayCertificate = './cert/cert.pem'; // 上⾯解密后得到的证书
protected $apppay = 'h.weixin.qq/v3/pay/transactions/app';
public function return_data($code,$msg,$data=array()){
return array('code' => $code,'msg'=>$msg,'data'=>$data);
}
/**
* @Notes: app⽀付
* @Interface pay
* @param $description 商品描述
* @param $out_trade_no 商户单号
* @param $notify_url 回调地址
* @param $total ⽀付⾦额分
* @author: Chengzhitao
* @Time: 2021/6/9 15:43
*/
public function pay($description,$out_trade_no,$notify_url,int $total){
// 构造⼀个WechatPayMiddleware
$wechatpayMiddleware = WechatPayMiddleware::builder()
$merchantPrivateKey = PemUtil::loadPrivateKey($this->merchantPrivateKey);
$wechatpayCertificate = PemUtil::loadCertificate($this->wechatpayCertificate);
->withMerchant($this->merchantId, $this->serial_no, $merchantPrivateKey) // 传⼊商户相关配置
->withWechatPay([ $wechatpayCertificate ]) // 可传⼊多个⽀付平台证书,参数类型为array
->build();
// 将WechatPayMiddleware添加到Guzzle的HandlerStack中
$stack = HandlerStack::create();
$stack->push($wechatpayMiddleware, 'wechatpay');
// 创建Guzzle HTTP Client时,将HandlerStack传⼊
$client = new Client(['handler' => $stack]);
// 接下来,正常使⽤Guzzle发起API请求,WechatPayMiddleware会⾃动地处理签名和验签
try {
php手机版下载$resp = $client->request('POST', $this->apppay, [
'json' => [ // JSON请求体
'appid' => $this->appid,
'mchid' => $this->merchantId,
'description' => $description,
'out_trade_no' => $out_trade_no,
'notify_url' => $notify_url,
'amount' => [
'total' => $total,
'currency' => 'CNY'
]
],
'headers' => [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'User-Agent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36' // 模拟⼿机 ]
]);
if ($resp->getStatusCode() == 200) {
$rep = json_decode($resp->getBody(),true);
$data = [
'appid' => $this->appid,
'partnerid' => $this->merchantId, // 商户号
'prepayid' => $rep['prepay_id'], // 预⽀付id
'package' => 'Sign=WXPay',
'noncestr' => $this->nonce_str(),
'timestamp' => time()
];
$data['sign'] = $this->sign($data);
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论