当前位置:网站首页>PHP wechat merchant transfer to change initiating merchant transfer API
PHP wechat merchant transfer to change initiating merchant transfer API
2022-06-30 00:42:00 【Muzi plum 826】
<?php
namespace app\api\controller;
use app\common\controller\Api;
use think\Response;
use think\Config;
/**
* Example interface
*/
class Demo extends Api
{
// If $noNeedLogin If it is empty, it means that all interfaces need to log in to request
// If $noNeedRight If it is empty, it means that all interfaces need to verify permissions to request
// If the interface has been set, no login is required , Then there is no need for authentication
//
// Interface without login ,* All
protected $noNeedLogin = ['*'];
// Authentication free interface ,* All
protected $noNeedRight = ['*'];
/**
* The test method
*
*/
public function test()
{
$batch_name = ' Balance withdrawal ';// Name of transfer
$out_trade_no = time().rand(1000,9999);// Odd Numbers
$money = 1;// amount of money
$openid = 'oTQgi5xbXc1diAZ7U_xxxxxxxxxx';// user openid
$this->tixian($batch_name,$out_trade_no,1,$openid);
}
public function tixian($batch_name,$out_trade_no,$money,$openid){
$url = 'https://api.mch.weixin.qq.com/v3/transfer/batches';
$pars = [];
$pars['appid'] = 'wxe801xxxxxxxxxxxx';// Directly connected merchants appid
$pars['out_batch_no'] = 'sjzz'.date('Ymd').mt_rand(1000, 9999);// Merchant batch number in merchant system , It is required that this parameter can only be represented by numbers 、 Upper and lower case letters make up , Unique within the merchant system
$pars['batch_name'] = $batch_name;// Name of the batch transfer
$pars['batch_remark'] = $batch_name;// Transfer instructions ,UTF8 code , Most allow 32 Characters
$pars['total_amount'] = intval($money * 100);// Total transfer amount Unit is “ branch ”
$pars['total_num'] = 1;// Total number of transfers
$pars['transfer_detail_list'][0] = [
'out_detail_no'=>'Dh'.$out_trade_no,
'transfer_amount'=>$pars['total_amount'],
'transfer_remark'=>$batch_name,
'openid'=>$openid
];// Transfer details list
$token = $this->getToken($pars);// obtain token
$res = $this->https_request($url,json_encode($pars),$token);// Send a request
$resArr = json_decode($res,true);
halt($resArr);
// Successfully returns
// array(3) {
// ["batch_id"] => string(40) "1030001016101247194272022062900873000000"
// ["create_time"] => string(25) "2022-06-29T10:21:30+08:00"
// ["out_batch_no"] => string(16) "sjzz202206291647001"
// }
}
function https_request($url,$data = null,$token){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, (string)$url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// Add request header
$headers = [
'Authorization:WECHATPAY2-SHA256-RSA2048 '.$token,
'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',
];
if(!empty($headers)){
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
}
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
public function getToken($pars)
{
// $url = 'https://api.mch.weixin.qq.com/v3/certificates';
$url = 'https://api.mch.weixin.qq.com/v3/transfer/batches';
$http_method = 'POST';// Request method (GET,POST,PUT)
$timestamp = time();// Request timestamp
$url_parts = parse_url($url);// Gets the absolute value of the request URL
$nonce = $timestamp.rand('10000','99999');// Request random string
$body = json_encode((object)$pars);// Request message body
$stream_opts = [
"ssl" => [
"verify_peer"=>false,
"verify_peer_name"=>false,
]
];
$apiclient_cert_path = 'https://'.$_SERVER['HTTP_HOST'].'/uploads/apiclient/apiclient_cert.pem';
$apiclient_key_path = 'https://'.$_SERVER['HTTP_HOST'].'/uploads/apiclient/apiclient_key.pem';
$apiclient_cert_arr = openssl_x509_parse(file_get_contents($apiclient_cert_path,false, stream_context_create($stream_opts)));
$serial_no = $apiclient_cert_arr['serialNumberHex'];// Certificate serial number
$mch_private_key = file_get_contents($apiclient_key_path,false, stream_context_create($stream_opts));// secret key
$merchant_id = '160xxxxxxx';// Merchant id
$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);// Signature
$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);// Wechat returns token
return $token;
}
}
边栏推荐
- Nested call and chained access of functions in handwritten C language
- 太卷了~ 八股文,面试最强王者!
- Relevance - canonical correlation analysis
- C语言课设心得之“推箱子”课设作品开源分享
- 关联性——典型相关分析
- 面试官:大量请求 Redis 不存在的数据,从而影响数据库,该如何解决?
- 炒股开户选择哪家证券公司比较好哪家平台更安全
- MySQL foundation 2
- How much is the fixed asset management system and the price of the fixed asset management system
- MySQL primary key constraint deletion
猜你喜欢

Outsourcing for 3 years is a waste

A Yu's Rainbow Bridge

Sword finger offer II 035 Minimum time difference

学位论文的引用

简要的说一下:Fragment 间的通信方式?

How to customize templates and quickly generate complete code in idea?

博途V16 更改PLC的型号和固件版本

面试官:大量请求 Redis 不存在的数据,从而影响数据库,该如何解决?

Developers, why does the maturity of container technology herald the arrival of cloud native era?

Common interview questions for network workers: Telnet, TTL, router and switch
随机推荐
【PHP】php压测,报错:通常每个套接字地址(协议/网络地址/端口)只允许使用
Modbus TCP RTU protocol chart
@Bugs caused by improper use of configurationproperties
[MySQL basic] general syntax 2
视频转图像-cv2.VideoCapture()用法
IDEA工具快捷键的使用
TwinCAT 3 EL7211模塊控制倍福伺服
Video to image -cv2 Videocapture() usage
月薪没到30K的程序员必须要背的面试八股,我先啃为敬!
开发者,为什么说容器技术的成熟预示着云原生时代的到来?
炒股开户选择哪家券商公司比较好哪家平台更安全
01 backpack problem
干外包3年,真废了...
Yunna | fixed assets system management, NC system management where are the fixed assets
Byte, word, doubleword relationship
浮点数通信
Time does not spare
手写C语言之函数的嵌套调用和链式访问
How to design test cases
[lorawan node application] the application and power consumption of Anxin ra-08/ra-08h module in lorawan network