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

本文实例为大家分享了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里面的代码,复制如下)

  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                                 请扫码关注
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">技术支持</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="http://ab.alipay.com/i/index.htm" target="_blank">关于支付宝</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">联系我们</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>支付宝版权所有</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 //设定时间格式化函数
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         //请求参数
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.config
26         //合作身份者ID,签约账号,以2088开头由16位纯数字组成的字符串
27         //查看地址:https://b.alipay.com/order/pidAndKey.htm
28         $alipay_config['partner'] = '*******需要申请ID*****'; //商户ID
29 
30         //收款支付宝账号,以2088开头由16位纯数字组成的字符串,一般情况下收款账号就是签约账号
31         $alipay_config['seller_id'] = $alipay_config['partner'];
32 
33         // MD5密钥,安全检验码,由数字和字母组成的32位字符串
34         // 查看地址:https://b.alipay.com/order/pidAndKey.htm
35         $alipay_config['key'] = '******需要申请KEY*********'; //商户KEY
36 
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-8
47         $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;若不支持请选择http
54         $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.1
66         $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.open.alipay.com/doc2/detail.htm?spm=a219a.7629140.0.0.kiX33I&treeId=62&articleId=103740&docType=1
86             //如"参数名"=>"参数值"
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*****'; //商户ID
2 
3         //收款支付宝账号,以2088开头由16位纯数字组成的字符串,一般情况下收款账号就是签约账号
4         $alipay_config['seller_id'] = $alipay_config['partner'];
5 
6         // MD5密钥,安全检验码,由数字和字母组成的32位字符串
7         // 查看地址:https://b.alipay.com/order/pidAndKey.htm
8         $alipay_config['key'] = '******需要申请KEY*********'; //商户KEY

 

posted @ 2017-02-09 10:59  梁大师  阅读(1128)  评论(0编辑  收藏  举报