这篇文章主要为大家详细介绍了PHP实现支付宝即时到账功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
具体内容如下
首先需要下载即时到账交易接口,传送门https://doc.open.alipay.com/doc2/detail?treeId=62&articleId=103566&docType=1
选择(create_direct_pay_by_user)
然后解压,选择MD5签名版本的文件
此文件夹里有个叫create_direct_pay_by_user-PHP-UTF-8的文件,我们就用这个文件~
接着打开文件后如图
并把文件夹lib里面的四个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,把以上四个php文件复制进去如图所示
然后,打开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代码:(这个代码其实就是index里面的代码,复制如下)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>支付宝即时到账交易接口</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 } .pier { 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> <p class="header"> <p class="container black"> <p class="qrcode"> <p class="littlecode"> <img src="/static/imghwm/default1.png" data-src="img/little_qrcode.jpg" class="lazy" style="max-width:90%" id="licode" alt="PHP實作支付寶即時到帳功能" > <p class="showqrs" id="showqrs"> <p class="shtoparrow"></p> <p class="guanzhuqr"> <img src="/static/imghwm/default1.png" data-src="img/guanzhu_qrcode.png" class="lazy" style="max-width:90%" alt="PHP實作支付寶即時到帳功能" > <p class="shmsg" style="margin-top:5px;"> 请扫码关注 </p> <p class="shmsg" style="margin-bottom:5px;"> 接收重要信息 </p> </p> </p> </p> </p> </p> <p class="container"> <p class="nav"> <a href="https://www.alipay.com/" class="logo"><img src="/static/imghwm/default1.png" data-src="img/alipay_logo.png" class="lazy" height="30px" alt="PHP實作支付寶即時到帳功能" ></a> <span class="pier"></span> <a href="http://open.alipay.com/platform/home.htm" class="open" target="_blank">开放平台</a> <ul class="navbar"> <li><a href="https://doc.open.alipay.com/doc2/detail?treeId=62&articleId=103566&docType=1" target="_blank">在线文档</a></li> <li><a href="https://cschannel.alipay.com/portal.htm?sourceId=213" target="_blank">技术支持</a></li> </ul> </p> </p> <p class="container blue"> <p class="title">支付宝即时到账(create_direct_pay_by_user)</p> </p> </p> <p class="content"> <form action="{:U('home/Pay/alipayapi')}" class="alipayform" method="post" target="_blank"> <p class="element" style="margin-top:60px;"> <p class="legend">支付宝即时到账交易接口快速通道 </p> </p> <p class="element"> <p class="etitle">商户订单号:</p> <p class="einput"><input type="text" name="WIDout_trade_no" id="out_trade_no"></p> <br> <p class="mark">注意:商户订单号(out_trade_no).必填(建议是英文字母和数字,不能含有特殊字符)</p> </p> <p class="element"> <p class="etitle">商品名称:</p> <p class="einput"><input type="text" name="WIDsubject" value="test商品123"></p> <br> <p class="mark">注意:产品名称(subject),必填(建议中文,英文,数字,不能含有特殊字符)</p> </p> <p class="element"> <p class="etitle">付款金额:</p> <p class="einput"><input type="text" name="WIDtotal_fee" value="0.01"></p> <br> <p class="mark">注意:付款金额(total_fee),必填(格式如:1.00,请精确到分)</p> </p> <p class="element"> <p class="etitle">商品描述:</p> <p class="einput"><input type="text" name="WIDbody" value="即时到账测试"></p> <br> <p class="mark">注意:商品描述(body),选填(建议中文,英文,数字,不能含有特殊字符)</p> </p> <p class="element"> <input type="submit" class="alisubmit" value ="确认支付"> </p> </form> </p> <p class="footer"> <p class="footer-sub"> <a href="http://ab.alipay.com/i/index.htm" target="_blank">关于支付宝</a><span>|</span> <a href="https://e.alipay.com/index.htm" target="_blank">商家中心</a><span>|</span> <a href="https://job.alibaba.com/zhaopin/index.htm" target="_blank">诚征英才</a><span>|</span> <a href="http://ab.alipay.com/i/lianxi.htm" target="_blank">联系我们</a><span>|</span> <a href="#" id="international" target="_blank">International Business</a><span>|</span> <a href="http://ab.alipay.com/i/jieshao.htm#en" target="_blank">About Alipay</a> <br> <span>支付宝版权所有</span> <span class="footer-date">2004-2016</span> <span><a href="http://fun.alipay.com/certificate/jyxkz.htm" target="_blank">ICP证:沪B2-20150087</a></span> </p> </p> </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"); //设定时间格式化函数 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控制代码(其实就是把alipay.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'); /***************************请求参数******************************************/ //商户订单号,商户网站订单系统中唯一订单号,必填 $out_trade_no = $_POST['WIDout_trade_no']; //订单名称,必填 $subject = $_POST['WIDsubject']; //付款金额,必填 $total_fee = $_POST['WIDtotal_fee']; //商品描述,可空 $body = $_POST['WIDbody']; /*******************************构造配置来自alipay.config******************************************/ //↓↓↓↓↓↓↓↓↓↓请在这里配置您的基本信息↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ //合作身份者ID,签约账号,以2088开头由16位纯数字组成的字符串,查看地址:https://b.alipay.com/order/pidAndKey.htm $alipay_config['partner'] = '*******需要申请ID*****';//商户ID //收款支付宝账号,以2088开头由16位纯数字组成的字符串,一般情况下收款账号就是签约账号 $alipay_config['seller_id'] = $alipay_config['partner']; // MD5密钥,安全检验码,由数字和字母组成的32位字符串,查看地址:https://b.alipay.com/order/pidAndKey.htm $alipay_config['key'] = '******需要申请KEY*********'; //商户KEY // 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类2088421713316394自定义参数,必须外网可以正常访问 $alipay_config['notify_url'] = "http://商户网址/create_direct_pay_by_user-PHP-UTF-8/notify_url.php"; // 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问 $alipay_config['return_url'] = "http://商户网址/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校验 //请保证cacert.pem文件在当前文件夹目录中 $alipay_config['cacert'] = getcwd().'\\cacert.pem'; //访问模式,根据自己的服务器是否支持ssl访问,若支持请选择https;若不支持请选择http $alipay_config['transport'] = 'http'; // 支付类型 ,无需修改 $alipay_config['payment_type'] = "1"; // 产品类型,无需修改 $alipay_config['service'] = "create_direct_pay_by_user"; //↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ //↓↓↓↓↓↓↓↓↓↓ 请在这里配置防钓鱼信息,如果没开通防钓鱼功能,为空即可 ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ // 防钓鱼时间戳 若要使用请调用类文件submit中的query_timestamp函数 $alipay_config['anti_phishing_key'] = ""; // 客户端的IP地址 非局域网的外网IP地址,如:221.0.0.1 $alipay_config['exter_invoke_ip'] = ""; //↑↑↑↑↑↑↑↑↑↑请在这里配置防钓鱼信息,如果没开通防钓鱼功能,为空即可 ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ /****************************************************************************/ //构造要请求的参数数组,无需改动 $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'])) //其他业务参数根据在线开发文档,添加参数.文档地址:https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.kiX33I&treeId=62&articleId=103740&docType=1 //如"参数名"=>"参数值" ); //建立请求 $alipaySubmit = new \AlipaySubmit($alipay_config); $html_text = $alipaySubmit->buildRequestForm($parameter,"get", "确认"); echo $html_text; } }
然后结果如图
确认支付后可能会出现支付宝错误代码 ILLEGAL_PARTNER
那是因为没有配置
$alipay_config['partner'] = '*******需要申请ID*****';//商户ID //收款支付宝账号,以2088开头由16位纯数字组成的字符串,一般情况下收款账号就是签约账号 $alipay_config['seller_id'] = $alipay_config['partner']; // MD5密钥,安全检验码,由数字和字母组成的32位字符串, //查看地址:https://b.alipay.com/order/pidAndKey.htm $alipay_config['key'] = '******需要申请KEY*********'; //商户KEY
ID和KEY需要公司申请,个人是没法申请的。
以上就是本文的全部内容,希望对大家的学习有所帮助。
相关推荐:
以上是PHP實作支付寶即時到帳功能的詳細內容。更多資訊請關注PHP中文網其他相關文章!

PHP和Python各有優勢,選擇應基於項目需求。 1.PHP適合web開發,語法簡單,執行效率高。 2.Python適用於數據科學和機器學習,語法簡潔,庫豐富。

PHP不是在消亡,而是在不斷適應和進化。 1)PHP從1994年起經歷多次版本迭代,適應新技術趨勢。 2)目前廣泛應用於電子商務、內容管理系統等領域。 3)PHP8引入JIT編譯器等功能,提升性能和現代化。 4)使用OPcache和遵循PSR-12標準可優化性能和代碼質量。

