溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

SpringBoot整合支付寶APP支付

發(fā)布時間:2020-10-06 00:38:13 來源:腳本之家 閱讀:136 作者:Snow、楊 欄目:編程語言

前言

現(xiàn)在是移動端產(chǎn)品瘋狂的年代,隨之,移動端支付也是熱門小技能,最近本公司在做一個移動端,要接入微信支付和支付寶支付,老習(xí)慣,功能做完之后做個復(fù)盤記錄,這邊主要講解支付寶APP支付

所需條件

1、創(chuàng)建螞蟻金服開放平臺公司賬號

2、選擇開發(fā)中心---->移動應(yīng)用

SpringBoot整合支付寶APP支付

3、選擇支付接入

SpringBoot整合支付寶APP支付

4、創(chuàng)建應(yīng)用

SpringBoot整合支付寶APP支付

5、查看應(yīng)用appId

SpringBoot整合支付寶APP支付

6、添加功能

兩個功能:支付寶授權(quán)功能和APP支付功能

SpringBoot整合支付寶APP支付

SpringBoot整合支付寶APP支付

7、功能簽約

SpringBoot整合支付寶APP支付

開發(fā)流程

引入支付寶支付SDK

<!-- 支付寶支付 SDK -->
<dependency>
 <groupId>com.alipay.sdk</groupId>
 <artifactId>alipay-sdk-java</artifactId>
 <version>3.7.4.ALL</version>
</dependency> 

1、支付寶參數(shù)配置類

/**
 * AlipayConfig.java
 * com.prereadweb.order.config
 * Copyright (c) 2019, 北京聚智未來科技有限公司版權(quán)所有.
 */
package com.prereadweb.order.config;
 
/**
 * @Description: 支付寶支付配置文件
 * @author: Administrator
 * @date: 2019/6/11 17:01
 */
public class AlipayConfig {
 
 // APPID
 public static String app_id = "你應(yīng)用的APPID";
 
 // 生成公鑰時對應(yīng)的私鑰(填自己的)
 public static String private_key = "你的秘鑰";
 
 //異步回調(diào)接口:得放到服務(wù)器上,且使用域名解析 IP
 public static String notify_url = "回調(diào)函數(shù)接口";
 
 
 //支付寶網(wǎng)關(guān)(注意沙箱alipaydev,正式則為 alipay)不需要修改
 public static String url = "https://openapi.alipay.com/gateway.do";
 
 //編碼類型
 public static String charset = "UTF-8";
 
 //數(shù)據(jù)類型
 public static String format = "json";
 
 // 公鑰
 public static String public_key = "你的公鑰";
 
 //簽名類型
 public static String signtype = "RSA2";
 
}

2、下單接口

controller層

 /**
 * @Function: 去支付
 * @author: YangXueFeng
 * @Date: 2019/6/11 16:10
 */
 @RequestMapping("/gotopay")
 public Object goToPay(@Param("orderId") Long orderId){
 return alipayViewService.setGotoPayInfos(orderId);
 }

service層

/**
 * @Function: 去支付
 * @author: YangXueFeng
 * @Date: 2019/6/11 16:11
 */
 @Override
 public Map<String, Object> setGotoPayInfos(Long orderId) {
 Map<String, Object> map = new HashMap<>();
 if(Util.isEmpty(orderId)){
 map.put("code", UserStatusEnum.EMPTY.intKey());
 map.put("msg", UserStatusEnum.EMPTY.value());
 return map;
 }
 /* 查詢訂單信息 */
 PayParameterForm payParameter = orderMapper.getPayParameter(orderId);
 AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.url, AlipayConfig.app_id, AlipayConfig.private_key, AlipayConfig.format, AlipayConfig.charset, AlipayConfig.public_key, AlipayConfig.signtype);//支付寶需要的參數(shù)serverUrl、appId、private_key、format、charset、public_key、signType
 AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
 AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
 model.setBody(payParameter.getTitle());//商品信息
 model.setSubject(payParameter.getTitle());//商品名稱
 model.setOutTradeNo(String.valueOf(payParameter.getOrderId()));//訂單號
 model.setTimeoutExpress("30m");//支付超時時間
