侧边栏壁纸
博主头像
JavaLYG 博主等级

行动起来,活在当下

  • 累计撰写 32 篇文章
  • 累计创建 8 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录

支付宝开放平台api接口封装统一调用方式

liuyg
2023-01-04 / 0 评论 / 0 点赞 / 49 阅读 / 0 字 / 正在检测是否收录...

在对接支付宝过程中,发现支付宝每个接口例子,都定义了AlipayClient和xxxRequest、xxxResponse。

老的写法

每次都去写new一个AlipayClient

package com.jws.smilepay.service;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.request.AlipayUserInfoShareRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AlipayUserInfoShareResponse;

/**
 * @author liuyg
 * @date 2022年09月01日 14:09
 */

public class dome {

    private static String url = "https://openapi.alipay.com/gateway.do";
    //应用id
    private static String appid = "app_id";

    //应用私钥
    private static String private_key = "your private_key";

    //支付宝公钥
    private static String alipay_public_key = "your alipay_public_key";

    
    /** 
     * @Author liuyg
     * @Description 换取授权访问令牌
     * @Date 2022/9/1 14:10
     */
    public void oauthToken() {
        AlipayClient alipayClient = new DefaultAlipayClient(url,appid,private_key,"json","GBK",alipay_public_key,"RSA2");
        AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
        request.setGrantType("authorization_code");
        request.setCode("4b203fe6c11548bcabd8da5bb087a83b");
        request.setRefreshToken("201208134b203fe6c11548bcabd8da5bb087a83b");
        AlipaySystemOauthTokenResponse response = null;
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }
        if(response.isSuccess()){
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
    }

    /** 
     * @Author liuyg
     * @Description 支付宝会员授权信息查询接口 
     * @Date 2022/9/1 14:18 
     * @Param accessToken 
     */
    public void infoShare(String accessToken){
        AlipayClient alipayClient = new DefaultAlipayClient(url,appid,private_key,"json","GBK",alipay_public_key,"RSA2");
        AlipayUserInfoShareRequest request = new AlipayUserInfoShareRequest();
        AlipayUserInfoShareResponse response = null;
        try {
            response = alipayClient.execute(request,accessToken);
        } catch (AlipayApiException e) {
            throw new RuntimeException(e);
        }
        if(response.isSuccess()){
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
    }
    
    
}

后来发现,调用什么接口要根据入参和返参来定义。所以不需要每个接口都写入这么多的AlipayClient,我们可以把AlipayClient统一封装,只需要定义xxxRequest、xxxResponse即可。

20220901142951903.png

改造后,可以做一些日志记录,数据处理等等。一个调用方法也可以缩减到最少3行的程度!

改造后代码如下

package com.jws.smilepay.service;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.*;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.request.AlipayUserInfoShareRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AlipayUserInfoShareResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author liuyg
 * @date 2022年09月01日 14:09
 */

public class dome {

    private Logger log = LoggerFactory.getLogger(getClass());
    private static String url = "https://openapi.alipay.com/gateway.do";
    //应用id
    private static String appid = "app_id";

    //应用私钥
    private static String private_key = "your private_key";

    //支付宝公钥
    private static String alipay_public_key = "your alipay_public_key";


    /**
     * @Author liuyg
     * @Description 换取授权访问令牌
     * @Date 2022/9/1 14:10
     */
    public void oauthToken() {
        AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
        request.setGrantType("authorization_code");
        request.setCode("4b203fe6c11548bcabd8da5bb087a83b");
        request.setRefreshToken("201208134b203fe6c11548bcabd8da5bb087a83b");
        AlipaySystemOauthTokenResponse response = new AlipaySystemOauthTokenResponse();
        response = (AlipaySystemOauthTokenResponse) postAli(request, response, null);
    }

    /**
     * @Author liuyg
     * @Description 支付宝会员授权信息查询接口
     * @Date 2022/9/1 14:18
     * @Param accessToken
     */
    public void infoShare(String accessToken) {
        AlipayUserInfoShareRequest request = new AlipayUserInfoShareRequest();
        AlipayUserInfoShareResponse response = new AlipayUserInfoShareResponse();
        response = (AlipayUserInfoShareResponse) postAli(request, response, accessToken);
    }

    /**
     * @Author liuyg
     * @Description 支付宝开放平台统一调用接口
     * @Date 2022/9/1 14:23
     * @Param request
     * @Param response
     * @Param accessToken
     * @Return com.alipay.api.AlipayResponse
     */
    public AlipayResponse postAli(AlipayRequest request, AlipayResponse response, String accessToken) {
        long startTime = 0;
        long endTime = 0;
        AlipayClient alipayClient = new DefaultAlipayClient(url, appid, private_key, "json", "GBK", alipay_public_key, "RSA2");
        try {
            log.info("支付宝请求参数:{}", JSONObject.toJSONString(request));
            startTime = System.currentTimeMillis();
            if (StringUtils.isNotBlank(accessToken)) {
                response = alipayClient.execute(request, accessToken);
            } else {
                response = alipayClient.execute(request);
            }
            endTime = System.currentTimeMillis();
            log.info("请求耗时:{},支付宝请求返回参数:{}", endTime - startTime, JSONObject.toJSONString(response));
        } catch (AlipayApiException e) {
            log.error("支付宝请求异常:", e);
        } catch (Exception e) {
            log.error("发送请求异常,请检查url地址是否正确,网络是否畅通!", e);
        }
        return response;
    }


}
0

评论区