亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java微信退款開(kāi)發(fā)

 更新時(shí)間:2018年09月29日 10:14:10   作者:thinkhui  
這篇文章主要為大家詳細(xì)介紹了Java微信退款開(kāi)發(fā)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

一、下載證書并導(dǎo)入到系統(tǒng)

微信支付接口中,涉及資金回滾的接口會(huì)使用到商戶證書,包括退款、撤銷接口。商家在申請(qǐng)微信支付成功后,可以按照以下路徑下載:微信商戶平臺(tái)(pay.weixin.qq.com)-->賬戶設(shè)置-->API安全-->證書下載。

下載的時(shí)候需要手機(jī)驗(yàn)證及登錄密碼。下載后找到apiclient_cert.p12這個(gè)證書,雙擊導(dǎo)入,導(dǎo)入的時(shí)候提示輸入密碼,這個(gè)密碼就是商戶ID,且必須是在自己的商戶平臺(tái)下載的證書。否則會(huì)出現(xiàn)密碼錯(cuò)誤的提示:

導(dǎo)入正確的提示:

二、編寫代碼

首先初始化退款接口中的請(qǐng)求參數(shù),如微信訂單號(hào)transaction_id(和商戶訂單號(hào)只需要知道一個(gè))、訂單金額total_fee等;其次調(diào)用MobiMessage中的RefundResData2xml方法解析成需要的類型;最后調(diào)用RefundRequest類的httpsRequest方法觸發(fā)請(qǐng)求。

/**
 * 處理退款請(qǐng)求
 * @param request
 * @return
 * @throws Exception
 */
 @RequestMapping("/refund")
 @ResponseBody
 public JsonApi refund(HttpServletRequest request) throws Exception {
  //獲得當(dāng)前目錄
  String path = request.getSession().getServletContext().getRealPath("/");
  LogUtils.trace(path);
 
  Date now = new Date();
  SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");//可以方便地修改日期格式
  String outRefundNo = "NO" + dateFormat.format( now );
 
  //獲得退款的傳入?yún)?shù)
  String transactionID = "4008202001201609012791655620";
  String outTradeNo = "20160901141024";
  Integer totalFee = 1;
  Integer refundFee = totalFee;
 
  RefundReqData refundReqData = new RefundReqData(transactionID,outTradeNo,outRefundNo,totalFee,refundFee);
 
  String info = MobiMessage.RefundReqData2xml(refundReqData).replaceAll("__", "_");
  LogUtils.trace(info);
 
  try {
   RefundRequest refundRequest = new RefundRequest();
   String result = refundRequest.httpsRequest(WxConfigure.REFUND_API, info, path);
   LogUtils.trace(result);
 
   Map<String, String> getMap = MobiMessage.parseXml(new String(result.toString().getBytes(), "utf-8"));
   if("SUCCESS".equals(getMap.get("return_code")) && "SUCCESS".equals(getMap.get("return_msg"))){
    return new JsonApi();
   }else{
    //返回錯(cuò)誤描述
    return new JsonApi(getMap.get("err_code_des"));
   }
  }catch(Exception e){
   e.printStackTrace();
   return new JsonApi();
  }
}

初始化退款接口需要的數(shù)據(jù),隱藏了get和set方法。

public class RefundReqData {
 
 //每個(gè)字段具體的意思請(qǐng)查看API文檔
 private String appid = "";
 private String mch_id = "";
 private String nonce_str = "";
 private String sign = "";
 private String transaction_id = "";
 private String out_trade_no = "";
 private String out_refund_no = "";
 private int total_fee = 0;
 private int refund_fee = 0;
 private String op_user_id = "";
 
 /**
  * 請(qǐng)求退款服務(wù)
  * @param transactionID 是微信系統(tǒng)為每一筆支付交易分配的訂單號(hào),通過(guò)這個(gè)訂單號(hào)可以標(biāo)識(shí)這筆交易,它由支付訂單API支付成功時(shí)返回的數(shù)據(jù)里面獲取到。建議優(yōu)先使用
  * @param outTradeNo 商戶系統(tǒng)內(nèi)部的訂單號(hào),transaction_id 、out_trade_no 二選一,如果同時(shí)存在優(yōu)先級(jí):transaction_id>out_trade_no
  * @param outRefundNo 商戶系統(tǒng)內(nèi)部的退款單號(hào),商戶系統(tǒng)內(nèi)部唯一,同一退款單號(hào)多次請(qǐng)求只退一筆
  * @param totalFee 訂單總金額,單位為分
  * @param refundFee 退款總金額,單位為分
  */
 public RefundReqData(String transactionID,String outTradeNo,String outRefundNo,int totalFee,int refundFee){
 
  //微信分配的公眾號(hào)ID(開(kāi)通公眾號(hào)之后可以獲取到)
  setAppid(WxConfigure.AppId);
 
  //微信支付分配的商戶號(hào)ID(開(kāi)通公眾號(hào)的微信支付功能之后可以獲取到)
  setMch_id(WxConfigure.Mch_id);
 
  //transaction_id是微信系統(tǒng)為每一筆支付交易分配的訂單號(hào),通過(guò)這個(gè)訂單號(hào)可以標(biāo)識(shí)這筆交易,它由支付訂單API支付成功時(shí)返回的數(shù)據(jù)里面獲取到。
  setTransaction_id(transactionID);
 
  //商戶系統(tǒng)自己生成的唯一的訂單號(hào)
  setOut_trade_no(outTradeNo);
 
  setOut_refund_no(outRefundNo);
 
  setTotal_fee(totalFee);
 
  setRefund_fee(refundFee);
 
  setOp_user_id(WxConfigure.Mch_id);
 
  //隨機(jī)字符串,不長(zhǎng)于32 位
  setNonce_str(StringUtil.generateRandomString(16));
 
 
  //根據(jù)API給的簽名規(guī)則進(jìn)行簽名
  SortedMap<Object, Object> parameters = new TreeMap<Object, Object>();
  parameters.put("appid", appid);
  parameters.put("mch_id", mch_id);
  parameters.put("nonce_str", nonce_str);
  parameters.put("transaction_id", transaction_id);
  parameters.put("out_trade_no", out_trade_no);
  parameters.put("out_refund_no", out_refund_no);
  parameters.put("total_fee", total_fee);
  parameters.put("refund_fee", refund_fee);
  parameters.put("op_user_id", op_user_id);
 
 
  String sign = DictionarySort.createSign(parameters);
  setSign(sign); //把簽名數(shù)據(jù)設(shè)置到Sign這個(gè)屬性中
 
 }

MobiMessage實(shí)現(xiàn)json數(shù)據(jù)類型和xml數(shù)據(jù)之間的轉(zhuǎn)換。

public class MobiMessage {
 
 public static Map<String,String> xml2map(HttpServletRequest request) throws IOException, DocumentException {
  Map<String,String> map = new HashMap<String, String>();
  SAXReader reader = new SAXReader();
  InputStream inputStream = request.getInputStream();
  Document document = reader.read(inputStream);
  Element root = document.getRootElement();
  List<Element> list = root.elements();
  for(Element e:list){
   map.put(e.getName(), e.getText());
  }
  inputStream.close();
  return map;
 }
 
 
 //訂單轉(zhuǎn)換成xml
 public static String JsApiReqData2xml(JsApiReqData jsApiReqData){
  /*XStream xStream = new XStream();
  xStream.alias("xml",productInfo.getClass());
  return xStream.toXML(productInfo);*/
  MobiMessage.xstream.alias("xml",jsApiReqData.getClass());
  return MobiMessage.xstream.toXML(jsApiReqData);
 }
 
 public static String RefundReqData2xml(RefundReqData refundReqData){
  /*XStream xStream = new XStream();
  xStream.alias("xml",productInfo.getClass());
  return xStream.toXML(productInfo);*/
  MobiMessage.xstream.alias("xml",refundReqData.getClass());
  return MobiMessage.xstream.toXML(refundReqData);
 }
 
 public static String class2xml(Object object){
 
  return "";
 }
 public static Map<String, String> parseXml(String xml) throws Exception {
  Map<String, String> map = new HashMap<String, String>();
  Document document = DocumentHelper.parseText(xml);
  Element root = document.getRootElement();
  List<Element> elementList = root.elements();
  for (Element e : elementList)
   map.put(e.getName(), e.getText());
  return map;
 }
 
