改成方法的了 大家拿到框架里面 进行封装就行了转载请注明出处哈
https://blog.csdn.net/qq_25861247/article/details/88283971
亲测有效大家试试
/*
* 获取公钥,格式为PKCS#1 转PKCS#8
* openssl rsa -RSAPublicKey_in -in pubkey.pem -pubout > newpubkey.pem
* */
function get_pub_key()
{
$data['mch_id'] = '';
$data['nonce_str'] = md5(time());
$sign =getParam($data);
$dataXML="
".$data['mch_id']."
".$data['nonce_str']."
".$sign."
";
$url = 'https://fraud.mch.weixin.qq.com/risk/getpublickey';
$ret = httpsPost($url,$dataXML,true);
print_r($ret);die;
if($ret['return_code'] == 'SUCCESS' && isset($ret['pub_key'])){
file_put_contents('./cert/pubkey.pem',$ret['pub_key']);
return $ret['pub_key'];
}else{
return null;
}
}
//企业付款到银行卡
function payBank()
{
$out_trade_no = date('Ymdhis', time()).substr(floor(microtime()*1000),0,1).rand(0,9);
$money = 100;
$enc_bank_no = '';
$enc_true_name = '';
$bank_name = '';
$desc = '企业付款到银行卡测试';
// $wxapi = new Wechat($appid,$appsecret,$mchid,$key,$sslcert,$sslkey);
$payment_no = payForBank($out_trade_no,$money,$enc_bank_no,$enc_true_name,$bank_name,$desc);
if($payment_no){
echo '微信付款到银行卡操作成功,微信订单号:'.$payment_no;
} else {
echo '微信付款到银行卡操作失败,请检查日记';
}
}
/*
* 企业付款到银行卡接口
* @params string $out_trade_no : 商户订单号
* @params int $amount : 付款金额,单位分
* @params string $enc_bank_no : 收款方银行卡号
* @params string $enc_true_name : 收款方用户名
* @params string $bank_name : 收款方开户行,根据银行名称获取银行编号bank_code
* @params string $desc : 付款备注
* return string $payment_no :支付成功的订单号
*/
/*
* 银行编号列表,详情参考:https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=24_4
* @params string $bank_name : 银行名称,4个汉字
* return int $bank_code : 银行编码
* */
function getBankCode($bank_name)
{
$bank_code = 0;
switch ($bank_name){
case '工商银行': $bank_code = 1002; break;
case '农业银行': $bank_code = 1005; break;
case '中国银行': $bank_code = 1026; break;
case '建设银行': $bank_code = 1003; break;
case '招商银行': $bank_code = 1001; break;
case '邮储银行': $bank_code = 1066; break;
case '交通银行': $bank_code = 1020; break;
case '浦发银行': $bank_code = 1004; break;
case '民生银行': $bank_code = 1006; break;
case '兴业银行': $bank_code = 1009; break;
case '平安银行': $bank_code = 1010; break;
case '中信银行': $bank_code = 1021; break;
case '华夏银行': $bank_code = 1025; break;
case '广发银行': $bank_code = 1027; break;
case '光大银行': $bank_code = 1022; break;
case '北京银行': $bank_code = 1032; break;
case '宁波银行': $bank_code = 1056; break;
}
return $bank_code;
}
/**
* 公钥加密,银行卡号和姓名需要RSA算法加密
* @param string $data 需要加密的字符串,银行卡/姓名
* @return null|string 加密后的字符串
*/
function publicEncrypt($data)
{
// 进行加密
// $pubkey = openssl_pkey_get_public(file_get_contents('./cert/newpubkey.pem'));
$public_key = file_get_contents('./cert/newpubkey.pem');
$encrypt_data = '';
$encrypted = '';
$r = openssl_public_encrypt($data,$encrypt_data,$public_key,OPENSSL_PKCS1_OAEP_PADDING);
if($r){//加密成功,返回base64编码的字符串
return base64_encode($encrypted.$encrypt_data);
}else{
return false;
}
// $public_key = file_get_contents('./cert/newpubkey.pem');
//
// $rsa = new RSA($public_key,'');
}
function payForBank($out_trade_no,$amount,$enc_bank_no,$enc_true_name,$bank_name,$desc='企业付款到银行卡')
{
$data['amount'] = $amount;
$data['bank_code'] = getBankCode($bank_name);
$data['desc'] = $desc;
$data['enc_bank_no'] = publicEncrypt($enc_bank_no);
$data['enc_true_name'] = publicEncrypt($enc_true_name);
$data['mch_id'] = '';
$data['nonce_str'] =md5(time());
$data['partner_trade_no'] = $out_trade_no;
$sign = getParam($data);
$dataXML="
".$data['amount']."
".$data['bank_code']."
".$data['desc']."
".$data['enc_bank_no']."
".$data['enc_true_name']."
".$data['mch_id']."
".$data['nonce_str']."
".$data['partner_trade_no']."
".$sign."
";
$url = 'https://api.mch.weixin.qq.com/mmpaysptrans/pay_bank';
$ret =httpsPost($url,$dataXML,true);
// print_r($ret);die;
if($ret['return_code'] == 'SUCCESS' && $ret['result_code'] == 'SUCCESS' && $ret['err_code'] == 'SUCCESS'){
return $ret['payment_no'];
}else{
$this->errorLog('微信付款到银行卡失败,appid:'.$this->appid,$ret);
return false;
}
}
/*
* 发起POST网络请求
* @params string $url : 请求的url链接地址
* @params string $data : 数据包
* @params bool $ssl : 是否加载证书
* return array $result : 返回的数据结果
*/
function httpsPost($url,$data,$ssl = false)
{
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_CUSTOMREQUEST, "POST" );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYHOST, FALSE );
if($ssl) {
curl_setopt ( $ch,CURLOPT_SSLCERT,'./cert/apiclient_cert.pem');
curl_setopt ( $ch,CURLOPT_SSLKEY,'./cert/apiclient_key.pem');
}
curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, 1 );
curl_setopt ( $ch, CURLOPT_AUTOREFERER, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
$result = curl_exec($ch);
if (curl_errno($ch)) {
return 'Errno: '.curl_error($ch);
}
curl_close($ch);
return xmlToArray($result);
}
function getParam($paramArray, $isencode=false)
{
$paramStr = '';
ksort($paramArray);
$i = 0;
foreach ($paramArray as $key => $value)
{
if ($key == 'Signature'){
continue;
}
if ($i == 0){
$paramStr .= '';
}else{
$paramStr .= '&';
}
$paramStr .= $key . '=' . ($isencode?urlencode($value):$value);
++$i;
}
$stringSignTemp=$paramStr."&key=".'WuVKPkP7yrmWEopUUMce7unAQKpeQli8';
$sign=strtoupper(md5($stringSignTemp));
return $sign;
}
/*
* 将xml转换成数组
* @params xml $xml : xml数据
* return array $data : 返回数组
*/
function xmlToArray($xml)
{
//禁止引用外部xml实体
libxml_disable_entity_loader(true);
$xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$val = json_decode(json_encode($xmlstring),true);
return $val;
}
//payBank();
/*
* 查询付款到银行卡状态
* @params string $out_trade_no : 商户订单号
* return array $ret:查询状态
* */
function queryBank($out_trade_no)
{
$data['mch_id'] = '';
$data['nonce_str'] = md5(time());
$data['partner_trade_no'] = $out_trade_no;
$sign = getParam($data);
$dataXML="
".$data['mch_id']."
".$data['nonce_str']."
".$data['partner_trade_no']."
".$sign."
";
$url = 'https://api.mch.weixin.qq.com/mmpaysptrans/query_bank';
$ret = httpsPost($url,$dataXML,true);
print_r($ret);die;
if($ret['return_code'] == 'SUCCESS' && $ret['result_code'] == 'SUCCESS' && $ret['err_code'] == 'SUCCESS'){
return $ret;
}else{
$this->errorLog('查询微信付款到银行卡失败,appid:'.$this->appid.',订单号:'.$out_trade_no,$ret);
return false;
}
}
请求成功截图
查询结果截图