PHP实现支付宝即时到账功能
本文實例為大家分享了PHP支付寶即時到賬功能的實現(xiàn)代碼,供大家參考,具體內(nèi)容如下
首先需要下載即時到賬交易接口,傳送門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文件復(fù)制進去如圖所示
然后,打開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里面的代碼,復(fù)制如下)
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 { 11 width: 100%; 12 min-width: 1200px; 13 height: auto; 14 padding: 0; 15 margin: 0; 16 font-family: "微軟雅黑"; 17 background-color: #242736 18 } 19 20 .header { 21 width: 100%; 22 margin: 0 auto; 23 height: 230px; 24 background-color: #fff 25 } 26 27 .container { 28 width: 100%; 29 min-width: 100px; 30 height: auto 31 } 32 33 .black { 34 background-color: #242736 35 } 36 37 .blue { 38 background-color: #0ae 39 } 40 41 .qrcode { 42 width: 1200px; 43 margin: 0 auto; 44 height: 30px; 45 background-color: #242736 46 } 47 48 .littlecode { 49 width: 16px; 50 height: 16px; 51 margin-top: 6px; 52 cursor: pointer; 53 float: right 54 } 55 56 .showqrs { 57 top: 30px; 58 position: absolute; 59 width: 100px; 60 margin-left: -65px; 61 height: 160px; 62 display: none 63 } 64 65 .shtoparrow { 66 width: 0; 67 height: 0; 68 margin-left: 65px; 69 border-left: 8px solid transparent; 70 border-right: 8px solid transparent; 71 border-bottom: 8px solid #e7e8eb; 72 margin-bottom: 0; 73 font-size: 0; 74 line-height: 0 75 } 76 77 .guanzhuqr { 78 text-align: center; 79 background-color: #e7e8eb; 80 border: 1px solid #e7e8eb 81 } 82 83 .guanzhuqr img { 84 margin-top: 10px; 85 width: 80px 86 } 87 88 .shmsg { 89 margin-left: 10px; 90 width: 80px; 91 height: 16px; 92 line-height: 16px; 93 font-size: 12px; 94 color: #242323; 95 text-align: center 96 } 97 98 .nav { 99 width: 1200px; 100 margin: 0 auto; 101 height: 70px; 102 } 103 104 .open, 105 .logo { 106 display: block; 107 float: left; 108 height: 40px; 109 width: 85px; 110 margin-top: 20px 111 } 112 113 .divier { 114 display: block; 115 float: left; 116 margin-left: 20px; 117 margin-right: 20px; 118 margin-top: 23px; 119 width: 1px; 120 height: 24px; 121 background-color: #d3d3d3 122 } 123 124 .open { 125 line-height: 30px; 126 font-size: 20px; 127 text-decoration: none; 128 color: #1a1a1a 129 } 130 131 .navbar { 132 float: right; 133 width: 200px; 134 height: 40px; 135 margin-top: 15px; 136 list-style: none 137 } 138 139 .navbar li { 140 float: left; 141 width: 100px; 142 height: 40px 143 } 144 145 .navbar li a { 146 display: inline-block; 147 width: 100px; 148 height: 40px; 149 line-height: 40px; 150 font-size: 16px; 151 color: #1a1a1a; 152 text-decoration: none; 153 text-align: center 154 } 155 156 .navbar li a:hover { 157 color: #00AAEE 158 } 159 160 .title { 161 width: 1200px; 162 margin: 0 auto; 163 height: 80px; 164 line-height: 80px; 165 font-size: 20px; 166 color: #FFF 167 } 168 169 .content { 170 width: 100%; 171 min-width: 1200px; 172 height: 660px; 173 background-color: #fff; 174 } 175 176 .alipayform { 177 width: 800px; 178 margin: 0 auto; 179 height: 600px; 180 border: 1px solid #0ae 181 } 182 183 .element { 184 width: 600px; 185 height: 80px; 186 margin-left: 100px; 187 font-size: 20px 188 } 189 190 .etitle, 191 .einput { 192 float: left; 193 height: 26px 194 } 195 196 .etitle { 197 width: 150px; 198 line-height: 26px; 199 text-align: right 200 } 201 202 .einput { 203 width: 200px; 204 margin-left: 20px 205 } 206 207 .einput input { 208 width: 398px; 209 height: 24px; 210 border: 1px solid #0ae; 211 font-size: 16px 212 } 213 214 .mark { 215 margin-top: 10px; 216 width: 500px; 217 height: 30px; 218 margin-left: 80px; 219 line-height: 30px; 220 font-size: 12px; 221 color: #999 222 } 223 224 .legend { 225 margin-left: 100px; 226 font-size: 24px 227 } 228 229 .alisubmit { 230 width: 400px; 231 height: 40px; 232 border: 0; 233 background-color: #0ae; 234 font-size: 16px; 235 color: #FFF; 236 cursor: pointer; 237 margin-left: 170px 238 } 239 240 .footer { 241 width: 100%; 242 height: 120px; 243 background-color: #242735 244 } 245 246 .footer-sub a, 247 span { 248 color: #808080; 249 font-size: 12px; 250 text-decoration: none 251 } 252 253 .footer-sub a:hover { 254 color: #00aeee 255 } 256 257 .footer-sub span { 258 margin: 0 3px 259 } 260 261 .footer-sub { 262 padding-top: 40px; 263 height: 20px; 264 width: 600px; 265 margin: 0 auto; 266 text-align: center 267 } 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 請掃碼關(guān)注 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="https://www.alipay.com/" class="logo"><img src="img/alipay_logo.png" height="30px"></a> 294 <span class="divier"></span> 295 <a href="http://open.alipay.com/platform/home.htm" class="open" target="_blank">開放平臺</a> 296 <ul class="navbar"> 297 <li><a href="https://doc.open.alipay.com/doc2/detail?treeId=62&articleId=103566&docType=1" target="_blank">在線文檔</a></li> 298 <li><a href="https://cschannel.alipay.com/portal.htm?sourceId=213" target="_blank">技術(shù)支持</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).必填(建議是英文字母和數(shù)字,不能含有特殊字符)</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">注意:產(chǎn)品名稱(subject),必填(建議中文,英文,數(shù)字,不能含有特殊字符)</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),選填(建議中文,英文,數(shù)字,不能含有特殊字符)</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="http://ab.alipay.com/i/index.htm" target="_blank">關(guān)于支付寶</a><span>|</span> 351 <a href="https://e.alipay.com/index.htm" target="_blank">商家中心</a><span>|</span> 352 <a href="https://job.alibaba.com/zhaopin/index.htm" target="_blank">誠征英才</a><span>|</span> 353 <a href="http://ab.alipay.com/i/lianxi.htm" target="_blank">聯(lián)系我們</a><span>|</span> 354 <a href="#" id="international" target="_blank">International Business</a><span>|</span> 355 <a href="http://ab.alipay.com/i/jieshao.htm#en" target="_blank">About Alipay</a> 356 <br> 357 <span>支付寶版權(quán)所有</span> 358 <span class="footer-date">2004-2016</span> 359 <span><a href="http://fun.alipay.com/certificate/jyxkz.htm" target="_blank">ICP證:滬B2-20150087</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() { 367 showqrs.style.display = "block"; 368 } 369 even.onmouseleave = function() { 370 showqrs.style.display = "none"; 371 } 372 373 var out_trade_no = document.getElementById("out_trade_no"); 374 375 //設(shè)定時間格式化函數(shù) 376 Date.prototype.format = function(format) { 377 var args = { 378 "M+": this.getMonth() + 1, 379 "d+": this.getDate(), 380 "h+": this.getHours(), 381 "m+": this.getMinutes(), 382 "s+": this.getSeconds(), 383 }; 384 if (/(y+)/.test(format)) 385 format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); 386 for (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 } 391 return 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 { 6 public function alipayapi() { 7 Vendor('Alipay.Corefunction'); 8 Vendor('Alipay.Md5function'); 9 Vendor('Alipay.Notify'); 10 Vendor('Alipay.Submit'); 11 12 //請求參數(shù) 13 //商戶訂單號,商戶網(wǎng)站訂單系統(tǒng)中唯一訂單號,必填 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 //構(gòu)造配置來自alipay.config 26 //合作身份者ID,簽約賬號,以2088開頭由16位純數(shù)字組成的字符串 27 //查看地址:https://b.alipay.com/order/pidAndKey.htm 28 $alipay_config['partner'] = '*******需要申請ID*****'; //商戶ID 29 30 //收款支付寶賬號,以2088開頭由16位純數(shù)字組成的字符串,一般情況下收款賬號就是簽約賬號 31 $alipay_config['seller_id'] = $alipay_config['partner']; 32 33 // MD5密鑰,安全檢驗碼,由數(shù)字和字母組成的32位字符串 34 // 查看地址:https://b.alipay.com/order/pidAndKey.htm 35 $alipay_config['key'] = '******需要申請KEY*********'; //商戶KEY 36 37 // 服務(wù)器異步通知頁面路徑 需http://格式的完整路徑,不能加?id=123這類2088421713316394自定義參數(shù),必須外網(wǎng)可以正常訪問 38 $alipay_config['notify_url'] = "http://商戶網(wǎng)址/create_direct_pay_by_user-PHP-UTF-8/notify_url.php"; 39 40 // 頁面跳轉(zhuǎn)同步通知頁面路徑 需http://格式的完整路徑,不能加?id=123這類自定義參數(shù),必須外網(wǎng)可以正常訪問 41 $alipay_config['return_url'] = "http://商戶網(wǎng)址/create_direct_pay_by_user-PHP-UTF-8/return_url.php"; 42 43 //簽名方式 44 $alipay_config['sign_type'] = strtoupper('MD5'); 45 46 //字符編碼格式 目前支持 gbk 或 utf-8 47 $alipay_config['input_charset'] = strtolower('utf-8'); 48 49 //ca證書路徑地址,用于curl中ssl校驗 50 //請保證cacert.pem文件在當(dāng)前文件夾目錄中 51 $alipay_config['cacert'] = getcwd() . '\\cacert.pem'; 52 53 //訪問模式,根據(jù)自己的服務(wù)器是否支持ssl訪問,若支持請選擇https;若不支持請選擇http 54 $alipay_config['transport'] = 'http'; 55 56 // 支付類型 ,無需修改 57 $alipay_config['payment_type'] = "1"; 58 59 // 產(chǎn)品類型,無需修改 60 $alipay_config['service'] = "create_direct_pay_by_user"; 61 62 // 防釣魚時間戳 若要使用請調(diào)用類文件submit中的query_timestamp函數(shù) 63 $alipay_config['anti_phishing_key'] = ""; 64 65 // 客戶端的IP地址 非局域網(wǎng)的外網(wǎng)IP地址,如:221.0.0.1 66 $alipay_config['exter_invoke_ip'] = ""; 67 68 //構(gòu)造要請求的參數(shù)數(shù)組,無需改動 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 //其他業(yè)務(wù)參數(shù)根據(jù)在線開發(fā)文檔,添加參數(shù). 85 //文檔地址:https://doc.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.kiX33I&treeId=62&articleId=103740&docType=1 86 //如"參數(shù)名"=>"參數(shù)值" 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 }然后結(jié)果如圖
確認支付后可能會出現(xiàn)支付寶錯誤代碼 ILLEGAL_PARTNER
那是因為沒有配置
1 $alipay_config['partner'] = '*******需要申請ID*****'; //商戶ID 2 3 //收款支付寶賬號,以2088開頭由16位純數(shù)字組成的字符串,一般情況下收款賬號就是簽約賬號 4 $alipay_config['seller_id'] = $alipay_config['partner']; 5 6 // MD5密鑰,安全檢驗碼,由數(shù)字和字母組成的32位字符串 7 // 查看地址:https://b.alipay.com/order/pidAndKey.htm 8 $alipay_config['key'] = '******需要申請KEY*********'; //商戶KEY?
轉(zhuǎn)載于:https://www.cnblogs.com/lmaster/p/6381022.html
總結(jié)
以上是生活随笔為你收集整理的PHP实现支付宝即时到账功能的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: UVa 242 邮票和信封(完全背包)
- 下一篇: Java IO学习笔记(四)打印流