微信支付申请退款 java_微信支付java版本之退款申请

package com.pay.controller.weixin;

import java.io.File;

import java.io.FileInputStream;

import java.security.KeyStore;

import java.text.SimpleDateFormat;

import java.util.Date;

import javax.net.ssl.SSLContext;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

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.conn.ssl.SSLContexts;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.util.EntityUtils;

import org.dom4j.Document;

import org.dom4j.Element;

import org.dom4j.io.SAXReader;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.HttpStatus;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.ResponseStatus;

import org.springframework.web.bind.annotation.RestController;

import com.pay.bo.ReturnHist;

import com.pay.service.WeiXinRefundService;

import com.pay.utils.weixin.ClientCustomSSL;

import com.pay.utils.weixin.CustomizedPropertyPlaceholderConfigurer;

@RestController

@RequestMapping("/pay/refund")

public class WeiXinRefundController {

@Autowired

WeiXinRefundService weiXinRefundService;

/**

* 微信退款

* @param request

* @param response

* @return

*/

@RequestMapping(value="/weiXinRefund",method=RequestMethod.POST)

@ResponseStatus(HttpStatus.OK)

public Object refund(HttpServletResponse response,@RequestBody String body){

System.out.println("==========================微信退款开始!!========================");

try{

JSONObject jsonO = JSONObject.fromObject(body);

KeyStore keyStore = KeyStore.getInstance("PKCS12");

FileInputStream instream = new FileInputStream(new File(

CustomizedPropertyPlaceholderConfigurer.getContextProperty("wx.cert").toString()));

try {

keyStore.load(instream, "见邮件".toCharArray());

}finally {

instream.close();

}

// Trust own CA and all self-signed certs

SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore,

"见邮件".toCharArray()).build();

// Allow TLSv1 protocol only

SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(

sslcontext, new String[] { "TLSv1" }, null,

SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

CloseableHttpClient httpclient = HttpClients.custom()

.setSSLSocketFactory(sslsf).build();

// HttpGet httpget = new

// HttpGet("https://api.mch.weixin.qq.com/secapi/pay/refund");

HttpPost httppost = new HttpPost(

"https://api.mch.weixin.qq.com/secapi/pay/refund");

String totalFee = jsonO.get("totalFee").toString();

String outTradeNo = weiXinRefundService.getTradePayNo(jsonO.getString("orderNo"));//"所退款订单编号"

String refundFee = jsonO.get("refundFee").toString();//"退款金额"

Date dt = new Date();

SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

String nonceStr = sdf.format(dt).toString();//"随机字符串";

String xml = ClientCustomSSL.RefundNativePackage(outTradeNo,nonceStr,totalFee,refundFee,nonceStr);

try {

StringEntity se = new StringEntity(xml);

httppost.setEntity(se);

System.out.println("executing request" + httppost.getRequestLine());

CloseableHttpResponse responseEntry = httpclient.execute(httppost);

try {

HttpEntity entity = responseEntry.getEntity();

System.out.println("----------------------------------------");

System.out.println(responseEntry.getStatusLine());

if (entity != null) {

System.out.println("Response content length: "

+ entity.getContentLength());

SAXReader saxReader = new SAXReader();

Document document = saxReader.read(entity.getContent());

Element rootElt = document.getRootElement();

System.out.println("根节点:" + rootElt.getName());

System.out.println("==="+rootElt.elementText("return_code"));

System.out.println("==="+rootElt.elementText("result_code"));

System.out.println("==="+rootElt.elementText("transaction_id"));

System.out.println("==="+rootElt.elementText("out_trade_no"));

System.out.println("==="+rootElt.elementText("out_refund_no"));

System.out.println("==="+rootElt.elementText("refund_id"));

System.out.println("==="+rootElt.elementText("refund_channel"));

System.out.println("==="+rootElt.elementText("refund_fee"));

System.out.println("==="+rootElt.elementText("coupon_refund_fee"));

String returnCode = rootElt.elementText("return_code");

JSONObject result = new JSONObject();

if(returnCode.equals("SUCCESS")){

ReturnHist rh = new ReturnHist();

rh.setOtherRefundId(rootElt.elementText("out_refund_no"));

rh.setReturnChannelType(rootElt.elementText("refund_channel"));

rh.setReturnMoneyBak(Float.parseFloat(totalFee)/100);

rh.setReturnMoneyBalance((Float.parseFloat(totalFee)-Float.parseFloat(rootElt.elementText("refund_fee")))/100);

rh.setReturnOrderNo(rootElt.elementText("out_refund_no"));

rh.setAppKey(jsonO.getString("appKey"));

weiXinRefundService.addRefundInfo(rh);

System.out.println("======================微信退款成功=================");

result.put("status","success");

result.put("msg","success");

result.put("returnChannelType", rh.getReturnChannelType());

result.put("otherRefundId", rh.getOtherRefundId());

result.put("returnMoneyBak", rh.getReturnMoneyBak());

result.put("returnMoneyBalance", rh.getReturnMoneyBalance());

result.put("returnOrderNo", rh.getReturnOrderNo());

}else{

result.put("status","false");

result.put("msg",rootElt.elementText("err_code_des"));

}

return result;

}

EntityUtils.consume(entity);

}

finally {

responseEntry.close();

}

}

finally {

httpclient.close();

}

return null;

}catch(Exception e){

e.printStackTrace();

JSONObject result = new JSONObject();

result.put("status","error");

result.put("msg",e.getMessage());

return result;

}

}

}

3.其他接口

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值