wx微信支付java開發

微信支付java開發

配置:

微信開發坑比較多 所以一定要按照開發文檔的要求來進行開發

<開發從第三步開始>

  1. 公衆號平臺配置

    • 功能設置 –> 配置其中的 ①業務域名 ② js接口安全域名 ③ 網頁授權域名

       

    • 基本配置 –> IP白名單配置

       

  2. 商戶平臺配置

    • 賬戶中心–> 下載安全證書 上傳至服務器 (http://域名/證書 能夠訪問)

       

    • API 安全–> API祕鑰設置 (自定義)

       

    • 開發配置–>支付配置–>公衆號配置–>配置支付授權目錄

       

  3. 獲取code

  1. - 訪問路徑:https://open.weixin.qq.com/connect/oauth2/authorize?

  2. - 參數:appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect

  3.  
  4. 參考鏈接(請在微信客戶端中打開此鏈接體驗):

  5. scope爲snsapi_base

  6. https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx520c15f417810387&redirect_uri=https%3A%2F%2Fchong.qq.com%2Fphp%2Findex.php%3Fd%3D%26c%3DwxAdapter%26m%3DmobileDeal%26showwxpaytitle%3D1%26vb2ctag%3D4_2030_5_1194_60&response_type=code&scope=snsapi_base&state=123#wechat_redirect

  7. scope爲snsapi_userinfo

  8. https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf0e81c3bee622d60&redirect_uri=http%3A%2F%2Fnba.bluewebgame.com%2Foauth_response.php&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect

參數說明

微信支付java開發

配置:

微信開發坑比較多 所以一定要按照開發文檔的要求來進行開發

<開發從第三步開始>

  1. 公衆號平臺配置

    • 功能設置 –> 配置其中的 ①業務域名 ② js接口安全域名 ③ 網頁授權域名

       

    • 基本配置 –> IP白名單配置

       

  2. 商戶平臺配置

    • 賬戶中心–> 下載安全證書 上傳至服務器 (http://域名/證書 能夠訪問)

       

    • API 安全–> API祕鑰設置 (自定義)

       

    • 開發配置–>支付配置–>公衆號配置–>配置支付授權目錄

       

  3. 獲取code

  1. - 訪問路徑:https://open.weixin.qq.com/connect/oauth2/authorize?

  2. - 參數:appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect

  3.  
  4. 參考鏈接(請在微信客戶端中打開此鏈接體驗):

  5. scope爲snsapi_base

  6. https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx520c15f417810387&redirect_uri=https%3A%2F%2Fchong.qq.com%2Fphp%2Findex.php%3Fd%3D%26c%3DwxAdapter%26m%3DmobileDeal%26showwxpaytitle%3D1%26vb2ctag%3D4_2030_5_1194_60&response_type=code&scope=snsapi_base&state=123#wechat_redirect

  7. scope爲snsapi_userinfo

  8. https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxf0e81c3bee622d60&redirect_uri=http%3A%2F%2Fnba.bluewebgame.com%2Foauth_response.php&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect

參數說明

參數 是否必須 說明
appid 公衆號的唯一標識
redirect_uri 授權後重定向的回調鏈接地址, 請使用 urlEncode 對鏈接進行處理
response_type 返回類型,請填寫code
scope 應用授權作用域,snsapi_base (不彈出授權頁面,直接跳轉,只能獲取用戶openid),snsapi_userinfo (彈出授權頁面,可通過openid拿到暱稱、性別、所在地。並且, 即使在未關注的情況下,只要用戶授權,也能獲取其信息 ) 拉取用戶信息(需scope爲 snsapi_userinfo)
state 重定向後會帶上state參數,開發者可以填寫a-zA-Z0-9的參數值,最多128字節
/#wechat_redirect 無論直接打開還是做頁面302重定向時候,必須帶此參數

4. 通過code獲取openid/access_token

  1. `如果用戶同意授權,頁面將跳轉至 redirect_uri/?code=CODE&state=STATE `

redirect_uri:第三步配置的url路徑.—–第三步請求成功之後會自動跳轉到此路徑並攜帶code值

  1. 獲取code後,請求以下鏈接獲取access_token: https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code

參數說明

參數 是否必須 說明
appid 公衆號的唯一標識
secret 公衆號的appsecret
code 填寫第一步獲取的code參數
grant_type 填寫爲authorization_code

返回說明

正確時返回的JSON數據包如下:

  1. { "access_token":"ACCESS_TOKEN",

  2. "expires_in":7200,

  3. "refresh_token":"REFRESH_TOKEN",

  4. "openid":"OPENID",

  5. "scope":"SCOPE" }

.

5. 通過openid獲取支付所需參數prepay_id

  1. 通過訪問統一下單接口連接:https://api.mch.weixin.qq.com/pay/unifiedorder

  2. 在微信支付開發文檔中下載SDK與demo

請求參數

字段名 變量名 必填 類型 示例值 描述
公衆賬號ID appid String(32) wxd678efh567hg6787 微信支付分配的公衆賬號ID(企業號corpid即爲此appId)
商戶號 mch_id String(32) 1230000109 微信支付分配的商戶號
隨機字符串 nonce_str String(32) 5K8264ILTKCH16CQ2502SI8ZNMTM67VS 隨機字符串,長度要求在32位以內。推薦隨機數生成算法
簽名 sign String(32) C380BEC2BFD727A4B6845133519F3AD6 通過簽名算法計算得出的簽名值,詳見簽名生成算法
商品描述 body String(128) 騰訊充值中心-QQ會員充值 商品簡單描述,該字段請按照規範傳遞,具體請見參數規定
商戶訂單號 out_trade_no String(32) 20150806125346 商戶系統內部訂單號,要求32個字符內,只能是數字、大小寫字母_-* 且在同一個商戶號下唯一。詳見商戶訂單號
標價金額 total_fee Int 88 訂單總金額,單位爲分,詳見支付金額
終端IP spbill_create_ip String(16) 123.12.12.123 支持IPV4和IPV6兩種格式的IP地址。調用微信支付API的機器IP
通知地址 notify_url String(256) http://www.weixin.qq.com/wxpay/pay.php 異步接收微信支付結果通知的回調地址,通知url必須爲外網可訪問的url,不能攜帶參數。
交易類型 trade_type String(16) JSAPI JSAPI -JSAPI支付 NATIVE -Native支付 APP -APP支付 說明詳見參數規定

.

6. 通過html(含微信h5支付js)請求後臺攜帶openid請求統一下單接口,並由h5支付js獲取支付參數完成支付

在html中嵌套

  1. $.ajax({

  2. type: "POST",

  3. url: "/wechat",

  4. data: {

  5. "openid": "on98b1sRZcShcmKrZVYeE3VQGXTc",

  6. "money": "1",

  7. "ordernum": odernum

  8. },

  9. success: function (date) {

  10. if (date.code == 200) {

  11.  
  12. // 微信支付必須要重新reload

  13. location.reload()

  14. //執行微信h5回調函數

  15. var obj = {

  16. "appId": xxx, //公衆號名稱,由商戶傳入

  17. "timeStamp": xxx, //時間戳,自1970年以來的秒數

  18. "nonceStr":xxx, //隨機串

  19. "package": "prepay_id=" +xxx ,//支付唯一憑證

  20. "signType": "MD5", //微信簽名方式:

  21. "paySign":xxx, //重新生成簽名

  22. }

  23.  
  24. if (typeof WeixinJSBridge == "undefined") {

  25. if (document.addEventListener) {

  26. document.addEventListener('WeixinJSBridgeReady', onBridgeReady(obj), false);

  27. } else if (document.attachEvent) {

  28. document.attachEvent('WeixinJSBridgeReady', onBridgeReady(obj));

  29. document.attachEvent('onWeixinJSBridgeReady', onBridgeReady(obj));

  30. }

  31. } else {

  32. onBridgeReady(obj);

  33. }

  34.  
  35. }

  36. }

  37. });

  38. })

  39.  
  40. function onBridgeReady(){

  41. WeixinJSBridge.invoke(

  42. 'getBrandWCPayRequest', obj,

  43. function(res){

  44. if(res.err_msg == "get_brand_wcpay_request:ok" ){

  45. // 使用以上方式判斷前端返回,微信團隊鄭重提示:

  46. //res.err_msg將在用戶支付成功後返回ok,但並不保證它絕對可靠。

  47. }

  48. });

  49. }

