- 一 前提条件
- 开发平台注册: 地址:https://openhome.alipay.com
- 创建应用: 开发者中心 ➡ 选择你对接的类型(我这里是网页&移动应用)➡ 创建应用
- 完善应用:
a、添加功能:app支付
b、签约
c、开发设置:应用信息里设置 接口加签方式 等
4. 申请上线:提交申请,大概需要一天时间审核
- 二 SDK下载
1、选择相应的语言下载即可,地址: https://docs.open.alipay.com/54/103419/
2、配置pom文件
- 三 代码
1、 发起支付接口
/** * 支付宝支付 * @param payRecord * @return */ public MapResult aliPay(PayRecord payRecord){ //实例化客户端 AlipayClient alipayClient = AliPayUtils.getAlipayClient(); //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest(); //SDK已经封装掉了公共参数,这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。 AlipayTradeAppPayModel model = new AlipayTradeAppPayModel(); //model.setBody("我是测试数据"); model.setSubject("玉米出行-油券"); model.setOutTradeNo(payRecord.getId()); model.setTimeoutExpress("10m"); model.setTotalAmount(payRecord.getPayMoney().toString()); model.setProductCode("QUICK_MSECURITY_PAY"); request.setBizModel(model); request.setNotifyUrl(notifyUrlAliPay); String orderString = ""; try { //这里和普通的接口调用不同,使用的是sdkExecute AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request); orderString = response.getBody(); System.out.println(response.getBody());//就是orderString 可以直接给客户端请求,无需再做处理。 } catch (AlipayApiException e) { e.printStackTrace(); } return MapResult.build(orderString); }
2、AliPayUtils工具类
/** * 2019/10/12 12:10 PM * * @author shoo * @describe 支付宝支付 */ public class AliPayUtils { public static final String APP_ID = "2019100912345678"; /** * 应用公钥 */ public static final String APP_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ"; /** * 支付宝公钥 */ public static final String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w"; public static final String APP_PRIVATE_KEY = "MIIEvwIBADANBgkqhkiG9w0BAQEFAA"; public static final String SIGN_TYPE = "RSA2"; public static final String CHARSET = "UTF-8"; public static final String GATEWAY = "https://openapi.alipay.com/gateway.do"; public static AlipayClient getAlipayClient(){ AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY,APP_ID,APP_PRIVATE_KEY,"json", CHARSET, APP_PUBLIC_KEY, SIGN_TYPE); return alipayClient; } }
3、支付成功后异步回调
/** * 支付宝回调 * */ @PostMapping("/aliPay") public String aliPay(HttpServletRequest request){ Map<String, String> params = convertRequestParamsToMap(request); // 将异步通知中收到的待验证所有参数都存放到map中 String paramsJson = JSON.toJSONString(params); logger.info("支付宝回调,{}", paramsJson); try { //验签 boolean signVerify = AlipaySignature.rsaCheckV1(params, AliPayUtils.ALIPAY_PUBLIC_KEY, AliPayUtils.CHARSET,AliPayUtils.SIGN_TYPE); if(signVerify){ //todo 严格来说,这里还应该做订单号、金额等校验 String out_trade_no = (String) params.get("out_trade_no");//本系统付款ID String trade_no = (String) params.get("trade_no");//淘宝系统中的交易流水号 //完成支付后的后续动作 updateOrder(out_trade_no, trade_no); }else{ logger.info("---支付宝回调,验签异常,param:[{}]" , paramsJson); return "fail"; } }catch (Exception ex){ ex.printStackTrace(); return "fail"; } return "success"; } // 将request中的参数转换成Map private static Map<String, String> convertRequestParamsToMap(HttpServletRequest request) { Map<String, String> retMap = new HashMap<>(); Set<Map.Entry<String, String[]>> entrySet = request.getParameterMap().entrySet(); for (Map.Entry<String, String[]> entry : entrySet) { String name = entry.getKey(); String[] values = entry.getValue(); int valLen = values.length; if (valLen == 1) { retMap.put(name, values[0]); } else if (valLen > 1) { StringBuilder sb = new StringBuilder(); for (String val : values) { sb.append(",").append(val); } retMap.put(name, sb.toString().substring(1)); } else { retMap.put(name, ""); } } return retMap; }