Home  >  Article  >  Backend Development  >  How to use php to implement Alipay payment

How to use php to implement Alipay payment

藏色散人
藏色散人Original
2021-12-02 10:18:156840browse

How to use php to implement Alipay payment: 1. Scan the code to log in to the Alipay open platform to register; 2. Find the development information and turn on the RSA2 key mode; 3. Check the Alipay private key and public key and other information; 4. Download the SDK for Alipay payment; 5. Just create a demo to implement Alipay payment.

How to use php to implement Alipay payment

The operating environment of this article: Windows 7 system, PHP version 7.4, DELL G3 computer

How to use php to implement Alipay payment?

PHP implements Alipay payment:

Tips: Use the demo here For sandbox payment, you need to first register a sandbox account, etc.


Register a sandbox account:

  1. First scan the QR code to log in to the Alipay open platform to register
    Address: https://open.alipay.com/

How to use php to implement Alipay payment

  1. ##Scan the QR code to log in and register to enter the console. Slide to the middle of the page to find the R&D service

    How to use php to implement Alipay payment

  2. Click on the R&D service to see relevant information about your own sandbox service

  3. Find the development information below and turn on the RSA2 key mode


    How to use php to implement Alipay payment Click to view and you can see that your Alipay private key, public key and other information should be saved and used later! ! !

For the settings of application gateway and authorization callback address, please view the document

Address: https://developer.aliyun.com/article/707583

At this point our sandbox environment has been registered, now we start to complete a small payment demo!


Download the SDK for Alipay payment:

Download the SDK for Alipay according to the programming language

Address: https://render.alipay.com/p/f/ fdjwq8nu2a/pages/home/index.html (here takes PHP language as an example)

How to use php to implement Alipay payment

The decompressed directory is as follows: demo.php was created by myself


How to use php to implement Alipay payment

Let’s write a simple demo to implement Alipay payment

<?php


require "alipay/aop/AopClient.php";  //引入alipay文件
require "alipay/aop/request/AlipayTradeWapPayRequest.php";  //引入alipay文件

//配置支付宝相关参数
$config = [
    &#39;alipay_appid&#39; = 2021xxxx,  //appid
    &#39;alipay_rsaprivateKey&#39;=>&#39;xxxx&#39;, //开发者私钥
    &#39;alipay_alipayrsapublicKey&#39;=>&#39;xxxx&#39;,  //支付宝公钥
    &#39;alipay_notify&#39;=>&#39;http://www.xxx.com/xxx/xxx&#39;, 
    //支付宝回调地址 支付成功后支付宝会把消息发送给此接口,在此接口中完成支付成功后的相关操作即可
];

$aop = new AopClient();
// $aop->gatewayUrl = "https://openapi.alipaydev.com/gateway.do";     //网关地址要使用沙箱网关alipaydev
$aop->gatewayUrl = "https://openapi.alipaydev.com/gateway.do"; //网关地址要使用沙箱网关alipaydev

//支付宝分配给开发者的应用ID
$aop->appId = $config[&#39;alipay_appid&#39;];

//请填写开发者私钥去头去尾去回车,一行字符串
$aop->rsaPrivateKey = $config[&#39;alipay_rsaprivateKey&#39;];

//请填写支付宝公钥,一行字符串
$aop->alipayrsaPublicKey = $config[&#39;alipay_alipayrsapublicKey&#39;];

返回数据格式
$aop->format = "json";

// 表单提交字符集编码
$aop->postCharset = "utf-8";

//签名类型
$aop->signType = "RSA2";

//api版本
$aop->apiVersion = &#39;1.0&#39;;
//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
$request = new AlipayTradeWapPayRequest();
//SDK已经封装掉了公共参数,这里只需要传入业务参数,沙箱环境的product_code只能是FAST_INSTANT_TRADE_PAY
$info = [
    &#39;body&#39; => &#39;xxx&#39;,
    &#39;subject&#39; => &#39;标题&#39;,  //订单标题。
    &#39;out_trade_no&#39; => time().rand(1111,9999),  //商户网站唯一订单号 自定义的订单号
    &#39;total_amount&#39; => 1, //订单总金额。单位为元,精确到小数点后两位,取值范围:[0.01,100000000] 。
];
$info = json_encode($info, JSON_UNESCAPED_UNICODE);

//支付宝服务器主动通知商户服务器里指定的页面http/https路径。 支付回调
$request->setNotifyUrl($config[&#39;alipay_notify&#39;]);
$request->setBizContent($info);
//这里和普通的接口调用不同,使用的是sdkExecute
$result = $aop->pageExecute($request);


return $result;


//$responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
//$resultCode = $result->$responseNode->code;
//if(!empty($resultCode)&&$resultCode == 10000){
//    echo "成功";
//} else {
//    echo "失败";
//}
详细参数可查看文档 https://opendocs.alipay.com/apis/api_1/alipay.trade.wap.pay?scene=21

Let’s test it next

How to use php to implement Alipay payment At this time, accessing our demo.php file has been successfully invoked For Alipay payment, we can log in to the sandbox payment buyer account to complete the payment.

How to use php to implement Alipay payment

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How to use php to implement Alipay payment. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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