亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

對于支付寶支付的開發(fā),盡管官網(wǎng)文檔描述的已經(jīng)比較清楚了,但還是有像我這樣的小白仍然還是不會。。。。今天好好的摸索了一天,在這里分享記錄了一下。

首先我們要拿到企業(yè)的支付寶開放平臺賬號(我做的是企業(yè),個人的我不清楚能不能做),登錄進(jìn)去后創(chuàng)建一個應(yīng)用(如果之前沒有創(chuàng)建的話);


5f3a1785d914d.jpg


然后我們就能看到你新創(chuàng)建的應(yīng)用了,但現(xiàn)在是用不了的,需要完善信息提交審核,通過之后才能使用,點(diǎn)擊查看進(jìn)入應(yīng)用

上傳應(yīng)用圖標(biāo),配置授權(quán)回調(diào)地址和應(yīng)用公鑰,這里的回調(diào)地址一定是公網(wǎng)可以訪問得到的,不是你本地的測試地址!


5f3a17cc46283.jpg

不會生成秘鑰的話就點(diǎn)擊上面 如何生成秘鑰  

填完了這些信息后就可以提交審核了,審核需要一點(diǎn)時間。這時我們可以先寫我們的邏輯代碼然后利用支付寶沙箱進(jìn)行測試。

開發(fā)可以下載支付寶官方的SDK參考,也可以自行百度大神們整理的代碼,這里小編直接貼出來:

