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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java微信服务商支付,Java 微信支付之APP支付服务端 (二)

發布時間:2025/3/15 java 61 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java微信服务商支付,Java 微信支付之APP支付服务端 (二) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

微信支付中設計的工具類

1.httpclient

package com.xunxin.util;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.util.HashMap;

import java.util.Iterator;

import java.util.List;

import java.util.Map;

import java.util.Map.Entry;

import org.apache.commons.httpclient.Header;

import org.apache.commons.httpclient.HttpClient;

import org.apache.commons.httpclient.HttpException;

import org.apache.commons.httpclient.methods.GetMethod;

import org.apache.commons.httpclient.methods.PostMethod;

import org.apache.commons.httpclient.methods.RequestEntity;

import org.apache.commons.httpclient.methods.StringRequestEntity;

import org.apache.commons.httpclient.params.HttpMethodParams;

import com.alibaba.fastjson.JSON;

/**

* Copyright ? 2017 noseparte(Libra) ? Like the wind, like rain

* @Author Noseparte

* @Compile 2017年10月24日 -- 下午6:03:20

* @Version 1.0

* @Description Apache HttpClient Util

*/

public class MaryunHttpUtils {

public static class UHeader{

/**

* the title of header.

*/

private String headerTitle;

private String headerValue;

public UHeader(String headerTitle, String headerValue) {

super();

this.headerTitle = headerTitle;

this.headerValue = headerValue;

}

public String getHeaderTitle() {

return headerTitle;

}

public void setHeaderTitle(String headerKey) {

this.headerTitle = headerKey;

}

public String getHeaderValue() {

return headerValue;

}

public void setHeaderValue(String headerValue) {

this.headerValue = headerValue;

}

}

public static String getResponse(String url,HashMap args,List headerList){

String info = "";

try {

HttpClient client = new HttpClient();

// client = setProxy(client);

// client.getHostConfiguration().setProxy("10.170.187.42", 3128);

client.setConnectionTimeout(60000);

client.setTimeout(60000);

GetMethod method = new GetMethod(url);

client.getParams().setContentCharset("UTF-8");

if(headerList.size()>0){

for(int i = 0;i

UHeader header = headerList.get(i);

method.setRequestHeader(header.getHeaderTitle(),header.getHeaderValue());

}

}

Iterator it = args.entrySet().iterator();

while(it.hasNext()){

Entry entry = (Entry)it.next();

HttpMethodParams pram = new HttpMethodParams();

pram.setParameter(entry.getKey().toString(), entry.getValue().toString());

method.setParams(pram);

}

method.getParams().setParameter(

HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");

client.executeMethod(method);

info = new String(method.getResponseBody(), "UTF-8");

} catch (HttpException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return info;

}

public static String httpGet(String url,HashMap args,List headerList){

String info = "";

try {

HttpClient client = new HttpClient();

//client = setProxy(client);

// client.getHostConfiguration().setProxy("10.170.187.42", 3128);

Iterator it = args.entrySet().iterator();

String sargs = "";

while(it.hasNext()){

Entry entry = (Entry)it.next();

sargs += "&"+entry.getKey().toString()+"="+entry.getValue();

}

if(!"".equals(sargs)){

sargs = "?"+sargs.substring(1, sargs.length());

}

System.out.println(url+sargs);

GetMethod method = new GetMethod(url+sargs);

// client.getParams().setContentCharset("UTF-8");

if(headerList!=null&&headerList.size()>0){

for(int i = 0;i

UHeader header = headerList.get(i);

method.setRequestHeader(header.getHeaderTitle(),header.getHeaderValue());

}

}

client.executeMethod(method);

info = new String(method.getResponseBody(), "UTF-8");

} catch (HttpException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return info;

}

public static String getPostResponse(String url,Map map,List headerList){

String info = "";

try {

HttpClient client = new HttpClient();

//client = setProxy(client);

// client.getHostConfiguration().setProxy("10.170.187.42", 3128);

PostMethod method = new PostMethod(url);

client.getParams().setContentCharset("UTF-8");

if(headerList.size()>0){

for(int i = 0;i

UHeader header = headerList.get(i);

method.setRequestHeader(header.getHeaderTitle(),header.getHeaderValue());

}

}

Iterator it = map.entrySet().iterator();

while(it.hasNext()){

Entry entry = (Entry)it.next();

method.addParameter(entry.getKey().toString(), entry.getValue().toString());

}

method.getParams().setParameter(

HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");

client.executeMethod(method);

info = new String(method.getResponseBody(), "UTF-8");

} catch (HttpException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return info;

}

public static String getPostResponseHeader(String url,HashMap args,List headerList,String headerName){

String info = "";

try {

HttpClient client = new HttpClient();

PostMethod method = new PostMethod(url);

client.getParams().setContentCharset("UTF-8");

if(headerList.size()>0){

for(int i = 0;i

UHeader header = headerList.get(i);

method.setRequestHeader(header.getHeaderTitle(),header.getHeaderValue());

}

}

Iterator it = args.entrySet().iterator();

while(it.hasNext()){

Entry entry = (Entry)it.next();

method.addParameter(entry.getKey().toString(), entry.getValue().toString());

}

method.getParams().setParameter(

HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");

client.executeMethod(method);

Header h = method.getResponseHeader(headerName);

String rb = new String(method.getResponseBody(), "UTF-8");

Map map = new HashMap();

map.put("ErrorCode", rb);

map.put(headerName, h.getValue());

info = JSON.toJSONString(map);

} catch (HttpException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

}

return info;

}

public static String getPostResponse(String url,String argJson,List headerList){

String info = "";

try {

HttpClient client = new HttpClient();

PostMethod method = new PostMethod(url);

client.getParams().setContentCharset("UTF-8");

if(headerList.size()>0){

for(int i = 0;i

UHeader header = headerList.get(i);

method.setRequestHeader(header.getHeaderTitle(),header.getHeaderValue());

}

}

method.getParams().setParameter(

HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");

if(argJson != null && !argJson.trim().equals("")) {

RequestEntity requestEntity = new StringRequestEntity(argJson,"application/json","UTF-8");

method.setRequestEntity(requestEntity);

}

method.releaseConnection();

client.executeMethod(method);

InputStream inputStream = method.getResponseBodyAsStream();

BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));

StringBuffer stringBuffer = new StringBuffer();

String str= "";

while((str = br.readLine()) != null){

stringBuffer.append(str);

}

info = new String(stringBuffer);

} catch (HttpException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return info;

}

public static String getPostResponseHeader(String url,String argJson,List headerList,String headerName){

String info = "";

try {

HttpClient client = new HttpClient();

PostMethod method = new PostMethod(url);

client.getParams().setContentCharset("UTF-8");

if(headerList.size()>0){

for(int i = 0;i

UHeader header = headerList.get(i);

method.setRequestHeader(header.getHeaderTitle(),header.getHeaderValue());

}

}

method.getParams().setParameter(

HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");

if(argJson != null && !argJson.trim().equals("")) {

RequestEntity requestEntity = new StringRequestEntity(argJson,"application/json","UTF-8");

method.setRequestEntity(requestEntity);

}

method.releaseConnection();

Header h = method.getResponseHeader(headerName);

info = h.getValue();

} catch (IOException e) {

e.printStackTrace();

}

return info;

}

}

2.商戶訂單號工具類 OrderGeneratedUtils

import java.text.SimpleDateFormat;

import java.util.Date;

/**

*

* Copyright ? 2017 noseparte(Libra) ? Like the wind, like rain

* @Author Noseparte

* @Compile 2017年11月2日 -- 下午5:18:35

* @Version 1.0

* @Description 訂單生成工具類

*/

public class OrderGeneratedUtils {

/**

* 鎖對象,可以為任意對象

*/

private static Object lockObj = "lockerOrder";

/**

* 訂單號生成計數器

*/

private static long orderNumCount = 0L;

/**

* 每毫秒生成訂單號數量最大值

*/

private static int maxPerMSECSize=1000;

/**

* 生成訂單編號

* @return

*/

public static synchronized String getOrderNo() {

try {

// 最終生成的訂單號

String finOrderNum = "";

synchronized (lockObj) {

// 取系統當前時間作為訂單號變量前半部分,精確到毫秒

long nowLong = Long.parseLong(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()));

// 計數器到最大值歸零,可擴展更大,目前1毫秒處理峰值1000個,1秒100萬

if (orderNumCount >= maxPerMSECSize) {

orderNumCount = 0L;

}

//組裝訂單號

String countStr=maxPerMSECSize +orderNumCount+"";

finOrderNum=nowLong+countStr.substring(1);

orderNumCount++;

System.out.println(finOrderNum);

// Thread.sleep(1000);

}

return finOrderNum;

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

3.微信支付隨機算法生成 WXRandomNumberUtil

import java.util.Random;

/**

*

* Copyright ? 2017 noseparte(Libra) ? Like the wind, like rain

* @Author Noseparte

* @Compile 2017年11月6日 -- 上午11:20:41

* @Version 1.0

* @Description 微信支付生成隨機數算法

*/

public class WXRandomNumberUtil {

/**

* 生成隨機數算法

*

* @param length

* @return

*/

public static String getRandomStringByLength(int length) {

String base = "abcdefghijklmnopqrstuvwxyz0123456789";

Random random = new Random();

StringBuffer sb = new StringBuffer();

for (int i = 0; i < length; i++) {

int number = random.nextInt(base.length());

sb.append(base.charAt(number));

}

return sb.toString();

}

/**

* 微信支付交易金額格式化

*

* @param amount

* @return

*/

public static int wx_format_PayAmount(String amount) {

int pay_amount = 0;

pay_amount = Integer.parseInt((amount.split(".")[0])) * 100;

return pay_amount;

}

總結

以上是生活随笔為你收集整理的java微信服务商支付,Java 微信支付之APP支付服务端 (二)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。