天天看點

PHP Thinkphp 支付寶當面付2.0代碼

支付寶官方給的SDK調了一天沒通,自己寫了三個方法,簡單易用。

//支付寶條碼支付,直接調用ali_tiaoma_pay方法,把條碼和金額傳進去就ok
function ali_tiaoma_pay($tiaoma,$price){

    $data['scene'] = 'bar_code'; //支付場景
    $data['out_trade_no'] = date("Ymd").rand(10000,99999);  //訂單号
    $data['total_amount'] = $price; //支付金額
    $data['auth_code'] = $tiaoma;   //支付授權碼
    $data['subject'] = '訂單标題'; //訂單标題
    $data['body'] = '訂單說明'; //訂單說明


    $data = json_encode($data);

    $params['app_id'] = $alipay_account;
    $params['method'] = 'alipay.trade.pay';
    $params['charset'] = 'utf-8';
    $params['sign_type'] = 'RSA2';
    $params['timestamp'] = date("Y-m-d H:i:s");
    $params['version'] = '1.0';
    $params['notify_url'] = "";  
    $params['biz_content'] = $data;

    ksort($params); //對将要簽名的數組排序

    $string = $this->toUrlParam($params); //将數組轉換成字元串

    $params['sign'] = $this->alipay_sign($string); //将字元串簽名

    $url = 'https://openapi.alipay.com/gateway.do?' . http_build_query($params);

    $return = file_get_contents($url); //打開支付請求連接配接 擷取二維碼位址

    $return = json_decode($return, true); //将傳回的字元串轉換為數組

    return $return;



}


//app支付寶簽名
function alipay_sign($data, $signType = "RSA2") {

    

    $res = "-----BEGIN RSA PRIVATE KEY-----\n" .
        wordwrap('你的商戶私鑰,必須和公匙比對', 64, "\n", true) .
        "\n-----END RSA PRIVATE KEY-----";

    ($res) or die('您使用的私鑰格式錯誤,請檢查RSA私鑰配置');

    if ("RSA2" == $signType) {
        openssl_sign($data, $sign, $res, "SHA256");
    } else {
        openssl_sign($data, $sign, $res);
    }

    $sign = base64_encode($sign);
    return $sign;
}

/*
 * 格式化參數格式化成url參數
*/
function toUrlParam($param){
    $buff = "";
    foreach($param as $k => $v)
    {
        if($k != "sign" && $v != "" && !is_array($v)){
            $buff .= $k."=".$v."&";
        }
    }

    $buff = trim($buff, "&");
    return $buff;
}
           

版權聲明:本文為司江龍原創文章,未經允許不得轉載。https://mp.csdn.net/postedit/81563758

繼續閱讀