ThinkPHP is one of the most popular PHP development frameworks in China. In e-commerce website development, payment function is an essential part. As an important form of domestic online payment, Alipay has also become a common payment method on e-commerce websites. This article will introduce how to use ThinkPHP to jump to Alipay payment.
1. Preliminary preparation
Use Composer to install Alipay SDK:
composer require alipay/easy-sdk
or require
in composer.json
Add:
"alipay/easy-sdk": "^3.3"
Create a new alipay.php
configuration file in the config
folder, containing the following content:
<?php return [ // 应用ID 'app_id' => '', // 支付宝公钥 'ali_public_key' => '', // 开发者私钥 'private_key' => '', // 支付宝网关 'gatewayUrl' => 'https://openapi.alipay.com/gateway.do', // 签名方式 'sign_type' => 'RSA2', // 异步通知地址 'notify_url' => '', // 同步跳转地址 'return_url' => '', ];
Among them, app_id
, ali_public_key
, private_key
, notify_url
and return_url
need to be filled in by themselves in Alipay Information obtained from the Developer Center. notify_url
and return_url
are the addresses of asynchronous and synchronous callbacks after payment respectively. The callbacks need to be processed in the application.
2. Alipay payment jump
Alipay provides a unified ordering and payment page interface, through which the payment link can be generated, and then the user can be directed to Complete the payment on the Alipay page. The following are the detailed steps for using EasySDK to jump to Alipay payment:
pay
method in your own controller, which receives an order parameter, pass the order number to Alipay's interface, obtain the payment link and jump to the payment page. <?php namespace app\index\controller; use think\Controller; use think\facade\Config; use Alipay\EasySDK\Kernel\Factory; class Order extends Controller { // 支付方法 public function pay($order_sn) { // 初始化SDK $config = Config::get('alipay'); $client = Factory::payment($config); // 创建订单请求 $request = array( 'out_trade_no' => $order_sn, 'total_amount' => '0.01', 'subject' => '商品名称', 'body' => '商品描述', ); $response = $client->common()->createOrder($request); // 获取支付链接并跳转到支付宝页面 $pay_url = $response->body->qrCode; header('Location:' . $pay_url); } }
<a href="{:url('Order/pay')}?order_sn={$order_sn}">去支付</a>
Summary
This article introduces the process of using ThinkPHP to jump to Alipay payment. You need to register a developer account and create an application, then install the Alipay SDK and add it to the configuration file. corresponding information. Define the payment method in the controller, obtain the payment link through the unified order interface, and guide the user to the Alipay page to complete the payment. At the same time, asynchronous and synchronous callbacks after payment need to be processed in the application to ensure order status updates and business logic processing after payment is completed.
It is worth noting that Alipay payment jump needs to ensure network stability and security, and at the same time follow corresponding transaction rules to provide users with a better transaction experience.
The above is the detailed content of How to redirect Alipay payment in ThinkPHP. For more information, please follow other related articles on the PHP Chinese website!