<?php
/**
*
*/
namespace app\common;
header("Content-type:text/html;charset=utf-8");
class PaySmallTiXian{
public $mchid = '1547487171'; //商户号
public $appid = 'wxda6977c32bebee5e'; //小程序appid
public $serial_no = '14079137E24F0653B84430C673BF908AFF8D1DD6'; // 证书序列号
public function tixian($amount,$openid){
$batch_name = '余额提现';//转账的名称
$money = (int)($amount * 100); // 提现金额
$openid = $openid; //用户openid
$trade_no = 'yetx'.date('Ymd').mt_rand(1000, 9999); //提现编号
$post_data = [
"appid" => $this->appid,//appid
"out_batch_no" => $trade_no,//商家批次单号
"batch_name" => $batch_name,//批次名称
"batch_remark" => $batch_name,//批次备注
"total_amount" => $money,// 转账金额单位为“分”
"total_num" => 1, // 转账总笔数
//此处可以多笔提现 组合二维数组放到transfer_detail_list即可
"transfer_detail_list" => [
[
'out_detail_no' => $trade_no,
'transfer_amount' => $money,
'transfer_remark' => $batch_name,
'openid' => $openid,
]
]
];
$url = 'https://api.mch.weixin.qq.com/v3/transfer/batches';
//JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE 防止中文被转义
$result = $this->wx_post($url, json_encode($post_data, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE));
$result = json_decode($result, true);
if (isset($result['prepay_id'])) {
return $result['prepay_id'];
}
return $result;
}
private function wx_post($url, $param)
{
$authorization = $this->getV3Sign($url, "POST", $param);
$curl = curl_init();
$headers = [
'Authorization:' . $authorization,
'Accept:application/json',
'Content-Type:application/json;charset=utf-8',
'User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
];
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POSTFIELDS, $param);
curl_setopt($curl, CURLOPT_POST, true);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
private function getV3Sign($url, $http_method, $body)
{
$nonce = strtoupper($this->createNonceStr(32));
$timestamp = time();
$url_parts = parse_url($url);
$canonical_url = ($url_parts['path'] . (!empty($url_parts['query']) ? "?${url_parts['query']}" : ""));
$cert_dir = ROOT_PATH.DS."config".DS."payment_cert".DS."wechatpay".DS;
$sslKeyPath = $cert_dir."apiclient_key.pem";
//拼接参数
$message = $http_method . "\n" .
$canonical_url . "\n" .
$timestamp . "\n" .
$nonce . "\n" .
$body . "\n";
$private_key = $this->getPrivateKey($sslKeyPath);
openssl_sign($message, $raw_sign, $private_key, 'sha256WithRSAEncryption');
$sign = base64_encode($raw_sign);
$token = sprintf('WECHATPAY2-SHA256-RSA2048 mchid="%s",nonce_str="%s",timestamp="%s",serial_no="%s",signature="%s"', $this->mchid, $nonce, $timestamp, $this->serial_no, $sign);
return $token;
}
/**
* 生成随机32位字符串
* @param $length
* @return string
*/
public function createNonceStr($length = 16) { //生成随机16个字符的字符串
$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
/**
* 获取私钥
* @param $filepath
* @return false|resource
*/
private function getPrivateKey($filepath = '')
{
if (empty($filepath)) {
//私钥位置
$cert_dir = ROOT_PATH.DS."config".DS."payment_cert".DS."wechatpay".DS;
$filepath = $cert_dir."apiclient_key.pem";
}
return openssl_get_privatekey(file_get_contents($filepath));
}
}
评论0