这篇文章主要介绍了SpringBoot下如何实现支付宝接口的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
前期准备:
参考之前写过的 支付宝接口引入servlet版本
Jar包引入:
<!-- 支付宝 --> <dependency> <groupId>net.guerlab</groupId> <artifactId>sdk-alipay-starter</artifactId> <version>${alipay.version}</version> </dependency>
参考版本:1.0.3
关于application的配置:
sdk: alipay: dev: true //true沙箱 //false正式 sign-type: RSA2 //验签方式 app-id: //APPID private-key: //私钥 alipay-public-key: //支付宝公钥
Controller的处理
支付请求的处理
@RequestMapping(value="/alipay/{orderId}/{money}/{subject}",method=RequestMethod.GET) public void pay(@PathVariable String orderId,@PathVariable String money,@PathVariable int subject,HttpServletResponse response) throws AlipayApiException, IOException{ JSONObject data=new JSONObject(); //订单号,必填 data.put("out_trade_no", orderId); //PC支付 FAST_INSTANT_TRADE_PAY, APP支付 QUICK_MSECURITY_PAY, 移动H5支付 QUICK_WAP_PAY data.put("product_code","FAST_INSTANT_TRADE_PAY"); //付款金额,必填 data.put("total_amount", money); //订单描述,必填 if(subject==0) data.put("subject","充值业务"); //该笔订单允许的最晚付款时间,逾期将关闭交易 //data.put("timeout_express",""); //公共校验参数 //data.put("passback_params",""); //PC支付 AlipayTradePagePayRequest request = new AlipayTradePagePayRequest(); //APP支付 //AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest(); //移动H5支付 //AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest(); //异步通知地址 request.setNotifyUrl("http://localhost:8765/pay/alipay/notify"); //同步通知地址 request.setReturnUrl("http://localhost:8765/pay/alipay/returnHandler"); //业务参数 request.setBizContent(data.toJSONString()); AlipayTradePagePayResponse alipayResponse=client.pageExecute(request); response.setContentType("text/html;charset=UTF-8"); response.getWriter().write(alipayResponse.getBody()); }
这里我传了三个参数作为示范,可以视情况自行修改,切记,如果支付方式有所变换,client.?Execute要 @RequestMapping(value="alipay/notify",method=RequestMethod.POST)
@ResponseBody public JSONObject notify(HttpServletRequest request) throws AlipayApiException, UnsupportedEncodingException { //获取支付宝POST过来反馈信息 Map<String, String> maps = new HashMap<String, String>(); Map<String, String[]> requestParams = request.getParameterMap(); for (Iterator<String> 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] + ","; } // 乱码解决,这段代码在出现乱码时使用。 //valueStr = new String(valueStr.getBytes("ISO-8859-1"),"utf-8"); maps.put(name, valueStr); } //调用SDK验证签名 boolean signVerified = AlipaySignature.rsaCheckV1(maps, "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAr4eu1yGt0mASOzTjIPCaRRkh+3LSr6hJGHN4ZSg00i96DD0zuo8FzL1md7ZET5dKTHgfcAg+ojNYWB30uU1fnDy9Xj1CW9GC/Ym0vbC1IfsWHN4WqobcyOdLtqU3c+SzCkLHkwV+5RP7mYnyiCpM00cZUv8NFRv/+L3epXnBRTvJhDYYtdaQf4hlQEGkbUKlXnfZxasRswuheNKjEkVziD4Bsk510qG1gefGosYspxDbSSHB8D/8Kdv/fi+0QoG3+uqqoIH8DosdoRjUrWwafd+0m8p+PMwQR4c1CHvvrEsmBADQAedb1W1peoZMk0hfTd4MoKRYsscSd7xkye57RwIDAQAB", "UTF-8", "RSA2"); if (signVerified) { // TODO 验签成功后 //处理结束之后,返回success,支付宝系统将不再发送异步回调请求 return ActionHelper.responseOk(); } return ActionHelper.responseFailed("支付验签失败"); } @RequestMapping(value="alipay/returnHandler",method=RequestMethod.GET) @ResponseBody public JSONObject returnHandler(HttpServletRequest request) { System.out.println("接收到支付宝的同步通知请求"); Map<String, String[]> maps = request.getParameterMap(); // for (Entry<String, String[]> map : maps.entrySet()) { // System.out.println(map.getKey()+"下的值------------"); // String[] value = map.getValue(); // for (String string : value) { // System.out.println(string); // } // } String orderId = maps.get("out_trade_no")[0];//获取订单号 //进行相应的业务处理return ActionHelper.responseOk(); }