JAVA实现微信小程序支付退款功能

JAVA实现微信小程序支付退款功能

  • 本如亲测有效(代码复制直接可以用的),退款的前提是必须有小程序的appid、商户号、商户密匙、和证书、
  • 这个是微信小程序退款的官网大家可以去看看:https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_9.shtml
  • 这个是微信官网里面的签名信息和API证书如何申请可以去看看:https://pay.weixin.qq.com/wiki/doc/apiv3/wechatpay/wechatpay4_0.shtml
  • 吧上面4个东西全部弄到保存好,并吧证书放在项目的resources层下,一般情况下证书名称就叫:apiclient_cert.p12 .
  • 全部填写好了之后就可以将下面代码复制到你的项目里面就可直接运行,返回类型根据你的项目需求进行修改就行了。
  • 注意:这里退款金额和订单金额都是以分为单位的
 // 有需要的话可以取这段代码进行金额转换,将金额转换成分
BigDecimal payMoney = new BigDecimal(BigDecimal.valueOf(Double.parseDouble(BigDecimalUtil.mulBig(money, new BigDecimal(100)).toString())).stripTrailingZeros().toPlainString());
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import javax.net.ssl.SSLContext;
import java.io.*;
import java.security.KeyStore;
import java.util.*;

public class PayUtil {
    private static String APPID = "";  //小程序的appid
    private static String MCH_ID = "";  //商户号
    private static String KEY = "";   //商户秘钥
    private byte [] certData;

    private String getRandomStringByLength(int length) {
        String base = "abcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    private static String mapToXml(Map param) {
        StringBuffer sb = new StringBuffer();
        sb.append("");
        for (Map.Entry entry : param.entrySet()) {
            sb.append("");
            sb.append(entry.getValue());
            sb.append("");
        }
        sb.append("");
        return sb.toString();
    }

    private static Map xmlToMap(String strxml) throws Exception {
        Map map = new HashMap<>();
        if (null == strxml || "".equals(strxml)) {
            return null;
        }
        InputStream in = String2Inputstream(strxml);
        SAXReader read = new SAXReader();
        Document doc = read.read(in);
        //得到xml根元素
        Element root = doc.getRootElement();
        //遍历  得到根元素的所有子节点
        @SuppressWarnings("unchecked")
        List list = root.elements();
        for (Element element : list) {
            //装进map
            map.put(element.getName(), element.getText());
        }
        //关闭流
        in.close();
        return map;
    }

    private static InputStream String2Inputstream(String strxml) throws IOException {
        return new ByteArrayInputStream(strxml.getBytes("UTF-8"));
    }

    /**
     * 把数组所有元素排序,并按照"参数=参数值"的模式用"&"字符拼接成字符串
     *
     * @param params 需要排序并参与字符拼接的参数组
     * @return 拼接后字符串
     */
    private static String createLinkString(Map params) {
        List keys = new ArrayList<>(params.keySet());
        Collections.sort(keys);
        String preStr = "";
        for (int i = 0; i < keys.size(); i++) {
            String key = keys.get(i);
            String value = params.get(key);
            if (i == keys.size() - 1) {// 拼接时,不包括最后一个&字符
                preStr = preStr + key + "=" + value;
            } else {
                preStr = preStr + key + "=" + value + "&";
            }
        }
        return preStr;
    }

    /**
     * 签名字符串
     *
     * @param text          需要签名的字符串
     * @param key           密钥
     * @param input_charset 编码格式
     * @return 签名结果
     */
    public static String sign(String text, String key, String input_charset) {
        text = text + "&key=" + key;
        return DigestUtils.md5Hex(getContentBytes(text, input_charset));
    }

    private static byte[] getContentBytes(String content, String charset) {
        if (charset == null || "".equals(charset)) {
            return content.getBytes();
        }
        try {
            return content.getBytes(charset);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("MD5签名过程中出现错误,指定的编码集不对,您目前指定的编码集是:" + charset);
        }
    }

