Home Backend Development PHP Tutorial How to access Alipay's instant payment function with PHP

How to access Alipay's instant payment function with PHP

Jun 01, 2018 am 11:07 AM
php Function method

这篇文章主要介绍了PHP 接入支付宝即时到账功能,非常不错,具有参考借鉴价值,需要的朋友可以参考下

首先请到支付宝那边申请一个及时到账的接口账户,需要提交相关材料申请。然后根据即时到账的API文档进行接入。API文档提供了各种语言版本的demo,我这里是下的php版demo,然后再进行相关修改操作。你也可以将demo版本的代码重新整合,我这里暂时为了走通充值及时到账流程,就不进行代码重构了。

API文档:https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7386797.0.0.eZb8FX&treeId=108&articleId=103950&docType=1

前台表单提交

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

<form action="/pay/alipayapi.php" class="tm-panel-small uk-form uk-form-stacked" method="post" target="_blank">                            

  <p class="element" style="margin-top:60px;">                                                             

  </p>                                                                                 

  <p class="uk-form-row">                                                                       

  <p class="uk-form-controls">                                                                     

      <label for="form-username" class="uk-form-label" style="display:inline-block;padding: 0 10px;vertical-align: middle;margin-left:-20px;">订单序号:</label>   

      <input class="uk-form-width-large" type="text" name="WIDout_trade_no" id="out_trade_no" value="<?php echo trim($_GET[&#39;orderid&#39;]);?>" >            

      <br>                                                                              

  </p>                                                                                 

  </p>                                                                                 

  <p class="uk-form-row">                                                                       

  <p class="uk-form-controls">                                                                     

      <label for="form-username" class="uk-form-label" style="display:inline-block;padding: 0 10px;vertical-align: middle;margin-left:-20px;">商品名称:</label>   

      <input class="uk-form-width-large" type="text" name="WIDsubject" value="<?php echo trim($_GET[&#39;orderid&#39;]);?>" >                        

      <br>                                                                              

  </p>                                                                                 

  </p>                                                                                 

  <p class="uk-form-row">                                                                       

  <p class="uk-form-controls">                                                                     

      <label for="form-username" class="uk-form-label" style="display:inline-block;padding: 0 10px;vertical-align: middle;margin-left:-20px;">付款金额:</label>   

      <input class="uk-form-width-large" type="text" name="WIDtotal_fee" value="<?php echo trim($_GET[&#39;amt&#39;]);?>">                          

      <br>                                                                              

  </p>                                                                                 

  </p>                                                                                 

  <p class="uk-form-row">                                                                       

  <p class="uk-form-controls">                                                                     

      <label for="form-username" class="uk-form-label" style="display:inline-block;padding: 0 10px;vertical-align: middle;margin-left:-20px;">商品描述:</label>   

      <input class="uk-form-width-large" type="text" name="WIDbody" value="订单测试支付">                                      

      <br>                                                                              

  </p>                                                                                 

  </p>                                                                                 

 <input type="hidden" name="uid" value="<?php echo $_GET[&#39;uid&#39;];?>">                                                   

 <input type="hidden" name="codes" value="<?php echo $_GET[&#39;code&#39;];?>">                                                 

 <input type="hidden" name="tags" value="<?php echo $_GET[&#39;tag&#39;] ?>">                                                  

  <p class="uk-form-row">                                                                       

   <input class="uk-button uk-button-primary uk-button-large uk-width-1-1" type="submit" class="alisubmit" value ="确认支付">                   

  </p>                                                                                 

 </p>                                                                                 

 </form>

Copy after login

设置alipay.config.php配置文件,主要配置一下几个参数

$alipay_config['partner'] 设置签约账号ID
$alipay_config['key'] 设置MD5校验key
$alipay_config['notify_url'] 设置异步回调地址
$alipay_config['return_url'] 设置回调返回地址
$alipay_config['transport'] 设置协议类型,默认为http

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

