企業付款到零錢(微信)

<?php
/*代碼部分*/


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


/**
 * [xmltoarray xml格式轉換爲數組]
 * @param  [type] $xml [xml]
 * @return [type]      [xml 轉化爲array]
 */
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;
}

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

/**
 * [createNoncestr 生成隨機字符串]
 * @param  integer $length [長度]
 * @return [type]          [字母大小寫加數字]
 */
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 發送curl_post數據]
 * @param  [type]  $url     [發送地址]
 * @param  [type]  $xmldata [發送文件格式]
 * @param  [type]  $second [設置執行最長秒數]
 * @param  [type]  $aHeader [設置頭部]
 * @return [type]           [description]
 */
function curl_post_ssl($url, $xmldata, $second = 30, $aHeader = array()){
    $isdir = $_SERVER['DOCUMENT_ROOT']."/cert/";//證書位置;絕對路徑

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

    curl_setopt($ch, CURLOPT_TIMEOUT, $second);//設置執行最長秒數
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求結果爲字符串且輸出到屏幕上
    curl_setopt($ch, CURLOPT_URL, $url);//抓取指定網頁
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 終止從服務端進行驗證
    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中規定的私鑰的加密類型
    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);//設置頭部
    }
    curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xmldata);//所有數據使用HTTP協議中的"POST"操做來發送

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


/**
 * [sendMoney 企業付款到零錢]
 * @param  [type] $amount     [發送的金額(分)目前發送金額不能少於1元]
 * @param  [type] $re_openid  [發送人的 openid]
 * @param  string $desc       [企業付款描述信息 (必填)]
 * @param  string $check_name [收款用戶姓名 (選填)]
 * @return [type]             [description]
 */
function sendMoney($amount,$re_openid,$desc='測試',$check_name=''){

    $total_amount = (100) * $amount;

    $data=array(
        'mch_appid'=>APPID,//商戶帳號appid
        'mchid'=> MCHID,//商戶號
        'nonce_str'=>createNoncestr(),//隨機字符串
        'partner_trade_no'=> date('YmdHis').rand(1000, 9999),//商戶訂單號
        'openid'=> $re_openid,//用戶openid
        'check_name'=>'NO_CHECK',//校驗用戶姓名選項,
        're_user_name'=> $check_name,//收款用戶姓名
        'amount'=>$total_amount,//金額
        'desc'=> $desc,//企業付款描述信息
        'spbill_create_ip'=> IP,//Ip地址
    );

    //生成簽名算法
    $secrect_key=SECRECT_KEY;///這個就是個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'; //調用接口
    $res=curl_post_ssl($url,$xml);
    $return=xmltoarray($res);


    print_r($return);
    //返回來的結果是xml,最後轉換成數組
    /*
    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  若是返回來SUCCESS,則發生成功,處理本身的邏輯

    return $res;
}


/*調用方法*/
sendMoney(1,'gdgfdg56456223423','xxxx測試','xxx');
相關文章
相關標籤/搜索