導航:首頁 > 編程語言 > 微信支付demojava

微信支付demojava

發布時間:2022-12-29 07:49:41

Ⅰ 用java怎麼實現微信支付

具體方法步驟:

一、准備階段:已認證微信號,且通過微信支付認證,這個可以看微信文檔,很詳細,這里就不再重復。

二、配置授權目錄,官方推薦使用https類型的url,不知道http能不能行,個人也推薦使用https的保證不會錯。

配置授權域名


三、微信支付二次開發所需要的參數:

APP_ID,APP_KEY,PARTNER,PARTNER_KEY(AppSecret)

APP_ID和PARTNER_KEY(AppSecret)

PARTNER

APP_KEY(自行設置32位字元)

四、具體編程

1、通過頁面跳轉到確認支付頁面,其中的redirect_uri必須是配置授權目錄下的。

2、獲取到openid,再經伺服器向微信請求獲取prepay_id,封裝欄位並進行簽名後通過jsapi調起微信支付

3、測試結果

android微信支付demo怎麼開通零錢支付

准備:

1,導入微信的libs包libammsdk.jar;

2,測試時使用weixinDemo中的debug_keystore;

3,需要注意應用要通過審核,並且幾個Key值正確,一下為微信支付Demo中的值:

?

1

2

3

4

5

6

7

8

9

10

//微信公眾平台id;

private

String app_wx_appid=WxConstants.app_wx_appid;

//微信開放平台和商戶約定的密鑰

private

String app_wx_secret_key="";

//微信公眾平台商戶模塊和商戶約定的密鑰

private

String app_wx_parent_key="";

//微信公眾平台商戶模塊和商戶約定的支付密鑰

private

String app_wx_pay_key="";

//
商家向財付通申請的商家id */

private

String app_tx_parent_key = "1900000109";

==========================================

根據微信支付Demo,微信支付分為三步:

第一步,獲取accessToken,accessToken值第二步要用;

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

private

class
GetAccessTokenTask extends

AsyncTask<void,
void,=""

wxgetaccesstokenresult="">
{

@Override

protected

WxGetAccessTokenResult doInBackground(Void... params) {

WxGetAccessTokenResult
result = getAccessToken();

return

result;

}

@Override

protected

void
onPostExecute(WxGetAccessTokenResult result) {

if

(result.localRetCode == WxLocalRetCode.ERR_OK) {

GetPrepayIdTask
getPrepayId = new

GetPrepayIdTask();

getPrepayId.execute(result);

}

}}</void,>
解析伺服器響應
?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

private

WxGetAccessTokenResult getAccessToken() {

WxGetAccessTokenResult
result = new

WxGetAccessTokenResult();

String
url = String.format(api_get_access_token,

"client_credential",

app_wx_appid,

app_wx_secret_key);

byte[]
buf = WeixinUtil.httpGet(url);

if

(buf == null

|| buf.length == 0)
{

result.localRetCode
= WxLocalRetCode.ERR_HTTP;

return

result;

}

String
content = new

String(buf);

result.parseFrom(content);

return

result;

}
第二步,根據第一步的accesstoken值,將 組裝的商品參數Post給微信伺服器
?

1

2

3

4

5

6

7

8

9

10

11

12

13

private

class
GetPrepayIdTask extends

AsyncTask<wxgetaccesstokenresult, void,=""

wxgetprepayidresult="">
{

@Override

protected

WxGetPrepayIdResult doInBackground(WxGetAccessTokenResult... params) {

WxGetPrepayIdResult
result = getPrepayId(params[0]);

return

result;

}

@Override

protected

void
onPostExecute(WxGetPrepayIdResult result) {

if

(result.localRetCode == WxLocalRetCode.ERR_OK) {

sendPayReq(result);

}

}

}</wxgetaccesstokenresult,>
組裝參數
?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

private

WxGetPrepayIdResult getPrepayId(WxGetAccessTokenResult accessTokenResult) {

String
url = String.format(api_get_preorder_id,accessTokenResult.accessToken);

String
entity = appSign.getWxPrepayAppSign();WxGetPrepayIdResult
result = new

WxGetPrepayIdResult();byte[]
buf = WeixinUtil.httpPost(url, entity);

if

(buf == null

|| buf.length == 0)
{

result.localRetCode
= WxLocalRetCode.ERR_HTTP;

return

result;

}String
content = new

String(buf);

result.parseFrom(content);

return

result;

}
Post給伺服器
?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

