PHP實(shí)現(xiàn)支付寶即時(shí)到賬功能
本文實(shí)例為大家分享了PHP支付寶即時(shí)到賬功能的實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
首先需要下載即時(shí)到賬交易接口,傳送門https://doc.open.alipay.com/doc2/detail?treeId=62&articleId=103566&docType=1
選擇(create_direct_pay_by_user)
然后解壓,選擇MD5簽名版本的文件
此文件夾里有個(gè)叫create_direct_pay_by_user-PHP-UTF-8的文件,我們就用這個(gè)文件~
接著打開文件后如圖
并把文件夾lib里面的四個(gè)PHP文件重命名
alipay_core.function.php重命名為:Corefunction.php;
alipay_md5.function.php重命名為:Md5function.php;
alipay_notify.class.php重命名為:Notify.php;
alipay_submit.class.php重命名為:Submit.php;
最后在THINKPHP\Library\Vendor目錄下新建文件夾命名為Alipay,把以上四個(gè)php文件復(fù)制進(jìn)去如圖所示
然后,打開Submit.php文件,把以下代碼去掉;
require_once("alipay_core.function.php");
require_once("alipay_md5.function.php");
同樣,打開Notify.php文件,把以下兩段代碼去掉;
require_once("alipay_core.function.php");
require_once("alipay_md5.function.php");
接下來可以寫代碼了(注意img文件夾里的圖片地址需要修改)
HTML代碼:(這個(gè)代碼其實(shí)就是index里面的代碼,復(fù)制如下)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>支付寶即時(shí)到賬交易接口</title> </head> <style> html,body { width:100%; min-width:1200px; height:auto; padding:0; margin:0; font-family:"微軟雅黑"; background-color:#242736 } .header { width:100%; margin:0 auto; height:230px; background-color:#fff } .container { width:100%; min-width:100px; height:auto } .black { background-color:#242736 } .blue { background-color:#0ae } .qrcode { width:1200px; margin:0 auto; height:30px; background-color:#242736 } .littlecode { width:16px; height:16px; margin-top:6px; cursor:pointer; float:right } .showqrs { top:30px; position:absolute; width:100px; margin-left:-65px; height:160px; display:none } .shtoparrow { width:0; height:0; margin-left:65px; border-left:8px solid transparent; border-right:8px solid transparent; border-bottom:8px solid #e7e8eb; margin-bottom:0; font-size:0; line-height:0 } .guanzhuqr { text-align:center; background-color:#e7e8eb; border:1px solid #e7e8eb } .guanzhuqr img { margin-top:10px; width:80px } .shmsg { margin-left:10px; width:80px; height:16px; line-height:16px; font-size:12px; color:#242323; text-align:center } .nav { width:1200px; margin:0 auto; height:70px; } .open,.logo { display:block; float:left; height:40px; width:85px; margin-top:20px } .divier { display:block; float:left; margin-left:20px; margin-right:20px; margin-top:23px; width:1px; height:24px; background-color:#d3d3d3 } .open { line-height:30px; font-size:20px; text-decoration:none; color:#1a1a1a } .navbar { float:right; width:200px; height:40px; margin-top:15px; list-style:none } .navbar li { float:left; width:100px; height:40px } .navbar li a { display:inline-block; width:100px; height:40px; line-height:40px; font-size:16px; color:#1a1a1a; text-decoration:none; text-align:center } .navbar li a:hover { color:#00AAEE } .title { width:1200px; margin:0 auto; height:80px; line-height:80px; font-size:20px; color:#FFF } .content { width:100%; min-width:1200px; height:660px; background-color:#fff; } .alipayform { width:800px; margin:0 auto; height:600px; border:1px solid #0ae } .element { width:600px; height:80px; margin-left:100px; font-size:20px } .etitle,.einput { float:left; height:26px } .etitle { width:150px; line-height:26px; text-align:right } .einput { width:200px; margin-left:20px } .einput input { width:398px; height:24px; border:1px solid #0ae; font-size:16px } .mark { margin-top: 10px; width:500px; height:30px; margin-left:80px; line-height:30px; font-size:12px; color:#999 } .legend { margin-left:100px; font-size:24px } .alisubmit { width:400px; height:40px; border:0; background-color:#0ae; font-size:16px; color:#FFF; cursor:pointer; margin-left:170px } .footer { width:100%; height:120px; background-color:#242735 } .footer-sub a,span { color:#808080; font-size:12px; text-decoration:none } .footer-sub a:hover { color:#00aeee } .footer-sub span { margin:0 3px } .footer-sub { padding-top:40px; height:20px; width:600px; margin:0 auto; text-align:center } </style> <body> <div class="header"> <div class="container black"> <div class="qrcode"> <div class="littlecode"> <img width="16px" src="img/little_qrcode.jpg" id="licode"> <div class="showqrs" id="showqrs"> <div class="shtoparrow"></div> <div class="guanzhuqr"> <img src="img/guanzhu_qrcode.png" width="80"> <div class="shmsg" style="margin-top:5px;"> 請掃碼關(guān)注 </div> <div class="shmsg" style="margin-bottom:5px;"> 接收重要信息 </div> </div> </div> </div> </div> </div> <div class="container"> <div class="nav"> <a class="logo"><img src="img/alipay_logo.png" height="30px"></a> <span class="divier"></span> <a class="open" target="_blank">開放平臺(tái)</a> <ul class="navbar"> <li><a target="_blank">在線文檔</a></li> <li><a target="_blank">技術(shù)支持</a></li> </ul> </div> </div> <div class="container blue"> <div class="title">支付寶即時(shí)到賬(create_direct_pay_by_user)</div> </div> </div> <div class="content"> <form action="{:U('home/Pay/alipayapi')}" class="alipayform" method="post" target="_blank"> <div class="element" style="margin-top:60px;"> <div class="legend">支付寶即時(shí)到賬交易接口快速通道 </div> </div> <div class="element"> <div class="etitle">商戶訂單號:</div> <div class="einput"><input type="text" name="WIDout_trade_no" id="out_trade_no"></div> <br> <div class="mark">注意:商戶訂單號(out_trade_no).必填(建議是英文字母和數(shù)字,不能含有特殊字符)</div> </div> <div class="element"> <div class="etitle">商品名稱:</div> <div class="einput"><input type="text" name="WIDsubject" value="test商品123"></div> <br> <div class="mark">注意:產(chǎn)品名稱(subject),必填(建議中文,英文,數(shù)字,不能含有特殊字符)</div> </div> <div class="element"> <div class="etitle">付款金額:</div> <div class="einput"><input type="text" name="WIDtotal_fee" value="0.01"></div> <br> <div class="mark">注意:付款金額(total_fee),必填(格式如:1.00,請精確到分)</div> </div> <div class="element"> <div class="etitle">商品描述:</div> <div class="einput"><input type="text" name="WIDbody" value="即時(shí)到賬測試"></div> <br> <div class="mark">注意:商品描述(body),選填(建議中文,英文,數(shù)字,不能含有特殊字符)</div> </div> <div class="element"> <input type="submit" class="alisubmit" value ="確認(rèn)支付"> </div> </form> </div> <div class="footer"> <p class="footer-sub"> <a target="_blank">關(guān)于支付寶</a><span>|</span> <a target="_blank">商家中心</a><span>|</span> <a target="_blank">誠征英才</a><span>|</span> <a target="_blank">聯(lián)系我們</a><span>|</span> <a href="#" id="international" target="_blank">International Business</a><span>|</span> <a target="_blank">About Alipay</a> <br> <span>支付寶版權(quán)所有</span> <span class="footer-date">2004-2016</span> <span><a target="_blank">ICP證:滬B2-20150087</a></span> </p> </div> </body> <script> var even = document.getElementById("licode"); var showqrs = document.getElementById("showqrs"); even.onmouseover = function(){ showqrs.style.display = "block"; } even.onmouseleave = function(){ showqrs.style.display = "none"; } var out_trade_no = document.getElementById("out_trade_no"); //設(shè)定時(shí)間格式化函數(shù) Date.prototype.format = function (format) { var args = { "M+": this.getMonth() + 1, "d+": this.getDate(), "h+": this.getHours(), "m+": this.getMinutes(), "s+": this.getSeconds(), }; if (/(y+)/.test(format)) format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var i in args) { var n = args[i]; if (new RegExp("(" + i + ")").test(format)) format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? n : ("00" + n).substr(("" + n).length)); } return format; }; out_trade_no.value = 'test'+ new Date().format("yyyyMMddhhmmss"); </script> </html>
PHP控制代碼(其實(shí)就是把a(bǔ)lipay.config和alipayapi代碼集合在一起)
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2016/12/7 0007 * Time: 下午 3:30 */ namespace Home\Controller; use Think\Controller; use Vendor\Alidayu; class PayController extends Controller { public function alipayapi() { Vendor('Alipay.Corefunction'); Vendor('Alipay.Md5function'); Vendor('Alipay.Notify'); Vendor('Alipay.Submit'); /***************************請求參數(shù)******************************************/ //商戶訂單號,商戶網(wǎng)站訂單系統(tǒng)中唯一訂單號,必填 $out_trade_no = $_POST['WIDout_trade_no']; //訂單名稱,必填 $subject = $_POST['WIDsubject']; //付款金額,必填 $total_fee = $_POST['WIDtotal_fee']; //商品描述,可空 $body = $_POST['WIDbody']; /*******************************構(gòu)造配置來自alipay.config******************************************/ //↓↓↓↓↓↓↓↓↓↓請?jiān)谶@里配置您的基本信息↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ //合作身份者ID,簽約賬號,以2088開頭由16位純數(shù)字組成的字符串,查看地址:https://b.alipay.com/order/pidAndKey.htm $alipay_config['partner'] = '*******需要申請ID*****';//商戶ID //收款支付寶賬號,以2088開頭由16位純數(shù)字組成的字符串,一般情況下收款賬號就是簽約賬號 $alipay_config['seller_id'] = $alipay_config['partner']; // MD5密鑰,安全檢驗(yàn)碼,由數(shù)字和字母組成的32位字符串,查看地址:https://b.alipay.com/order/pidAndKey.htm $alipay_config['key'] = '******需要申請KEY*********'; //商戶KEY // 服務(wù)器異步通知頁面路徑 需http://格式的完整路徑,不能加?id=123這類2088421713316394自定義參數(shù),必須外網(wǎng)可以正常訪問 $alipay_config['notify_url'] = "http://商戶網(wǎng)址/create_direct_pay_by_user-PHP-UTF-8/notify_url.php"; // 頁面跳轉(zhuǎn)同步通知頁面路徑 需http://格式的完整路徑,不能加?id=123這類自定義參數(shù),必須外網(wǎng)可以正常訪問 $alipay_config['return_url'] = "http://商戶網(wǎng)址/create_direct_pay_by_user-PHP-UTF-8/return_url.php"; //簽名方式 $alipay_config['sign_type'] = strtoupper('MD5'); //字符編碼格式 目前支持 gbk 或 utf-8 $alipay_config['input_charset']= strtolower('utf-8'); //ca證書路徑地址,用于curl中ssl校驗(yàn) //請保證cacert.pem文件在當(dāng)前文件夾目錄中 $alipay_config['cacert'] = getcwd().'\\cacert.pem'; //訪問模式,根據(jù)自己的服務(wù)器是否支持ssl訪問,若支持請選擇https;若不支持請選擇http $alipay_config['transport'] = 'http'; // 支付類型 ,無需修改 $alipay_config['payment_type'] = "1"; // 產(chǎn)品類型,無需修改 $alipay_config['service'] = "create_direct_pay_by_user"; //↑↑↑↑↑↑↑↑↑↑請?jiān)谶@里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ //↓↓↓↓↓↓↓↓↓↓ 請?jiān)谶@里配置防釣魚信息,如果沒開通防釣魚功能,為空即可 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ // 防釣魚時(shí)間戳 若要使用請調(diào)用類文件submit中的query_timestamp函數(shù) $alipay_config['anti_phishing_key'] = ""; // 客戶端的IP地址 非局域網(wǎng)的外網(wǎng)IP地址,如:221.0.0.1 $alipay_config['exter_invoke_ip'] = ""; //↑↑↑↑↑↑↑↑↑↑請?jiān)谶@里配置防釣魚信息,如果沒開通防釣魚功能,為空即可 ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ /****************************************************************************/ //構(gòu)造要請求的參數(shù)數(shù)組,無需改動(dòng) $parameter = array( "service" => $alipay_config['service'], "partner" => $alipay_config['partner'], "seller_id" => $alipay_config['seller_id'], "payment_type" => $alipay_config['payment_type'], "notify_url" => $alipay_config['notify_url'], "return_url" => $alipay_config['return_url'], "anti_phishing_key"=>$alipay_config['anti_phishing_key'], "exter_invoke_ip"=>$alipay_config['exter_invoke_ip'], "out_trade_no" => $out_trade_no, "subject" => $subject, "total_fee" => $total_fee, "body" => $body, "_input_charset" => trim(strtolower($alipay_config['input_charset'])) //其他業(yè)務(wù)參數(shù)根據(jù)在線開發(fā)文檔,添加參數(shù).文檔地址:https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.kiX33I&treeId=62&articleId=103740&docType=1 //如"參數(shù)名"=>"參數(shù)值" ); //建立請求 $alipaySubmit = new \AlipaySubmit($alipay_config); $html_text = $alipaySubmit->buildRequestForm($parameter,"get", "確認(rèn)"); echo $html_text; } }
然后結(jié)果如圖
確認(rèn)支付后可能會(huì)出現(xiàn)支付寶錯(cuò)誤代碼 ILLEGAL_PARTNER
那是因?yàn)闆]有配置
$alipay_config['partner'] = '*******需要申請ID*****';//商戶ID //收款支付寶賬號,以2088開頭由16位純數(shù)字組成的字符串,一般情況下收款賬號就是簽約賬號 $alipay_config['seller_id'] = $alipay_config['partner']; // MD5密鑰,安全檢驗(yàn)碼,由數(shù)字和字母組成的32位字符串, //查看地址:https://b.alipay.com/order/pidAndKey.htm $alipay_config['key'] = '******需要申請KEY*********'; //商戶KEY
ID和KEY需要公司申請,個(gè)人是沒法申請的。
謝謝觀看~
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解ThinkPHP3.2.3驗(yàn)證碼顯示、刷新、校驗(yàn)
本篇文章主要介紹了ThinkPHP3.2.3驗(yàn)證碼顯示、刷新、校驗(yàn) ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-12-12PHP isset()與empty()的使用區(qū)別詳解
本文主要介紹了PHP isset()與empty()的使用區(qū)別。具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-02-02php+ajax導(dǎo)入大數(shù)據(jù)時(shí)產(chǎn)生的問題處理
介紹:就是想實(shí)現(xiàn)簡單的ajax上傳數(shù)據(jù),但是當(dāng)數(shù)據(jù)量較大的時(shí)候,問題就一個(gè)一個(gè)接著來了,其實(shí)數(shù)據(jù)也不是很大,就是csv格式數(shù)據(jù) 不到5w條數(shù)據(jù)。大小5M,一開始認(rèn)為這個(gè)很簡單,就是先上傳一下文件,然后讀取一下,存到數(shù)據(jù)庫就好了,結(jié)果,可能我比較菜,弄了半天做出這個(gè)功能。環(huán)境是linux.2014-06-06Codeigniter實(shí)現(xiàn)智能裁剪圖片的方法
這篇文章主要介紹了Codeigniter實(shí)現(xiàn)智能裁剪圖片的方法,可以做到裁剪后不失真,盡可能保留圖片主題含義。需要的朋友可以參考下2014-06-06PHP網(wǎng)頁游戲?qū)W習(xí)之Xnova(ogame)源碼解讀(六)
這篇文章主要介紹了PHP網(wǎng)頁游戲Xnova(ogame)源碼解讀的公共代碼,需要的朋友可以參考下2014-06-06PHP json_encode() 函數(shù)詳解及中文亂碼問題
這篇文章主要介紹了PHP json_encode() 函數(shù)詳解及中文亂碼問題的相關(guān)資料,需要的朋友可以參考下2015-11-11Swoole-1.7.22 版本已發(fā)布,修復(fù)PHP7相關(guān)問題
swoole-1.7.22 版本已發(fā)布,此版本是一個(gè)BUG修復(fù)版本,專門針對PHP7做了大量修改,可完美運(yùn)行于PHP7環(huán)境2015-12-12Laravel最佳分割路由文件(routes.php)的方式
本文是一篇關(guān)于Laravel分割路由文件(routes.php)的最佳方式教程文章,內(nèi)容介紹的很詳細(xì),學(xué)習(xí)Laravel的小伙伴可以參考學(xué)習(xí)。2016-08-08