溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用PHP怎么實(shí)現(xiàn)微信商戶支付企業(yè)付款到零錢功能

發(fā)布時(shí)間:2021-05-22 16:31:16 來(lái)源:億速云 閱讀:310 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

本篇文章為大家展示了使用PHP怎么實(shí)現(xiàn)微信商戶支付企業(yè)付款到零錢功能,內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。

一、開(kāi)通條件

企業(yè)付款為企業(yè)提供付款至用戶零錢的能力,支持通過(guò)API接口付款,或通過(guò)微信支付商戶平臺(tái)(pay.weixin.qq.com)網(wǎng)頁(yè)操作付款。

使用條件

  • 商戶號(hào)(或同主體其他非服務(wù)商商戶號(hào))已入駐90日

  • 商戶號(hào)(或同主體其他非服務(wù)商商戶號(hào))有30天連續(xù)正常交易

  • 登錄微信支付商戶平臺(tái)-產(chǎn)品中心,開(kāi)通企業(yè)付款。

使用PHP怎么實(shí)現(xiàn)微信商戶支付企業(yè)付款到零錢功能

具體的可以看微信支付開(kāi)發(fā)文檔

二、代碼展示

//企業(yè)付款到微信零錢,PHP接口調(diào)用方法
define("APPID", "wxe062425f740c30d8"); // 商戶賬號(hào)appid
define("MCHID", "10000098");  // 商戶號(hào)
define("SECRECT_KEY", "453436425252"); //支付密鑰簽名
define("IP", "xxx.xxx.xx.xx"); //IP


 /**
 * [xmltoarray xml格式轉(zhuǎn)換為數(shù)組]
 * @param [type] $xml [xml]
 * @return [type]  [xml 轉(zhuǎn)化為array]
 */
 function xmltoarray($xml) { 
  //禁止引用外部xml實(shí)體 
  libxml_disable_entity_loader(true); 
  $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); 
  $val = json_decode(json_encode($xmlstring),true); 
  return $val;
 }

 /**
 * [arraytoxml 將數(shù)組轉(zhuǎn)換成xml格式(簡(jiǎn)單方法):]
 * @param [type] $data [數(shù)組]
 * @return [type]  [array 轉(zhuǎn) xml]
 */
 function arraytoxml($data){
  $str='<xml>';
  foreach($data as $k=>$v) {
   $str.='<'.$k.'>'.$v.'</'.$k.'>';
  }
  $str.='</xml>';
  return $str;
 }

 /**
 * [createNoncestr 生成隨機(jī)字符串]
 * @param integer $length [長(zhǎng)度]
 * @return [type]   [字母大小寫加數(shù)字]
 */
 function createNoncestr($length =32){
  $chars = "ABCDEFGHIJKLMNOPQRSTUVWXYabcdefghijklmnopqrstuvwxyz0123456789"; 
  $str ="";

  for($i=0;$i<$length;$i++){ 
   $str.= substr($chars, mt_rand(0, strlen($chars)-1), 1); 
  } 
  return $str;
 }

 /**
 * [curl_post_ssl 發(fā)送curl_post數(shù)據(jù)]
 * @param [type] $url  [發(fā)送地址]
 * @param [type] $xmldata [發(fā)送文件格式]
 * @param [type] $second [設(shè)置執(zhí)行最長(zhǎng)秒數(shù)]
 * @param [type] $aHeader [設(shè)置頭部]
 * @return [type]   [description]
 */
 function curl_post_ssl($url, $xmldata, $second = 30, $aHeader = array()){
  $isdir = $_SERVER['DOCUMENT_ROOT']."/cert/";//證書位置;絕對(duì)路徑

  $ch = curl_init();//初始化curl

  curl_setopt($ch, CURLOPT_TIMEOUT, $second);//設(shè)置執(zhí)行最長(zhǎng)秒數(shù)
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求結(jié)果為字符串且輸出到屏幕上
  curl_setopt($ch, CURLOPT_URL, $url);//抓取指定網(wǎng)頁(yè)
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 終止從服務(wù)端進(jìn)行驗(yàn)證
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//
  curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');//證書類型
  curl_setopt($ch, CURLOPT_SSLCERT, $isdir . 'apiclient_cert.pem');//證書位置
  curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');//CURLOPT_SSLKEY中規(guī)定的私鑰的加密類型
  curl_setopt($ch, CURLOPT_SSLKEY, $isdir . 'apiclient_key.pem');//證書位置
  curl_setopt($ch, CURLOPT_CAINFO, 'PEM');
  curl_setopt($ch, CURLOPT_CAINFO, $isdir . 'rootca.pem');
  if (count($aHeader) >= 1) {
   curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);//設(shè)置頭部
  }
  curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
  curl_setopt($ch, CURLOPT_POSTFIELDS, $xmldata);//全部數(shù)據(jù)使用HTTP協(xié)議中的"POST"操作來(lái)發(fā)送

  $data = curl_exec($ch);//執(zhí)行回話
  if ($data) {
   curl_close($ch);
   return $data;
  } else {
   $error = curl_errno($ch);
   echo "call faild, errorCode:$error\n";
   curl_close($ch);
   return false;
  }
 }


 /**
 * [sendMoney 企業(yè)付款到零錢]
 * @param [type] $amount  [發(fā)送的金額(分)目前發(fā)送金額不能少于1元]
 * @param [type] $re_openid [發(fā)送人的 openid]
 * @param string $desc  [企業(yè)付款描述信息 (必填)]
 * @param string $check_name [收款用戶姓名 (選填)]
 * @return [type]    [description]
 */
 function sendMoney($amount,$re_openid,$desc='測(cè)試',$check_name=''){
  
  $total_amount = (100) * $amount;
  
  $data=array(
   'mch_appid'=>APPID,//商戶賬號(hào)appid
   'mchid'=> MCHID,//商戶號(hào)
   'nonce_str'=>createNoncestr(),//隨機(jī)字符串
   'partner_trade_no'=> date('YmdHis').rand(1000, 9999),//商戶訂單號(hào)
   'openid'=> $re_openid,//用戶openid
   'check_name'=>'NO_CHECK',//校驗(yàn)用戶姓名選項(xiàng),
   're_user_name'=> $check_name,//收款用戶姓名
   'amount'=>$total_amount,//金額
   'desc'=> $desc,//企業(yè)付款描述信息
   'spbill_create_ip'=> IP,//Ip地址
  );

  //生成簽名算法
  $secrect_key=SECRECT_KEY;///這個(gè)就是個(gè)API密碼。MD5 32位。
  $data=array_filter($data);
  ksort($data);
  $str='';
  foreach($data as $k=>$v) {
   $str.=$k.'='.$v.'&';
  }
  $str.='key='.$secrect_key;
  $data['sign']=md5($str);
  //生成簽名算法


  $xml=arraytoxml($data);
 
  $url='https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers'; //調(diào)用接口
  $res=curl_post_ssl($url,$xml);
  $return=xmltoarray($res);
  
 
  print_r($return);
  //返回來(lái)的結(jié)果是xml,最后轉(zhuǎn)換成數(shù)組
 /*
 array(9) {
  ["return_code"]=>
  string(7) "SUCCESS"
  ["return_msg"]=>
  array(0) {
  }
  ["mch_appid"]=>
  string(18) "wx57676786465544b2a5"
  ["mchid"]=>
  string(10) "143345612"
  ["nonce_str"]=>
  string(32) "iw6TtHdOySMAfS81qcnqXojwUMn8l8mY"
  ["result_code"]=>
  string(7) "SUCCESS"
  ["partner_trade_no"]=>
  string(18) "201807011410504098"
  ["payment_no"]=>
  string(28) "1000018301201807019357038738"
  ["payment_time"]=>
  string(19) "2018-07-01 14:56:35"
 }
 */

  
  $responseObj = simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA);
  echo $res= $responseObj->return_code; //SUCCESS 如果返回來(lái)SUCCESS,則發(fā)生成功,處理自己的邏輯
  
  return $res;
 }

三、調(diào)用方法

sendMoney(1,'gdgfdg56456223423','xxxx測(cè)試','xxx');

php有什么用

php是一個(gè)嵌套的縮寫名稱,是英文超級(jí)文本預(yù)處理語(yǔ)言,它的語(yǔ)法混合了C、Java、Perl以及php自創(chuàng)新的語(yǔ)法,主要用來(lái)做網(wǎng)站開(kāi)發(fā),許多小型網(wǎng)站都用php開(kāi)發(fā),因?yàn)閜hp是開(kāi)源的,從而使得php經(jīng)久不衰。

上述內(nèi)容就是使用PHP怎么實(shí)現(xiàn)微信商戶支付企業(yè)付款到零錢功能,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI