日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

Java实现提现到微信的功能

發(fā)布時(shí)間:2024/10/5 java 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java实现提现到微信的功能 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.添加依賴(lài)

<dependency><groupId>com.github.wxpay</groupId><artifactId>WXPay-SDK-Java</artifactId><version>0.0.4</version> </dependency>

2.配置參數(shù)

3.實(shí)現(xiàn)方法?

@ApiOperation(value = "企業(yè)轉(zhuǎn)賬到零錢(qián)", httpMethod = "POST", produces = "application/json;charset=UTF-8")@ApiImplicitParams(value = {@ApiImplicitParam(value = "*用戶(hù)token", name = "token",defaultValue ="", dataType = "String",paramType="header"),@ApiImplicitParam(value = "金額", name = "money",defaultValue ="", dataType = "String",paramType="query")})@PostMapping("/wxpay/transfer")public Result transfer(HttpServletRequest request,BigDecimal money,String openID) {// 1.0 拼湊企業(yè)支付需要的參數(shù)String appid = APPID; // APP對(duì)應(yīng)的微信的appidString mch_id = MCHID; // 商戶(hù)號(hào)String nonce_str = WXPayUtil.generateNonceStr(); // 生成隨機(jī)數(shù)String partner_trade_no = WXPayUtil.generateNonceStr(); // 生成商戶(hù)訂單號(hào)String openid = openID; // 收款用戶(hù)openidString check_name = "NO_CHECK"; // 是否驗(yàn)證真實(shí)姓名呢String re_user_name = "KOLO"; // 收款用戶(hù)姓名(非必須)String amount = String.valueOf(money); // 企業(yè)付款金額,最少為100,單位為分String desc = "恭喜你,完成了一個(gè)訂單!"; // 企業(yè)付款操作說(shuō)明信息。必填。String spbill_create_ip = IpKit.getIpAddr(request); // 用戶(hù)的ip地址// 2.0 生成map集合SortedMap<String, String> packageParams = new TreeMap<String, String>();packageParams.put("mch_appid", appid); // 微信公眾號(hào)的appidpackageParams.put("mchid", mch_id); // 商務(wù)號(hào)packageParams.put("nonce_str", nonce_str); // 隨機(jī)生成后數(shù)字,保證安全性packageParams.put("partner_trade_no", partner_trade_no); // 生成商戶(hù)訂單號(hào)packageParams.put("openid", openid); // 支付給用戶(hù)openidpackageParams.put("check_name", check_name); // 是否驗(yàn)證真實(shí)姓名呢packageParams.put("re_user_name", re_user_name);// 收款用戶(hù)姓名packageParams.put("amount", amount); // 企業(yè)付款金額,單位為分packageParams.put("desc", desc); // 企業(yè)付款操作說(shuō)明信息。必填。packageParams.put("spbill_create_ip", spbill_create_ip); // 調(diào)用接口的機(jī)器Ip地址try {// 3.0 利用上面的參數(shù),先去生成自己的簽名String sign = WXPayUtil.generateSignature(packageParams, PATERNERKEY);// 4.0 將簽名再放回map中,它也是一個(gè)參數(shù)packageParams.put("sign", sign);// 5.0將當(dāng)前的map結(jié)合轉(zhuǎn)化成xml格式String xml = WXPayUtil.mapToXml(packageParams);// 6.0獲取需要發(fā)送的url地址String wxUrl = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers"; // 獲取退款的api接口System.out.println("發(fā)送前的xml為:" + xml);// 7,向微信發(fā)送請(qǐng)求轉(zhuǎn)賬請(qǐng)求String returnXml = certHttpUtil.postData(wxUrl, xml, MCHID, CERTPATH);System.out.println("返回的returnXml為:" + returnXml);// 8,將微信返回的xml結(jié)果轉(zhuǎn)成map格式Map<String, String> returnMap = WXPayUtil.xmlToMap(returnXml);if (returnMap.get("result_code").equals("SUCCESS")) {// 付款成功System.out.println("returnMap為:" + returnMap);}else {return Result.error(902,returnMap.get("err_code_des"));}return Result.ok(returnMap.toString());} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();return Result.error(901,"微信轉(zhuǎn)賬失敗!");}}

4.用到的工具類(lèi)

import java.io.File;import java.io.FileInputStream;import java.io.IOException; import java.io.InputStream; import java.security.KeyStore;import javax.net.ssl.SSLContext;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.config.RequestConfig;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.springframework.stereotype.Component;/*** 獲取微信apiclient_cert.p12證書(shū)*/@Componentpublic class CertHttpUtil {private static int socketTimeout = 10000;// 連接超時(shí)時(shí)間,默認(rèn)10秒private static int connectTimeout = 30000;// 傳輸超時(shí)時(shí)間,默認(rèn)30秒private static RequestConfig requestConfig;// 請(qǐng)求器的配置private static CloseableHttpClient httpClient;// HTTP請(qǐng)求器/*** 通過(guò)Https往API post xml數(shù)據(jù)** @param url API地址* @param xmlObj 要提交的XML數(shù)據(jù)對(duì)象* @param mchId 商戶(hù)ID* @param certPath 證書(shū)位置* @return*/public String postData(String url, String xmlObj, String mchId, String certPath) {// 加載證書(shū)try {initCert(mchId, certPath);} catch (Exception e) {e.printStackTrace();}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);// 根據(jù)默認(rèn)超時(shí)限制初始化requestConfigrequestConfig = RequestConfig.custom().setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout).build();// 設(shè)置請(qǐng)求器的配置httpPost.setConfig(requestConfig);try {HttpResponse response = null;try {response = httpClient.execute(httpPost);} catch (IOException e) {e.printStackTrace();}HttpEntity entity = response.getEntity();try {result = EntityUtils.toString(entity, "UTF-8");} catch (IOException e) {e.printStackTrace();}} finally {httpPost.abort();}return result;}/*** 加載證書(shū)** @param mchId 商戶(hù)ID* @param certPath 證書(shū)位置* @throws Exception*/private void initCert(String mchId, String certPath) throws Exception {// 證書(shū)密碼,默認(rèn)為商戶(hù)IDString key = mchId;// 證書(shū)的路徑String path = certPath;// 指定讀取證書(shū)格式為PKCS12KeyStore keyStore = KeyStore.getInstance("PKCS12");// 讀取本機(jī)存放的PKCS12證書(shū)文件File file = new File(path);InputStream in = new FileInputStream(file);try {// 指定PKCS12的密碼(商戶(hù)ID)keyStore.load(in, key.toCharArray());} finally {in.close();}SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, key.toCharArray()).build();SSLConnectionSocketFactory sslsf =new SSLConnectionSocketFactory(sslcontext, new String[] {"TLSv1"}, null,SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();}}

注意,這個(gè)功能的實(shí)現(xiàn)需要先獲取微信授權(quán),授權(quán)后獲取微信用戶(hù)的openid才能完成提現(xiàn),同時(shí)該方法適合微信公眾號(hào)提現(xiàn)和微信小程序提現(xiàn)。

總結(jié)

以上是生活随笔為你收集整理的Java实现提现到微信的功能的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。