Home > Article > PHP Framework > How to use Alipay open platform in ThinkPHP6?
With the popularity and development of e-commerce, safe and convenient online payment methods have become an indispensable service for consumers. As one of the largest online payment platforms in China, Alipay provides trust and convenience to hundreds of millions of users. For enterprises, it is also an indispensable choice for payment business. This article will introduce how to use the Alipay open platform under the ThinkPHP6 framework to facilitate enterprises to conduct payment services.
1. Preparation
Before using the Alipay open platform for development, corresponding developer registration and application creation are required. The specific steps are as follows:
1. Register as a developer
First, developers need to enter the Alipay Open Platform (https://open.alipay.com/platform/home.htm) and complete Register and get developer ID and KEY.
2. Create an application
Application creation needs to be done in the backend of the open platform. Select Create Application and fill in the basic information of the application.
After the creation is completed, APP ID, key and other related information will be generated.
2. Configure Alipay Open Platform SDK
Pay API is a common open interface of Alibaba Open Platform. Using Pay API for payment can greatly reduce the difficulty of development. Pay API provides out-of-the-box payment functionality, allowing users to quickly and easily develop complete payment processes.
1. Introduction of SDK
To develop the Alipay open platform in ThinkPHP6, you need to introduce the SDK first and add:
"require": { "alipay/easysdk":"^2.1" }
in composer.json and then execute composer install After installing the SDK, you can start Alipay-related development.
2. Configuration file
To configure in ThinkPHP6, you need to create a new configuration file config/easysdk.php, and then copy the following content into it:
<?php return [ 'app_id' => '', //应用ID 'notify_url' => '', //支付回调通知地址 'return_url' => '', //支付结束后跳转地址 'ali_public_key' => '', //支付宝公钥 'private_key' => '', //应用私钥 'log' => [ //日志配置 'file' => './logs/easysdk.log', 'level' => 'debug', 'type' => 'daily', //按日志文件 ], 'http' => [ //API请求配置 'timeout' => 5.0, 'connect_timeout' => 5.0, ], ];
3. Initiate payment Request
After you have the preparations and configuration files, you can initiate a payment request in ThinkPHP6. The specific steps are as follows:
1. Write the controller
First, you need to create a new controller file AlipyController.php in the app/controller directory. Write the following content in the file:
<?php namespace appcontroller; use thinkApp; use thinkhelperArr; use thinkacadeConfig; use alipayEasySDKKernelFactory; class AlipayController { public function pay() { //支付金额 $totalAmount = 10.00; $pay = Factory::payment(Config::get('easysdk')); //构造支付请求参数 $builder = $pay->common()->precreate( [ 'out_trade_no' => '20191120' . uniqid(), 'total_amount' => $totalAmount, 'subject' => '思音联合会 -- 支付测试', ] ); //发起支付请求,获取响应 $response = $builder->getResponse(); $qrCode = Arr::get($response->bizContent, 'qr_code'); //展示支付二维码 echo "<img src='{$qrCode}'>"; } }
2. Make a request
Enter the route of the above controller in the browser to make a payment request. The page will display the payment QR code.
The above are the steps for payment development using Alipay open platform in ThinkPHP6. Using the Pay API for payment allows developers to focus more on business process development and reduce development time and costs. At the same time, the payment methods provided by Alipay are also very safe and convenient, providing convenience for enterprises.
The above is the detailed content of How to use Alipay open platform in ThinkPHP6?. For more information, please follow other related articles on the PHP Chinese website!