Home >Backend Development >PHP7 >How to use PHP7.0 to implement an online payment system?
As online payment becomes more and more popular, more and more websites need to integrate online payment functions. As a popular server-side programming language, PHP supports an increasingly rich set of online payment processing and other services. This article will introduce how to use PHP7.0 to implement an online payment system based on Alipay.
First, you need to enter the Alipay Developer Center to register an account, create an application, apply for developer permissions, and obtain the APPID assigned by Alipay , application public key, application private key and other key information. This important information will be used for subsequent payment interface integration work.
Alipay provides a PHP version of the SDK to provide an interface class library for docking Alipay payment functions. Before using the SDK, you need to download and install the SDK. When starting to use the SDK, you need to introduce the SDK file into the corresponding file.
In the process of accessing Alipay payment function, the core is the preparation of payment interface.
(1) The core code of the payment interface
The core code of the payment interface is as follows, taking the adaptive interface of Alipay mobile website as an example:
1) Initialize the payment class AlipayTradeWapPayContentBuilder
$payRequestBuilder = new AlipayTradeWapPayContentBuilder();
// The following are the basic parameter settings of the payment class, such as product name, order number, price, etc.
$payRequestBuilder->setBody ($order['body']);
$payRequestBuilder->setSubject($order['subject']);
$payRequestBuilder->setOutTradeNo($order['out_trade_no ']);
$payRequestBuilder->setTotalAmount($order['total_amount']);
(2) Execute payment request
$payRequestBuilder->setProductCode ('QUICK_WAP_PAY');
$payRequestBuilder->setNotifyUrl($this->alipay_config['notify_url']);
$payRequest = new AlipayTradeService($this->alipay_config );
$result = $payRequest->wapPay($payRequestBuilder, $this->alipay_config['return_url'], $this->alipay_config['notify_url']);
(3) Processing payment results
The code for processing payment results is as follows. When Alipay calls back, perform corresponding operations based on the payment results:
$alipayrsaPublicKey = $this->alipay_config[ 'alipay_public_key'];
$checkResult = AlipaySignature::check($params, $alipayrsaPublicKey, $sign_type);
if ($checkResult) {
$status = $this->order_model->handle_payed_order($out_trade_no, $trade_no);
if ($status) {
echo 'success';
} else {
echo 'fail';
}
}
(4) Submit payment form
When implementing mobile website payment, it is required Submit using form.
echo $result;
(5) Process the callback address
The last step to implement mobile website payment is to process the callback address.
$request = Request::instance();
$params = $request->param();
For merchants using Alipay functions, relevant authorization is required in the Alipay account.
(1) Get the application authorization URL
$oauth2 = new OAuth2Service($this->alipay_config);
$url = $oauth2->getAuthorizeUrl($ this->alipay_config['oauth2_scope'], $this->alipay_config['oauth2_callback']);
(2) Perform application authorization
Paste the application authorization URL in the browser , click the "Agree and Authorize" button to complete application authorization. After authorization, direct payment function operations can be performed directly in the merchant management center.
This article introduces the specific steps to implement an online payment system using PHP7.0, mainly including Alipay Developer Center registration, integration of Alipay SDK, and payment interface Writing, Alipay account authorization and other operations. Through the introduction of this article, I believe readers can quickly grasp the basic points of online payment system development and provide strong support for their own payment function development work.
The above is the detailed content of How to use PHP7.0 to implement an online payment system?. For more information, please follow other related articles on the PHP Chinese website!