<?php
header('Content-type:text/html; Charset=utf-8');
$appid = 'xxxxx'; //https://open.alipay.com 賬戶中心->密鑰管理->開放平臺密鑰,填寫添加了電腦網(wǎng)站支付的應(yīng)用的APPID
$notifyUrl = 'http://www.xxx.com/alipay/notify.php';   //付款成功后的異步回調(diào)地址
$outTradeNo = uniqid();   //你自己的商品訂單號
$payAmount = 0.01;     //付款金額,單位:元
$orderName = '支付測試';  //訂單標(biāo)題
$signType = 'RSA2';    //簽名算法類型,支持RSA2和RSA,推薦使用RSA2
//商戶私鑰,填寫對應(yīng)簽名算法類型的私鑰,如何生成密鑰參考:https://docs.open.alipay.com/291/105971和https://docs.open.alipay.com/200/105310
$saPrivateKey='這里填你的商戶私鑰';
$aliPay = new AlipayService($appid,$returnUrl,$notifyUrl,$saPrivateKey);
$result = $aliPay->doPay($payAmount,$outTradeNo,$orderName,$returnUrl,$notifyUrl);
$result = $result['alipay_trade_precreate_response'];
if($result['code'] && $result['code']=='10000'){
    //生成二維碼
    $url = 'http://pan.baidu.com/share/qrcode?w=300&h=300&url='.$result['qr_code'];
    echo "<img src='{$url}' style='width:300px;'><br>";
    echo '二維碼內(nèi)容:'.$result['qr_code'];
}else{
    echo $result['msg'].' : '.$result['sub_msg'];
}
class AlipayService
{
    protected $appId;
    protected $returnUrl;
    protected $notifyUrl;
    //私鑰文件路徑
    protected $rsaPrivateKeyFilePath;
    //私鑰值
    protected $rsaPrivateKey;
    public function __construct($appid, $returnUrl, $notifyUrl,$saPrivateKey)
    {
        $this->appId = $appid;
        $this->returnUrl = $returnUrl;
        $this->notifyUrl = $notifyUrl;
        $this->charset = 'utf8';
        $this->rsaPrivateKey=$saPrivateKey;
    }
    /**
     * 發(fā)起訂單
     * @param float $totalFee 收款總費(fèi)用 單位元
     * @param string $outTradeNo 唯一的訂單號
     * @param string $orderName 訂單名稱
     * @param string $notifyUrl 支付結(jié)果通知url 不要有問號
     * @param string $timestamp 訂單發(fā)起時間
     * @return array
     */
    public function doPay($totalFee, $outTradeNo, $orderName, $returnUrl,$notifyUrl)
    {
        //請求參數(shù)
        $requestConfigs = array(
            'out_trade_no'=>$outTradeNo,
            'total_amount'=>$totalFee, //單位 元
            'subject'=>$orderName, //訂單標(biāo)題
        );
        $commonConfigs = array(
            //公共參數(shù)
            'app_id' => $this->appId,
            'method' => 'alipay.trade.precreate',       //接口名稱
            'format' => 'JSON',
            'charset'=>$this->charset,
            'sign_type'=>'RSA2',
            'timestamp'=>date('Y-m-d H:i:s'),
            'version'=>'1.0',
            'notify_url' => $notifyUrl,
            'biz_content'=>json_encode($requestConfigs),
        );
        $commonConfigs["sign"] = $this->generateSign($commonConfigs, $commonConfigs['sign_type']);
        $result = $this->curlPost('https://openapi.alipay.com/gateway.do',$commonConfigs);
        return json_decode($result,true);
    }
    public function generateSign($params, $signType = "RSA") {
        return $this->sign($this->getSignContent($params), $signType);
    }
    protected function sign($data, $signType = "RSA") {
        $priKey=$this->rsaPrivateKey;
        $res = "-----BEGIN RSA PRIVATE KEY-----\n" .
        wordwrap($priKey, 64, "\n", true) .
        "\n-----END RSA PRIVATE KEY-----";
        ($res) or die('您使用的私鑰格式錯誤,請檢查RSA私鑰配置');
        if ("RSA2" == $signType) {
            openssl_sign($data, $sign, $res, version_compare(PHP_VERSION,'5.4.0', '<') ? SHA256 : OPENSSL_ALGO_SHA256); //OPENSSL_ALGO_SHA256是php5.4.8以上版本才支持
        } else {
            openssl_sign($data, $sign, $res);
        }
        $sign = base64_encode($sign);
        return $sign;
    }
    /**
     * 校驗$value是否非空
     * if not set ,return true;
     *  if is null , return true;
     **/
    protected function checkEmpty($value) {
        if (!isset($value))
            return true;
        if ($value === null)
            return true;
        if (trim($value) === "")
            return true;
        return false;
    }
    public function getSignContent($params) {
        ksort($params);
        $stringToBeSigned = "";
        $i = 0;
        foreach ($params as $k => $v) {
            if (false === $this->checkEmpty($v) && "@" != substr($v, 0, 1)) {
                // 轉(zhuǎn)換成目標(biāo)字符集
                $v = $this->characet($v, $this->charset);
                if ($i == 0) {
                    $stringToBeSigned .= "$k" . "=" . "$v";
                } else {
                    $stringToBeSigned .= "&" . "$k" . "=" . "$v";
                }
                $i++;
            }
        }
        unset ($k, $v);
        return $stringToBeSigned;
    }
    /**
     * 轉(zhuǎn)換字符集編碼
     * @param $data
     * @param $targetCharset
     * @return string
     */
    function characet($data, $targetCharset) {
        if (!empty($data)) {
            $fileType = $this->charset;
            if (strcasecmp($fileType, $targetCharset) != 0) {
                $data = mb_convert_encoding($data, $targetCharset, $fileType);
                //$data = iconv($fileType, $targetCharset.'//IGNORE', $data);
            }
        }
        return $data;
    }
    public function curlPost($url = '', $postData = '', $options = array())
    {
        if (is_array($postData)) {
            $postData = http_build_query($postData);
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30); //設(shè)置cURL允許執(zhí)行的最長秒數(shù)
        if (!empty($options)) {
            curl_setopt_array($ch, $options);
        }
        //https請求 不驗證證書和host
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
}

之后我們可以利用支付寶的沙箱環(huán)境進(jìn)行測試我們的代碼是否ok,在開發(fā)者中心找到沙箱環(huán)境。


5f3a19907248b.jpg


這里他會給你分配好appid這些信息,公鑰可以用你的應(yīng)用的公鑰(我是這樣用的,不知道有沒問題),然后你就可以拿這appid,公鑰秘鑰等去進(jìn)行測試了(沙箱的網(wǎng)關(guān)地址和正式環(huán)境的網(wǎng)關(guān)地址是不同的!!!這里特別注意!!!),以下是我獲取到的二維碼信息,通過第三方工具可以生成二維碼(比如jquery的qrcode)


5f3a19c2c48f1.jpg


然后我們就在頁面將返回的支付碼供用戶掃碼支付了。


5f3a1a09a72e8.jpg


這里是不能直接用你的支付寶去掃的,不然的話會提示二維碼已失效,請刷新的字樣。沙箱環(huán)境的需要下載沙箱版的支付寶進(jìn)行掃碼支付,下載地址https://openhome.alipay.com/platform/appDaily.htm,目前只有安卓版,用安卓手機(jī)下載就好,安裝好了直接登錄,不是使用你自己的支付寶賬號去登錄,沙箱賬號在


5f3a1a480f58b.jpg


用買家賬號登錄進(jìn)去掃碼支付就好了,等待支付成功我們的任務(wù)就完成啦,等待應(yīng)用審核通過把正式的appid等(一定要記得替換網(wǎng)關(guān)地址)替換掉就可以使用啦。

這里只是實(shí)現(xiàn)了掃碼,沒有實(shí)現(xiàn)驗簽,驗簽是灰常重要的,不過這塊比較簡單,按照支付寶官方的文檔操作就行了。




分享到:
標(biāo)簽:PHP開發(fā) 支付寶PC掃碼 掃碼支付 支付寶當(dāng)面付開發(fā)
用戶無頭像

網(wǎng)友整理

注冊時間:

網(wǎng)站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運(yùn)動步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績評定2018-06-03

通用課目體育訓(xùn)練成績評定