    /**
     * 调用微信退款接口
     */
    private String doRefund(String url, String data) throws Exception {
        KeyStore keyStore = KeyStore.getInstance("PKCS12"); //证书格式
        try {
           InputStream certStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("apiclient_cert.p12");
            this.certData = IOUtils.toByteArray(certStream);
            certStream.close();
          } catch (Exception e) {
                e.printStackTrace();
             }
        ByteArrayInputStream is = new ByteArrayInputStream(this.certData);
           try {
            keyStore.load(is, PayUtil.MCH_ID.toCharArray());
        } finally {
            is.close();
        }
        // Trust own CA and all self-signed certs
        SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(
                keyStore,
                PayUtil.MCH_ID.toCharArray())
                .build();
        // Allow TLSv1 protocol only
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                sslcontext,
                new String[]{"TLSv1"},
                null,
                SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER
        );
        CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
        try {
            HttpPost httpost = new HttpPost(url); // 设置响应头信息
            httpost.addHeader("Connection", "keep-alive");
            httpost.addHeader("Accept", "*/*");
            httpost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            httpost.addHeader("Host", "api.mch.weixin.qq.com");
            httpost.addHeader("X-Requested-With", "XMLHttpRequest");
            httpost.addHeader("Cache-Control", "max-age=0");
            httpost.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");
            httpost.setEntity(new StringEntity(data, "UTF-8"));
            CloseableHttpResponse response = httpclient.execute(httpost);
            try {
                HttpEntity entity = response.getEntity();

                String jsonStr = EntityUtils.toString(response.getEntity(), "UTF-8");
                EntityUtils.consume(entity);
                return jsonStr;
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    }

    /**
     * 申请退款
     *
     * @param orderId       商户订单号
     * @param refundId      商户退款单号
     * @param totalFee      订单金额
     * @param refundFee     退款金额
     * @param refundAccount 退款资金来源(默认传 "REFUND_SOURCE_UNSETTLED_FUNDS")
     * 注: 退款金额不能大于订单金额
     */
    public Map refund(String orderId, String refundId, String totalFee,
                                      String refundFee, String refundAccount) {

        Map params = new HashMap<>();
        params.put("appid", PayUtil.APPID);
        params.put("mch_id", PayUtil.MCH_ID);
        params.put("nonce_str", getRandomStringByLength(32));
        params.put("out_trade_no", orderId); //商户订单号和微信订单号二选一(我这里选的是商户订单号)
        params.put("out_refund_no", refundId);
        params.put("total_fee", totalFee);
        params.put("refund_fee", refundFee);
        params.put("refund_account", refundAccount);
        params.put("sign_type", "MD5");
        String preStr = PayUtil.createLinkString(params);

        //签名算法
        String sign = (sign(preStr, PayUtil.KEY, "utf-8")).toUpperCase();
        params.put("sign", sign);

        Map map = new HashMap<>();
        try {
            String xml = mapToXml(params);
            String xmlStr = doRefund("https://api.mch.weixin.qq.com/secapi/pay/refund", xml);
            map = xmlToMap(xmlStr);
            // 这里是将返回类型转换成了 map 如果要判断是否退款成功可以获取 map里面的 result_code 值,如果这个值等于 SUCCESS就代表退款成功,这里就可以写退款之后的逻辑,比如修改订单状态之类的,
               if ("SUCCESS".equals(status)){
                // 退款成功
                // 这里就写逻辑,当然,如果不需要写逻辑的话,可以直接吧if else 删掉也是可以直接跑起来的

              }else {
                // 退款失败
                return Result.fail("退款失败");
               }

        } catch (Exception e) {
             }
        return map;
    }
}

Original: https://www.cnblogs.com/gongss/p/16729064.html
Author: Gongss
Title: JAVA实现微信小程序支付退款功能

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/582530/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • Golang 实现 Redis(8): TCC分布式事务

    本文是使用 golang 实现 redis 系列的第八篇, 将介绍如何在分布式缓存中使用 Try-Commit-Catch 方式来解决分布式一致性问题。 在上一篇文章中我们使用一致…

    Linux 2023年5月28日
    093
  • 【设计模式】Java设计模式-组合模式

