• 支付宝退款demo


    首先支付宝退款需要导入一个支付宝的jar包(支付和退款都依赖这个包,用maven下载即可)

    <dependency>
        <groupId>com.alipay.sdk</groupId>
        <artifactId>alipay-sdk-java</artifactId>
        <version>4.7.12.ALL</version>
    </dependency>

    那第二步就是需要定义下需要的常量(这里需要公司或个人向支付宝申请的支付退款的商家号)

    public class AliPayConfig {
    public static String APP_ID="替换为自己的";
    public static String APP_PRIVATE_KEY = "替换为自己的";// 私钥
    private static String requestUrl = "https://openapi.alipay.com/gateway.do";//支付宝请求退款的网关地址
    /**
    * 该公钥对应开放平台密钥的支付宝公钥,*/
    public static String ALIPAY_PUBLIC_KEY="替换为自己的";//支付宝公钥
    public static String CHARSET = "utf-8";//字符编码 public static String SIGN_TYPE = "RSA";//签名方式 }

    第三就是主体代码逻辑(我这里偷懒就就写在一起了没用常量),可写主方法测试下(前提你已经付过款并且拿到了商户号或订单号)

     public boolean refund22(String orderId, String tradeNo, BigDecimal refundAmount ) {
            //实例化客户端
            AlipayClient alipayClient = new DefaultAlipayClient("网关 https://openapi.alipay.com/gateway.do",
                    "appId",
                    "私钥",
                    "数据类型 json",
              "utf-8", "公钥", "RSA2"); AlipayTradeRefundRequest request = new AlipayTradeRefundRequest(); Map params = new HashMap(); if (orderId!=null&&!"".equals(orderId))params.put("out_trade_no",orderId);//商家订单号 if (tradeNo!=null&&!"".equals(tradeNo))params.put("trade_no",tradeNo);//支付宝交易号(一般必选) //请求退款的金额 params.put("refund_amount",refundAmount); //该参数支持部分退款(如果不加则进行全额退款,可用随机数去生成) params.put("out_request_no","BZ35581R88001"); //可加参数 String paramsJsonStr = JSONObject.toJSONString(params); request.setBizContent(paramsJsonStr); try { AlipayTradeRefundResponse response = alipayClient.execute(request); return response.isSuccess(); }catch (Exception e){ String json = e.getMessage(); json = json.substring(json.indexOf("{"),json.indexOf("}")+1); Map retMap = JSONObject.parseObject(json); return "10000".equals(retMap.get("code").toString()); } }
  • 相关阅读:
    数据结构64:冒泡排序算法(起泡排序)
    数据结构63:希尔排序算法(缩小增量排序)
    Python3 内置函数
    Python3 解压序列
    Python3 装饰器
    Python3 函数
    Python3 迭代器和生成器
    Python3 函数式编程自带函数
    Python3 函数式编程
    Python3 匿名函数
  • 原文地址:https://www.cnblogs.com/magepi/p/12831489.html
Copyright © 2020-2023  润新知