Alipay interface uses documentation to describe alipay asynchronous notification (notifY_URL) and return_URL.
Now the notice that pays treasure has two kinds. A server notification, corresponding parameter is notify_URL, Alipay notification uses POST notification, B page hop notification, corresponding parameter is return_URL, Alipay notification uses GET notification (the notification address does not need to be set in the account as before, but the customer sends me the address by parameter when paying.
For example notify_url=www.xxx.com/notify\_ali… Note: www.XXX.com is the domain name of your website, or you can use an IP address instead. For server notification, the IP address must be public network, private address (such as 10.2.1.1 or 192.168.1.1) Alipay cannot notify the client)
The following content is from the official website of Alipay, but due to the address change, the original post can not be accessed, through the snapshot to find its content, special share.
1. Check whether the interface you are using uses notifY_URL or return_URL. 2. Notify_url is the server notification. Alipay can guarantee 99.9999% notification arrival rate, provided that your network is smooth. 3. Return_url refers to the webpage redirection notification, which is triggered by the client’s browser. If the client goes to online banking for payment, it will also be affected by the bank interface. After successful payment, the buyer will jump to the page where return_URL is located, which can be displayed to the customer. The page will jump only once if the payment is successful. Notify_url: server background notification. This page is the link address that alipay server automatically calls to modify the order status of the website according to the feedback information from Alipay. After the update is completed, a success message should be returned to Alipay, which cannot contain any other characters including HTML language. Procedure: After buyer pays (trade_status=WAIT_SELLER_SEND_GOODS)– > Alipay notification Notify_url –> If the feedback to Alipay is “success” (indicating success, there is no feedback in this state. If there is no further notification, the interval between first sending and second sending is 3 minutes), the seller sends the goods, the buyer confirms receipt of the goods, and the transaction is successful
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — you must have this confusion – to pay treasure to interface code, although the program has comments, interface code package also accompanied by the development, But I still don’t know where to start. It’s not hard to imagine why, because you don’t understand how this interface works. So this article is to show you all the things about Alipay interface, so that you can quickly access the interface into their own projects, but also to help those who have some understanding of Alipay interface program developers to better understand some general rules, special uses of Alipay.
To begin with
A) Generally, it consists of two parts, the access part and the notification return part. The access part is to synthesize hyperlinks for transferring parameters and other information, and use this link to jump. The notification return part refers to the notification and return of the detailed information of the order to the merchant server after alipay server completes the processing of the order. The merchant server receives the notification and processes the data. B) Take the program in the physical standard double interface ASP code as an example. I. Page files of the access part include: configuration page alipay_config. Java, payment detail page Alipay /alipayapi.jap, program entry page index.jsp. Ii. The page file for the notification return section contains: After payment is completed (after alipay is processed), the customized page return_url will automatically jump back to, and the notification page notify_url.jsp for mutual interaction between two servers (invisible to the naked eye). ———————————————-
Now that you have some understanding of the structure, let’s start to analyze how this interface works. I. Step 1 — Parameter selection: Select parameters to be transferred to Alipay server based on technical documents and interface code DEMO, taking physical standard dual interface as an example. For example, service, partner, seller_email, sign, sign_type, and out_trade are mandatory _no, price, subject, quantity, payment_type and at least one group of logistics information parameters logistics_type, logistics_fee, logistics_payment, etc. Select body, discount, show_URL, etc. Ii. Step 2 — Sorting: the official Demo has given to III. Step 3 — encryption: At present, the general encryption method is MD5. To sort good all the parameters above (not including the gateway parameters: a string of gateway = “HTTP: / / www.alipay.com/cooperate/g… ?” 😉 In a circular manner, a long string is concatenated with the ampersand character (note that all parameters are concatenated with the ampersand character, and then directly concatenated with the security check Key, as you can see in the program, the Key is directly added to the string instead of the ampersand character), and then encrypted. The resulting set of encrypted strings is stored in the sign parameter. Iv. Step 4 — Concatenate string into URL link We have got the parameters, the value of the parameters and the encrypted string, so the format of all parameter information should be a group of service= “trade_create_by_buyer” string. For concatenation, all such strings are iterated through in a loop. Because the concatenation is to form a URL link, the gateway gaetway and the encryption type parameter SIG n_type, which were previously excluded, will also be concatenated. In this case, the concatenated characters will use the well-known character ‘&’. In this way, a complete URL link address is obtained, such as:
www.alipay.com/cooperate/g…
The link is from alipay’s official technical document “Standard physical dual interface Technical Document” V. Fifth step – automatic jump in the fourth step has been calculated URL link string, we want to make it alive, so live up to the way is – with procedures to call it, is the so-called page automatic jump. This will jump to the official cashier page of Alipay. It can be said that alipay interface has been successfully integrated into everyone’s own website, and can use Alipay to pay. Notice return is two pages, that is, the corresponding page file of notify_URL parameter sent to Alipay (notify_url.jsp in Java) is called notification page. The page file (return_url. JSP) corresponding to the return_url parameter passed to Alipay is called the return page. 1. Step 1 — verify whether it is a request from alipay server: official Demo valid checking method. 1. It is not difficult to find that when the program is running in the notification page, the method of obtaining parameters is POST, while when the program is running in the return page, the method of obtaining parameters is GET. From this, we can know some basic information — the parameter information returned by the return page is stored in the URL link, while the parameter information of the notification page is not in the URL link, and we can infer the difference in function between the two. Response.write (“success”); response.write (“success”); For details of the different functions, you can see section 4 below. ——————————————————————-
First of all, we have a question. Many parameters are given in the input parameter list in the technical document, but only part of the parameters are written in the code to convey information. Why is this? So let’s move on with that question. The parameters discussed below do not cover gateway, the encryption parameter sign, and the encryption type sign_type, as these are required. Take the physical standard dual interface as an example, parameters can be regarded as several functional components a) indispensable parameters I. service Service parameters, which are used to distinguish the interface used by the interface and therefore cannot be modified. Ii. Partner Partner ID and key security check code or private key can only be obtained after the contract comes into effect. Partner is to identify which merchant signs a contract with Alipay, and this key is as important as a key. Iii. Seller_email Payee alipay account, alipay account with mobile phone type and email type can use this parameter. Iv. Subject is directly associated with the name of the commodity in the cashier desk of Alipay, but to be more accurate, this parameter is the name of the transaction, because the transaction does not necessarily buy only one commodity. Its role is not only in the cashier can be clearly displayed, and in the list of transaction details of alipay accounts, it is also in the first column, so it can be inferred that it has financial reconciliation, as a screening condition for transaction inquiries, and many other functions. Very important. The transaction number given in v. out_trade_no technical document is the merchant transaction number (to ensure that it is unique in the merchant system). As the name implies, this is the only order number in the order system of our website, rather than alipay’s. It needs to be emphasized that this order number must be unique. How can it be unique? The order number of the order system in oneself website is absolutely unique, what pay treasure asks is this only, why must be unique? Alipay will use the order number to determine whether the order is unique to the merchant’s transactions. There are two ways to set the price of a commodity, quantity (i.e., greater than or equal to 1). The other is that the quantity is 1, and the amount represents the total amount, even including the freight. Why do most clients do this? The reason is simple. First, the items in your shopping cart are not necessarily one or more of the same items, so it is difficult to set the amount of items, so the total amount is best here, and the quantity defaults to 1. Second, the setting of freight many customers are signed with various express companies, the express cost of each item is not the same, in order to save trouble, in the calculation of the program simply add freight. So the only thing we need to remember is that the amount of this price is called the total. Vii. The payment_type payment type is written as 1 without modification. Viii. Logistics information LogisticS_type, logisticS_fee, and logisticS_payment This is a group of logistics information. There must be at least one group of logistics information in the standard dual interface. Logistics_type_1, logisticS_fee_1, logisticS_paymen T_1 (group 2); Logistics_type_2, logisticS_FEE_2, logisticS_payment_2 (group 3). The latter two groups are optional. The logistics freight can be set to 0, i.e. Logistics_fee = “0”. The other two information can be filled in by referring to the technical document, because it needs to be selected from the enumeration list in the technical document, so it should not be filled in randomly. B) Useful parameters that can be added I. Logistics information is at most three groups and at least one group, which has been mentioned in the previous part and will not be detailed here. Ii. _input_charset, which must be used in utF-8 encoding format and cannot be null, that is, _input_charset= “UTF-8” iii. notifY_URL, return_URL, Return_url means that the payment will automatically jump back from alipay’s official page. Notify_url is the first and best tool to prevent order calls. Iv. Body is shown in the description of goods in the cashier’s desk of Alipay. If subject is the name of the order, then the body is most accurately called as the description of the order. A lot of people are very depressed alipay, why not like other companies, the parameters of the interface has a custom to hold the customers want, in fact, the body also have similar this kind of function, it is not only contain the information of all parameters in the biggest, but is also stored in the form of a string, personally think it’s really one of the indispensable parameter is also very important. If less than 0, we use the original quantity+(discount), the actual amount is less than the original total. Now some merchants have alipay coupon, and the use of coupon is also reflected in this parameter, the specific approach is the same as before. Vi. Show_url product display address, the function of this link is to underline “details” next to the product link at the cashier’s desk of Alipay, and click the link to pop up a new page is this product display address page. Receive_name, Receive_ADDRESS, Receive_ZIP, receive_phone, receive_Mobile, if these parameters are also set as one of the parameters to be sent to Alipay, So when you click the next step at the cashier desk of Alipay, the page for filling in the receiving information disappears, and directly jumps to the next page of the receiving information page. Many merchants have a TAB to fill in the receipt information in the shopping process of their own websites. In order to save the trouble of filling in the receipt information at the cashier desk of Alipay, these parameters of the receipt information come in handy. It is worth noting that the consignee name and address is mandatory, otherwise there will be a receiving information fill in page. Viii. Buyer_email Buyer’s Alipay account. The effect of this setting is that the empty Alipay account input field now has a Alipay account. C) The remaining parameters need not be ignored
The whole physical standard double interface parameters are introduced, then other interface parameters also need to be introduced? Do you see anything from the above analysis against the parameter list in the technical documentation?
1. The last column of the parameter list is called “nullable”, where N stands for not being allowed to be empty and Y stands for being allowed to be empty. By comparing the above indispensable parameters with the added useful parameters, it is not difficult to find that all indispensable parameters are N. Select buyer_email, buyer_id, buyer_email, buyer_id, buyer_email, buyer_id, buyer_email For example, seller_email and seller_id are mandatory, and buyer_email and buyer_id are mandatory. All the parameters of the interface can be analyzed in this way to determine which parameters are important and which are not, combined with technical documentation and programming interface research can be clear at a glance. ——————————————————–
A) Return the page file corresponding to the return_URL parameter when the page is transferred to Alipay. 1. When the buyer’s purchase process in the payment interface has gone to Alipay and Alipay prompts payment success, the page will automatically jump back to this page of its own website. 2, synchronous, no time difference 3, the method of obtaining parameters is to use get method. If (sign = mysign and responseTxt = true); 5, this is not the Alipay server called the page, but through the combination and splicing parameters formed by the PRINCIPLE of the URL link, spliced out of the URL link, after the program to do automatic jump. 6. For reason 5, this page can be debugged and run on the local machine rather than on the server. B) Properties of the page file corresponding to notifY_URL parameter when the notification page is transferred to Alipay: 1. The notification page can only be started when invoked by Alipay. 2, server interaction, unlike the return page can be seen by the naked eye, this is not visible.
Map<String,String> params = new HashMap<String,String>(); Map requestParams = request.getParameterMap(); for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) { String name = (String) iter.next(); String[] values = (String[]) requestParams.get(name); String valueStr = ""; for (int i = 0; i < values.length; i++) { valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ","; } // This code is used when there is a garble. //valueStr = new String(valuestr.getBytes ("ISO-8859-1"), "GBK "); params.put(name, valueStr); } // Get alipay notification return parameters, For details, see the page jump synchronization notification parameters list in the technical documentation (for reference only)// // Merchant order Number String out_trade_no = new String(request.getParameter("out_trade_no").getBytes("ISO-8859-1"),"UTF-8"); String trade_no = new String(request.getParameter("trade_no").getBytes("ISO-8859-1"),"UTF-8"); String trade_status = new String(request.getParameter("trade_status").getBytes("ISO-8859-1"),"UTF-8"); // Trade_status = new String(request.getParameter("trade_status").getBytes("ISO-8859-1"),"UTF-8"); // Get alipay notification return parameters, // If (alipaynotify.verify (params)){// The verification succeeded / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / please add merchants business logic code here -- if(trade_status.equals("TRADE_FINISHED")){// Trade completed // Update order status}} else if (trade_status.equals("TRADE_SUCCESS")){// Update order status <pre name="code" class=" Java "> }// -- out.println("success"); Please don't modify, or delete / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /} else {/ / validation failure out. Prin tln("fail"); }Copy the code
3. The method of obtaining parameters is to obtain them by POST. 4. If the transaction exists in Alipay and the transaction status changes, it will be called. If (sign = mysign and responseTxt = true) if(sign = mysign and responseTxt = true) if(sign = mysign and responseTxt = true) 6, asynchronous, the first received order information (hereinafter referred to as “notice”) is almost the same or the same synchronization time with the return page, in the case of unsuccessful judgment, will receive the second and third times of notification, time interval from the first one or two minutes, to the following several hours. Expiration time is 48 hours. For reason 6, program debugging on this page must be debugged and run on the server. 8. The program must be executed successfully before writing the page response.write (” success “); Response. Write(” fail “); Alipay decides whether to send the notification again according to success.
9. The Html page of this page must be blank, without any Html tag, without any space, and does not allow page skipping.
C) Among the numerous interfaces of Alipay, not all of them have notification pages and return pages. Some interfaces only have return pages; Some interfaces have notification pages and display content in XML format on the current page; Some have no notification page or return page and simply display content in XML format on the current page. Therefore, we should do the corresponding data management according to the technical documents and program instances of each interface. D) There is a question here, we generally do is to update the database in the return page, but in many cases, the order is not synchronized that the phenomenon of order drop. Why is that? A: Return page is the current page automatically jump, although the jump response speed is good, but the manual closing of the page operation can absolutely make it before the jump back to close the page, at this time the original database update program has not been started, which directly leads to the single, so the general large business, In particular, the online game industry real-time account recharge technology approach is: return page order processing procedures, notice page also have, when the order back page has not done processing, notice page data processing procedures will start; In this way, the single problem can be almost 100% solved (there are also a single reason is that everyone’s own server problems, such as MS3xml. DLL problem, there is no way to solve this problem, only reinstall or replace the server, and some servers because of poisoning). This article from www.k686.com/ — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
Five, the debugging access part is done, the notification return part is also done, so start debugging. Debugging is also done in two main parts. A) Some websites use frame mode, but this is not applicable to the interface program of Alipay, so the interface page of Alipay cannot be placed under the frame of the whole website. B) Decide whether to use POST or GET to pass parameters. Because some sites do not necessarily have only one interface entry, so the entire site must maintain consistency, can not use this interface POST, that interface GET, which directly leads to a series of follow-up even find the cause of the phenomenon is extremely difficult. C) access part of debugging work, is the value of the input alipay format, such as the subject, the body is not permitted in the value of the amount of illegal characters, format must be two digits after the decimal point or a positive integer and not amount format, as well as a very important principle, parameters passed or not passed this parameter (namely the passing of many arguments, This parameter does not exist at all), or this parameter is not allowed to be null. A large part of the reason why so many people have a series of “debug errors” when debugging is that the parameters are set incorrectly. D) The encoding format must be confirmed and re-confirmed. There are only two reasons for the direct occurrence of “debugging error, SIGN is wrong” when payment is made. One is the setting of parameters mentioned in Part C, and the other is the encoding format. The encoding format is very important, never use GBK in one place and UTF-8 in another. E) through interfaces up a real operation, if the payment interface, is a real deal, the amount is RMB 0.01 (alipay, there is no test environment, so please to go for a real honest use their contract number trading), don’t feel very trouble, don’t send this work to pay a manager or your boss to do, Because it is directly related to your subsequent operation steps and debugging smooth degree. F) returned parts in the machine can finish testing, computer have mentioned not to pay for testing before to their beyond others, can fully explain here, no matter what kind of language has its own style of single step to monitor the ability of program code, return parts will monitor the execution of the step by step, Mysign == sign && responseTxt == “true”; (2) is it inside this statement instead of else? 3, whether the database update program is successfully executed, rather than stuck; 4. After the database is updated, whether the program is finished. Basically the problem is in the first step, so don’t wonder why you drop the order? G) The debugging of the notification page is more troublesome. First, the Alipay interface has been completed and put on the server, so that others can make payment through the Internet; Trade_status, tr ADE_NO, out_trade_no, price, sign, mysign, responseTxt, etc. Mysign == sign && responseTxt == “true” Specific detection methods: 1. Directly use the Internet to visit www.xxx.com/alipay/noti… Whether the access is accessible, and the word “fail” is displayed, and other content such as blank or other program error message is a program execution error. Select * from mysgin and responseTxt where sign=mysgin and responseTxt = “true” ELSE; It indicates that there is a problem with the parameter information transmitted by your interface program during payment or your encoding format, partner ID and security check code Settings. Sign =mysgin and responseTxt = “true”, response.Write(“success”), response.Write(“success”), response.Write(“success”);
———————————————————-
A) Some interfaces, such as the payment interface, support POST or GET to pass parameters. I. POST delivery: Note here:
Value is the action in the gateway + coding format parameters, namely www.alipay.com/cooperate/g… ? _input_charset= UTF-8. The GBK encoding format can be www.alipay.com/cooperate /gateway.do?. Ii. GET: a long string of URL links linked by & characters.Redirect(aliay_url) is used instead of Redirect.