java微信退款(支持部分退款)

这里介绍两种退款方式:已封装和未封装两种形式


一、未封装

退款需要准备证书,apiclient_cert.p12这个证书,证书下载地址看这里     http://kf.qq.com/faq/161222NneAJf161222U7fARv.html    

放到resource中即可,在编译的时候保证能将它编译到class中,下面会说到

证书还有一点要注意,如果你放到resource中了但是不好使,具体报什么错忘了,需要加上支持:

           <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <!-- 过滤后缀为pem、pfx的证书文件 -->
                    <nonFilteredFileExtensions>
                        <nonFilteredFileExtension>pem</nonFilteredFileExtension>
                        <nonFilteredFileExtension>pfx</nonFilteredFileExtension>
                        <nonFilteredFileExtension>p12</nonFilteredFileExtension>
                    </nonFilteredFileExtensions>
                </configuration>
            </plugin>


工具类都是全的,只需要小改一下自己的东西就可以实现了,目录如下:



里面有详细的说明,需要改动的是RefundUtil和ClientCustomSSL,其他不用动

RefundUtil


做过支付才做退款对吧, 所以我不多说了,上面的内容都改成自己平台的appid和密钥,具体在哪找自己百度


ClientCustomSSL


这个类需要指定证书,上面说过了,而

String path=this.getClass().getClassLoader().getResource("/").getPath();

就是获取证书的路径,自己可以编译一下看是不是在那个文件夹下,如果在就对了,如果不在,那肯定不行(这里我们采用的相对路径,这样放到哪里都不会错也不用改)


ok了,没什么了,默认读取证书密码退款就可以了

这是调用的代码:

        RefundUtil refundUtil = new RefundUtil();
        Map<String, String> map = refundUtil.wechatRefund(transaction_id, total_fee, refund_fee, request);
        log.info("返回map集合:" + map);
        String result_code = map.get("result_code");
        log.info("result_code退款状态:" + result_code);
        if (result_code.equals("SUCCESS")) {

成功后处理自己业务逻辑


退款成功返回map形式:以上面的形式取出要用的就可以

{
    refund_id=50000105282018010,
    refund_fee=9900,
    coupon_refund_fee=0,
    refund_channel=,
    return_msg=OK,
    appid=wxdlsfjldk,
    nonce_str=FCAvhi2VeL0kYYn2,
    out_trade_no=20183242336,
    out_refund_no=3133e760-ccb9-4345-9466-ab2b68be,
    transaction_id=42002343452454243231346161,
    coupon_refund_count=0,
    sign=BE4CCE8009E6A5CC32342342391B,
    result_code=SUCCESS,
    mch_id=134234234,
    total_fee=9900,
    return_code=SUCCESS,
    cash_refund_fee=9900,
    cash_fee=9900
}


二、未封装(ijpay的封装方式)

@ResponseBody
	@RequestMapping(value ="/refund.do",method = {RequestMethod.POST,RequestMethod.GET})
	public String refund(){
		String certPath=this.getClass().getClassLoader().getResource("/").getPath()+"apiclient_cert.p12";
		System.out.println("certPath:"+certPath);
		String transaction_id = null;
		String out_trade_no = "";

		if (StrKit.isBlank(out_trade_no) && StrKit.isBlank(transaction_id)) {
			System.out.println("transactionId、out_trade_no二选一");
			return null;
		}

		Map<String, String> params = new HashMap<String, String>();
		params.put("appid", appID);
		params.put("mch_id", mchID);
		params.put("nonce_str", System.currentTimeMillis()+"");
		if (StrKit.notBlank(transaction_id)) {
			params.put("transaction_id", transaction_id);
		}else {
			params.put("out_trade_no", out_trade_no);
		}
		params.put("out_refund_no", System.currentTimeMillis()+"");
		params.put("total_fee", "100");
		params.put("refund_fee", "90");
		params.put("sign", PaymentKit.createSign(params, partnerKey));
		String refund = WxPayApi.orderRefund(false, params , certPath, mchID);
		System.out.println("refund:"+refund);
		return refund;
	}


已测试,很简单,相信聪明的你一看就懂了




未封装工具类下载地址看这里看这里:   http://download.csdn.net/download/goligory/10243034


引用退款博客链接:https://blog.csdn.net/zyw_java/article/details/79370616


猜你喜欢

转载自blog.csdn.net/Goligory/article/details/79274416