Step 1: Return the order information to the front end and make it call Alipay.
public String zfb_payrequest( ){ String orderInfo = null; Map <String, Object> map= new HashMap<> (); // . . . . . Omit insert order list operation // Company account information String APP_PRIVATE_KEY="xxxxxxxxx" ; String ALIPAY_PUBLIC_KEY="xxxxxxx"; String APP_ID = "xxxxxxx" ; // Instantiate client AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",APP_ID, APP_PRIVATE_KEY, "json", "UTF-8", ALIPAY_PUBLIC_KEY , "RSA2" ); // Instantiate the request class corresponding to the specific API, the class name corresponds to the interface name, the current calling interface name: alipay.trade.app.pay AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest(); // The SDK has encapsulated the public parameters, only the business parameters need to be passed in here. The following method is the model input method of sdk (take biz_content when model and biz_content exist at the same time). AlipayTradeAppPayModel model = new AlipayTradeAppPayModel(); // model.setPassbackParams(user_id);; // Add additional data to description information model.setSubject("Recharge"); // Product title model.setOutTradeNo(out_trade_no); // Merchant order number model.setTimeoutExpress ("30m"); // timeout to close the order time model.setTotalAmount(money); // total order amount , which is a fixed value of QUICK_MSECURITY_PAY request.setBizModel(model); request.setNotifyUrl( "http://xxx.xx.xx.xx/xx/xx/xx"); // callback address (accessible from external network) try { // This is different from ordinary interface calls, using sdkExecute AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request); System.out.println(response.getBody()); // That is, the orderString can be requested directly to the client, No further processing is required. orderInfo = response.getBody(); }catch (AlipayApiException e){ e.printStackTrace (); } map.put("orderString",orderInfo); map.put("result", 1); String jsonString = JSON.toJSONString(map); return jsonString; }
The second step is to receive Alipay asynchronous callback information
For transactions generated by App payment, Alipay will notify the merchant of the payment result as a parameter in the form of a POST request according to the asynchronous notification address notify_url passed in the original payment API.
public String aliPay_notify(Map requestParams,HttpServletRequest request){
System.out.println("被调用了。。。。。"); String APP_PRIVATE_KEY ="xxxxx" ; // Remember that alipaypublickey is Alipay's public key String alipaypublickey="xxxxxxxxxxxxxx" ; String APP_ID="2018041202543416"; requestParams=request.getParameterMap(); System.out.println( "Alipay payment result notification"+ requestParams.toString()); // Get feedback from Alipay POST Map<String,String> params = new HashMap<String,String> (); 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] + ","; } // The garbled code is solved, this code is used when there is garbled code. // valueStr = new String(valueStr.getBytes("ISO-8859-1"), "utf-8"); params.put(name, valueStr); } System.out.println("--0101110110101--"); System.out.println(params.toString() +"++" ); // Remember that alipaypublickey is Alipay's public key, please go to open.alipay.com to view it under the corresponding application. // boolean AlipaySignature.rsaCheckV1(Map<String, String> params, String publicKey, String charset, String sign_type) try { // Verify the signature (to judge whether it is from Alipay) boolean flag = AlipaySignature.rsaCheckV1(params, alipaypublickey, "UTF-8", "RSA2" ); System.out.println( "Result: "+flag+"---------" ); if (flag){ if ("TRADE_SUCCESS".equals(params.get("trade_status" ))){ // Payment amount String money = params.get("buyer_pay_amount" ); // merchant order number String out_trade_no = params.get("out_trade_no" ); // Alipay transaction number String trade_no = params.get("trade_no"); // trade status String trade_status = params.get("trade_status" ); // additional data
String passback_params = URLDecoder.decode(params.get("passback_params"));
//Successful payment if (trade_status.equals("TRADE_SUCCESS" )) { System.out.println( "Operation after successful payment...star" ); } } } catch (AlipayApiException e) { // TODO Auto-generated catch block e.printStackTrace(); }return "success"; }
At this point, the payment ends.