700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > PHP实现支付宝即时到账功能

PHP实现支付宝即时到账功能

时间:2022-10-03 20:49:53

相关推荐

PHP实现支付宝即时到账功能

本文实例为大家分享了PHP支付宝即时到账功能的实现代码,供大家参考,具体内容如下

首先需要下载即时到账交易接口,传送门https://doc./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里面的代码,复制如下)

1 <!DOCTYPE html> 2 <html lang="en"> 3 4 <head> 5<meta charset="UTF-8"> 6<title>支付宝即时到账交易接口</title> 7 </head> 8 <style> 9 html, 10 body {11width: 100%; 12min-width: 1200px; 13height: auto; 14padding: 0; 15margin: 0; 16font-family: "微软雅黑"; 17background-color: #242736 18 } 19 20 .header {21width: 100%; 22margin: 0 auto; 23height: 230px; 24background-color: #fff 25 } 26 27 .container {28width: 100%; 29min-width: 100px; 30height: auto 31 } 32 33 .black {34background-color: #242736 35 } 36 37 .blue {38background-color: #0ae 39 } 40 41 .qrcode {42width: 1200px; 43margin: 0 auto; 44height: 30px; 45background-color: #242736 46 } 47 48 .littlecode {49width: 16px; 50height: 16px; 51margin-top: 6px; 52cursor: pointer; 53float: right 54 } 55 56 .showqrs {57top: 30px; 58position: absolute; 59width: 100px; 60margin-left: -65px; 61height: 160px; 62display: none 63 } 64 65 .shtoparrow {66width: 0; 67height: 0; 68margin-left: 65px; 69border-left: 8px solid transparent; 70border-right: 8px solid transparent; 71border-bottom: 8px solid #e7e8eb; 72margin-bottom: 0; 73font-size: 0; 74line-height: 0 75 } 76 77 .guanzhuqr {78text-align: center; 79background-color: #e7e8eb; 80border: 1px solid #e7e8eb 81 } 82 83 .guanzhuqr img {84margin-top: 10px; 85width: 80px 86 } 87 88 .shmsg {89margin-left: 10px; 90width: 80px; 91height: 16px; 92line-height: 16px; 93font-size: 12px; 94color: #242323; 95text-align: center 96 } 97 98 .nav {99width: 1200px;100margin: 0 auto;101height: 70px;102 }103 104 .open,105 .logo {106display: block;107float: left;108height: 40px;109width: 85px;110margin-top: 20px111 }112 113 .divier {114display: block;115float: left;116margin-left: 20px;117margin-right: 20px;118margin-top: 23px;119width: 1px;120height: 24px;121background-color: #d3d3d3122 }123 124 .open {125line-height: 30px;126font-size: 20px;127text-decoration: none;128color: #1a1a1a129 }130 131 .navbar {132float: right;133width: 200px;134height: 40px;135margin-top: 15px;136list-style: none137 }138 139 .navbar li {140float: left;141width: 100px;142height: 40px143 }144 145 .navbar li a {146display: inline-block;147width: 100px;148height: 40px;149line-height: 40px;150font-size: 16px;151color: #1a1a1a;152text-decoration: none;153text-align: center154 }155 156 .navbar li a:hover {157color: #00AAEE158 }159 160 .title {161width: 1200px;162margin: 0 auto;163height: 80px;164line-height: 80px;165font-size: 20px;166color: #FFF167 }168 169 .content {170width: 100%;171min-width: 1200px;172height: 660px;173background-color: #fff;174 }175 176 .alipayform {177width: 800px;178margin: 0 auto;179height: 600px;180border: 1px solid #0ae181 }182 183 .element {184width: 600px;185height: 80px;186margin-left: 100px;187font-size: 20px188 }189 190 .etitle,191 .einput {192float: left;193height: 26px194 }195 196 .etitle {197width: 150px;198line-height: 26px;199text-align: right200 }201 202 .einput {203width: 200px;204margin-left: 20px205 }206 207 .einput input {208width: 398px;209height: 24px;210border: 1px solid #0ae;211font-size: 16px212 }213 214 .mark {215margin-top: 10px;216width: 500px;217height: 30px;218margin-left: 80px;219line-height: 30px;220font-size: 12px;221color: #999222 }223 224 .legend {225margin-left: 100px;226font-size: 24px227 }228 229 .alisubmit {230width: 400px;231height: 40px;232border: 0;233background-color: #0ae;234font-size: 16px;235color: #FFF;236cursor: pointer;237margin-left: 170px238 }239 240 .footer {241width: 100%;242height: 120px;243background-color: #242735244 }245 246 .footer-sub a,247 span {248color: #808080;249font-size: 12px;250text-decoration: none251 }252 253 .footer-sub a:hover {254color: #00aeee255 }256 257 .footer-sub span {258margin: 0 3px259 }260 261 .footer-sub {262padding-top: 40px;263height: 20px;264width: 600px;265margin: 0 auto;266text-align: center267 }268 </style>269 270 <body>271<div class="header">272 <div class="container black">273 <div class="qrcode">274 <div class="littlecode">275 <img width="16px" src="img/little_qrcode.jpg" id="licode">276 <div class="showqrs" id="showqrs">277<div class="shtoparrow"></div>278<div class="guanzhuqr">279 <img src="img/guanzhu_qrcode.png" width="80">280 <div class="shmsg" style="margin-top:5px;">281 请扫码关注282 </div>283 <div class="shmsg" style="margin-bottom:5px;">284 接收重要信息285 </div>286</div>287 </div>288 </div>289 </div>290 </div>291 <div class="container">292 <div class="nav">293 <a href="/" class="logo"><img src="img/alipay_logo.png" height="30px"></a>294 <span class="divier"></span>295 <a href="/platform/home.htm" class="open" target="_blank">开放平台</a>296 <ul class="navbar">297 <li><a href="https://doc./doc2/detail?treeId=62&articleId=103566&docType=1" target="_blank">在线文档</a></li>298 <li><a href="/portal.htm?sourceId=213" target="_blank">技术支持</a></li>299 </ul>300 </div>301 </div>302 <div class="container blue">303 <div class="title">支付宝即时到账(create_direct_pay_by_user)</div>304 </div>305</div>306<div class="content">307 <form action="{:U('home/Pay/alipayapi')}" class="alipayform" method="post" target="_blank">308 <div class="element" style="margin-top:60px;">309 <div class="legend">支付宝即时到账交易接口快速通道 </div>310 </div>311 <div class="element">312 <div class="etitle">商户订单号:</div>313 <div class="einput">314 <input type="text" name="WIDout_trade_no" id="out_trade_no">315 </div>316 <br>317 <div class="mark">注意:商户订单号(out_trade_no).必填(建议是英文字母和数字,不能含有特殊字符)</div>318 </div>319 <div class="element">320 <div class="etitle">商品名称:</div>321 <div class="einput">322 <input type="text" name="WIDsubject" value="test商品123">323 </div>324 <br>325 <div class="mark">注意:产品名称(subject),必填(建议中文,英文,数字,不能含有特殊字符)</div>326 </div>327 <div class="element">328 <div class="etitle">付款金额:</div>329 <div class="einput">330 <input type="text" name="WIDtotal_fee" value="0.01">331 </div>332 <br>333 <div class="mark">注意:付款金额(total_fee),必填(格式如:1.00,请精确到分)</div>334 </div>335 <div class="element">336 <div class="etitle">商品描述:</div>337 <div class="einput">338 <input type="text" name="WIDbody" value="即时到账测试">339 </div>340 <br>341 <div class="mark">注意:商品描述(body),选填(建议中文,英文,数字,不能含有特殊字符)</div>342 </div>343 <div class="element">344 <input type="submit" class="alisubmit" value="确认支付">345 </div>346 </form>347</div>348<div class="footer">349 <p class="footer-sub">350 <a href="/i/index.htm" target="_blank">关于支付宝</a><span>|</span>351 <a href="/index.htm" target="_blank">商家中心</a><span>|</span>352 <a href="/zhaopin/index.htm" target="_blank">诚征英才</a><span>|</span>353 <a href="/i/lianxi.htm" target="_blank">联系我们</a><span>|</span>354 <a href="#" id="international" target="_blank">International Business</a><span>|</span>355 <a href="/i/jieshao.htm#en" target="_blank">About Alipay</a>356 <br>357 <span>支付宝版权所有</span>358 <span class="footer-date">-</span>359 <span><a href="/certificate/jyxkz.htm" target="_blank">ICP证:沪B2-0087</a></span>360 </p>361</div>362 </body>363 <script>364 var even = document.getElementById("licode");365 var showqrs = document.getElementById("showqrs");366 even.onmouseover = function() {367showqrs.style.display = "block";368 }369 even.onmouseleave = function() {370showqrs.style.display = "none";371 }372 373 var out_trade_no = document.getElementById("out_trade_no");374 375 //设定时间格式化函数376 Date.prototype.format = function(format) {377var args = {378 "M+": this.getMonth() + 1,379 "d+": this.getDate(),380 "h+": this.getHours(),381 "m+": this.getMinutes(),382 "s+": this.getSeconds(),383};384if (/(y+)/.test(format))385 format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));386for (var i in args) {387 var n = args[i];388 if (new RegExp("(" + i + ")").test(format))389 format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? n : ("00" + n).substr(("" + n).length));390}391return format;392 };393 394 out_trade_no.value = 'test' + new Date().format("yyyyMMddhhmmss");395 </script>396 397 </html>

PHP控制代码(其实就是把alipay.config和alipayapi代码集合在一起)

1 <?php 2 namespace Home\Controller; 3 use Think\Controller; 4 5 class PayController extends Controller { 6public function alipayapi() { 7 Vendor('Alipay.Corefunction'); 8 Vendor('Alipay.Md5function'); 9 Vendor('Alipay.Notify');10 Vendor('Alipay.Submit');11 12 //请求参数13 //商户订单号,商户网站订单系统中唯一订单号,必填14 $out_trade_no = $_POST['WIDout_trade_no'];15 16 //订单名称,必填17 $subject = $_POST['WIDsubject'];18 19 //付款金额,必填20 $total_fee = $_POST['WIDtotal_fee'];21 22 //商品描述,可空23 $body = $_POST['WIDbody'];24 25 //构造配置来自alipay.config26 //合作身份者ID,签约账号,以2088开头由16位纯数字组成的字符串27 //查看地址:/order/pidAndKey.htm28 $alipay_config['partner'] = '*******需要申请ID*****'; //商户ID29 30 //收款支付宝账号,以2088开头由16位纯数字组成的字符串,一般情况下收款账号就是签约账号31 $alipay_config['seller_id'] = $alipay_config['partner'];32 33 // MD5密钥,安全检验码,由数字和字母组成的32位字符串34 // 查看地址:/order/pidAndKey.htm35 $alipay_config['key'] = '******需要申请KEY*********'; //商户KEY36 37 // 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类2088421713316394自定义参数,必须外网可以正常访问38 $alipay_config['notify_url'] = "http://商户网址/create_direct_pay_by_user-PHP-UTF-8/notify_url.php";39 40 // 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问41 $alipay_config['return_url'] = "http://商户网址/create_direct_pay_by_user-PHP-UTF-8/return_url.php";42 43 //签名方式44 $alipay_config['sign_type'] = strtoupper('MD5');45 46 //字符编码格式 目前支持 gbk 或 utf-847 $alipay_config['input_charset'] = strtolower('utf-8');48 49 //ca证书路径地址,用于curl中ssl校验50 //请保证cacert.pem文件在当前文件夹目录中51 $alipay_config['cacert'] = getcwd() . '\\cacert.pem';52 53 //访问模式,根据自己的服务器是否支持ssl访问,若支持请选择https;若不支持请选择http54 $alipay_config['transport'] = 'http';55 56 // 支付类型 ,无需修改57 $alipay_config['payment_type'] = "1";58 59 // 产品类型,无需修改60 $alipay_config['service'] = "create_direct_pay_by_user";61 62 // 防钓鱼时间戳 若要使用请调用类文件submit中的query_timestamp函数63 $alipay_config['anti_phishing_key'] = "";64 65 // 客户端的IP地址 非局域网的外网IP地址,如:221.0.0.166 $alipay_config['exter_invoke_ip'] = "";67 68 //构造要请求的参数数组,无需改动69 $parameter = array(70 "service" => $alipay_config['service'],71 "partner" => $alipay_config['partner'],72 "seller_id" => $alipay_config['seller_id'],73 "payment_type"=> $alipay_config['payment_type'],74 "notify_url" => $alipay_config['notify_url'],75 "return_url" => $alipay_config['return_url'],76 77 "anti_phishing_key" => $alipay_config['anti_phishing_key'],78 "exter_invoke_ip" => $alipay_config['exter_invoke_ip'],79 "out_trade_no"=> $out_trade_no,80 "subject" => $subject,81 "total_fee" => $total_fee,82 "body" => $body,83 "_input_charset" => trim(strtolower($alipay_config['input_charset'])),84 //其他业务参数根据在线开发文档,添加参数.85 //文档地址:https://doc./doc2/detail.htm?spm=a219a.7629140.0.0.kiX33I&treeId=62&articleId=103740&docType=186 //如"参数名"=>"参数值"87 88 );89 90 //建立请求91 $alipaySubmit = new \AlipaySubmit($alipay_config);92 $html_text = $alipaySubmit->buildRequestForm($parameter, "get", "确认");93 echo $html_text;94 95}96 97 }

然后结果如图

确认支付后可能会出现支付宝错误代码 ILLEGAL_PARTNER

那是因为没有配置

1 $alipay_config['partner'] = '*******需要申请ID*****'; //商户ID2 3 //收款支付宝账号,以2088开头由16位纯数字组成的字符串,一般情况下收款账号就是签约账号4 $alipay_config['seller_id'] = $alipay_config['partner'];5 6 // MD5密钥,安全检验码,由数字和字母组成的32位字符串7 // 查看地址:/order/pidAndKey.htm8 $alipay_config['key'] = '******需要申请KEY*********'; //商户KEY

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。