.

7. 訂單詳情查詢,訪問微信訂單查詢接口

  1. https://api.mch.weixin.qq.com/pay/orderquery

  2.  
  3. <param>:

  4. map.put("appid", "wxf874e15f78cc84a7");//APPID

  5. map.put("mch_id", "1487005412");//商戶號

  6. map.put("out_trade_no", "20190109162035");//訂單編號

  7. map.put("nonce_str", "5K8264ILTKCH16CQ2502SI8ZNMTM67VS");//隨時數

.

8. 調用微信refund退款接口

  1. 接口鏈接:https://api.mch.weixin.qq.com/secapi/pay/refund

  2.  
  3. 需要證書 .p12

  4.  
  5. 請求參數:

  6. refundMessage.put("appid", wxPayConfig.getAppID());

  7. refundMessage.put("mch_id", wxPayConfig.getMchID());

  8. refundMessage.put("nonce_str", WXPayUtil.generateNonceStr());

  9. refundMessage.put("out_trade_no", out_trade_no);//商戶訂單號

  10. refundMessage.put("out_refund_no", "SDTK" +String.valueOf(System.currentTimeMillis()));//商戶退款單號

  11. refundMessage.put("total_fee", total_fee);//訂單金額

  12. refundMessage.put("refund_fee", total_fee);//退款金額

  13. String signature = WXPayUtil.generateSignature(refundMessage, wxPayConfig.getKey());

  14. refundMessage.put("sign", signature);

