zoukankan      html  css  js  c++  java
  • 支付宝支付-常用支付API详解(查询、退款、提现等)-转

    所有的接口支持沙盒环境的测试

    1、前言

    前面几篇文件详细介绍了 支付宝提现扫码支付条码支付Wap支付App支付

    其中也断断续续的提到了一些接口。本片文章主要是总结支付宝支付中常用的一些接口

    2、常用的接口总结

    这里使用表格的方式列出 官方接口列表以及详细的参数说明

    API列表类型描述使用场景
    alipay.trade.query 免费 统一收单线下交易查询 提供所有支付宝支付订单的查询
    alipay.trade.refund 免费 统一收单交易退款接口 买家或者卖家的原因需要退款时
    alipay.trade.fastpay.refund.query 免费 统一收单交易退款查询 查看退款请求是否执行成功
    alipay.trade.pay 免费 统一收单交易支付接口 将二维码或条码信息/声波信息通过本接口上送至支付宝发起支付。
    alipay.trade.precreate 免费 统一收单线下交易预创建 二维码支付
    alipay.trade.cancel 免费 统一收单交易撤销接口 撤销交易
    alipay.trade.create 免费 统一收单交易创建接口 创建下单
    alipay.trade.close 免费 统一收单交易关闭接口 用于交易创建后,用户在一定时间内未进行支付时可以通过此接口关闭订单
    alipay.trade.order.settle 免费 统一收单交易结算接口 用于在线下场景交易支付后,进行结算
    alipay.fund.trans.toaccount.transfer 免费 单笔转账到支付宝账户接口 支付宝提现
    alipay.fund.trans.order.query 免费 查询转账订单接口 提现结果查询
    alipay.data.dataservice.bill.downloadurl.query 免费 查询对账单下载地址 为方便商户快速查账

    3、使用服务端SDK封装接口

    3.1 服务端SDK下载及其使用方法

    参考 开放平台服务端SDK

    Maven项目引用JAR包可以参考 支付宝Wap支付你了解多少? 里面有详细的介绍

    重要说明
    1、接口使用的编码格式为 UTF-8
    2、接口数据交互使用的是 json
    3、接口加密的模式使用官方推荐的 RSA2
    4、本片文章主要是介绍Java的使用方法与封装

    3.2 初始化SDK

    在SDK调用前需要进行初始化

    AlipayClient alipayClient = new DefaultAlipayClient(URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);
    

    关键参数说明:

    配置参数示例值解释获取方式/示例值
    URL 支付网关(固定) 正式环境:https://openapi.alipay.com/gateway.do 沙箱环境:https://openapi.alipaydev.com/gateway.do
    APP_ID APPID即创建应用后生成 获取见上面创建应用并获取APPID
    APP_PRIVATE_KEY 开发者应用私钥,由开发者自己生成 获取详见上面配置密钥
    FORMAT 参数返回格式,只支持json json(固定)
    CHARSET 请求和签名使用的字符编码格式,支持GBK和UTF-8 开发者根据实际工程编码配置
    ALIPAY_PUBLIC_KEY 支付宝公钥,由支付宝生成 获取详见上面配置密钥
    SIGN_TYPE 商户生成签名字符串所使用的签名算法类型,目前支持RSA2和RSA,推荐使用RSA2 RSA2
    3.3 API接口封装
    3.3.1 alipay.trade.query接口封装

    该接口提供所有支付宝支付订单的查询,商户可以通过该接口主动查询订单状态,完成下一步的业务逻辑。 需要调用查询接口的情况: 当商户后台、网络、服务器等出现异常,商户系统最终未接收到支付通知; 调用支付接口后,返回系统错误或未知交易状态情况; 调用alipay.trade.pay,返回INPROCESS的状态; 调用alipay.trade.cancel之前,需确认支付状态;

    /**
         * 交易查询接口
         * https://doc.open.alipay.com/docs/api.htm?spm=a219a.7395905.0.0.8H2JzG&docType=4&apiId=757
         * @param bizContent
         * @return
         * @throws AlipayApiException
         */
        public static boolean isTradeQuery(AlipayTradeQueryModel model) throws AlipayApiException{
            AlipayTradeQueryResponse response = tradeQuery(model);
            if(response.isSuccess()){
                return true;
            }
            return false;
        }
        
        public static AlipayTradeQueryResponse  tradeQuery(AlipayTradeQueryModel model) throws AlipayApiException{
            AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
            request.setBizModel(model);
            return alipayClient.execute(request);
        }
    3.3.2 alipay.trade.refund 接口封装

    当交易发生之后一段时间内,由于买家或者卖家的原因需要退款时,卖家可以通过退款接口将支付款退还给买家,支付宝将在收到退款请求并且验证成功之后,按照退款规则将支付款按原路退到买家帐号上。 交易超过约定时间(签约时设置的可退款时间)的订单无法进行退款 支付宝退款支持单笔交易分多次退款,多次退款需要提交原支付订单的商户订单号和设置不同的退款单号。一笔退款失败后重新提交,要采用原来的退款单号。总退款金额不能超过用户实际支付金额

    /**
         * 退款
         * https://doc.open.alipay.com/docs/api.htm?spm=a219a.7395905.0.0.SAyEeI&docType=4&apiId=759
         * @param content
         * @return
         * @throws AlipayApiException
         */
        public static String tradeRefund(AlipayTradeRefundModel model) throws AlipayApiException{
            AlipayTradeRefundResponse response = tradeRefundToResponse(model);
            return response.getBody();
        }
        public static AlipayTradeRefundResponse tradeRefundToResponse(AlipayTradeRefundModel model) throws AlipayApiException{
            AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
            request.setBizModel(model);
            return alipayClient.execute(request);
        }
    3.3.3 alipay.trade.fastpay.refund.query接口封装

    商户可使用该接口查询自已通过alipay.trade.refund提交的退款请求是否执行成功。 该接口的返回码10000,仅代表本次查询操作成功,不代表退款成功。如果该接口返回了查询数据,则代表退款成功,如果没有查询到则代表未退款成功,可以调用退款接口进行重试。重试时请务必保证退款请求号一致。

    /**
         * 退款查询
         * https://doc.open.alipay.com/docs/api.htm?spm=a219a.7629065.0.0.KQeTSa&apiId=1049&docType=4
         * @param model
         * @return
         * @throws AlipayApiException
         */
        public static String tradeRefundQuery(AlipayTradeFastpayRefundQueryModel model) throws AlipayApiException{
            AlipayTradeFastpayRefundQueryResponse response = tradeRefundQueryToResponse(model);
            return response.getBody();
        }
        
        public static AlipayTradeFastpayRefundQueryResponse tradeRefundQueryToResponse(AlipayTradeFastpayRefundQueryModel model) throws AlipayApiException{
            AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();
            request.setBizModel(model);
            return alipayClient.execute(request);
        }
    3.3.4 alipay.trade.pay接口封装

    收银员使用扫码设备读取用户手机支付宝“付款码”/声波获取设备(如麦克风)读取用户手机支付宝的声波信息后,将二维码或条码信息/声波信息通过本接口上送至支付宝发起支付。

    /**
         * 条形码支付、声波支付
         * https://doc.open.alipay.com/docs/api.htm?spm=a219a.7629065.0.0.XVqALk&apiId=850&docType=4
         * @param notifyUrl 
         * @throws AlipayApiException
         */
        public static String tradePay(AlipayTradePayModel model, String notifyUrl) throws AlipayApiException {
            AlipayTradePayResponse response = tradePayToResponse(model,notifyUrl);
            return response.getBody();
        }
        
        public static AlipayTradePayResponse tradePayToResponse(AlipayTradePayModel model, String notifyUrl) throws AlipayApiException{
            AlipayTradePayRequest request = new AlipayTradePayRequest();
            request.setBizModel(model);// 填充业务参数
            request.setNotifyUrl(notifyUrl);
            return alipayClient.execute(request); // 通过alipayClient调用API,获得对应的response类
        }
    3.3.5 alipay.trade.precreate 接口封装

    收银员通过收银台或商户后台调用支付宝接口,生成二维码后,展示给用户,由用户扫描二维码完成订单支付。

    /**
         * 扫码支付
         * https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.i0UVZn&treeId=193&articleId=105170&docType=1#s4
         * @param notifyUrl 
         * @return
         * @throws AlipayApiException 
         */
        public static String tradePrecreatePay(AlipayTradePrecreateModel model, String notifyUrl) throws AlipayApiException{
            AlipayTradePrecreateResponse response = tradePrecreatePayToResponse(model,notifyUrl);
            return response.getBody();
        }
        public static AlipayTradePrecreateResponse tradePrecreatePayToResponse(AlipayTradePrecreateModel model, String notifyUrl) throws AlipayApiException{
            AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
            request.setBizModel(model);
            request.setNotifyUrl(notifyUrl);
            return alipayClient.execute(request);
        }
    3.3.6 alipay.trade.cancel 接口封装

    支付交易返回失败或支付系统超时,调用该接口撤销交易。如果此订单用户支付失败,支付宝系统会将此订单关闭;如果用户支付成功,支付宝系统会将此订单资金退还给用户。 注意:只有发生支付系统超时或者支付结果未知时可调用撤销,其他正常支付的单如需实现相同功能请调用申请退款API。提交支付交易后调用【查询订单API】,没有明确的支付结果再调用【撤销订单API】。

    /**
         * 交易撤销接口
         * https://doc.open.alipay.com/docs/api.htm?spm=a219a.7395905.0.0.XInh6e&docType=4&apiId=866
         * @param bizContent
         * @return
         * @throws AlipayApiException
         */
        public static boolean isTradeCancel(AlipayTradeCancelModel model) throws AlipayApiException{
            AlipayTradeCancelResponse response = tradeCancel(model);
            if(response.isSuccess()){
                return true;
            }
            return false;
        }
        
        public static AlipayTradeCancelResponse tradeCancel(AlipayTradeCancelModel model) throws AlipayApiException{
            AlipayTradeCancelRequest request = new AlipayTradeCancelRequest();
            request.setBizModel(model);
            AlipayTradeCancelResponse response = alipayClient.execute(request);
            return response;
        }
    3.3.7 alipay.trade.create 接口封装

    商户通过该接口进行交易的创建下单

    /**
         * 统一收单交易创建接口
         * https://doc.open.alipay.com/docs/api.htm?spm=a219a.7629065.0.0.21yRUe&apiId=1046&docType=4
         * @param model
         * @param notifyUrl 
         * @return
         * @throws AlipayApiException
         */
        public static AlipayTradeCreateResponse tradeCreate(AlipayTradeCreateModel model, String notifyUrl) throws AlipayApiException{
            AlipayTradeCreateRequest request = new AlipayTradeCreateRequest();
            request.setBizModel(model);
            request.setNotifyUrl(notifyUrl);
            return alipayClient.execute(request);
        }
    3.3.8 alipay.trade.close 接口封装

    用于交易创建后,用户在一定时间内未进行支付,可调用该接口直接将未付款的交易进行关闭。

    /**
         * 关闭订单
         * https://doc.open.alipay.com/docs/api.htm?spm=a219a.7629065.0.0.21yRUe&apiId=1058&docType=4
         * @param model
         * @return
         * @throws AlipayApiException
         */
        public static boolean isTradeClose(AlipayTradeCloseModel model) throws AlipayApiException{
            AlipayTradeCloseResponse response = tradeClose(model);
            if(response.isSuccess()){
                return true;
            }
            return false;
        }
        
        public static AlipayTradeCloseResponse tradeClose(AlipayTradeCloseModel model) throws AlipayApiException{
            AlipayTradeCloseRequest request = new AlipayTradeCloseRequest();
            request.setBizModel(model);
            return alipayClient.execute(request);
            
        }
    3.3.9 alipay.trade.order.settle接口封装

    用于在线下场景交易支付后,进行结算

    /**
         * 交易结算接口
         * https://doc.open.alipay.com/docs/api.htm?spm=a219a.7395905.0.0.nl0RS3&docType=4&apiId=1147
         * @param bizContent
         * @return
         * @throws AlipayApiException
         */
        public static boolean isTradeOrderSettle(AlipayTradeOrderSettleModel model) throws AlipayApiException{
            AlipayTradeOrderSettleResponse  response  = tradeOrderSettle(model);
            if(response.isSuccess()){
                return true;
            }
            return false;
        }
        
        public static AlipayTradeOrderSettleResponse tradeOrderSettle(AlipayTradeOrderSettleModel model) throws AlipayApiException{
            AlipayTradeOrderSettleRequest request = new AlipayTradeOrderSettleRequest();
            request.setBizModel(model);
            return alipayClient.execute(request);
        }
    3.3.10 alipay.fund.trans.toaccount.transfer接口封装

    可以参考 支付宝支付-提现到个人支付宝

    3.3.11 alipay.fund.trans.order.query接口封装

    可以参考 支付宝支付-提现到个人支付宝

    3.3.12 alipay.data.dataservice.bill.downloadurl.query 接口封装

    为方便商户快速查账,支持商户通过本接口获取商户离线账单下载地址

    /**
         * 查询对账单下载地址
         * @param bizContent
         * @return
         * @throws AlipayApiException
         */
        public static String billDownloadurlQuery(AlipayDataDataserviceBillDownloadurlQueryModel model) throws AlipayApiException{
            AlipayDataDataserviceBillDownloadurlQueryResponse response =  billDownloadurlQueryToResponse(model);
            return response.getBillDownloadUrl();
        }
        
        public static AlipayDataDataserviceBillDownloadurlQueryResponse  billDownloadurlQueryToResponse (AlipayDataDataserviceBillDownloadurlQueryModel model) throws AlipayApiException{
            AlipayDataDataserviceBillDownloadurlQueryRequest request = new AlipayDataDataserviceBillDownloadurlQueryRequest();
            request.setBizModel(model);
            return alipayClient.execute(request);
        }

    4、异步通知封装

    将异步通知的参数转化为Map为验签做准备

    /**
         * 将异步通知的参数转化为Map
         * @param request
         * @return
         */
        public static Map<String, String> toMap(HttpServletRequest request) {
            System.out.println(">>>>" + request.getQueryString());
            Map<String, String> params = 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");
                params.put(name, valueStr);
            }
            return params;
        }

    使用AlipaySignature.rsaCheckV1(....)接口进行验证签名

    public void notify_url() {
            try {
                // 获取支付宝POST过来反馈信息
                Map<String, String> params = AliPayApi.toMap(getRequest());
    
                for (Map.Entry<String, String> entry : params.entrySet()) {
                    System.out.println(entry.getKey() + " = " + entry.getValue());
                }
    
                boolean verify_result = AlipaySignature.rsaCheckV1(params, AliPayApi.ALIPAY_PUBLIC_KEY, AliPayApi.CHARSET,
                        AliPayApi.SIGN_TYPE);
    
                if (verify_result) {// 验证成功
                    // TODO 请在这里加上商户的业务逻辑程序代码 异步通知可能出现订单重复通知 需要做去重处理
                    System.out.println("notify_url 验证成功succcess");
                    renderText("success");
                    return;
                } else {
                    System.out.println("notify_url 验证失败");
                    // TODO
                    renderText("failure");
                    return;
                }
            } catch (AlipayApiException e) {
                e.printStackTrace();
                renderText("failure");
            }
        }

    此项目已开源欢迎Start、PR、发起Issues一起讨论交流共同进步

    https://github.com/Javen205/IJPay
    http://git.oschina.net/javen205/IJPay

    http://www.cnblogs.com/zyw-205520/p/6920568.html

  • 相关阅读:
    mysql
    jvm--动态对象年龄判定
    JVM中的STW和CMS
    jvm中的垃圾收集器serial、parNew、parallel Scavenge;serial old、parallel old、CMS、G1
    concurrenthashmap在1.8和1.7里面有什么区别
    java中的深浅拷贝
    JDK动态代理和CGLIB代理的区别
    java 锁的底层原理
    死锁
    jvm新生代和老年代分别使用什么垃圾回收算法
  • 原文地址:https://www.cnblogs.com/soundcode/p/6922716.html
Copyright © 2011-2022 走看看