微信退款

import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.InputStreamReader;import java.security.KeyStore;import java.util.SortedMap;import java.util.TreeMap;import java.util.UUID;import javax.net.ssl.SSLContext;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;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 com.enation.app.shop.component.payment.plugin.wxap.RequestHandler;import com.enation.app.shop.component.system.weixin.config.WeixinConfig;/** * This example demonstrates how to create secure connections with a custom SSL * context. */public class ClientCustomSSL { public final static void main(String[] args) throws Exception { String refundid="4000432001201708044384869413"; String orderId="ewo201708041032425031121"; String total_fee="2"; String refund_fee="2"; wxRefund(null, null, refundid, orderId, total_fee, refund_fee); } private static void wxRefund(HttpServletRequest request,HttpServletResponse response, String refundid,String orderId,String total_fee,String refund_fee) throws Exception{ KeyStore keyStore = KeyStore.getInstance("PKCS12"); FileInputStream instream = new FileInputStream(new File("D:/apiclient_cert.p12")); try { keyStore.load(instream, WeixinConfig.MCH_ID.toCharArray()); } finally { instream.close(); } // Trust own CA and all self-signed certs SSLContext sslcontext = SSLContexts.custom() .loadKeyMaterial(keyStore, WeixinConfig.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 httpPost = new HttpPost("https://api.mch.weixin.qq.com/secapi/pay/refund"); System.out.println("executing request" + httpPost.getRequestLine()); String nonce_str=UUID.randomUUID().toString().replace("-", ""); /*----- 1.生成预支付订单需要的的package数据-----*/ SortedMap packageParams = new TreeMap(); packageParams.put("appid", WeixinConfig.APPID); packageParams.put("mch_id", WeixinConfig.MCH_ID); packageParams.put("nonce_str", nonce_str); packageParams.put("out_trade_no", orderId); packageParams.put("out_refund_no", refundid); packageParams.put("total_fee",total_fee); packageParams.put("refund_fee", refund_fee); /*----2.根据package生成签名sign---- */ RequestHandler reqHandler = new RequestHandler(null, null); reqHandler.init(WeixinConfig.APPID, WeixinConfig.ANDROID_APP_SECRET, WeixinConfig.API_KEY,null); String sign = reqHandler.createSign(packageParams); String xmlstring=""; httpPost.setEntity(new StringEntity(xmlstring)); httpPost.setHeader("Accept", "application/json"); httpPost.setHeader("Content-type", "application/json"); CloseableHttpResponse wxresponse = httpclient.execute(httpPost); try { HttpEntity entity = wxresponse.getEntity(); System.out.println("----------------------------------------"); System.out.println(wxresponse.getStatusLine()); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent())); String text; while ((text = bufferedReader.readLine()) != null) { System.out.println(text); } } EntityUtils.consume(entity); } finally { wxresponse.close(); } } finally { httpclient.close(); } }}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值