PHP的未來將通過適應新技術趨勢和引入創新特性來實現:1)適應云計算、容器化和微服務架構,支持Docker和Kubernetes;2)引入JIT編譯器和枚舉類型,提升性能和數據處理效率;3)持續優化性能和推廣最佳實踐。

在PHP中,trait適用於需要方法復用但不適合使用繼承的情況。 1)trait允許在類中復用方法,避免多重繼承複雜性。 2)使用trait時需注意方法衝突,可通過insteadof和as關鍵字解決。 3)應避免過度使用trait,保持其單一職責,以優化性能和提高代碼可維護性。

依賴注入容器(DIC)是一種管理和提供對象依賴關係的工具,用於PHP項目中。 DIC的主要好處包括:1.解耦,使組件獨立,代碼易維護和測試;2.靈活性,易替換或修改依賴關係;3.可測試性,方便注入mock對象進行單元測試。

SplFixedArray在PHP中是一種固定大小的數組,適用於需要高性能和低內存使用量的場景。 1)它在創建時需指定大小,避免動態調整帶來的開銷。 2)基於C語言數組,直接操作內存,訪問速度快。 3)適合大規模數據處理和內存敏感環境,但需謹慎使用,因其大小固定。

PHP通過$\_FILES變量處理文件上傳,確保安全性的方法包括:1.檢查上傳錯誤,2.驗證文件類型和大小,3.防止文件覆蓋,4.移動文件到永久存儲位置。

JavaScript中處理空值可以使用NullCoalescingOperator(??)和NullCoalescingAssignmentOperator(??=)。 1.??返回第一個非null或非undefined的操作數。 2.??=將變量賦值為右操作數的值,但前提是該變量為null或undefined。這些操作符簡化了代碼邏輯,提高了可讀性和性能。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

SublimeText3 Linux新版
SublimeText3 Linux最新版

Dreamweaver Mac版
視覺化網頁開發工具

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中