先在支付宝客户端申请自己的应用。获取所有需要用的参数。
maven引入:
<dependency><groupId>com.alipay.sdk</groupId><artifactId>alipay-sdk-java</artifactId><exclusions><exclusion><artifactId>fastjson</artifactId><groupId>com.alibaba</groupId></exclusion></exclusions></dependency>
静态类:AliConstants
package com.jeecms.jspgouexclusive.alipay;public class AliConstants {//支付宝网关public static final String gatewayUrl = "/gateway.do";//应用IDpublic static final String appId = "";//应用私钥public static final String privateKey = "";//应用公钥public static final String publicKey = "";//支付宝公钥public static final String alipayPublicKey = "";//加密方式public static final String signType = "RSA2";//字符编码格式public static final String charset = "utf-8";}
工具类:AlipayUtils
package com.jeecms.jspgouexclusive.alipay;import com.alibaba.fastjson.JSONObject;import com.alipay.api.AlipayApiException;import com.alipay.api.AlipayClient;import com.alipay.api.DefaultAlipayClient;import com.alipay.api.domain.AlipayTradeAppPayModel;import com.alipay.api.domain.AlipayTradeRefundModel;import com.alipay.api.request.AlipayTradeAppPayRequest;import com.alipay.api.request.AlipayTradeQueryRequest;import com.alipay.api.request.AlipayTradeRefundRequest;import com.alipay.api.response.AlipayTradeAppPayResponse;import com.alipay.api.response.AlipayTradeQueryResponse;import com.alipay.api.response.AlipayTradeRefundResponse;import org.ponent;@Componentpublic class AlipayUtils {public String getPay(String orderId,String totalAmount,String goodsName){//实例化客户端AlipayClient alipayClient = new DefaultAlipayClient(AliConstants.gatewayUrl, AliConstants.appId, AliConstants.privateKey, "json", AliConstants.charset, AliConstants.publicKey, AliConstants.signType);//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.payAlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();//SDK已经封装掉了公共参数,这里只需要传入业务参数。以下方法为sdk的model入参方式(model和biz_content同时存在的情况下取biz_content)。AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();model.setBody("支付宝支付测试");model.setSubject("App订单支付,商品名称:" + goodsName);model.setOutTradeNo(orderId);model.setTimeoutExpress("30m");model.setTotalAmount(totalAmount);model.setProductCode("QUICK_MSECURITY_PAY");request.setBizModel(model);request.setNotifyUrl("成功回调接口,域名应该配置在支付宝");try {//这里和普通的接口调用不同,使用的是sdkExecuteAlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);return response.getBody();} catch (AlipayApiException e) {e.printStackTrace();}return null;}public String getResult(String orderId) {//实例化客户端AlipayClient alipayClient = new DefaultAlipayClient("/gateway.do",AliConstants.appId,AliConstants.privateKey,"json","utf-8",AliConstants.alipayPublicKey,"RSA2");//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.payAlipayTradeQueryRequest request = new AlipayTradeQueryRequest();JSONObject bizContent = new JSONObject();bizContent.put("out_trade_no", orderId);//bizContent.put("trade_no", alipayId);request.setBizContent(bizContent.toString());AlipayTradeQueryResponse response = null;try {response = alipayClient.execute(request);System.out.println(response.getBody());if(response.isSuccess()){System.out.println("调用成功");} else {System.out.println("调用失败");}return response.getBody();} catch (AlipayApiException e) {e.printStackTrace();}return null;}/*** 统一收单交易退款查询接口* @return*/public String tuikuan() {AlipayClient alipayClient = new DefaultAlipayClient("/gateway.do",AliConstants.appId,AliConstants.privateKey,"json","GBK",AliConstants.alipayPublicKey,"RSA2");//AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();AlipayTradeRefundModel model= new AlipayTradeRefundModel();//外部订单号,与tradeNo二选一model.setOutTradeNo("");model.setTradeNo("");//model.setOutRequestNo("");model.setRefundAmount("0.01");request.setBizModel(model);try {AlipayTradeRefundResponse response = alipayClient.execute(request);System.out.println(response.getBody());if(response.isSuccess()){System.out.println("调用成功");} else {System.out.println("调用失败");}return response.getBody();} catch (AlipayApiException e) {e.printStackTrace();}return null;}/*** 退款接口* @return*/public String refund(String orderId, double amount){AlipayClient alipayClient = new DefaultAlipayClient("/gateway.do",AliConstants.appId,AliConstants.privateKey,"json","GBK",AliConstants.alipayPublicKey,"RSA2");AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();JSONObject bizContent = new JSONObject();//外部订单号,与tradeNo二选一bizContent.put("out_trade_no", orderId);//bizContent.put("trade_no", "");bizContent.put("refund_amount", amount);bizContent.put("out_request_no", "HZ01RF001");request.setBizContent(bizContent.toString());AlipayTradeRefundResponse response = null;try {response = alipayClient.execute(request);System.out.println(response.getBody());if(response.isSuccess()){System.out.println("调用成功");} else {System.out.println("调用失败");}return response.getBody();} catch (AlipayApiException e) {e.printStackTrace();}return null;}public static void main(String[] args) {AlipayUtils alipayUtils = new AlipayUtils();alipayUtils.refund("订单号",0.01);}}
成功回调方法:
@ApiOperation(value = "获取支付宝返回结果")@RequestMapping("/getAliPayResult")public void getresult(HttpServletRequest request) {Map<String, String[]> requestMap = request.getParameterMap();Map<String, String> paramsMap = new HashMap<>();requestMap.forEach((key, values) -> {String strs = "";for(String value : values) {strs = strs + value;}paramsMap.put(key, strs);});//调用SDK验证签名try {AlipaySignature.rsaCheckV1(paramsMap, AliConstants.publicKey, AliConstants.charset, AliConstants.signType);System.out.println("*********************验签成功********************");//支付宝支付订单号String tradeNo = paramsMap.get("trade_no");//订单idString outTradeNo = paramsMap.get("out_trade_no");if(StringUtils.isBlank(tradeNo) || StringUtils.isBlank(outTradeNo)){throw new GlobalException("订单ID或支付ID不能为空!");}//后续是业务流程操作。。。。。} catch (AlipayApiException e) {e.printStackTrace();System.out.println("*********************验签失败********************");}}