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

How to realize the PC payment function of Alipay by Java

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

Share

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

This article introduces the relevant knowledge of "how to achieve the PC payment function of Alipay by Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

One: preparatory work

1: if you do not have an account number, please go to Alipay merchant platform to register the merchant number first.

2: before development, you need to import Alipay SDK Alipay SDK download link Maven dependency is also included.

3: to generate Alipay key and public key, you need to use Alipay key generation tool key generation tool download link, the key is used for signature

4: asynchronous notification of payment result on computer website (a blog will be written after the asynchronous notification of payment result, which needs to be paid attention to)

Note: please first take a detailed look at the official documents and develop them step by step. All official documents are mainly official documents for Alipay. If you use the old interface, please move to the old version of the instant account document.

5: be sure to use intranet penetration software when testing, otherwise an error will be reported.

Second: development code

The sandbox environment is used here. Please change the environment to a formal environment when you officially launch.

AlipayConfig:

Public class AlipayConfig {public static String APPID = ""; / the private key generated by Alipay key generation tool public static String RSA_PRIVATE_KEY = ""; / / the asynchronous notification of the payment result of the computer website. You can refer to the asynchronous documentation. It must be a public static String notify_url that can be accessed by the public network = "". / / synchronous notification of payment result on computer website, which is used to redirect to the user's own page, which must be public static String return_url = ""; / / sandbox interface accessible to the public network. When officially launched, please use the official payment interface public static String URL = "https://openapi.alipaydev.com/gateway.do"; public static String CHARSET =" UTF-8 "; public static String FORMAT =" json ". / / the public key generated by Alipay key generation tool public static String ALIPAY_PUBLIC_KEY = ""; public static String log_path = "/ log"; public static String SIGNTYPE = "RSA2"; public AlipayConfig () {}}

AlipayService:

Public interface AlipayService {public String toAlipay (Map sourceMap) throws IOException;}

AlipayServiceImpl:

@ Servicepublic class AlipayServiceImpl implements AlipayService {@ Override public String toAlipay (Map sourceMap) throws IOException {AlipayClient alipayClient = new DefaultAlipayClient (AlipayConfig.URL,AlipayConfig.APPID,AlipayConfig.RSA_PRIVATE_KEY,AlipayConfig.FORMAT,AlipayConfig.CHARSET,AlipayConfig.ALIPAY_PUBLIC_KEY,AlipayConfig.SIGNTYPE); AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest (); alipayRequest.setReturnUrl (AlipayConfig.return_url); alipayRequest.setNotifyUrl (AlipayConfig.notify_url); AlipayTradeWapPayModel alipayTradeWapPayModel = new AlipayTradeWapPayModel (); alipayTradeWapPayModel.setOutTradeNo (sourceMap.get ("out_trade_no")) AlipayTradeWapPayModel.setProductCode ("FAST_INSTANT_TRADE_PAY"); alipayTradeWapPayModel.setTotalAmount (sourceMap.get ("total_amount")); alipayTradeWapPayModel.setSubject ("Alipay"); alipayTradeWapPayModel.setBody (sourceMap.get ("body")); alipayRequest.setBizModel (alipayTradeWapPayModel); String form= ""; try {form= alipayClient.pageExecute (alipayRequest). GetBody ();} catch (AlipayApiException e) {e.printStackTrace ();} return form;}}

AlipayController:

@ Controllerpublic class AlipayController {@ Autowired public AlipayService alipayService; @ RequestMapping ("pay") public void toAlipay (HttpServletRequest httpRequest, HttpServletResponse httpResponse, String payId, String totalFee, String body) throws IOException {/ / generate a payment record and change the payment status to Map sourceMap = new HashMap () when the payment is completed; sourceMap.put ("out_trade_no", payId); sourceMap.put ("total_amount", totalFee); sourceMap.put ("body", getUTF8XMLString (body)) String form = alipayService.toAlipay (sourceMap); httpResponse.setContentType ("text/html;charset=" + AlipayConfig.CHARSET); httpResponse.getWriter () .write (form); httpResponse.getWriter () .flush (); httpResponse.getWriter () .close ();} / / Note that the asynchronous return result notification is @ RequestMapping ("notifyUrl") public String notify_url (HttpServletRequest request) {Map paramsMap = convertRequestParamsToMap (request) returned in the form of a post request String out_trade_no= paramsMap.get ("out_trade_no"); try {boolean signVerified = AlipaySignature.rsaCheckV1 (paramsMap, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.CHARSET, AlipayConfig.SIGNTYPE) / / verify the signature if (signVerified) {if ("TRADE_FINISHED") | | trade_status.equals ("TRADE_SUCCESS") {/ / handle the business logic of your system, whether synchronous or asynchronous. For example, if you change the payment record status to success, you need to return a string success to tell Alipay server "success". } else {/ / payment failure does not handle business logic return "failure";}} else {/ / signature verification failure does not handle business logic return "failure";}} catch (AlipayApiException e) {e.printStackTrace (); return "failure" }} / / Note the result returned by synchronization is @ RequestMapping ("returnUrl") public String return_url (HttpServletRequest request) {Map paramsMap = convertRequestParamsToMap (request) returned in the form of get request; try {boolean signVerified = AlipaySignature.rsaCheckV1 (paramsMap, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.CHARSET, AlipayConfig.SIGNTYPE); if (signVerified) {/ / Jump payment success interface return "payment success page" } else {/ / Jump payment failure interface return "failure";}} catch (AlipayApiException e) {e.printStackTrace ();} return "success";} / / convert the parameters in the request to Map public static Map convertRequestParamsToMap (HttpServletRequest request) {Map retMap = new HashMap (); Set entrySet = request.getParameterMap (). EntrySet (); Iterator var3 = entrySet.iterator () While (true) {while (var3.hasNext ()) {Entry entry = (Entry) var3.next (); String name = (String) entry.getKey (); String [] values = (String []) entry.getValue (); int valLen = values.length; if (valLen = = 1) {retMap.put (name, values [0]);} else if (valLen

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