    Java设计模式 – 组合模式 😄 不断学习才是王道🔥 继续踏上学习之路,学之分享笔记👊 总有一天我也能像各位大佬一样🏆原创作品,更多关注我CSDN: 一个有梦有戏的人…

    Linux 2023年6月6日
    0122
  • NoteOfMySQL-14-日志管理

    一、MySQL日志 日志是MySQL数据库的重要组成部分,日志文件记录了MySQL数据库的日常操作和错误信息,可以通过分析这些日志文件了解MySQL数据库的运行情况。MySQL数据…

    Linux 2023年6月14日
    090
  • ThinkPHP5权限管理

    自己写的权限管理,大致思路:用户登陆成功之后,查出该用户的权限列表,并把权限列表存到session中,进入系统后,再判断该模块是否在session中,如果存在就说明有该权限,就显示…

    Linux 2023年6月7日
    097
  • Python 多线程

    import threading import time def userTest(aa,bb): print(aa) time.sleep(3) print(bb) if __n…

    Linux 2023年6月6日
    080
  • linux bash 手册

    1、Linux shell简介 shell是一种特殊的交互式工具,包含了一组内部命令,这些命令可以完成复制文件,移动文件,显示和终止程序等操作。shell的核心是命令行提示符,它允…

    Linux 2023年6月7日
    087
  • RAID磁盘阵列

    RAID磁盘阵列 *本章重点:了解各RAID级别的原理优缺点及常用级别实现,企业中厂商大多提供了硬RAID方案。 1、什么是RAID? “RAID”一词是由…

    Linux 2023年6月7日
    091
  • Java刷题笔记7.25

    一个类构造方法的作用是什么? &#x4E3B;&#x8981;&#x662F;&#x5B8C;&#x6210;&#x5BF9;&am…

    Linux 2023年6月7日
    0110
  • 使用shell脚本连接钉钉机器人发送消息

    一、前言 服务器上有时 定时任务、重要接口 等出现异常,导致数据不正常,不能及时通知到服务负责人,及时处理问题。所以引入”钉钉”作为通知工具,当服务出现异常…

    Linux 2023年5月28日
    0116
  • Redis采用不同内存分配器碎片率对比

    我们知道Redis并没有自己实现内存池,没有在标准的系统内存分配器上再加上自己的东西。所以系统内存分配器的性能及碎片率会对Redis造成一些性能上的影响。在Redis的 zmall…

    Linux 2023年5月28日
    091
  • Django Model 如何返回空的 QuerySet

    >>> from django.contrib.auth.models import User >>> User.objects.none() …

    Linux 2023年6月7日
    088
  • 详解IP地址、子网掩码、网络号、主机号、网络地址、主机地址

    详解IP地址、子网掩码、网络号、主机号、网络地址、主机地址 概念 IP地址:一般是指逻辑ip; 子网掩码:将IP划分为网络号和主机号的IP; 网络号/主机号:子网掩码转成二进制后,…

    Linux 2023年6月6日
    0120
  • SlugRelatedField字段

    该字段用于外键字段该字段在序列化的时候多用于反向查询,在反序列化的时候用于接收关联表的唯一字段来生成该关联对象eg: 序列化 class PublishListSerializer…

    Linux 2023年6月14日
    099
  • Redis:redis常用操作命令

    redis登录 #登录命令 -h 登录地址 -p 端口 ./redis-cli -h 127.0.0.1 -p 6379 查看缓存大小 #查看缓存大小 dbsize 查看所有Key…

    Linux 2023年5月28日
    0133
  • zabbix快速安装(yum)

    1、先卸载系统自带数据库 [root@bogon ~]# rpm -e mariadb-libs-5.5.56-2.el7.x86_64 –nodeps 2、安装mys…

    Linux 2023年6月6日
    087
  • linux应急响应具体操作

    第一件事情应该是切断网络,但是有些环境不允许网络断开,就只能跳过这一步。 1、查看历史命令 ​发现Linux 服务器被攻击,要做应急响应,登录主机后的第一件事,就是查看主机的历史命…

    Linux 2023年6月14日
    098
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球