<?php
namespace app\api\controller;
use think\Controller;
class Wxscore extends Controller
{
public function __construct()
{
parent::__construct();
// 微信支付 商户号
$this->mch_id = '***';
// 二级商户号,需要走进件系统生成
$this->sub_mch_id = '';
// 微信支付 商户号绑定的appid
$this->app_id = '***';
// 商户私钥证书
$this->private_key = '-----BEGIN PRIVATE KEY-----
MIIEvgIBADANB*****************JdQiKaldzJbOB2XUzHcil6Jw
-----END PRIVATE KEY-----
';
// 商户证书序列号
// 如何查看证书序列号:https://wechatpay-api.gitbook.io/wechatpay-api-v3/chang-jian-wen-ti/zheng-shu-xiang-guan#ru-he-cha-kan-zheng-shu-xu-lie-hao
$this->serial_no = '**********';
// apiv3秘钥:https://wechatpay-api.gitbook.io/wechatpay-api-v3/ren-zheng/api-v3-mi-yao
$this->mch_key = '**********';
}
/**
* @param $serialNo
* @return mixed
*/
public function getCertBySerialNo($serialNo='')
{
$url = 'https://api.mch.weixin.qq.com/v3/certificates';
$timestamp = time();
$nonce = $this->nonce_str();
$body = '';
$sign = $this->sign($url, 'GET', $timestamp, $nonce, $body, $this->getPrivateKey($this->private_key), $this->mch_id,$this->serial_no);
$header = [
'Authorization: WECHATPAY2-SHA256-RSA2048 ' . $sign,
'Accept:application/json',
'User-Agent:' . $this->mch_id,
];
$result = $this->curl($url, '', $header, 'GET');
$cert = json_decode($result, true);
$return = [];
if (!empty($cert['data'])) {
foreach ($cert['data'] as $item) {
if ($serialNo == $item['serial_no']) {
$return = $item;
break;
}
}
}
// var_dump($return);
return $return;
}
/**
* @param $key
* @return bool|resource
*/
protected function getPrivateKey($key)
{
return openssl_get_privatekey($key);
}
/**
* @return array
*/
private function getHeaders()
{
$headers = array();
foreach ($_SERVER as $key => $value) {