1. 程式人生 > >php微信企業付款到個人零錢

php微信企業付款到個人零錢

1.基本配置

//公眾賬號appid
     $data["mch_appid"] = 'appid';
//商戶號    
     $data["mchid"]  = '';
//隨機字串   
     $data["nonce_str"] = 'suiji'.mt_rand(100,999); 
//商戶訂單號       
 $data["partner_trade_no"]=date('YmdHis').mt_rand(1000,9999);  
//金額 使用者輸入的提現金額需要乘以100             
    $data["amount"] = $money;     
//企業付款描述
    $data
["desc"] = '企業付款到個人零錢'; //使用者openid $data["openid"] = $openid; //不檢驗使用者姓名 $data["check_name"] = 'NO_CHECK'; //獲取IP $data['spbill_create_ip']=$_SERVER['SERVER_ADDR']; //商戶金鑰 $data['key']=''; //商戶證書 商戶平臺的API安全證書下載 $data['apiclient_cert.pem'
] $data['apiclient_key.pem']

2.PHP程式碼

/**
**開始支付
/
 public function userpay(){
        $money = ‘使用者輸入提現金額’;
        $info['money'] = ‘使用者餘額’;
        if ($this->openid && $money){
            if ($money>$info['money'] ){
                echo json_encode([
                    'status'
=> 1, 'message' => '餘額不足,不能提現!', 'code'=>'餘額不足,不能提現!' ]); }elseif ($money<1){ echo json_encode([ 'status' => 2, 'message' => '提現金額不能小於1元', 'code'=>'提現金額太低' ]); }else{ $openid = $this->openid; $trade_no = date('YmdHis').mt_rand(1000,9999); $res = $this->pay($openid,$trade_no,$money*100,'微信提現'); //結果列印 if($res['result_code']=="SUCCESS"){ echo json_encode([ 'status' => 3, 'message' => '提現成功!', ]); }elseif ($res['err_code']=="SENDNUM_LIMIT"){ echo json_encode([ 'status' => 4, 'message' => '提現失敗!', 'code'=>'每日僅能提現一次', ]); }else{ echo json_encode([ 'status' => 5, 'message' => '提現失敗!', 'code'=>$res['err_code'], ]); } } }else{ echo json_encode([ 'status' => 5, 'message' => '未檢測到您當前微信賬號~', ]); } }
/**
*支付方法
/
public function pay($openid,$trade_no,$money,$desc){
   $params["mch_appid"]='';       
   $params["mchid"] = '';   
   $params["nonce_str"]= 'suiji'.mt_rand(100,999);   
   $params["partner_trade_no"]  = $trade_no;           
   $params["amount"]= $money;          
   $params["desc"]= $desc;         
   $params["openid"]= $openid;          
   $params["check_name"]= 'NO_CHECK';      
   $params['spbill_create_ip'] = $_SERVER['SERVER_ADDR'];   

   //生成簽名
    $str = 'amount='.$params["amount"].'&check_name='.$params["check_name"].'&desc='.$params["desc"].'&mch_appid='.$params["mch_appid"].'&mchid='.$params["mchid"].'&nonce_str='.$params["nonce_str"].'&openid='.$params["openid"].'&partner_trade_no='.$params["partner_trade_no"].'&spbill_create_ip='.$params['spbill_create_ip'].'&key=商戶金鑰';

 //md5加密 轉換成大寫
   $sign = strtoupper(md5($str));
 //生成簽名
   $params['sign'] = $sign;

 //構造XML資料
   $xmldata = $this->array_to_xml($params);  //陣列轉XML
 $url='https://api.mch.weixin.qq.com/mmpaymkttransfers/prom       otion/transfers';

  //傳送post請求
  $res = $this->curl_post_ssl($url, $xmldata); //curl請求 
  if(!$res){
    return array('status'=>1, 
                 'msg'=>"伺服器連線失敗" );
    }

  //付款結果分析
    $content = $this->xml_to_array($res);  //xml轉陣列
    return $content;
    }
/**
* curl請求
/
public function curl_post_ssl($url, $xmldata,           $second=30,$aHeader=array()){
 $ch = curl_init();
 //超時時間
 curl_setopt($ch,CURLOPT_TIMEOUT,$second);
 curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
 //這裡設定代理,如果有的話
 //curl_setopt($ch,CURLOPT_PROXY, '10.206.30.98');
 //curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
 curl_setopt($ch,CURLOPT_URL,$url);
 curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
 curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);

 //預設格式為PEM,可以註釋
   curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
//絕對地址可使用 dirname(__DIR__)列印,如果不是絕對地址會報 58 錯誤
    curl_setopt($ch,CURLOPT_SSLCERT,' 絕對地址/apiclient_cert.pem');
    curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
    curl_setopt($ch,CURLOPT_SSLKEY,'絕對地址/apiclient_key.pem');
   if( count($aHeader) >= 1 ){
            curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
        }
  curl_setopt($ch,CURLOPT_POST, 1);
  curl_setopt($ch,CURLOPT_POSTFIELDS,$xmldata);
  $data = curl_exec($ch);
  if($data){
    curl_close($ch);
    return $data;
        }
  else {
   $error = curl_errno($ch);
   echo "call faild, errorCode:$error\n";
   die();
   curl_close($ch);
   return false;
        }
    }
/**
  * array 轉 xml
 * 用於生成簽名
*/
public function array_to_xml($arr){
  $xml = "<xml>";
   foreach ($arr as $key => $val) {
     if (is_numeric($val)) {
        $xml .= "<" .$key.">".$val."</".$key.">";
     } else
      $xml .= "<".$key."><![CDATA[".$val."]]></".$key.">";
    }
   $xml .= "</xml>";
  return $xml;
    }
/**
* xml 轉化為array
*/
public function xml_to_array($xml){
   //禁止引用外部xml實體
  libxml_disable_entity_loader(true);
   $values =  json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
   return $values;
    }