微信支付java版本之退款申请
来源:互联网 发布:翠微居网络小手论坛 编辑:程序博客网 时间:2024/04/27 20:54
1.接口简介
2.代码实现
[java] view plain copy print?
- 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;
- }
- }
- }
0 0
- 微信支付java版本之退款申请
- 微信支付java版本之退款申请
- 微信支付之退款
- 微信支付一 :公众号支付3 申请退款
- java-微信支付-微信退款
- 微信支付---申请退款的https双向认证demo
- 微信支付,退款
- 微信支付之退款查询
- 使用Delphi Xe8 开发微信功能 -- (四)微信支付商户平台之申请退款
- java服务器端微信,支付宝支付和退款
- 微信支付退款接口
- 微信支付退款接口
- 一个PHP文件搞定微信支付系列之退款
- java 微信与支付宝退款接口开发总结
- 微信支付-退款(v3版微信支付)
- 微信 退款 java
- 微信退款和支付宝退款接口调用(java版)
- 微信退款和支付宝退款接口调用(java版)
- 基于CYCCNT时钟周期计数器的高精度延时接口
- MySQL源码分析以及目录结构
- 微信支付java版本之查询订单
- 设计模式 - C++单例模式应用
- 理解及使用adb
- 微信支付java版本之退款申请
- 编程思想:面向对象和面向过程
- Android性能调优利器StrictMode
- 浅析PHP中静态方法与非静态方法的用法区别
- Struts2学习——1700Struts通用标签
- 使用JSON格式来进行数据交换
- 友盟分享纯图片到微信
- 使用HTML5 Canvas绘制一个时钟
- TensorFlow之深入理解Fast Neural Style