当前位置:网站首页>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;
}
}
边栏推荐
- 【编程题】迷宫问题
- 科创人·味多美CIO胡博:数字化是不流血的革命,正确答案藏在业务的田间地头
- Sword finger offer II 037 Asteroid collision
- 证券开户有优惠吗究竟网上开户是否安全么?
- 中小企业签署ERP合同时,需要注意这几点
- A Si's mood swings
- DL: deep learning model optimization model training skills summary timely automatic adjustment of learning rate implementation code
- 炒股开户选择哪家券商公司比较好哪家平台更安全
- [cloud native] kernel security in container scenario
- Which department should the company's fixed assets be managed? How should the company's fixed assets be managed
猜你喜欢

Top performance version 2 reading notes (IV) -- memory monitoring

MySQL高级篇1
![[qnx hypervisor 2.2 user manual]6.2.2 communication between guest and host](/img/a4/a84f916d3aa2cc59f5b686cd32797a.png)
[qnx hypervisor 2.2 user manual]6.2.2 communication between guest and host

How much is the fixed asset management system and the price of the fixed asset management system

Interviewer: how to solve the problem of massive requests for data that does not exist in redis, which affects the database?

Citation of Dissertation

简单的页面

Mysql Duplicate entry ‘xxx‘ for key ‘xxx‘

HDCP Paring

Time does not spare
随机推荐
【每日一题】二叉树的前后序遍历
How about stock online account opening and account opening process? Also, is it safe to open an account online?
leetcode-1. 两数之和
字节、字、双字 关系
Intermittent sampling and forwarding interference
HDCP Paring
【服装软件】服装出产办理体系选型的准则有哪些?
中小企业签署ERP合同时,需要注意这几点
干外包3年,真废了...
字符串之间的比较之 localeCompare
IDEA中的常用设置
开发者,为什么说容器技术的成熟预示着云原生时代的到来?
字节面试惨遭滑铁卢:一面就被吊打,幸得华为内推,三面拿到offer
炒股开户选择哪家券商公司比较好哪家平台更安全
间歇采样转发干扰
如何在IDEA中自定義模板、快速生成完整的代碼?
Which department should the company's fixed assets be managed? How should the company's fixed assets be managed
Applying for let's encrypt SSL certificate with certbot
Sofaregistry source code | data synchronization module analysis
[daily question 1] traversal of binary tree