/*
 model.setTotalAmount(String.valueOf(payParameter.getActualPrice()));// 支付金額
*/
 model.setTotalAmount(String.valueOf(0.01));// 支付金額
 request.setBizModel(model);
 // 回調(diào)地址(充值訂單)
 request.setNotifyUrl(AlipayConfig.notify_url);// 回調(diào)地址
 //這里和普通的接口調(diào)用不同,使用的是sdkExecute
 AlipayTradeAppPayResponse response = null;
 try {
 response = alipayClient.sdkExecute(request);
 map.put("code", UserStatusEnum.SUCCESS.intKey());
 map.put("msg", UserStatusEnum.SUCCESS.value());
 Map<String, Object> dataMap = new HashMap<>();
 dataMap.put("payPath", response.getBody());
 map.put("data", dataMap);
 return map;
 } catch (AlipayApiException e) {
 e.printStackTrace();
 }
 map.put("code", UserStatusEnum.ERROR.intKey());
 map.put("msg", UserStatusEnum.ERROR.value());
 return map;
 }

3、回調(diào)接口

controller層

/**
 * @Function: 支付寶異步通知回調(diào)
 * @author: YangXueFeng
 * @Date: 2019/6/11 20:02
 */
 @ResponseBody
 @RequestMapping("/notify")
 public String notify(HttpServletRequest request, HttpServletResponse response) {
 return alipayViewService.notify(request, response);
 }

service層

/**
 * @Function: 支付寶異步回調(diào)接口
 * @author: YangXueFeng
 * @Date: 2019/6/11 20:03
 */
 @Override
 public String notify(HttpServletRequest request, HttpServletResponse response) {
 Map<String, String> params = new HashMap<String, String>();
 //從支付寶回調(diào)的request域中取值
 Map<String, String[]> requestParams = request.getParameterMap();
 
 for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext();) {
 String name = iter.next();
 String[] values = requestParams.get(name);
 String valueStr = "";
 for (int i = 0; i < values.length; i++) {
 valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
 }
 // 亂碼解決,這段代碼在出現(xiàn)亂碼時使用。如果mysign和sign不相等也可以使用這段代碼轉(zhuǎn)化
 // valueStr = new String(valueStr.getBytes("ISO-8859-1"), "gbk");
 params.put(name, valueStr);
 }
 //商品訂單號
 String out_trade_no = request.getParameter("out_trade_no"); // 商戶訂單號
 // 當(dāng)前交易狀態(tài)
 String tradeStatus = request.getParameter("trade_status"); //交易狀態(tài)
 // 支付金額
 String totalAmount = request.getParameter("total_amount"); //交易狀態(tài)
 // 支付時間
 String payDate = request.getParameter("gmt_payment"); //交易狀態(tài)
 //3.簽名驗證(對支付寶返回的數(shù)據(jù)驗證,確定是支付寶返回的)
 boolean signVerified = false;
 try {
 //3.1調(diào)用SDK驗證簽名
 signVerified = AlipaySignature.rsaCheckV1(params, AlipayConfig.public_key, AlipayConfig.charset, AlipayConfig.signtype);
 } catch (AlipayApiException e) {
 e.printStackTrace();
 }
 //返回狀態(tài)存入redis中
 //對驗簽進(jìn)行處理
 if (signVerified) {
 //驗簽通過
 if(tradeStatus.equals("TRADE_SUCCESS")) {
 //支付成功后的業(yè)務(wù)處理
 OrderEntity order = orderMapper.getOrderInfo(Long.valueOf(out_trade_no));
 if(!Util.isEmpty(order)){
  order.setStatus(CalculatStaticConstant.CHECK_ONE);
  order.setCompleteTime(DateUtil.currentDate());
  orderMapper.updateOrder(order);
 }
 /* 添加支付信息 */
 OrderPayEntity orderPay = new OrderPayEntity();
 orderPay.setId(Long.valueOf(IdUtils.getPrimaryKey()));
 orderPay.setOrderId(order.getId());
 orderPay.setUserId(order.getUserId());
 orderPay.setPayPrice(Double.valueOf(totalAmount));
 orderPay.setPayType(PayTypeEnum.ALI_PAY.intKey());
 orderPay.setStatus(CalculatStaticConstant.CHECK_ONE);
 orderPay.setPayTime(payDate);
 orderMapper.saveOrderPay(orderPay);
 RedisUtil.set("ali"+out_trade_no, tradeStatus,300);
 return "success";
 }
 } else { //驗簽不通過
 System.err.println("驗簽失敗");
 return "failure";
 }
 return "failure";
 }

此處回調(diào)函數(shù)接口要有返回值,成功后返回success(success是小寫,別大寫),此處如果沒有返回值的話,支付寶會一直進(jìn)行回調(diào)通知

至此,支付寶APP支付OK

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI