当前位置:网站首页>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;
}
}
边栏推荐
- [MRCTF2020]Ezpop-1|php序列化
- The SQL statement concat cannot find the result
- 简单的页面
- Yunna | fixed assets information system management, information-based fixed assets management
- ML:置信区间的简介(精密度/准确度/精确度的三者区别及其关系)、使用方法、案例应用之详细攻略
- 科创人·味多美CIO胡博:数字化是不流血的革命,正确答案藏在业务的田间地头
- SOFARegistry 源码|数据同步模块解析
- Which securities company is better and which platform is safer for stock speculation account opening
- MySQL basics 1
- HDCP Paring
猜你喜欢

MySQL advanced 1

微信支付 APP端 第三弹 申请退款

传统微服务框架如何无缝过渡到服务网格 ASM

01 backpack problem
![[lorawan node application] the application and power consumption of Anxin ra-08/ra-08h module in lorawan network](/img/5d/9cff7bd25841c1ca6e5ab8e2994f51.png)
[lorawan node application] the application and power consumption of Anxin ra-08/ra-08h module in lorawan network

HDCP Paring

分布式任务调度 ElasticJob demo

传统微服务框架如何无缝过渡到服务网格 ASM

Le module twincat 3 el7211 contrôle les servocommandes baffle

Botu V16 changes the model and firmware version of PLC
随机推荐
[graduation season 𞓜 advanced technology Er] employees who have worked for seven years do not want you to take another detour
微信支付 APP端 第三弹 申请退款
Comment personnaliser les modèles et générer rapidement le code complet dans l'idée?
How about stock online account opening and account opening process? Also, is it safe to open an account online?
Is there any discount for securities account opening? Is it safe to open an account online?
[qnx hypervisor 2.2 user manual]6.2.2 communication between guest and host
DL:深度学习模型优化之模型训练技巧总结之适时自动调整学习率实现代码
数据库学习笔记(SQL03)
如何做好测试用例设计
Quick pow: how to quickly find power
Sofaregistry source code | data synchronization module analysis
Database learning notes (sql03)
[programming problem] maze problem
Applying for let's encrypt SSL certificate with certbot
MySQL primary key constraint deletion
【Spark】scala基础操作(持续更新)
网络方向哪个发展更好?数据通信工程师学习路线分享
中小企业签署ERP合同时,需要注意这几点
A Si's mood swings
mysql 死锁