This article shares the specific code for Java using Alipay to scan the code for payment, for your reference, the specific content is as follows
Preparation
First, open the Alipay sandbox test account, which will include consumer accounts and payee accounts
Scan the QR code to download the mobile app
Basic configuration
Required jar package
AlipayConfig
package com.alipay.config;import java.io.FileWriter;import java.io.IOException;import java.util.ResourceBundle;/* * *Class name: AlipayConfig *Function: Basic configuration class *Details: Set account-related information and return path *Modification date: 2017-04-05 *Note: *The following code is just a sample code provided for convenience for merchant testing. Merchants can write according to the needs of their own website and according to technical documents, and it is not necessary to use this code. *This code is for learning and researching the Alipay interface only, and is only provided as a reference. */public class AlipayConfig { //↓↓↓↓↓↓↓↓↓↓↓↓↓↓ Please configure your basic information here // Application ID, your APPID, and the payment account is the corresponding Alipay account public static String app_id = "2016080403162340"; // Merchant private key, your PKCS8 format RSA2 private key public static String merchant_private_key = "MIIEvAID2tulSSmawG5+F4NZbexpnxi8NKQJPZEeAA=="; // Alipay public key, view the address: https://openhome.alipay.com/platform/keyManage.htm The corresponding Alipay public key under the APPID. public static String alipay_public_key = "MIIBIjt26tLTKar8S1ERDWI25viBcMz7PLMxVVUmHf5tdBWfbMhUs3QIDAQAB"; // The server asynchronous notification page path requires the full path in the format of http://, and cannot add custom parameters like ?id=123. It must be accessed normally by the external network. public static String notify_url = "http://localhost:8080/alipay.trade.page.pay-JAVA-UTF-8/notify_url.jsp"; // Page jump synchronous notification page path requires the complete path of http:// format. Custom parameters such as id=123 cannot be added. Public static String return_url = "http://localhost:8080/Exam/index/gouMai"; // Signature method public static String sign_type = "RSA2"; // Character encoding format public static String charset = "utf-8"; // Alipay gateway public static String gatewayUrl = "https://openapi.alipaydev.com/gateway.do"; // Alipay gateway public static String log_path = "E://"; //↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ Please configure your basic information here/** * Write logs for easy testing (see website requirements, you can also change to store records in the database) * @param sWord To write text content in the log*/ public static void logResult(String sWord ) { FileWriter writer = null; try { writer = new FileWriter(log_path + "alipay_log_" + System.currentTimeMillis()+".txt"); writer.write(sWord); } catch (Exception e) { e.printStackTrace(); } finally { if (writer != null) { try { writer.close(); } catch (IOException e) { e.printStackTrace(); } } } } }}Controller
// Generate a page with a QR code that can be used to scan the code to pay for payment @RequestMapping(value = "aliPay") public String aliPay(HttpServletResponse response,ModelMap map,String chapterId,HttpServletRequest request, String WIDout_trade_no,String WIDtotal_amount,String WIDsubject,String WIDbody) throws IOException, AlipayApiException{// String a,String urlName,String couName...+"&a="+a+"&urlName="+urlName+"&couName="+couName //Get initialized AlipayClient AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type); //Set the request parameter AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest(); alipayRequest.setReturnUrl(AlipayConfig.return_url+"?chapterId="+chapterId); alipayRequest.setNotifyUrl(AlipayConfig.notify_url); //Pay ID, required String out_trade_no = WIDout_trade_no; //Pay amount, required String total_amount = WIDtotal_amount; total_amount=URLDecoder.decode(total_amount,"UTF-8"); //Transcoding//Order name, required String subject = WIDsubject; subject=URLDecoder.decode(subject,"UTF-8"); //Product description, null String body = WIDbody; alipayRequest.setBizContent("{/"out_trade_no/":/"+ out_trade_no +"/"," + "/"total_amount/":/"+ total_amount +"/"," + "/"subject/":/"+ subject +"/"," + "/"body/":/"+ body +"/"," + "/"timeout_express/":/"1m/"," + "/"product_code/":/"FAST_INSTANT_TRADE_PAY/"}"); //Request String result = alipayClient.pageExecute(alipayRequest).getBody(); response.setContentType("text/html; charset=utf-8"); PrintWriter out = response.getWriter(); out.println(result); return null; }Pay successfully and return the page (return_url)
The return path after success is taken, go to Controller, see the configuration in AlipayConfig for details
//Click to buy and save the course in the purchase table @RequestMapping(value="gouMai") @ResponseBody public ModelAndView gouMai(String chapterId,HttpServletRequest req,String a,String urlName,String couName,ModelMap map){ ModelAndView mav = new ModelAndView(); Map<String,String> map1 = new HashMap<String,String>();// SysUserTab login_user = sysuserService.getSysUserById(userId); HttpSession session = req.getSession(); SysUserTab login_user1 = (SysUserTab) session.getAttribute("login_user"); String userId = login_user1.getUserId();// session.setAttribute("login_user", login_user); map1.put("userId", userId); map1.put("chapterId", chapterId); int num = sysBuyService.getBuyCount(mapp1); if(num==0){ mapp1.put("buyId", UUID.randomUUID().toString().replace("-", "")); sysBuyService.insertBuy(mapp1); } //Query course content// String fanhui = showFH(req,chapterId,urlName,couName,map, a); mav.setViewName("jsp/pay/paySuccess"); return mav; }After the payment is successful, the page jumps to the paySuccess.jsp page.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.