準備工作:

  1. 域名/證書(1.apiclient_cert.p12:支付必須/2.MP_verify_5dkzuGfuanhFOeA8.txt)

   
     
     
     
     
     
     

4. 通過code獲取openid/access_token

  1. `如果用戶同意授權,頁面將跳轉至 redirect_uri/?code=CODE&state=STATE `

redirect_uri:第三步配置的url路徑.—–第三步請求成功之後會自動跳轉到此路徑並攜帶code值

  1. 獲取code後,請求以下鏈接獲取access_token: https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code

參數說明

參數 是否必須 說明
appid 公衆號的唯一標識
secret 公衆號的appsecret
code 填寫第一步獲取的code參數
grant_type 填寫爲authorization_code

返回說明

正確時返回的JSON數據包如下:

  1. { "access_token":"ACCESS_TOKEN",

  2. "expires_in":7200,

  3. "refresh_token":"REFRESH_TOKEN",

  4. "openid":"OPENID",

  5. "scope":"SCOPE" }

.

5. 通過openid獲取支付所需參數prepay_id

  1. 通過訪問統一下單接口連接:https://api.mch.weixin.qq.com/pay/unifiedorder

  2. 在微信支付開發文檔中下載SDK與demo

請求參數

字段名 變量名 必填 類型 示例值 描述
公衆賬號ID appid String(32) wxd678efh567hg6787 微信支付分配的公衆賬號ID(企業號corpid即爲此appId)
商戶號 mch_id String(32) 1230000109 微信支付分配的商戶號
隨機字符串 nonce_str String(32) 5K8264ILTKCH16CQ2502SI8ZNMTM67VS 隨機字符串,長度要求在32位以內。推薦隨機數生成算法
簽名 sign String(32) C380BEC2BFD727A4B6845133519F3AD6 通過簽名算法計算得出的簽名值,詳見簽名生成算法
商品描述 body String(128) 騰訊充值中心-QQ會員充值 商品簡單描述,該字段請按照規範傳遞,具體請見參數規定
商戶訂單號 out_trade_no String(32) 20150806125346 商戶系統內部訂單號,要求32個字符內,只能是數字、大小寫字母_-* 且在同一個商戶號下唯一。詳見商戶訂單號
標價金額 total_fee Int 88 訂單總金額,單位爲分,詳見支付金額
終端IP spbill_create_ip String(16) 123.12.12.123 支持IPV4和IPV6兩種格式的IP地址。調用微信支付API的機器IP
通知地址 notify_url String(256) http://www.weixin.qq.com/wxpay/pay.php 異步接收微信支付結果通知的回調地址,通知url必須爲外網可訪問的url,不能攜帶參數。
交易類型 trade_type String(16) JSAPI JSAPI -JSAPI支付 NATIVE -Native支付 APP -APP支付 說明詳見參數規定

