If you do online business with Chinese, you are almost inevitable to integrate alipay payment gateway into your website. Today, we learn how to integrate alipay gateway on your site using php.
The integration of alipay payment gateway often takes the form of a “Pay” button placed on the payment webpage or somewhere on the shopping cart. Customer clicks the button, then is brought to the alipay’s payment webpage, where he completes the payment and may(or may not) be brought back to your website.
The core work of integrating the payment gateway is to construct the url customer is taken to after clicking the “Pay” button. The url contains the order information, the payment amount, the id of the alipay account that is used to receive the payment, etc. The url is like this:
You can see that the url contains enough information for alipay to complete the payment. The “subject” and the “body” parameter describe the payment. The “out_trade_no” parameter is the order number generated by your website which keeps unique in your system. The “total_fee” parameter is the amount of this payment(order). The “exter_invoke_ip” parameter is the ip address of the customer. The “partner” parameter is the partner id of your alipay account which is used to receive the payment. There are two urls you must provide: return_url and notify_url. What is the difference between notify_url and return_url? Well, let me explain to you. The return_url parameter is where the customer will go after successful payment, i.e., alipay payment webpage will jump to this url after customer completes the payment. Alipay payment page will never jump to this page if the payment fails. Customer may also choose to close his browser before the return_url is loaded. So how does your website know the payment is completed successfully and update the order status accordingly? That is why notify_url is used here. notify_url is used by Alipay server to notify your site of the completion of the payment. It is a server to server call. Your notify_url must be designed to reply to the call with a string “success” , otherwise, alipay server will repeatedly call this url until it receives the “success” response.
Here you can download the php files to integrate the alipay payment gateway.
Now we know all our efforts to integrate alipay gateway on our website focus on constructing the alipay url. Specifically, we need to generate a new order(payment) record in database and reference it by out_trade_no. We need to provide return_url and notify_url to handle the information passed from alipay. Whether alipay calls return_url or notify_url, it passes the same set of parameters: the out_trade_no got from you, the transaction number generated by alipay referring to the payment, and the transaction status. It passes the parameters to return_url as GET parameters and to notify_url as POST parameters. return_url and notify_url should check the transaction status, and if it indicates a succeessful transaction, update the status of the order(payment) record in the database.