zoukankan      html  css  js  c++  java
  • 支付宝退款

    注册服务

    • 申请账号

      https://memberprod.alipay.com/account/reg/enterpriseIndex.htm

    • 创建应用

        去支付宝的开放平台
            https://open.alipay.com/platform/home.htm
        添加应用:
             https://docs.open.alipay.com/200/105310 

      按照这个些文档一步一步将公司账号注册成功,包括要是用的应用

    退款接口文档

      https://docs.open.alipay.com/api_1/alipay.trade.refund

    下载SDK

      https://docs.open.alipay.com/291/105974

    生成密钥

      https://docs.open.alipay.com/291/105971

    将SDK集成近项目

      

        将这个jar上传到私服(如何将jar上传到私服

    java代码

    RefundConfig类文件
    package com.ccytsoft.wkc.refund;
    
    import com.we.core.common.util.DateTimeUtil;
    import lombok.Data;
    
    /**
     * 退款配置
     * <P>
     * 这里放置的是退款的公共请求参数
     *
     * @author kuangxiang(kuangxiang666@yeah.net)
     * @Date 15:41 2017/12/11
     */
    @Data
    public class RefundConfig {
    
        /**
         * 支付宝网关
         */
        public String gatewayUrl = "https://openapi.alipay.com/gateway.do";
    
        /**
         * 支付宝分配给开发者的应用ID
         * <P>
         * 例如:2014072300007148
         */
        private String app_id="";
    
        /**
         *     接口名称
         *     <P>
         *  退款的接口名
         */
        private String method="alipay.trade.refund";
    
        /**
         *数据格式
         * <P>
         * 仅支持JSON
         *
         */
        private String format="JSON";
    
        /**
         *请求使用的编码格式,
         *     <P>
         *如utf-8,gbk,gb2312等
         */
        private String charset="utf-8";
    
        /**
         *商户生成签名字符串所使用的签名算法类型
         *     <P>
         *目前支持RSA2和RSA,推荐使用RSA2
         */
        private String sign_type = "RSA2";
    
        /**
         *商户请求参数的签名串
         *     <P>
         *支付宝开放平台SDK封装了签名和验签过程,只需配置账号及密钥参数即可
         */
        private String sign="";
    
        /**
         *时间戳
         *     <P>
         *发送请求的时间,格式"yyyy-MM-dd HH:mm:ss"
         */
        private String timestamp= DateTimeUtil.format(DateTimeUtil.nowDate(),"yyyy-MM-dd HH:mm:ss");
    
        /**
         *调用的接口版本,固定为:1.0
         *
         */
        private String version="1.0";
    
        /**
         *这个不是必须的
         *     <P>
         *
         */
        private String app_auth_token="";
    
        /**
         *请求参数的集合
         *     <P>
         *最大长度不限,除公共参数外所有请求参数都必须放在这个参数中传递,具体参照各产品快速接入文档
         */
        private String biz_content;
        /**
         * 商户私钥,您的PKCS8格式RSA2私钥
         */
        public String merchant_private_key = "";
    
        /**
         * 支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
         */
        public String alipay_public_key = "";
    
    }

    service代码

    package com.ccytsoft.wkc.refund;
    
    import com.alipay.api.AlipayClient;
    import com.alipay.api.DefaultAlipayClient;
    import com.alipay.api.request.AlipayTradeRefundRequest;
    import com.alipay.api.response.AlipayTradeRefundResponse;
    import com.we.core.common.util.ConvertUtil;
    import com.we.core.common.util.ExceptionUtil;
    import com.we.core.common.util.Util;
    import org.springframework.stereotype.Service;
    
    /**
     * 阿里巴巴退款service
     *
     * @author kuangxiang(kuangxiang666@yeah.net)
     * @Date 16:29 2017/12/11
     */
    @Service
    public class AlipayRefundBusinessService {
        /**
         * 阿里的配置文件对象
         */
        private RefundConfig refundConfig;
        /**
         * 构建退款请求
         *
         * @param outTradeNo 订单号
         * @param refundAmount 付款金额
         * @param refundReason 退款原因
         * @param operatorId 操作人Id
         *
         */
        public void buildRefundRequest(long outTradeNo,long refundAmount,String refundReason,long operatorId) throws Exception {
            AlipayClient alipayClient = getAlipayClient();
            String bizContent = getBizContent(ConvertUtil.obj2str(outTradeNo), refundAmount + "", refundReason, ConvertUtil.obj2str(operatorId));
            AlipayTradeRefundRequest request = getAlipayTradeRefundRequest(bizContent);
            AlipayTradeRefundResponse response = alipayClient.execute(request);
            if(response.isSuccess()){
                //TODO 退款成功后的业务逻辑
                System.out.println("调用成功");
            } else {
                System.out.println("调用失败");
            }
        }
    
        /**
         * 设置阿里退款请求参数
         *
         * @param bizContent 业务请求参数字符串
         *
         * @return
         */
        private AlipayTradeRefundRequest getAlipayTradeRefundRequest(String bizContent) {
            AlipayTradeRefundRequest  request = new AlipayTradeRefundRequest();
            request.setBizContent(bizContent);
            return request;
        }
    
        /**
         * 获取业务的关键内容
         *
         * @param outTradeNo 订单Id
         * @param refundAmount 退款金额
         * @param refundReason 退款理由
         * @param operatorId 操作Id
         *
         * @return 拼接后的业务请求参数字符串
         */
        private String getBizContent(String outTradeNo,String refundAmount,String refundReason,String operatorId){
            ExceptionUtil.checkEmpty(outTradeNo,"订单Id不能为空");
            ExceptionUtil.checkEmpty(refundAmount,"退款金额能为空");
            StringBuffer sb = new StringBuffer();
            sb.append("{");
            sb.append(""out_trade_no":"").append(outTradeNo).append("",");
            sb.append(""refund_amount":"").append(refundAmount).append("",");
            sb.append(""refund_reason":"").append(Util.isEmpty(refundReason)?"正常退款":refundReason ).append("",");
            sb.append(""operator_id":"").append(Util.isEmpty(operatorId)?0:operatorId).append("",");
            sb.append("}");
            return sb.toString();
        }
    
        /**
         * 获取AlipayClient对象
         *
         * @return AlipayClient对象
         */
        private AlipayClient getAlipayClient() {
            return new DefaultAlipayClient(refundConfig.getGatewayUrl(),refundConfig.getApp_id(),refundConfig.merchant_private_key,refundConfig.getFormat(),refundConfig.getCharset(),refundConfig.alipay_public_key,refundConfig.getSign_type());
        }
    }
  • 相关阅读:
    [大牛翻译系列]Hadoop(4)MapReduce 连接:选择最佳连接策略
    [大牛翻译系列]Hadoop(2)MapReduce 连接:复制连接(Replication join)
    [大牛翻译系列]Hadoop(3)MapReduce 连接:半连接(Semijoin)
    [大牛翻译系列]Hadoop(1)MapReduce 连接:重分区连接(Repartition join)
    Springboot启动流程分析
    行为型模式模板&策略&命令
    Springboot自动装配原理
    Springboot零配置原理
    行为型模式中介者&迭代器&访问者
    结构型模式组合&享元
  • 原文地址:https://www.cnblogs.com/htyj/p/8024775.html
Copyright © 2011-2022 走看看