(编辑:jimmy 日期: 2024/11/20 浏览:2)
微信支付已上线企业付款至银行卡功能。商户可以将商户号余额付款至指定的收款银行账户。通过指定收款银行账户户名、卡号,以及收款银行信息即可实现付款。功能目前为灰度开放,已灰度新资金流直连普通商户及普通受理模式子商户,其他商户类型将在后续开放。
一、微信商户号须开通此功能;
二、应用环境TP5+Mysql+Centos
三、编写代码
3.1、Model层,Wechat.php
<"<xml> <amount>".$data['amount']."</amount> <bank_code>".$data['bank_code']."</bank_code> <desc>".$data['desc']."</desc> <enc_bank_no>".$data['enc_bank_no']."</enc_bank_no> <enc_true_name>".$data['enc_true_name']."</enc_true_name> <mch_id>".$data['mch_id']."</mch_id> <nonce_str>".$data['nonce_str']."</nonce_str> <partner_trade_no>".$data['partner_trade_no']."</partner_trade_no> <sign>".$sign."</sign> </xml>"; $url = 'https://api.mch.weixin.qq.com/mmpaysptrans/pay_bank'; $ret = $this->httpsPost($url,$dataXML,true); 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; } } /* * 查询付款到银行卡状态 * @params string $out_trade_no : 商户订单号 * return array $ret:查询状态 * */ public function queryBank($out_trade_no) { $data['mch_id'] = $this->mchid; $data['nonce_str'] = $this->random(12); $data['partner_trade_no'] = $out_trade_no; $sign = $this->getParam($data); $dataXML="<xml> <mch_id>".$data['mch_id']."</mch_id> <nonce_str>".$data['nonce_str']."</nonce_str> <partner_trade_no>".$data['partner_trade_no']."</partner_trade_no> <sign>".$sign."</sign> </xml>"; $url = 'https://api.mch.weixin.qq.com/mmpaysptrans/query_bank'; $ret = $this->httpsPost($url,$dataXML,true); 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; } } /* * 银行编号列表,详情参考:https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php"<xml> <mch_id>".$data['mch_id']."</mch_id> <nonce_str>".$data['nonce_str']."</nonce_str> <sign>".$sign."</sign> </xml>"; $url = 'https://fraud.mch.weixin.qq.com/risk/getpublickey'; $ret = $this->httpsPost($url,$dataXML,true); if($ret['return_code'] == 'SUCCESS' && isset($ret['pub_key'])){ file_put_contents($rsafile,$ret['pub_key']); return $ret['pub_key']; }else{ return null; } }else{ return file_get_contents($rsafile); } } /* * 发起POST网络请求 * @params string $url : 请求的url链接地址 * @params string $data : 数据包 * @params bool $ssl : 是否加载证书 * return array $result : 返回的数据结果 */ private 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,$this->sslcert); curl_setopt ( $ch,CURLOPT_SSLKEY,$this->sslkey); } 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 $this->xmlToArray($result); } //对参数排序,生成MD5加密签名 private 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"&key=".$this->key; $sign=strtoupper(md5($stringSignTemp)); return $sign; } /* * 将xml转换成数组 * @params xml $xml : xml数据 * return array $data : 返回数组 */ private 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; } /* * 随机字符串 * @param int $length 长度 * @param string $type 类型 * @param int $convert 转换大小写 1大写 0小写 * @return string */ private function random($length=10,$type='letter',$convert=false) { $config = array( 'number'=>'1234567890', 'letter'=>'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', 'string'=>'abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789', 'all'=>'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890' ); if(!isset($config[$type])) $type = 'letter'; $string = $config[$type]; $code = ''; $strlen = strlen($string) -1; for($i = 0; $i < $length; $i++){ $code .= $string{mt_rand(0, $strlen)}; } if(!empty($convert)){ $code = ($convert > 0)"[" . date('Y-m-d H:i:s') . "] ".$msg."," .json_encode($ret).PHP_EOL, FILE_APPEND); } }
3.2、Controller控制层调用,Wxpay.php