Home  >  Article  >  PHP Framework  >  How to redirect Alipay payment in ThinkPHP

How to redirect Alipay payment in ThinkPHP

PHPz
PHPzOriginal
2023-04-11 10:30:141361browse

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

  1. Get an Alipay developer account
    Before using Alipay to pay, you need to register a developer account and create an application. After creating an application, you can obtain the application's APPID, developer private key, Alipay public key and other information.
  2. Install Alipay SDK

Use Composer to install Alipay SDK:

composer require alipay/easy-sdk

or require in composer.json Add:

"alipay/easy-sdk": "^3.3"
  1. Create a new alipay.php configuration file in the config folder, containing the following content:

    <?php
    return [
     // 应用ID
     &#39;app_id&#39; => '',
     // 支付宝公钥
     '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:

  1. Define payment method
    Define a 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(&#39;alipay&#39;);
        $client = Factory::payment($config);

        // 创建订单请求
        $request = array(
            &#39;out_trade_no&#39; => $order_sn,
            'total_amount' => '0.01',
            'subject' => '商品名称',
            'body' => '商品描述',
        );
        $response = $client->common()->createOrder($request);

        // 获取支付链接并跳转到支付宝页面
        $pay_url = $response->body->qrCode;
        header('Location:' . $pay_url);
    }
}
  1. Page jump
    In the template, create a payment button, pass the order number to the controller method, and call the payment method to jump to the Alipay page for payment.
<a href="{:url(&#39;Order/pay&#39;)}?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!

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