.

6. 通過html(含微信h5支付js)請求後臺攜帶openid請求統一下單接口,並由h5支付js獲取支付參數完成支付

在html中嵌套

  1. $.ajax({

  2. type: "POST",

  3. url: "/wechat",

  4. data: {

  5. "openid": "on98b1sRZcShcmKrZVYeE3VQGXTc",

  6. "money": "1",

  7. "ordernum": odernum

  8. },

  9. success: function (date) {

  10. if (date.code == 200) {

  11.  
  12. // 微信支付必須要重新reload

  13. location.reload()

  14. //執行微信h5回調函數

  15. var obj = {

  16. "appId": xxx, //公衆號名稱,由商戶傳入

  17. "timeStamp": xxx, //時間戳,自1970年以來的秒數

  18. "nonceStr":xxx, //隨機串

  19. "package": "prepay_id=" +xxx ,//支付唯一憑證

  20. "signType": "MD5", //微信簽名方式:

  21. "paySign":xxx, //重新生成簽名

  22. }

  23.  
  24. if (typeof WeixinJSBridge == "undefined") {

  25. if (document.addEventListener) {

  26. document.addEventListener('WeixinJSBridgeReady', onBridgeReady(obj), false);

  27. } else if (document.attachEvent) {

  28. document.attachEvent('WeixinJSBridgeReady', onBridgeReady(obj));

  29. document.attachEvent('onWeixinJSBridgeReady', onBridgeReady(obj));

  30. }

  31. } else {

  32. onBridgeReady(obj);

  33. }

  34.  
  35. }

  36. }

  37. });

  38. })

  39.  
  40. function onBridgeReady(){

  41. WeixinJSBridge.invoke(

  42. 'getBrandWCPayRequest', obj,

  43. function(res){

  44. if(res.err_msg == "get_brand_wcpay_request:ok" ){

  45. // 使用以上方式判斷前端返回,微信團隊鄭重提示:

  46. //res.err_msg將在用戶支付成功後返回ok,但並不保證它絕對可靠。

  47. }

  48. });

  49. }

.

7. 訂單詳情查詢,訪問微信訂單查詢接口

  1. https://api.mch.weixin.qq.com/pay/orderquery

  2.  
  3. <param>:

  4. map.put("appid", "wxf874e15f78cc84a7");//APPID

  5. map.put("mch_id", "1487005412");//商戶號

  6. map.put("out_trade_no", "20190109162035");//訂單編號

  7. map.put("nonce_str", "5K8264ILTKCH16CQ2502SI8ZNMTM67VS");//隨時數

.

8. 調用微信refund退款接口

  1. 接口鏈接:https://api.mch.weixin.qq.com/secapi/pay/refund

  2.  
  3. 需要證書 .p12

  4.  
  5. 請求參數:

  6. refundMessage.put("appid", wxPayConfig.getAppID());

  7. refundMessage.put("mch_id", wxPayConfig.getMchID());

  8. refundMessage.put("nonce_str", WXPayUtil.generateNonceStr());

  9. refundMessage.put("out_trade_no", out_trade_no);//商戶訂單號

  10. refundMessage.put("out_refund_no", "SDTK" +String.valueOf(System.currentTimeMillis()));//商戶退款單號

  11. refundMessage.put("total_fee", total_fee);//訂單金額

  12. refundMessage.put("refund_fee", total_fee);//退款金額

  13. String signature = WXPayUtil.generateSignature(refundMessage, wxPayConfig.getKey());

  14. refundMessage.put("sign", signature);

準備工作:

域名/證書(1.apiclient_cert.p12:支付必須/2.MP_verify_5dkzuGfuanhFOeA8.txt)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章