$alipay_config[&#39;partner&#39;]        = &#39;xxxxxxxxxxxx&#39;;                                 

//收款支付宝账号,以2088开头由16位纯数字组成的字符串,一般情况下收款账号就是签约账号                   

$alipay_config[&#39;seller_id&#39;]   = $alipay_config[&#39;partner&#39;];                               

// MD5密钥,安全检验码,由数字和字母组成的32位字符串,查看地址:https://b.alipay.com/order/pidAndKey.htm         

$alipay_config[&#39;key&#39;]          = &#39;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&#39;;                       

// 服务器异步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问           

$alipay_config[&#39;notify_url&#39;] = "http://xx.xx.xx.xx/pay/notify_url.php";                          

// 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问          

$alipay_config[&#39;return_url&#39;] = "http://xx.xx.xx.xx/pay/return_url.php";                          

//签名方式                                                        

$alipay_config[&#39;sign_type&#39;]  = strtoupper(&#39;MD5&#39;);                                    

//字符编码格式 目前支持 gbk 或 utf-8                                           

$alipay_config[&#39;input_charset&#39;]= strtolower(&#39;utf-8&#39;);                                   

//ca证书路径地址,用于curl中ssl校验                                            

//请保证cacert.pem文件在当前文件夹目录中                                         

$alipay_config[&#39;cacert&#39;]  = getcwd().&#39;\\cacert.pem&#39;;                                  

//访问模式,根据自己的服务器是否支持ssl访问,若支持请选择https;若不支持请选择http                     

$alipay_config[&#39;transport&#39;]  = &#39;http&#39;;                                         

// 支付类型 ,无需修改                                                  

$alipay_config[&#39;payment_type&#39;] = "1";                                           

// 产品类型,无需修改                                                   

$alipay_config[&#39;service&#39;] = "create_direct_pay_by_user";

Copy after login

交易处理完成后会发送订单信息及交易状态到你指定好的回传return_url.php中,你可以通过回传给你的订单状态判断交易是否成功,成功则进入下一步逻辑(你自己的订单处理逻辑),否则返回失败。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

<?php

/* *

 * 功能:支付宝页面跳转同步通知页面

 * 版本:3.3

 * 日期:2012-07-23

 * 说明:

 * 以下代码只是为了方便商户测试而提供的样例代码,商户可以根据自己网站的需要,按照技术文档编写,并非一定要使用该代码。

 * 该代码仅供学习和研究支付宝接口使用,只是提供一个参考。

 *************************页面功能说明*************************

 * 该页面可在本机电脑测试

 * 可放入HTML等美化页面的代码、商户业务逻辑程序代码

 * 该页面可以使用PHP开发工具调试,也可以使用写文本函数logResult,该函数已被默认关闭,见alipay_notify_class.php中的函数verifyReturn

 require_once("alipay.config.php");

 require_once("lib/alipay_notify.class.php");

 ?>

 <!DOCTYPE HTML>

 <html>

   <head>

     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

 <?php

 //计算得出通知验证结果

 $alipayNotify = new AlipayNotify($alipay_config);

 $verify_result = $alipayNotify->verifyReturn();

 if($verify_result) {//验证成功

     /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

     //请在这里加上商户的业务逻辑程序代码

     //——请根据您的业务逻辑来编写程序(以下代码仅作参考)——

   //获取支付宝的通知返回参数,可参考技术文档中页面跳转同步通知参数列表

     //商户订单号^M

     $out_trade_no = $_GET[&#39;out_trade_no&#39;];

     //支付宝交易号^M

     $trade_no = $_GET[&#39;trade_no&#39;];

     //交易状态

     $trade_status = $_GET[&#39;trade_status&#39;];

     $total_fee = $_GET[&#39;total_fee&#39;];

     //获取用户返回数据

     $user_info = $_GET[&#39;extra_common_param&#39;];

     $user_arr = explode(&#39;.&#39;,$user_info);

     $uid = $user_arr[0];

     $code = $user_arr[1];

     $tags = $user_arr[2];

   if($_GET[&#39;trade_status&#39;] == &#39;TRADE_FINISHED&#39; || $_GET[&#39;trade_status&#39;] == &#39;TRADE_SUCCESS&#39;) {

         $types = "alipay";

         $user_url = "/user_obj/do_orderinfo.php";

         die("<script>;window.location=&#39;{$user_url}?uid={$uid}&code={$code}&tags={$tags}&payAmount={$total_fee}&order={$out_trade_no}&types={$types}&#39;;</script>");

         //判断该笔订单是否在商户网站中已经做过处理

         //如果没有做过处理,根据订单号(out_trade_no)在商户网站的订单系统中查到该笔订单的详细,并执行商户的业务程序

         //如果有做过处理,不执行商户的业务程序

   }

   else {

    echo "trade_status=".$_GET[&#39;trade_status&#39;];

   }

 var_dump($verify_result);

     echo "验证成功<br />";

     //——请根据您的业务逻辑来编写程序(以上代码仅作参考)——

     /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

 }

 else {

   //验证失败

   //如要调试,请看alipay_notify.php页面的verifyReturn函数

   echo "验证失败";

 }

 ?>

     <title>支付宝即时到账交易接口</title>

     </head>

   <body>

   </body>

 </html>

Copy after login

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。

相关推荐:

PHP仿微信发红包领红包效果

php将服务端的文件读出来显示在web页面实例

利用php实现一周之内自动登录存储机制

The above is the detailed content of How to access Alipay's instant payment function with PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

See all articles