 //擴(kuò)展xstream,使其支持CDATA塊
 private static XStream xstream = new XStream(new XppDriver() {
  public HierarchicalStreamWriter createWriter(Writer out) {
   return new PrettyPrintWriter(out) {
    // 對(duì)所有xml節(jié)點(diǎn)的轉(zhuǎn)換都增加CDATA標(biāo)記
    boolean cdata = true;
 
    //@SuppressWarnings("unchecked")
    public void startNode(String name, Class clazz) {
     super.startNode(name, clazz);
    }
 
    protected void writeText(QuickWriter writer, String text) {
     if (cdata) {
      writer.write("<![CDATA[");
      writer.write(text);
      writer.write("]]>");
     } else {
      writer.write(text);
     }
    }
   };
  }
 });
 
 
}

RefundRequest類中initCert方法加載證書到系統(tǒng)中,其中證書地址如下:

public static String certLocalPath = "/WEB-INF/cert/apiclient_cert.p12";  

RefundRequest類中httpsRequest方法調(diào)用微信接口,觸發(fā)請(qǐng)求。

/**
 * User: rizenguo
 * Date: 2014/10/29
 * Time: 14:36
 */
public class RefundRequest {
 
 //連接超時(shí)時(shí)間,默認(rèn)10秒
 private int socketTimeout = 10000;
 
 //傳輸超時(shí)時(shí)間,默認(rèn)30秒
 private int connectTimeout = 30000;
 
 //請(qǐng)求器的配置
 private RequestConfig requestConfig;
 
 //HTTP請(qǐng)求器
 private CloseableHttpClient httpClient;
 
