Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of callback Operation of Alipay Instant arrival Interface in thinkPHP Framework

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the example analysis of the callback operation of the thinkPHP framework docking Alipay instant arrival interface. The article is very detailed and has a certain reference value. Interested friends must finish reading it!

The details are as follows:

With regard to the docking process of the Alipay instant collection interface, it is very simple, and many people have sent it. I am no longer verbose here. After the docking is completed, the callback after the success of online payment is relatively difficult. I'll focus on sharing my experience.

When I was developing the second-generation tourism CMS (http://www.erdaicms.com)), I also spent a lot of time in the callback.

Whether it is Alipay interface or WeChat Pay interface, callback is divided into jump callback and asynchronous notification callback. Jump callback is not safe. If you join the guest and close the payment page immediately after the payment is completed, you will not be notified that this order has been paid, so we need to use asynchronous notification callback:

$alipay_config ['notify_url'] = ". $ss [' web_url']." / v.php/Index-alipay_notify_url.html "

First set the address that introduces the asynchronous callback

The specific handling function of asynchronous callback is also posted here for reference:

/ * Alipay Asynchronous Notification * / public function alipay_notify_url () {vendor ('Alipay.Corefunction'); vendor (' Alipay.Md5function'); vendor ('Alipay.Notify'); vendor (' Alipay.Submit'); $info=M ('rewrite')-> where (array (' name'= > 'alipay'))-> find (); $info=json_decode ($info [' content'], true) / / ↓ Please configure your basic information ↓ / / partner ID here, sign the account number, a string of 16-digit pure numbers starting with 2088, check the address: https://b.alipay.com/order/pidAndKey.htm$alipay_config['partner'] = $info ['alipay_pid'] / / receipt Alipay account, which starts with 2088 and consists of a string of 16 digits. Normally, the receipt account is the signed account $alipay_config ['seller_id'] = $info [' alipay_pid']; / / MD5 key, security check code, 32-bit string composed of numbers and letters. Check the address: https://b.alipay.com/order/pidAndKey.htm$alipay_config['key'] = $info ['alipay_key']. $ss=S ('config'); / / the path of the server asynchronous notification page requires a full path in http:// format, and custom parameters such as id=123 cannot be added. The public network must be able to access $alipay_config [' notify_url'] = ". $ss ['web_url']." / v.php/Index-alipay_notify_url.html " / / the page path of page jump synchronization notification requires a complete path in http:// format, and custom parameters such as id=123 cannot be added. $alipay_config ['return_url'] = ". $ss [' web_url']." / member.php "; / / signature method $alipay_config ['sign_type'] = strtoupper (' MD5') / / character encoding format currently supports gbk or utf-8 $alipay_config ['input_charset'] = strtolower (' utf-8'); / / ca certificate path address for ssl verification in curl / / Please make sure that the cacert.pem file is in the current folder directory $alipay_config ['cacert'] = getcwd ().\\ cacert.pem';// access mode, depending on whether your server supports ssl access, select https if you do. If it is not supported, select http$alipay_config ['transport'] =' http';// payment type, no need to modify $alipay_config ['payment_type'] = "1"; / / Product type, no need to modify $alipay_config [' service'] = "create_direct_pay_by_user" / / ↑ Please configure your basic information here ↑ / / ↓ Please configure anti-phishing information here, if the anti-phishing feature is not enabled To use the ↓ / / antiphishing timestamp, call the query_timestamp function $alipay_config ['anti_phishing_key'] = "in the class file submit. / / the IP address of the client is not the public IP address of the local area network, such as: 221.0.0.1 $alipay_config ['exter_invoke_ip'] = ""; $alipayNotify = new\ AlipayNotify ($alipay_config); $verify_result = $alipayNotify- > verifyNotify () If ($verify_result) {/ / verify successfully / / Please add the merchant's business logic program here /-- Please write the program according to your business logic (the following code is for reference only)-/ / get the notification return parameters of Alipay Please refer to the server asynchronous notification parameter list / / merchant order number $out_trade_no = $_ POST ['out_trade_no'] in the technical documentation. / / Alipay transaction number $trade_no = $_ POST ['trade_no']; / / transaction status $trade_status = $_ POST [' trade_status']; $total_fee=$_POST ['total_fee']; / / if ($info [' alipay_pid']! = $seller_id) return''; / / payment account inconsistency $info=M ('order')-> where (array (' no'= > $out_trade_no))-> find (); if (! $info) return'' / / the order number does not exist if ($_ POST ['trade_status'] = =' TRADE_FINISHED') {/ / determine whether the order has been processed on the merchant's website / / if not, look up the details of the order in the order system of the merchant's website according to the order number (out_trade_no) And execute the merchant's business program / / be sure to judge that the total_fee and seller_id at the time of the request are consistent with the total_fee and seller_id obtained at the time of notification / / if it has been processed, do not execute the merchant's business program $data='' $data ['status'] = 2; $data [' pay_price'] = $total_fee; $data ['pay_type'] =' Alipay'; $data ['buyer'] = $_ POST [' buyer_email']; $data ['trade_no'] = $trade_no; $data [' pay_time'] = time (); M ('order')-> where (array (' no'= > $out_trade_no)-> save ($data) $content M ('smtp_templates')-> where (array (' id'= > 5))-> find (); if ($c ['status'] = = 1) {$content = str_replace (' {title}', $info ['goods_name'], $c [' content']); $content = str_replace ('{id}', $info ['goods_id'], $content); $content = str_replace (' {price}', $total_fee, $content) $content = str_replace ('{time}', date), $content); $email_note' M ('email_note')-> where (array (' pay_id'= > $out_trade_no))-> find (); if (! $c) {$eyed emailment; $e ['email'] = $info [' clockmail']; $e ['content'] = $content $e ['pay_id'] = $out_trade_no; M (' email_note')-> add ($e); $m=explode ('|', $c ['ather']); foreach ($m as $mail) {if (validate_email ($mail)) {$e [' email'] = $mail; M ('email_note')-> add ($e) } / Note: / / when the refund date exceeds the refundable period (such as a refund for three months), Alipay system sends the transaction status notification / / transfer trial, and writes a text function to record whether the program is running normally / / logResult ("write here the value of the code variable you want to debug, or other running result records") } else if ($_ POST ['trade_status'] = =' TRADE_SUCCESS') {/ / determine whether the order has been processed on the merchant's website / / if not, look up the details of the order in the order system of the merchant's website according to the order number (out_trade_no) And execute the merchant's business program / / be sure to judge that the total_fee and seller_id at the time of the request are consistent with the total_fee and seller_id obtained at the time of notification / / if it has been processed, do not execute the merchant's business program $data='' $data ['status'] = 2; $data [' pay_price'] = $total_fee; $data ['pay_type'] =' Alipay'; $data ['buyer'] = $_ POST [' buyer_email']; $data ['trade_no'] = $trade_no; $data [' pay_time'] = time (); M ('order')-> where (array (' no'= > $out_trade_no)-> save ($data) $content M ('smtp_templates')-> where (array (' id'= > 5))-> find (); if ($c ['status'] = = 1) {$content = str_replace (' {title}', $info ['goods_name'], $c [' content']); $content = str_replace ('{id}', $info ['goods_id'], $content); $content = str_replace (' {price}', $total_fee, $content) $content = str_replace ('{time}', date), $content); $email_note' M ('email_note')-> where (array (' pay_id'= > $out_trade_no))-> find (); if (! $c) {$eyed emailment; $e ['email'] = $info [' clockmail']; $e ['content'] = $content $e ['pay_id'] = $out_trade_no; M (' email_note')-> add ($e); $m=explode ('|', $c ['ather']); foreach ($m as $mail) {if (validate_email ($mail)) {$e [' email'] = $mail; M ('email_note')-> add ($e) } / Note: / / after the payment is completed, Alipay system sends the transaction status notification / / call trial, and writes a text function to record whether the program is running normally / / logResult ("write here the value of the code variable you want to debug, or other running result records") } /-- Please write programs according to your business logic (the above code is for reference only)-- echo "success" / Please do not modify or delete / / /} else {/ / Verification failed echo "fail" / / write a text function to record whether the program is running normally / / logResult ("write the value of the code variable you want to debug, or other running result records");}}

Because it is an asynchronous notification, debugging is troublesome and cannot be obtained by WYSIWYG. That is to say, generally speaking, there is no way to print the output result with echo or print_r, and there is no way to know where it is executed or the result. At this time, we use log file output, code:

$file ='. / log.txt';// the file name to write to (can be any file name). If the file does not exist, it will create a $content = "paid successfully". $bdata ['total_fee']. "\ n"; / / the content to be written file_put_contents ($file, $content,FILE_APPEND); / / write to the file

The above code automatically generates a log.txt file in the root directory, so that you can easily know the execution result and facilitate debugging.

Alipay's asynchronous notification interface will notify you many times until you return success, that is, if you submit a test order, you can test it many times, because if you do not return success, it will notify you at regular intervals.

The above is "thinkPHP framework docking Alipay instant account interface callback operation example analysis" all the content of this article, thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report