支付宝支付接口调用

发布时间:2022-11-12 23:00

先在支付宝客户端申请自己的应用。获取所有需要用的参数。

maven引入:


            com.alipay.sdk
            alipay-sdk-java
            
                
                    fastjson
                    com.alibaba
                
            
        

静态类:AliConstants

package com.jeecms.jspgouexclusive.alipay;

public class AliConstants {

    //支付宝网关
    public static final String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    //应用ID
    public 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.springframework.stereotype.Component;

@Component
public 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.pay
        AlipayTradeAppPayRequest 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 {
            //这里和普通的接口调用不同,使用的是sdkExecute
            AlipayTradeAppPayResponse response = alipayClient.sdkExecute(request);
            return response.getBody();
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String getResult(String orderId) {
        //实例化客户端
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",AliConstants.appId,AliConstants.privateKey,"json","utf-8",AliConstants.alipayPublicKey,"RSA2");
        //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
        AlipayTradeQueryRequest 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("https://openapi.alipay.com/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("https://openapi.alipay.com/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 requestMap = request.getParameterMap();
        Map 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");
            //订单id
            String 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("*********************验签失败********************");
        }
    }

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号