微信支付java版本之退款申请 - Go语言中文社区

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


1.接口简介



2.代码实现

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.其他接口


微信jsApi支付+发送模板消息: http://blog.csdn.net/wangxuewei111/article/details/43982381

微信支付native支付+工具类: http://blog.csdn.net/wangxuewei111/article/details/43954857




版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/wangxuewei111/article/details/44021035
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2021-06-13 11:00:08
  • 阅读 ( 544 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