<span
style="white-space:pre">
</span>private

void
sendPayReq(WxGetPrepayIdResult result) {PayReq
req = new

PayReq();

req.appId
= app_wx_appid;

req.partnerId
= app_tx_parent_key;

req.prepayId
= result.prepayId;

req.nonceStr
= appSign.getNoncestr();

req.timeStamp
= appSign.getTimestamp();

req.packageValue
= "Sign="

+ appSign.getPackageSign();List<namevaluepair>
signParams = new

LinkedList<namevaluepair>();

signParams.add(new

BasicNameValuePair("appid",
req.appId));

signParams.add(new

BasicNameValuePair("appkey",
app_wx_pay_key));

signParams.add(new

BasicNameValuePair("noncestr",
req.nonceStr));

signParams.add(new

BasicNameValuePair("package",
req.packageValue));

signParams.add(new

BasicNameValuePair("partnerid",
req.partnerId));

signParams.add(new

BasicNameValuePair("prepayid",
req.prepayId));

signParams.add(new

BasicNameValuePair("timestamp",
req.timeStamp));

req.sign
= WeixinUtil.genSign(signParams);wxRequest.sendReq(req);

}</namevaluepair></namevaluepair>
?

1


?

1


第三步:在項目下新建一個包wxapi,建立一個類名為WXPayEntryActivity作為接受微信的支付結果,不過最終結果以伺服器的返回為准notify_url:
?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

package

net.sourceforge.simcpux.wxapi;

public

class
WXPayEntryActivity extends

Activity implements

