微信pay.php,微信合单支付demo - PHP

/**

* 初始化参数

*/

public function __construct(){

parent::__construct();

// 微信支付 商户号

$this->mch_id = '';

// 二级商户号,需要走进件系统生成

$this->sub_mch_id = '';

// 微信支付 商户号绑定的appid

$this->app_id = '';

// 商户私钥

$this->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

*/

private 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;

}

}

}

return $return;

}

/**

* @param $url

* @param $body

* @param $method

* @return array

*/

protected function getCurlHeader($url, $body, $method){

$timestamp = time();

$nonce = $this->nonce_str();

$sign = $this->sign($url, $method, $timestamp, $nonce, $body, $this->getPrivateKey($this->private_key), $this->mch_id,

$this->serial_no);

return [

'Authorization: WECHATPAY2-SHA256-RSA2048 ' . $sign,

'Accept:application/json',

'User-Agent:' . $this->mch_id,

'Content-Type:application/json',

'Wechatpay-Serial:' . $this->getCert(),

];

}

/**

* @return string

*/

protected function nonce_str(){

static $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

$charactersLength = strlen($characters);

$randomString = '';

for ($i = 0; $i < 32; $i++) {

$randomString .= $characters[rand(0, $charactersLength - 1)];

}

return $randomString;

}

/**

* @param $key

* @return bool|resource

*/

protected function getPrivateKey($key){

return openssl_get_privatekey($key);

}

/**

* @param $key

* @return resource

*/

protected function getPublicKey($key){

return openssl_get_publickey($key);

}

/**

* @param $url

* @param $http_method

* @param $timestamp

* @param $nonce

* @param $body

* @param $mch_private_key

* @param $merchant_id

* @param $serial_no

* @return string

*/

protected function sign($url, $http_method, $timestamp, $nonce, $body, $mch_private_key, $merchant_id, $serial_no){

$url_parts = parse_url($url);

$canonical_url = ($url_parts['path'] . (!empty($url_parts['query']) ? "?${url_parts['query']}" : ""));

$message = $http_method . "\n" .

$canonical_url . "\n" .

$timestamp . "\n" .

$nonce . "\n" .

$body . "\n";

openssl_sign($message, $raw_sign, $mch_private_key, 'sha256WithRSAEncryption');

$sign = base64_encode($raw_sign);

$schema = 'WECHATPAY2-SHA256-RSA2048 ';

$token = sprintf('mchid="%s",nonce_str="%s",timestamp="%d",serial_no="%s",signature="%s"',

$merchant_id, $nonce, $timestamp, $serial_no, $sign);

return $token;

}

/**

* @param $message

* @param $signature

* @param $merchantPublicKey

* @return bool|int

*/

private function verify($message, $signature, $merchantPublicKey){

if (empty($merchantPublicKey)) {

return false;

}

if (!in_array('sha256WithRSAEncryption', \openssl_get_md_methods(true))) {

throw new \RuntimeException("当前PHP环境不支持SHA256withRSA");

}

$signature = base64_decode($signature);

return openssl_verify($message, $signature, $this->getPublicKey($merchantPublicKey), 'sha256WithRSAEncryption');

}

/**

* @param $url

* @param array $data

* @param $header

* @param string $method

* @param int $time_out

* @return mixed

*/

private function curl($url, $data = [], $header, $method = 'POST', $time_out = 3){

$curl = curl_init();

// 设置curl允许执行的最长秒数

curl_setopt($curl, CURLOPT_URL, $url);

curl_setopt($curl, CURLOPT_HTTPHEADER, $header);

curl_setopt($curl, CURLOPT_HEADER, false);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($curl, CURLOPT_TIMEOUT, $time_out);

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

if ($method == 'POST') {

curl_setopt($curl, CURLOPT_POST, true);

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

}

// 执行操作

$result = curl_exec($curl);

curl_close($curl);

return $result;

}

/**

* @param $associatedData

* @param $nonceStr

* @param $ciphertext

* @param $aesKey

* @return bool|string

*/

private function decryptToString($associatedData, $nonceStr, $ciphertext, $aesKey = ''){

if (empty($aesKey)) {

$aesKey = $this->mch_key;

}

$ciphertext = \base64_decode($ciphertext);

if (strlen($ciphertext) <= self::AUTH_TAG_LENGTH_BYTE) {

return false;

}

// ext-sodium (default installed on >= PHP 7.2)

if (function_exists('\sodium_crypto_aead_aes256gcm_is_available') &&

\sodium_crypto_aead_aes256gcm_is_available()) {

return \sodium_crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $aesKey);

}

// ext-libsodium (need install libsodium-php 1.x via pecl)

if (function_exists('\Sodium\crypto_aead_aes256gcm_is_available') &&

\Sodium\crypto_aead_aes256gcm_is_available()) {

return \Sodium\crypto_aead_aes256gcm_decrypt($ciphertext, $associatedData, $nonceStr, $aesKey);

}

// openssl (PHP >= 7.1 support AEAD)

if (PHP_VERSION_ID >= 70100 && in_array('aes-256-gcm', \openssl_get_cipher_methods())) {

$ctext = substr($ciphertext, 0, -self::AUTH_TAG_LENGTH_BYTE);

$authTag = substr($ciphertext, -self::AUTH_TAG_LENGTH_BYTE);

return \openssl_decrypt($ctext, 'aes-256-gcm', $aesKey, \OPENSSL_RAW_DATA, $nonceStr,

$authTag, $associatedData);

}

throw new \RuntimeException('AEAD_AES_256_GCM需要PHP 7.1以上或者安装libsodium-php');

}

/**

* @return array

*/

private function getHeaders(){

$headers = array();

foreach ($_SERVER as $key => $value) {

if ('HTTP_' == substr($key, 0, 5)) {

$headers[str_replace('_', '-', substr($key, 5))] = $value;

}

if (isset($_SERVER['PHP_AUTH_DIGEST'])) {

$header['AUTHORIZATION'] = $_SERVER['PHP_AUTH_DIGEST'];

} elseif (isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {

$header['AUTHORIZATION'] = base64_encode($_SERVER['PHP_AUTH_USER'] . ':' . $_SERVER['PHP_AUTH_PW']);

}

if (isset($_SERVER['CONTENT_LENGTH'])) {

$header['CONTENT-LENGTH'] = $_SERVER['CONTENT_LENGTH'];

}

if (isset($_SERVER['CONTENT_TYPE'])) {

$header['CONTENT-TYPE'] = $_SERVER['CONTENT_TYPE'];

}

}

return $headers;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值