 /**
  * 加載證書
  * @param path
  * @throws IOException
  * @throws KeyStoreException
  * @throws UnrecoverableKeyException
  * @throws NoSuchAlgorithmException
  * @throws KeyManagementException
  */
 private void initCert(String path) throws IOException, KeyStoreException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException {
  //拼接證書的路徑
  path = path + WxConfigure.certLocalPath;
  KeyStore keyStore = KeyStore.getInstance("PKCS12");
 
  //加載本地的證書進(jìn)行https加密傳輸
  FileInputStream instream = new FileInputStream(new File(path));
  try {
   keyStore.load(instream, WxConfigure.Mch_id.toCharArray()); //加載證書密碼,默認(rèn)為商戶ID
  } catch (CertificateException e) {
   e.printStackTrace();
  } catch (NoSuchAlgorithmException e) {
   e.printStackTrace();
  } finally {
   instream.close();
  }
 
  // Trust own CA and all self-signed certs
  SSLContext sslcontext = SSLContexts.custom()
    .loadKeyMaterial(keyStore, WxConfigure.Mch_id.toCharArray())  //加載證書密碼,默認(rèn)為商戶ID
    .build();
  // Allow TLSv1 protocol only
  SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
    sslcontext,
    new String[]{"TLSv1"},
    null,
    SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
 
  httpClient = HttpClients.custom()
    .setSSLSocketFactory(sslsf)
    .build();
 
  //根據(jù)默認(rèn)超時(shí)限制初始化requestConfig
  requestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout).build();
 
 }
 
 
 /**
  * 通過(guò)Https往API post xml數(shù)據(jù)
  * @param url API地址
  * @param xmlObj 要提交的XML數(shù)據(jù)對(duì)象
  * @param path 當(dāng)前目錄,用于加載證書
  * @return
  * @throws IOException
  * @throws KeyStoreException
  * @throws UnrecoverableKeyException
  * @throws NoSuchAlgorithmException
  * @throws KeyManagementException
  */
 public String httpsRequest(String url, String xmlObj, String path) throws IOException, KeyStoreException, UnrecoverableKeyException, NoSuchAlgorithmException, KeyManagementException {
  //加載證書
  initCert(path);
 
  String result = null;
 
  HttpPost httpPost = new HttpPost(url);
 
  //得指明使用UTF-8編碼,否則到API服務(wù)器XML的中文不能被成功識(shí)別
  StringEntity postEntity = new StringEntity(xmlObj, "UTF-8");
  httpPost.addHeader("Content-Type", "text/xml");
  httpPost.setEntity(postEntity);
 
  //設(shè)置請(qǐng)求器的配置
  httpPost.setConfig(requestConfig);
 
  try {
   HttpResponse response = httpClient.execute(httpPost);
 
   HttpEntity entity = response.getEntity();
 
   result = EntityUtils.toString(entity, "UTF-8");
 
  } catch (ConnectionPoolTimeoutException e) {
   LogUtils.trace("http get throw ConnectionPoolTimeoutException(wait time out)");
 
  } catch (ConnectTimeoutException e) {
   LogUtils.trace("http get throw ConnectTimeoutException");
 
  } catch (SocketTimeoutException e) {
    LogUtils.trace("http get throw SocketTimeoutException");
 
  } catch (Exception e) {
    LogUtils.trace("http get throw Exception");
 
  } finally {
   httpPost.abort();
  }
 
  return result;
 }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解Elasticsearch如何把一個(gè)索引變?yōu)橹蛔x

    詳解Elasticsearch如何把一個(gè)索引變?yōu)橹蛔x

    這篇文章主要為大家介紹了詳解Elasticsearch如何把一個(gè)索引變?yōu)橹蛔x示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-02-02
  • SpringBoot實(shí)現(xiàn)License認(rèn)證(只校驗(yàn)有效期)的詳細(xì)過(guò)程

    SpringBoot實(shí)現(xiàn)License認(rèn)證(只校驗(yàn)有效期)的詳細(xì)過(guò)程

    License也就是版權(quán)許可證書,一般用于收費(fèi)軟件給付費(fèi)用戶提供的訪問(wèn)許可證明,這篇文章主要介紹了SpringBoot實(shí)現(xiàn)License認(rèn)證(只校驗(yàn)有效期),需要的朋友可以參考下
    2024-04-04
  • Intellij 下 mybatis 插件 MyBatisCodeHelperPro破解步驟詳解

    Intellij 下 mybatis 插件 MyBatisCodeHelperPro破解步驟詳解

    這篇文章主要介紹了Intellij 下 mybatis 插件 MyBatisCodeHelperPro破解步驟,本文分步驟給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • 在IntelliJ IDEA中.idea文件是什么可以刪除嗎

    在IntelliJ IDEA中.idea文件是什么可以刪除嗎

    相信有很多小伙伴,在用idea寫java代碼的時(shí)候,創(chuàng)建工程總是會(huì)出現(xiàn).idea文件,該文件也從來(lái)沒(méi)去打開(kāi)使用過(guò),那么它在我們項(xiàng)目里面,扮演什么角色,到底能不能刪除它呢?這篇文章主要介紹了在IntelliJ IDEA中.idea文件是什么可以刪除嗎,需要的朋友可以參考下
    2024-01-01
  • IDEA中配置多個(gè)版本的JDK的實(shí)現(xiàn)示例

    IDEA中配置多個(gè)版本的JDK的實(shí)現(xiàn)示例

    IDEA可以配置多個(gè)JDK,根據(jù)需要使用不同版本的,本文就來(lái)介紹一下IDEA中配置多個(gè)版本的JDK的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-03-03
  • SpringSessionRedis配置及發(fā)現(xiàn)的問(wèn)題講解

    SpringSessionRedis配置及發(fā)現(xiàn)的問(wèn)題講解

    今天小編就為大家分享一篇關(guān)于SpringSessionRedis配置及發(fā)現(xiàn)的問(wèn)題講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-03-03
  • Java ConcurrentModificationException異常解決案例詳解

    Java ConcurrentModificationException異常解決案例詳解

    這篇文章主要介紹了Java ConcurrentModificationException異常解決案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09
  • Spring AOP結(jié)合注解實(shí)現(xiàn)接口層操作日志記錄

    Spring AOP結(jié)合注解實(shí)現(xiàn)接口層操作日志記錄

    在項(xiàng)目開(kāi)發(fā)中我們需要記錄接口的操作日志:包含請(qǐng)求參數(shù)、響應(yīng)參數(shù)、接口所屬模塊、接口功能描述、請(qǐng)求地址、ip地址等信息;實(shí)現(xiàn)思路很簡(jiǎn)單就是基于注解和aop的方式去記錄日志,主要的難點(diǎn)在于日志表結(jié)構(gòu)、注解的設(shè)計(jì)已經(jīng)aop實(shí)現(xiàn)的一些比較好的實(shí)現(xiàn)方式的借鑒
    2022-08-08
  • java并發(fā)編程之ThreadLocal詳解

    java并發(fā)編程之ThreadLocal詳解

    在鎖的使用中會(huì)導(dǎo)致運(yùn)行效率降低,ThreadLocal的使用徹底避免對(duì)共享資源的競(jìng)爭(zhēng),同時(shí)又可以不影響效率。本文詳細(xì)講解了ThreadLocal,需要了解的小伙伴可以看一看這篇文章
    2021-08-08
  • java8 Math新增方法介紹

    java8 Math新增方法介紹

    這篇文章主要介紹了java8 Math新增方法介紹,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-08-08

最新評(píng)論