IWXAPIEventHandler{<pre name="code"

class="java"><span
style="white-space:pre">
</span>@Override

public

void
onResp(BaseResp resp) {

Log.d(TAG,
"onPayFinish,
errCode = "
+ resp.errCode);if

(resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {

AlertDialog.Builder
builder = new

AlertDialog.Builder(this);

builder.setTitle("支付結果");

builder.setMessage("支付結果"+String.valueOf(resp.errCode));

builder.show();

}

}</pre>}

<p></p>

<pre
class="brush:java;"></pre>

==========================================

<p></p>

<p>暫時沒想到其他想說的,先看個效果</p>

<p>1,包結構,需要注意的就是接收微信返回結果的那個類名;</p>

<p><img
src="http://www.2cto.com/uploadfile/Collfiles/20140901/201409010901522.png"

alt="\"
style="display:
inline; width: 239px; height: 238px;"></p>

<p>2,組裝數據,規則在文檔中有說明<喎�"http://www.2cto.com/kf/ware/vc/"

target="_blank"

class="keylink">vcD4KPHByZSBjbGFzcz0="brush:java;">//package_
欄位生成方法

//package生成方法:

//A)對所有傳入參數按照欄位名的ASCII
碼從小到大排序(字典序)後,使用URL 鍵值對的格式(即key1=value1&key2=value2…)拼接成字元串string1;

//B)
在string1 最後拼接上key=partnerKey 得到stringSignTemp 字元串, 並對 stringSignTemp進行md5 運算,再將得到的字元串所有字元轉換為大寫,得到sign值signValue。

//C)對string1
中的所有鍵值對中的value 進行urlencode 轉碼,按照a 步驟重新拼接成字元串,得到string2。對於js 前端程序,一定要使用函數encodeURIComponent 進行urlencode編碼(注意!進行urlencode時要將空格轉化為%20而不是+)。

//D)將sign=signValue
拼接到string1 後面得到最終的package 字元串。//app_signature生成方法:

//A)參與簽名的欄位包括:appid、appkey、noncestr、package、timestamp以及
traceid

//B)對所有待簽名參數按照欄位名的ASCII
碼從小到大排序(字典序)後,使用URL 鍵值對的格式(即key1=value1&key2=value2…)拼接成字元串string1。 注意:所有參數名均為小寫字元

//C)對string1
作簽名演算法,欄位名和欄位值都採用原始值,不進行URL 轉義。具體簽名演算法為SHA1</p>
\

Ⅲ 怎麼用java調用微信支付介面

java調用微信支付介面方法:x0d=newRequestHandler(super.getRequest(),super.getResponse());x0dx0ax0dx0a//獲取token//兩小時內有效,兩小時後重新獲取x0dx0ax0dx0aToken=requestHandler.GetToken();x0dx0ax0dx0a//更新token到應用中x0dx0ax0dx0arequestHandler.getTokenReal();x0dx0ax0dx0aSystem.out.println("微信支付獲取token=======================:"+Token);x0dx0ax0dx0ax0dx0ax0dx0a//requestHandler初始化x0dx0ax0dx0arequestHandler.init();x0dx0ax0dx0arequestHandler.init(appid,appsecret,appkey,partnerkey,key);x0dx0ax0dx0ax0dx0ax0dx0a//--------------------------------本地系統生成訂單-------------------------------------x0dx0ax0dx0a//設置package訂單參數x0dx0ax0dx0aSortedMappackageParams=newTreeMap();x0dx0ax0dx0apackageParams.put("bank_type","WX");//支付類型x0dx0ax0dx0apackageParams.put("body","xxxx");//商品描述x0dx0ax0dx0apackageParams.put("fee_type","1");//銀行幣種x0dx0ax0dx0apackageParams.put("input_charset","UTF-8");//字元集x0dx0ax0dx0apackageParams.put("notify_url","http://xxxx.com/xxxx/wxcallback");//通知地址這里的通知地址使用外網地址測試,注意80埠是否打開。x0dx0ax0dx0apackageParams.put("out_trade_no",no);//商戶訂單號x0dx0ax0dx0apackageParams.put("partner",partenerid);//設置商戶號x0dx0ax0dx0apackageParams.put("spbill_create_ip",super.getRequest().getRemoteHost());//訂單生成的機器IP,指用戶瀏覽器端IPx0dx0ax0dx0apackageParams.put("total_fee",String.valueOf(rstotal));//商品總金額,以分為單位x0dx0ax0dx0ax0dx0ax0dx0a//設置支付參數x0dx0ax0dx0aSortedMapsignParams=newTreeMap();x0dx0ax0dx0asignParams.put("appid",appid);x0dx0ax0dx0asignParams.put("noncestr",noncestr);x0dx0ax0dx0asignParams.put("traceid",PropertiesUtils.getOrderNO());x0dx0ax0dx0asignParams.put("timestamp",timestamp);x0dx0ax0dx0asignParams.put("package",packageValue);x0dx0ax0dx0asignParams.put("appkey",this.appkey);x0dx0ax0dx0ax0dx0ax0dx0a//生成支付簽名,要採用URLENCODER的原始值進行SHA1演算法!x0dx0ax0dx0aStringsign="";x0dx0ax0dx0atry{x0dx0ax0dx0asign=Sha1Util.createSHA1Sign(signParams);x0dx0ax0dx0a}catch(Exceptione){x0dx0ax0dx0ae.printStackTrace();x0dx0ax0dx0a}x0dx0ax0dx0ax0dx0ax0dx0a//增加非參與簽名的額外參數x0dx0ax0dx0asignParams.put("sign_method","sha1");x0dx0ax0dx0asignParams.put("app_signature",sign);//api支付拼包結束------------------------------------x0dx0ax0dx0ax0dx0ax0dx0a//獲取=requestHandler.sendPrepay(signParams);x0dx0ax0dx0aSystem.out.println("prepayid:"+prepayid);x0dx0ax0dx0a//--------------------------------生成完成---------------------------------------------x0dx0ax0dx0ax0dx0ax0dx0a//生成預付快訂單完成,返回給android,ios掉起微信所需要的參數。x0dx0ax0dx0aSortedMappayParams=newTreeMap();x0dx0ax0dx0apayParams.put("appid",appid);x0dx0ax0dx0apayParams.put("noncestr",noncestr);x0dx0ax0dx0apayParams.put("package","Sign=WXPay");x0dx0ax0dx0apayParams.put("partnerid",partenerid);x0dx0ax0dx0apayParams.put("prepayid",prepayid);x0dx0ax0dx0apayParams.put("appkey",this.appkey);x0dx0ax0dx0a//這里除1000是因為參數長度限制。x0dx0ax0dx0ainttime=(int)(System.currentTimeMillis()/1000);x0dx0ax0dx0apayParams.put("timestamp",String.valueOf(time));.out.println("timestamp:"+time);x0dx0ax0dx0ax0dx0ax0dx0a//簽名x0dx0ax0dx0aStringpaysign="";x0dx0ax0dx0atry{x0dx0ax0dx0apaysign=Sha1Util.createSHA1Sign(payParams);x0dx0ax0dx0a}catch(Exceptione){x0dx0ax0dx0ae.printStackTrace();x0dx0ax0dx0a}x0dx0ax0dx0apayParams.put("sign",paysign);x0dx0ax0dx0ax0dx0ax0dx0a//拼json數據返回給客戶端=newBasicDBObject();x0dx0ax0dx0abackObject.put("appid",appid);x0dx0ax0dx0abackObject.put("noncestr",payParams.get("noncestr"));x0dx0ax0dx0abackObject.put("package","Sign=WXPay");x0dx0ax0dx0abackObject.put("partnerid",payParams.get("partnerid"));x0dx0ax0dx0abackObject.put("prepayid",payParams.get("prepayid"));x0dx0ax0dx0abackObject.put("appkey",this.appkey);x0dx0ax0dx0abackObject.put("timestamp",payParams.get("timestamp"));x0dx0ax0dx0abackObject.put("sign",payParams.get("sign"));=dataObject.toString();x0dx0ax0dx0aSystem.out.println("backstr:"+backstr);;x0dx0ax0dx0ax0dx0ax0dx0a====================到此為止,預付款訂單已生成,並且已返回客戶端====================x0dx0ax0dx0ax0dx0ax0dx0a//坐等微信伺服器通知,通知的地址就是生成預付款訂單的notify_=newResponseHandler(request,response);x0dx0ax0dx0aresHandler.setKey(partnerkey);x0dx0ax0dx0a//創建請求對象x0dx0ax0dx0a//RequestHandlerqueryReq=newRequestHandler(request,response);x0dx0ax0dx0a//queryReq.init();x0dx0ax0dx0aif(resHandler.isTenpaySign()==true){x0dx0ax0dx0a//商戶訂單號x0dx0ax0dx0aStringout_trade_no=resHandler.getParameter("out_trade_no");x0dx0ax0dx0aSystem.out.println("out_trade_no:"+out_trade_no);x0dx0ax0dx0a//財付通訂單號x0dx0ax0dx0aStringtransaction_id=resHandler.getParameter("transaction_id");x0dx0ax0dx0aSystem.out.println("transaction_id:"+transaction_id);x0dx0ax0dx0a//金額,以分為單位x0dx0ax0dx0aStringtotal_fee=resHandler.getParameter("total_fee");x0dx0ax0dx0a//如果有使用折扣券,discount有值,total_fee+discount=原請求的total_feex0dx0ax0dx0aStringdiscount=resHandler.getParameter("discount");x0dx0ax0dx0a//支付結果x0dx0ax0dx0aStringtrade_state=resHandler.getParameter("trade_state");x0dx0ax0dx0ax0dx0ax0dx0a//判斷簽名及結果x0dx0ax0dx0aif("0".equals(trade_state)){x0dx0ax0dx0a//------------------------------x0dx0ax0dx0a//即時到賬處理業務開始x0dx0ax0dx0a//------------------------------.out.println("----------------業務邏輯執行-----------------");x0dx0ax0dx0ax0dx0ax0dx0a//——請根據您的業務邏輯來編寫程序(以上代碼僅作參考)——x0dx0ax0dx0aSystem.out.println("----------------業務邏輯執行完畢-----------------");x0dx0ax0dx0aSystem.out.println("success");//請不要修改或刪除.out.println("即時到賬支付成功");x0dx0ax0dx0a//給財付通系統發送成功信息,財付通系統收到此結果後不再進行後續通知x0dx0ax0dx0aresHandler.sendToCFT("success");x0dx0ax0dx0ax0dx0ax0dx0a//給微信伺服器返回success否則30分鍾通知8次x0dx0ax0dx0areturn"success";x0dx0ax0dx0a}else{x0dx0ax0dx0aSystem.out.println("通知簽名驗證失敗");x0dx0ax0dx0aresHandler.sendToCFT("fail");x0dx0ax0dx0aresponse.setCharacterEncoding("utf-8");x0dx0ax0dx0a}x0dx0ax0dx0a}else{x0dx0ax0dx0aSystem.out.println("fail-Md5failed");

Ⅳ java實現微信支付,通過JSAPI發起支付請求

在config.jsp填 好 在ResponseHandler填下APPKEY 用wxm-pay-api-demo.html或者jsapi.jsp我測試是都可以用.試能用哪個用哪個吧 JS已經搞好了

ResponseHandler,ResponseHandler,payNotifyUrl.jsp都修了..

Ⅳ 怎麼用java調用微信支付介面

java調用微信支付介面方法:
RequestHandler requestHandler = new RequestHandler(super.getRequest(),super.getResponse());

//獲取token //兩小時內有效,兩小時後重新獲取

Token = requestHandler.GetToken();

//更新token 到應用中

requestHandler.getTokenReal();

System.out.println("微信支付獲取token=======================:" +Token);

//requestHandler 初始化

requestHandler.init();

requestHandler.init(appid,appsecret, appkey,partnerkey, key);

// --------------------------------本地系統生成訂單-------------------------------------

// 設置package訂單參數

SortedMap<String, String> packageParams = new TreeMap<String, String>();

packageParams.put("bank_type", "WX"); // 支付類型

packageParams.put("body", "xxxx"); // 商品描述

packageParams.put("fee_type", "1"); // 銀行幣種

packageParams.put("input_charset", "UTF-8"); // 字元集

packageParams.put("notify_url", "http://xxxx.com/xxxx/wxcallback"); // 通知地址 這里的通知地址使用外網地址測試,注意80埠是否打開。

packageParams.put("out_trade_no", no); // 商戶訂單號

packageParams.put("partner", partenerid); // 設置商戶號

packageParams.put("spbill_create_ip", super.getRequest().getRemoteHost()); // 訂單生成的機器IP,指用戶瀏覽器端IP

packageParams.put("total_fee", String.valueOf(rstotal)); // 商品總金額,以分為單位

// 設置支付參數

SortedMap<String, String> signParams = new TreeMap<String, String>();

signParams.put("appid", appid);

signParams.put("noncestr", noncestr);

signParams.put("traceid", PropertiesUtils.getOrderNO());

signParams.put("timestamp", timestamp);

signParams.put("package", packageValue);

signParams.put("appkey", this.appkey);

// 生成支付簽名,要採用URLENCODER的原始值進行SHA1演算法!

String sign ="";

try {

sign = Sha1Util.createSHA1Sign(signParams);

} catch (Exception e) {

e.printStackTrace();

}

// 增加非參與簽名的額外參數

signParams.put("sign_method", "sha1");

signParams.put("app_signature", sign);

// api支付拼包結束------------------------------------

//獲取prepayid

String prepayid = requestHandler.sendPrepay(signParams);

System.out.println("prepayid :" + prepayid);

// --------------------------------生成完成---------------------------------------------

//生成預付快訂單完成,返回給android,ios 掉起微信所需要的參數。

SortedMap<String, String> payParams = new TreeMap<String, String>();

payParams.put("appid", appid);

payParams.put("noncestr", noncestr);

payParams.put("package", "Sign=WXPay");

payParams.put("partnerid", partenerid);

payParams.put("prepayid", prepayid);

payParams.put("appkey", this.appkey);

//這里除1000 是因為參數長度限制。

int time = (int) (System.currentTimeMillis() / 1000);

payParams.put("timestamp",String.valueOf(time));

System.out.println("timestamp:" + time);

//簽名

String paysign ="";

try {

paysign = Sha1Util.createSHA1Sign(payParams);

} catch (Exception e) {

e.printStackTrace();

}

payParams.put("sign", paysign);

//拼json 數據返回給客戶端

BasicDBObject backObject = new BasicDBObject();

backObject.put("appid", appid);

backObject.put("noncestr", payParams.get("noncestr"));

backObject.put("package", "Sign=WXPay");

backObject.put("partnerid", payParams.get("partnerid"));

backObject.put("prepayid", payParams.get("prepayid"));

backObject.put("appkey", this.appkey);

backObject.put("timestamp",payParams.get("timestamp"));

backObject.put("sign",payParams.get("sign"));

String backstr = dataObject.toString();

System.out.println("backstr:" + backstr);

return backstr;

====================到此為止,預付款訂單已生成,並且已返回客戶端====================

//坐等微信伺服器通知,通知的地址就是生成預付款訂單的notify_url

ResponseHandler resHandler = new ResponseHandler(request, response);

resHandler.setKey(partnerkey);

//創建請求對象

//RequestHandler queryReq = new RequestHandler(request, response);

//queryReq.init();

if (resHandler.isTenpaySign() == true) {

//商戶訂單號

String out_trade_no = resHandler.getParameter("out_trade_no");

System.out.println("out_trade_no:" + out_trade_no);

//財付通訂單號

String transaction_id = resHandler.getParameter("transaction_id");

System.out.println("transaction_id:" + transaction_id);

//金額,以分為單位

String total_fee = resHandler.getParameter("total_fee");

//如果有使用折扣券,discount有值,total_fee+discount=原請求的total_fee

String discount = resHandler.getParameter("discount");

//支付結果

String trade_state = resHandler.getParameter("trade_state");

//判斷簽名及結果

if ("0".equals(trade_state)) {

//------------------------------

//即時到賬處理業務開始

//------------------------------

System.out.println("----------------業務邏輯執行-----------------");

//——請根據您的業務邏輯來編寫程序(以上代碼僅作參考)——

System.out.println("----------------業務邏輯執行完畢-----------------");

System.out.println("success"); // 請不要修改或刪除

System.out.println("即時到賬支付成功");

//給財付通系統發送成功信息,財付通系統收到此結果後不再進行後續通知

resHandler.sendToCFT("success");

//給微信伺服器返回success 否則30分鍾通知8次

return "success";

}else{

System.out.println("通知簽名驗證失敗");

resHandler.sendToCFT("fail");

response.setCharacterEncoding("utf-8");

}

}else {

System.out.println("fail -Md5 failed");

Ⅵ 微信支付的java版dome在哪

直接打開網路瀏覽器,搜索微信支付的java版dome,就能看到下載列表,點擊下載按鈕,如果電腦上有迅雷,也可以用迅雷下載的。

Ⅶ 微信支付Java如何判斷回調

微信支付Java判斷回調方法:
微信支付完成支付調用的時候,在傳入的參數中有一個是執行支付完成之後結果回調的參數,這個回調函數就是微信調用這個介面來將支付成功的結果。

Ⅷ 微信支付後端篇

微信支付系列文章

微信支付-java後端實現

微信支付-vue 前端實現

java demo: 下載地址文章底部

技術棧

Spring boot

java

XML (微信在http協議中數據傳輸方案)

MD5 簽名

微信支付術語

openid (OpenID是公眾號一對一對應用戶身份的標識)

app_id (公眾號id,登錄微信公眾號–開發–基本配置中獲得;)

key (收款商戶後台進行配置,登錄微信商戶平台–賬戶中心–API安全-設置秘鑰,設置32位key值;)

mch_id (收款商家商戶號;)

certPath (API證書, 登錄微信商戶平台–賬戶中心-API安全-下載證書)

後端流程

服務端需要的核心操作, 總共分為以下幾步:

統一下單

前端調起微信支付必要參數 (需加密)

訂單結果主動通知 (回調介面)

查詢訂單結果

結束訂單支付介面(關閉訂單,支付訂單關閉)

代碼

微信總共支持多種語言的sdk, 在官網可以下載例子, java程序也可以引入微信支付的sdk包, 但是github上的sdk已經很久沒有更新了, 最好的選擇, 也是我的選擇, 在官網上下載sdk項目, 將其中所有java類到自己的項目中.

官網sdk下載目錄

鏈接: 商戶平台首頁

####根據微信sdk生成配置類 WXPayConfig

創建IWxPayConfig.class, 繼承sdk WXPayConfig.class, 實現sdk中部分抽象方法, 讀取本地證書, 載入到配置類中.

package core.com.chidori.wxpay;

import core.com.wxpay.IWXPayDomain;

import core.com.wxpay.WXPayConfig;

import core.com.wxpay.WXPayConstants;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.stereotype.Service;

import java.io.ByteArrayInputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStream;

@Service

public class IWxPayConfig extends WXPayConfig { // 繼承sdk WXPayConfig 實現sdk中部分抽象方法

private byte[] certData;

@Value("${vendor.wx.config.app_id}")

private String app_id;

@Value("${vendor.wx.pay.key}")

private String wx_pay_key;

@Value("${vendor.wx.pay.mch_id}")

private String wx_pay_mch_id;

public IWxPayConfig() throws Exception { // 構造方法讀取證書, 通過getCertStream 可以使sdk獲取到證書

String certPath = "/data/config/chidori/apiclient_cert.p12";

File file = new File(certPath);

InputStream certStream = new FileInputStream(file);

this.certData = new byte[(int) file.length()];

certStream.read(this.certData);

certStream.close();

}

@Override

public String getAppID() {

return app_id;

}

@Override

public String getMchID() {

return wx_pay_mch_id;

}

@Override

public String getKey() {

return wx_pay_key;

}

@Override

public InputStream getCertStream() {

return new ByteArrayInputStream(this.certData);

}

@Override

public IWXPayDomain getWXPayDomain() { // 這個方法需要這樣實現, 否則無法正常初始化WXPay

IWXPayDomain iwxPayDomain = new IWXPayDomain() {

@Override

public void report(String domain, long elapsedTimeMillis, Exception ex) {

}

@Override

public DomainInfo getDomain(WXPayConfig config) {

return new IWXPayDomain.DomainInfo(WXPayConstants.DOMAIN_API, true);

}

};

return iwxPayDomain;

}

}

發起統一下單 AND 前端調起微信支付必要參數

// 發起微信支付

WXPay wxpay = null;

Map result = new HashMap>();

try {

// ******************************************

//

// 統一下單

//

// ******************************************

wxpay = new WXPay(iWxPayConfig); // *** 注入自己實現的微信配置類, 創建WXPay核心類, WXPay 包括統一下單介面

Map data = new HashMap ();

data.put("body", "訂單詳情");

data.put("out_trade_no", transOrder.getGlobalOrderId()); // 訂單唯一編號, 不允許重復

data.put("total_fee", String.valueOf(transOrder.getOrderAmount().multiply(new BigDecimal(100)).intValue())); // 訂單金額, 單位分

data.put("spbill_create_ip", "192.168.31.166"); // 下單ip

data.put("openid", openId); // 微信公眾號統一標示openid

data.put("notify_url", "http://wxlj.oopmind.com/payCallback"); // 訂單結果通知, 微信主動回調此介面

data.put("trade_type", "JSAPI"); // 固定填寫

logger.info("發起微信支付下單介面, request={}", data);

Map response = wxpay.unifiedOrder(data); // 微信sdk集成方法, 統一下單介面unifiedOrder, 此處請求 MD5加密 加密方式

logger.info("微信支付下單成功, 返回值 response={}", response);

String returnCode = response.get("return_code");

if (!SUCCESS.equals(returnCode)) {

return null;

}

String resultCode = response.get("result_code");

if (!SUCCESS.equals(resultCode)) {

return null;

}

String prepay_id = response.get("prepay_id");

if (prepay_id == null) {

return null;

}

// ******************************************

//

// 前端調起微信支付必要參數

//

// ******************************************

String packages = "prepay_id=" + prepay_id;

Map wxPayMap = new HashMap ();

wxPayMap.put("appId", iWxPayConfig.getAppID());

wxPayMap.put("timeStamp", String.valueOf(Utility.getCurrentTimeStamp()));

wxPayMap.put("nonceStr", Utility.generateUUID());

wxPayMap.put("package", packages);

wxPayMap.put("signType", "MD5");

// 加密串中包括 appId timeStamp nonceStr package signType 5個參數, 通過sdk WXPayUtil類加密, 注意, 此處使用 MD5加密 方式

String sign = WXPayUtil.generateSignature(wxPayMap, iWxPayConfig.getKey());

// ******************************************

//

// 返回給前端調起微信支付的必要參數

//

// ******************************************

result.put("prepay_id", prepay_id);

result.put("sign", sign);

result.putAll(wxPayMap);

return result;

} catch (Exception e) {

}

回調結果處理

核心是支付訂單回調時, 需校驗加密簽名是否匹配, 防止出現模擬成功通知

@RequestMapping(value = "/payCallback", method = RequestMethod.POST)

public String payCallback(HttpServletRequest request, HttpServletResponse response) {

logger.info("進入微信支付非同步通知");

String resXml="";

try{

//

InputStream is = request.getInputStream();

//將InputStream轉換成String

BufferedReader reader = new BufferedReader(new InputStreamReader(is));

StringBuilder sb = new StringBuilder();

String line = null;

try {

while ((line = reader.readLine()) != null) {

sb.append(line + " ");

}

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

is.close();

} catch (IOException e) {

e.printStackTrace();

}

}

resXml=sb.toString();

logger.info("微信支付非同步通知請求包: {}", resXml);

return wxTicketService.payBack(resXml);

}catch (Exception e){

logger.error("微信支付回調通知失敗",e);

String result = " ";

return result;

}

}

@Override

public String payBack(String notifyData) {

logger.info("payBack() start, notifyData={}", notifyData);

String xmlBack="";

Map notifyMap = null;

try {

WXPay wxpay = new WXPay(iWxPayConfig);

notifyMap = WXPayUtil.xmlToMap(notifyData); // 轉換成map

if (wxpay.(notifyMap)) {

// 簽名正確

// 進行處理。

// 注意特殊情況:訂單已經退款,但收到了支付結果成功的通知,不應把商戶側訂單狀態從退款改成支付成功

String return_code = notifyMap.get("return_code");//狀態

String out_trade_no = notifyMap.get("out_trade_no");//訂單號

if (out_trade_no == null) {

logger.info("微信支付回調失敗訂單號: {}", notifyMap);

xmlBack = " ";

return xmlBack;

}

// 業務邏輯處理 ****************************

logger.info("微信支付回調成功訂單號: {}", notifyMap);

xmlBack = " ";

return xmlBack;

} else {

logger.error("微信支付回調通知簽名錯誤");

xmlBack = " ";

return xmlBack;

}

} catch (Exception e) {

logger.error("微信支付回調通知失敗",e);

xmlBack = " ";

}

return xmlBack;

}

統一下單的簽名和後續前端拉取微信支付的簽名需要統一, 也就是都採用MD5加密, 如果2者不同, 會導致前端拉取微信支付fail, 這是一個巨大的坑, 因為這個原因調試了好久, 微信在文檔里沒有明確標出統一下單的簽名校驗方式 需要和前端拉取微信支付的簽名校驗保持一致.

微信sdk里的源碼需要針對這個問題調整一下, 調整如下:

WXPay類需要修改下加密判斷,在WXPay構造方法中,調整如下

public WXPay(final WXPayConfig config, final String notifyUrl, final boolean autoReport, final boolean useSandbox) throws Exception {

this.config = config;

this.notifyUrl = notifyUrl;

this.autoReport = autoReport;

this.useSandbox = useSandbox;

if (useSandbox) {

this.signType = SignType.MD5; // 沙箱環境

}

else {

this.signType = SignType.MD5;// 將這里的加密方式修改為SignType.MD5, 保持跟前端吊起微信加密方式保持一致

}

this.wxPayRequest = new WXPayRequest(config);

}

結束語

做完以後, 微信支付的後端邏輯還是很清晰的, 但是在開發過程中很煎熬, 不清楚每個專業術語在微信哪裡配置, 加密方式亂的很

閱讀全文

與微信支付demojava相關的資料

熱點內容
dvd光碟存儲漢子演算法 瀏覽:757
蘋果郵件無法連接伺服器地址 瀏覽:962
phpffmpeg轉碼 瀏覽:671
長沙好玩的解壓項目 瀏覽:144
專屬學情分析報告是什麼app 瀏覽:564
php工程部署 瀏覽:833
android全屏透明 瀏覽:736
阿里雲伺服器已開通怎麼辦 瀏覽:803
光遇為什麼登錄時伺服器已滿 瀏覽:302
PDF分析 瀏覽:484
h3c光纖全工半全工設置命令 瀏覽:143
公司法pdf下載 瀏覽:381
linuxmarkdown 瀏覽:350
華為手機怎麼多選文件夾 瀏覽:683
如何取消命令方塊指令 瀏覽:349
風翼app為什麼進不去了 瀏覽:778
im4java壓縮圖片 瀏覽:362
數據查詢網站源碼 瀏覽:150
伊克塞爾文檔怎麼進行加密 瀏覽:892
app轉賬是什麼 瀏覽:163