1、首先设置支付宝账户有关信息及返回路径
/**说明: *以下代码只是为了方便商户测试而提供的样例代码,商户可以根据自己网站的需要,按照技术文档编写,并非一定要使用该代码。 *该代码仅供学习和研究支付宝接口使用,只是提供一个参考。 */public class AlipayConfig { public static String partner = "2**************1"; public static String private_key = "*************="; public static String alipay_public_key = "*********"; public static String sign_type = "RSA"; public static String log_path ="C://"; public static String input_charset = "utf-8"; public static String service = "http://60.***.***.00/callbacks.do"; public static String app_id="2016**********12";}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
2、支付宝APP支付–申请支付请求参数
[申请支付请求参数说明] (https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.O57CIo&treeId=193&articleId=105465&docType=1)
@ResponseBody@RequestMapping(value = "/alipay.do", produces = "text/html;charset=UTF-8",method={RequestMethod.GET})public static String alipay(String body, String subject, String out_trade_no, String total_amount) throws Exception { //公共参数 Map<String, String> map = new HashMap<String, String>() map.put("app_id", AlipayConfig.app_id) map.put("method", "alipay.trade.app.pay") map.put("format", "json") map.put("charset", "utf-8") map.put("sign_type", "RSA") map.put("timestamp", UtilDate.getDateFormatter()) map.put("version", "1.0") map.put("notify_url", AlipayConfig.service) Map<String, String> m = new HashMap<String, String>() m.put("body", body) m.put("subject", subject) m.put("out_trade_no", out_trade_no) m.put("timeout_express", "30m") m.put("total_amount", total_amount) m.put("seller_id", AlipayConfig.partner) m.put("product_code", "QUICK_MSECURITY_PAY") JSONObject bizcontentJson= JSONObject.fromObject(m) map.put("biz_content", bizcontentJson.toString()) //对未签名原始字符串进行签名 String rsaSign = AlipaySignature.rsaSign(map, AlipayConfig.private_key, "utf-8") Map<String, String> map4 = new HashMap<String, String>() map4.put("app_id", AlipayConfig.app_id) map4.put("method", "alipay.trade.app.pay") map4.put("format", "json") map4.put("charset", "utf-8") map4.put("sign_type", "RSA") map4.put("timestamp", URLEncoder.encode(UtilDate.getDateFormatter(),"UTF-8")) map4.put("version", "1.0") map4.put("notify_url", URLEncoder.encode(AlipayConfig.service,"UTF-8")) //最后对请求字符串的所有一级value(biz_content作为一个value)进行encode,编码格式按请求串中的charset为准,没传charset按UTF-8处理 map4.put("biz_content", URLEncoder.encode(bizcontentJson.toString(), "UTF-8")) Map par = AlipayCore.paraFilter(map4) String json4 = AlipayCore.createLinkString(map4) json4=json4 + "&sign=" + URLEncoder.encode(rsaSign, "UTF-8") System.out.println(json4.toString()) AliPayMsg apm = new AliPayMsg() apm.setCode("1") apm.setMsg("支付成功") apm.setData(json4.toString()) JSONObject json = JSONObject.fromObject(apm) System.out.println(json.toString()) return json.toString() }
3、支付宝支付结果异步通知业务处理
[支付结果异步通知] (https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.afod0z&treeId=193&articleId=105301&docType=1)
@ResponseBody@RequestMapping(value = "/callbacks.do", produces = "text/html;charset=UTF-8",method={RequestMethod.POST})public String callbacks( HttpServletRequest request ) throws Exception { //接收支付宝返回的请求参数 Map requestParams = request.getParameterMap() JSONObject json = JSONObject.fromObject(requestParams) String trade_status = json.get("trade_status").toString().substring(2,json.get("trade_status").toString().length()-2) String out_trade_no = json.get("out_trade_no").toString().substring(2,json.get("out_trade_no").toString().length()-2) String notify_id = json.get("notify_id").toString().substring(2,json.get("notify_id").toString().length()-2) System.out.println("====================================================") System.out.println(json.toString()) System.out.println("支付宝回调地址!") System.out.println("商户的订单编号:" + out_trade_no) System.out.println("支付的状态:" + trade_status) if(trade_status.equals("TRADE_SUCCESS")) { return "SUCCESS" } }else { return "SUCCESS" } return "SUCCESS" }
1 0