{"id":29460,"date":"2024-11-25T11:29:19","date_gmt":"2024-11-25T03:29:19","guid":{"rendered":"https:\/\/fwq.ai\/blog\/29460\/"},"modified":"2024-11-25T11:29:19","modified_gmt":"2024-11-25T03:29:19","slug":"%e8%af%a6%e8%a7%a3java%e9%80%9a%e8%bf%87jsapi%e6%96%b9%e5%bc%8f%e5%ae%9e%e7%8e%b0%e5%be%ae%e4%bf%a1%e6%94%af%e4%bb%98%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/29460\/","title":{"rendered":"\u8be6\u89e3Java\u901a\u8fc7JsApi\u65b9\u5f0f\u5b9e\u73b0\u5fae\u4fe1\u652f\u4ed8\u65b9\u6cd5"},"content":{"rendered":"<p style=\"text-align: left\">\u672c\u6587\u8bb2\u89e3\u4e86Java\u5982\u4f55\u5b9e\u73b0JsApi\u65b9\u5f0f\u7684\u5fae\u4fe1\u652f\u4ed8\uff0c\u4ee3\u7801\u5185\u5bb9\u8be6\u7ec6\uff0c\u6587\u7ae0\u601d\u8def\u6e05\u6670,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b<\/p>\n<p style=\"text-align: left\">\u8981\u4f7f\u7528JsApi\u8fdb\u884c\u5fae\u4fe1\u652f\u4ed8\uff0c\u9996\u5148\u8981\u4ece\u5fae\u4fe1\u83b7\u5f97\u4e00\u4e2aprepay_id\uff0c\u7136\u540e\u901a\u8fc7\u8c03\u7528\u5fae\u4fe1\u7684jsapi\u5b8c\u6210\u652f\u4ed8\uff0cJS API\u7684\u8fd4\u56de\u7ed3\u679cget_brand_wcpay_request:ok\u4ec5\u5728\u7528\u6237\u6210\u529f\u5b8c\u6210\u652f\u4ed8\u65f6\u8fd4\u56de\u3002\u7531\u4e8e\u524d\u7aef\u4ea4\u4e92\u590d\u6742\uff0cget_brand_wcpay_request:cancel\u6216\u8005get_brand_wcpay_request:fail\u53ef\u4ee5\u7edf\u4e00\u5904\u7406\u4e3a\u7528\u6237\u9047\u5230\u9519\u8bef\u6216\u8005\u4e3b\u52a8\u653e\u5f03\uff0c\u4e0d\u5fc5\u7ec6\u5316\u533a\u5206\u3002<br \/>\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>function onBridgeReady(){\r\n WeixinJSBridge.invoke(\r\n 'getBrandWCPayRequest', {\r\n  \"appId\" : \"wx2421b1c4370ec43b\", \/\/\u516c\u4f17\u53f7\u540d\u79f0\uff0c\u7531\u5546\u6237\u4f20\u5165 \r\n  \"timeStamp\":\" 1395712654\",  \/\/\u65f6\u95f4\u6233\uff0c\u81ea1970\u5e74\u4ee5\u6765\u7684\u79d2\u6570 \r\n  \"nonceStr\" : \"e61463f8efa94090b1f366cccfbbb444\", \/\/\u968f\u673a\u4e32 \r\n  \"package\" : \"u802345jgfjsdfgsdg888\", \r\n  \"signType\" : \"MD5\",  \/\/\u5fae\u4fe1\u7b7e\u540d\u65b9\u5f0f: \r\n  \"paySign\" : \"70EA570631E4BB79628FBCA90534C63FF7FADD89\" \/\/\u5fae\u4fe1\u7b7e\u540d \r\n },\r\n function(res){ \r\n  if(res.err_msg == \"get_brand_wcpay_request:ok\" ) {} \/\/ \u4f7f\u7528\u4ee5\u4e0a\u65b9\u5f0f\u5224\u65ad\u524d\u7aef\u8fd4\u56de,\u5fae\u4fe1\u56e2\u961f\u90d1\u91cd\u63d0\u793a\uff1ares.err_msg\u5c06\u5728\u7528\u6237\u652f\u4ed8\u6210\u529f\u540e\u8fd4\u56de ok\uff0c\u4f46\u5e76\u4e0d\u4fdd\u8bc1\u5b83\u7edd\u5bf9\u53ef\u9760\u3002 \r\n }\r\n ); \r\n}\r\nif (typeof WeixinJSBridge == \"undefined\"){\r\n if( document.addEventListener ){\r\n document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);\r\n }else if (document.attachEvent){\r\n document.attachEvent('WeixinJSBridgeReady', onBridgeReady); \r\n document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);\r\n }\r\n}else{\r\n onBridgeReady();\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u4ee5\u4e0a\u4f20\u5165\u7684\u53c2\u6570package\uff0c\u5373\u4e3aprepay_id<\/p>\n<p style=\"text-align: left\">\u4e0b\u9762\u8bb2\u7684\u662f\u83b7\u5f97\u53c2\u6570\u6765\u8c03\u7528jsapi<br \/>\u6211\u4eec\u8c03\u7528JSAPI\u65f6\uff0c\u5fc5\u987b\u83b7\u5f97\u7528\u6237\u7684openid\uff0c\uff08trade_type=JSAPI\uff0copenid\u4e3a\u5fc5\u586b\u53c2\u6570\u3002\uff09<br \/>\u9996\u5148\u5b9a\u4e49\u4e00\u4e2a\u8bf7\u6c42\u7684\uff1a<\/p>\n<pre>package com.unstoppedable.protocol;\r\nimport com.unstoppedable.common.Configure;\r\nimport com.unstoppedable.common.HttpService;\r\nimport com.unstoppedable.common.RandomStringGenerator;\r\nimport com.unstoppedable.common.Signature;\r\nimport java.lang.reflect.Field;\r\nimport java.util.HashMap;\r\nimport java.util.Map;\r\npublic class UnifiedOrderReqData {\r\n private String appid;\r\n private String mch_id;\r\n private String device_info;\r\n private String nonce_str;\r\n private String sign;\r\n private String body;\r\n private String detail;\r\n private String attach;\r\n private String out_trade_no;\r\n private String fee_type;\r\n private int total_fee;\r\n private String spbill_create_ip;\r\n private String time_start;\r\n private String time_expire;\r\n private String goods_tag;\r\n private String notify_url;\r\n private String trade_type;\r\n private String product_id;\r\n private String limit_pay;\r\n private String openid;\r\n private UnifiedOrderReqData(UnifiedOrderReqDataBuilder builder) {\r\n this.appid = builder.appid;\r\n this.mch_id = builder.mch_id;\r\n this.device_info = builder.device_info;\r\n this.nonce_str = RandomStringGenerator.getRandomStringByLength(32);\r\n this.body = builder.body;\r\n this.detail = builder.detail;\r\n this.attach = builder.attach;\r\n this.out_trade_no = builder.out_trade_no;\r\n this.fee_type = builder.fee_type;\r\n this.total_fee = builder.total_fee;\r\n this.spbill_create_ip = builder.spbill_create_ip;\r\n this.time_start = builder.time_start;\r\n this.time_expire = builder.time_expire;\r\n this.goods_tag = builder.goods_tag;\r\n this.notify_url = builder.notify_url;\r\n this.trade_type = builder.trade_type;\r\n this.product_id = builder.product_id;\r\n this.limit_pay = builder.limit_pay;\r\n this.openid = builder.openid;\r\n this.sign = Signature.getSign(toMap());\r\n }\r\n public void setAppid(String appid) {\r\n this.appid = appid;\r\n }\r\n public void setMch_id(String mch_id) {\r\n this.mch_id = mch_id;\r\n }\r\n public void setDevice_info(String device_info) {\r\n this.device_info = device_info;\r\n }\r\n public void setNonce_str(String nonce_str) {\r\n this.nonce_str = nonce_str;\r\n }\r\n public void setSign(String sign) {\r\n this.sign = sign;\r\n }\r\n public void setBody(String body) {\r\n this.body = body;\r\n }\r\n public void setDetail(String detail) {\r\n this.detail = detail;\r\n }\r\n public void setAttach(String attach) {\r\n this.attach = attach;\r\n }\r\n public void setOut_trade_no(String out_trade_no) {\r\n this.out_trade_no = out_trade_no;\r\n }\r\n public void setFee_type(String fee_type) {\r\n this.fee_type = fee_type;\r\n }\r\n public void setTotal_fee(int total_fee) {\r\n this.total_fee = total_fee;\r\n }\r\n public void setSpbill_create_ip(String spbill_create_ip) {\r\n this.spbill_create_ip = spbill_create_ip;\r\n }\r\n public void setTime_start(String time_start) {\r\n this.time_start = time_start;\r\n }\r\n public void setTime_expire(String time_expire) {\r\n this.time_expire = time_expire;\r\n }\r\n public void setGoods_tag(String goods_tag) {\r\n this.goods_tag = goods_tag;\r\n }\r\n public void setNotify_url(String notify_url) {\r\n this.notify_url = notify_url;\r\n }\r\n public void setTrade_type(String trade_type) {\r\n this.trade_type = trade_type;\r\n }\r\n public void setProduct_id(String product_id) {\r\n this.product_id = product_id;\r\n }\r\n public void setLimit_pay(String limit_pay) {\r\n this.limit_pay = limit_pay;\r\n }\r\n public void setOpenid(String openid) {\r\n this.openid = openid;\r\n }\r\n public Map&lt;String, Object&gt; toMap() {\r\n Map&lt;String, Object&gt; map = new HashMap&lt;String, Object&gt;();\r\n Field[] fields = this.getClass().getDeclaredFields();\r\n for (Field field : fields) {\r\n  Object obj;\r\n  try {\r\n  obj = field.get(this);\r\n  if (obj != null) {\r\n   map.put(field.getName(), obj);\r\n  }\r\n  } catch (IllegalArgumentException e) {\r\n  e.printStackTrace();\r\n  } catch (IllegalAccessException e) {\r\n  e.printStackTrace();\r\n  }\r\n }\r\n return map;\r\n }\r\n public static class UnifiedOrderReqDataBuilder {\r\n private String appid;\r\n private String mch_id;\r\n private String device_info;\r\n private String body;\r\n private String detail;\r\n private String attach;\r\n private String out_trade_no;\r\n private String fee_type;\r\n private int total_fee;\r\n private String spbill_create_ip;\r\n private String time_start;\r\n private String time_expire;\r\n private String goods_tag;\r\n private String notify_url;\r\n private String trade_type;\r\n private String product_id;\r\n private String limit_pay;\r\n private String openid;\r\n public UnifiedOrderReqDataBuilder(String appid, String mch_id, String body, String out_trade_no, Integer total_fee,\r\n      String spbill_create_ip, String notify_url, String trade_type) {\r\n  if (appid == null) {\r\n  throw new IllegalArgumentException(\"\u4f20\u5165\u53c2\u6570appid\u4e0d\u80fd\u4e3anull\");\r\n  }\r\n  if (mch_id == null) {\r\n  throw new IllegalArgumentException(\"\u4f20\u5165\u53c2\u6570mch_id\u4e0d\u80fd\u4e3anull\");\r\n  }\r\n  if (body == null) {\r\n  throw new IllegalArgumentException(\"\u4f20\u5165\u53c2\u6570body\u4e0d\u80fd\u4e3anull\");\r\n  }\r\n  if (out_trade_no == null) {\r\n  throw new IllegalArgumentException(\"\u4f20\u5165\u53c2\u6570out_trade_no\u4e0d\u80fd\u4e3anull\");\r\n  }\r\n  if (total_fee == null) {\r\n  throw new IllegalArgumentException(\"\u4f20\u5165\u53c2\u6570total_fee\u4e0d\u80fd\u4e3anull\");\r\n  }\r\n  if (spbill_create_ip == null) {\r\n  throw new IllegalArgumentException(\"\u4f20\u5165\u53c2\u6570spbill_create_ip\u4e0d\u80fd\u4e3anull\");\r\n  }\r\n  if (notify_url == null) {\r\n  throw new IllegalArgumentException(\"\u4f20\u5165\u53c2\u6570notify_url\u4e0d\u80fd\u4e3anull\");\r\n  }\r\n  if (trade_type == null) {\r\n  throw new IllegalArgumentException(\"\u4f20\u5165\u53c2\u6570trade_type\u4e0d\u80fd\u4e3anull\");\r\n  }\r\n  this.appid = appid;\r\n  this.mch_id = mch_id;\r\n  this.body = body;\r\n  this.out_trade_no = out_trade_no;\r\n  this.total_fee = total_fee;\r\n  this.spbill_create_ip = spbill_create_ip;\r\n  this.notify_url = notify_url;\r\n  this.trade_type = trade_type;\r\n }\r\n public UnifiedOrderReqDataBuilder setDevice_info(String device_info) {\r\n  this.device_info = device_info;\r\n  return this;\r\n }\r\n public UnifiedOrderReqDataBuilder setDetail(String detail) {\r\n  this.detail = detail;\r\n  return this;\r\n }\r\n public UnifiedOrderReqDataBuilder setAttach(String attach) {\r\n  this.attach = attach;\r\n  return this;\r\n }\r\n public UnifiedOrderReqDataBuilder setFee_type(String fee_type) {\r\n  this.fee_type = fee_type;\r\n  return this;\r\n }\r\n public UnifiedOrderReqDataBuilder setTime_start(String time_start) {\r\n  this.time_start = time_start;\r\n  return this;\r\n }\r\n public UnifiedOrderReqDataBuilder setTime_expire(String time_expire) {\r\n  this.time_expire = time_expire;\r\n  return this;\r\n }\r\n public UnifiedOrderReqDataBuilder setGoods_tag(String goods_tag) {\r\n  this.goods_tag = goods_tag;\r\n  return this;\r\n }\r\n public UnifiedOrderReqDataBuilder setProduct_id(String product_id) {\r\n  this.product_id = product_id;\r\n  return this;\r\n }\r\n public UnifiedOrderReqDataBuilder setLimit_pay(String limit_pay) {\r\n  this.limit_pay = limit_pay;\r\n  return this;\r\n }\r\n public UnifiedOrderReqDataBuilder setOpenid(String openid) {\r\n  this.openid = openid;\r\n  return this;\r\n }\r\n public UnifiedOrderReqData build() {\r\n  if(\"JSAPI\".equals(this.trade_type) &amp;&amp; this.openid == null) {\r\n  throw new IllegalArgumentException(\"\u5f53\u4f20\u5165trade_type\u4e3aJSAPI\u65f6\uff0copenid\u4e3a\u5fc5\u586b\u53c2\u6570\");\r\n  }\r\n  if(\"NATIVE\".equals(this.trade_type) &amp;&amp; this.product_id == null) {\r\n  throw new IllegalArgumentException(\"\u5f53\u4f20\u5165trade_type\u4e3aNATIVE\u65f6\uff0cproduct_id\u4e3a\u5fc5\u586b\u53c2\u6570\");\r\n  }\r\n  return new UnifiedOrderReqData(this);\r\n }\r\n }\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u56e0\u4e3a\u6709\u4e9b\u53c2\u6570\u4e3a\u5fc5\u586b\uff0c\u6709\u4e9b\u53c2\u6570\u4e3a\u9009\u586b\u3002\u800c\u4e14sign\u8981\u7b49\u6240\u6709\u53c2\u6570\u4f20\u5165\u4e4b\u540e\u624d\u80fd\u8ba1\u7b97\u7684\u51fa\uff0c\u6240\u4ee5\u8fd9\u91cc\u7528\u4e86builder\u6a21\u5f0f\u3002\u5173\u4e8ebuilder\u6a21\u5f0f\u3002<\/p>\n<p style=\"text-align: left\">\u6211\u4eec\u9009\u7528httpclient\u8fdb\u884c\u7f51\u7edc\u4f20\u8f93\u3002<\/p>\n<pre>package com.unstoppedable.common;\r\nimport com.thoughtworks.xstream.XStream;\r\nimport com.thoughtworks.xstream.io.xml.DomDriver;\r\nimport com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder;\r\nimport org.apache.commons.logging.Log;\r\nimport org.apache.commons.logging.LogFactory;\r\nimport org.apache.http.HttpEntity;\r\nimport org.apache.http.HttpResponse;\r\nimport org.apache.http.client.ClientProtocolException;\r\nimport org.apache.http.client.ResponseHandler;\r\nimport org.apache.http.client.config.RequestConfig;\r\nimport org.apache.http.client.methods.HttpGet;\r\nimport org.apache.http.client.methods.HttpPost;\r\nimport org.apache.http.conn.ConnectTimeoutException;\r\nimport org.apache.http.conn.ConnectionPoolTimeoutException;\r\nimport org.apache.http.conn.ssl.SSLConnectionSocketFactory;\r\nimport org.apache.http.conn.ssl.SSLContexts;\r\nimport org.apache.http.entity.StringEntity;\r\nimport org.apache.http.impl.client.CloseableHttpClient;\r\nimport org.apache.http.impl.client.HttpClients;\r\nimport org.apache.http.util.EntityUtils;\r\nimport javax.net.ssl.SSLContext;\r\nimport java.io.File;\r\nimport java.io.FileInputStream;\r\nimport java.io.IOException;\r\nimport java.net.SocketTimeoutException;\r\nimport java.security.KeyStore;\r\n\/**\r\n * Created by hupeng on 2015\/7\/28.\r\n *\/\r\npublic class HttpService {\r\n private static Log logger = LogFactory.getLog(HttpService.class);\r\n private static CloseableHttpClient httpClient = buildHttpClient();\r\n \/\/\u8fde\u63a5\u8d85\u65f6\u65f6\u95f4\uff0c\u9ed8\u8ba410\u79d2\r\n private static int socketTimeout = 5000;\r\n \/\/\u4f20\u8f93\u8d85\u65f6\u65f6\u95f4\uff0c\u9ed8\u8ba430\u79d2\r\n private static int connectTimeout = 5000;\r\n private static int requestTimeout = 5000;\r\n public static CloseableHttpClient buildHttpClient() {\r\n try {\r\n  KeyStore keyStore = KeyStore.getInstance(\"PKCS12\");\r\n  FileInputStream instream = new FileInputStream(new File(Configure.getCertLocalPath()));\/\/\u52a0\u8f7d\u672c\u5730\u7684\u8bc1\u4e66\u8fdb\u884chttps\u52a0\u5bc6\u4f20\u8f93\r\n  try {\r\n  keyStore.load(instream, Configure.getCertPassword().toCharArray());\/\/\u8bbe\u7f6e\u8bc1\u4e66\u5bc6\u7801\r\n  } finally {\r\n  instream.close();\r\n  }\r\n  \/\/ Trust own CA and all self-signed certs\r\n  SSLContext sslcontext = SSLContexts.custom()\r\n   .loadKeyMaterial(keyStore, Configure.getCertPassword().toCharArray())\r\n   .build();\r\n  \/\/ Allow TLSv1 protocol only\r\n  SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(\r\n   sslcontext,\r\n   new String[]{\"TLSv1\"},\r\n   null,\r\n   SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);\r\n  RequestConfig requestConfig = RequestConfig.custom()\r\n   .setConnectTimeout(connectTimeout)\r\n   .setConnectionRequestTimeout(requestTimeout)\r\n   .setSocketTimeout(socketTimeout).build();\r\n  httpClient = HttpClients.custom()\r\n   .setDefaultRequestConfig(requestConfig)\r\n   .setSSLSocketFactory(sslsf)\r\n   .build();\r\n  return httpClient;\r\n } catch (Exception e) {\r\n  throw new RuntimeException(\"error create httpclient......\", e);\r\n }\r\n }\r\n public static String doGet(String requestUrl) throws Exception {\r\n HttpGet httpget = new HttpGet(requestUrl);\r\n try {\r\n  logger.debug(\"Executing request \" + httpget.getRequestLine());\r\n  \/\/ Create a custom response handler\r\n  ResponseHandler&lt;String&gt; responseHandler = new ResponseHandler&lt;String&gt;() {\r\n  @Override\r\n  public String handleResponse(\r\n   final HttpResponse response) throws ClientProtocolException, IOException {\r\n   int status = response.getStatusLine().getStatusCode();\r\n   if (status &gt;= 200 &amp;&amp; status &lt; 300) {\r\n   HttpEntity entity = response.getEntity();\r\n   return entity != null ? EntityUtils.toString(entity) : null;\r\n   } else {\r\n   throw new ClientProtocolException(\"Unexpected response status: \" + status);\r\n   }\r\n  }\r\n  };\r\n  return httpClient.execute(httpget, responseHandler);\r\n } finally {\r\n  httpget.releaseConnection();\r\n }\r\n }\r\n public static String doPost(String url, Object object2Xml) {\r\n String result = null;\r\n HttpPost httpPost = new HttpPost(url);\r\n \/\/\u89e3\u51b3XStream\u5bf9\u51fa\u73b0\u53cc\u4e0b\u5212\u7ebf\u7684bug\r\n XStream xStreamForRequestPostData = new XStream(new DomDriver(\"UTF-8\", new XmlFriendlyNameCoder(\"-_\", \"_\")));\r\n \/\/\u5c06\u8981\u63d0\u4ea4\u7ed9API\u7684\u6570\u636e\u5bf9\u8c61\u8f6c\u6362\u6210XML\u683c\u5f0f\u6570\u636ePost\u7ed9API\r\n String postDataXML = xStreamForRequestPostData.toXML(object2Xml);\r\n logger.info(\"API\uff0cPOST\u8fc7\u53bb\u7684\u6570\u636e\u662f\uff1a\");\r\n logger.info(postDataXML);\r\n \/\/\u5f97\u6307\u660e\u4f7f\u7528UTF-8\u7f16\u7801\uff0c\u5426\u5219\u5230API\u670d\u52a1\u5668XML\u7684\u4e2d\u6587\u4e0d\u80fd\u88ab\u6210\u529f\u8bc6\u522b\r\n StringEntity postEntity = new StringEntity(postDataXML, \"UTF-8\");\r\n httpPost.addHeader(\"Content-Type\", \"text\/xml\");\r\n httpPost.setEntity(postEntity);\r\n \/\/\u8bbe\u7f6e\u8bf7\u6c42\u5668\u7684\u914d\u7f6e\r\n logger.info(\"executing request\" + httpPost.getRequestLine());\r\n try {\r\n  HttpResponse response = httpClient.execute(httpPost);\r\n  HttpEntity entity = response.getEntity();\r\n  result = EntityUtils.toString(entity, \"UTF-8\");\r\n } catch (ConnectionPoolTimeoutException e) {\r\n  logger.error(\"http get throw ConnectionPoolTimeoutException(wait time out)\", e);\r\n } catch (ConnectTimeoutException e) {\r\n  logger.error(\"http get throw ConnectTimeoutException\", e);\r\n } catch (SocketTimeoutException e) {\r\n  logger.error(\"http get throw SocketTimeoutException\", e);\r\n } catch (Exception e) {\r\n  logger.error(\"http get throw Exception\", e);\r\n } finally {\r\n  httpPost.abort();\r\n }\r\n return result;\r\n }\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u7136\u540e\u662f\u6211\u4eec\u7684\u603b\u5165\u53e3\uff1a<\/p>\n<pre>package com.unstoppedable.service;\r\nimport com.unstoppedable.common.Configure;\r\nimport com.unstoppedable.common.HttpService;\r\nimport com.unstoppedable.common.XMLParser;\r\nimport com.unstoppedable.protocol.UnifiedOrderReqData;\r\nimport org.xml.sax.SAXException;\r\nimport javax.xml.parsers.ParserConfigurationException;\r\nimport java.io.IOException;\r\nimport java.util.Map;\r\n\/**\r\n * Created by hupeng on 2015\/7\/28.\r\n *\/\r\npublic class WxPayApi {\r\n public static Map&lt;String,Object&gt; UnifiedOrder(UnifiedOrderReqData reqData) throws IOException, SAXException, ParserConfigurationException {\r\n String res = HttpService.doPost(Configure.UNIFIED_ORDER_API, reqData);\r\n return XMLParser.getMapFromXML(res);\r\n }\r\n public static void main(String[] args) throws Exception {\r\n UnifiedOrderReqData reqData = new UnifiedOrderReqData.UnifiedOrderReqDataBuilder(\"appid\", \"mch_id\", \"body\", \"out_trade_no\", 1, \"spbill_create_ip\", \"notify_url\", \"JSAPI\").setOpenid(\"openid\").build();\r\n System.out.println(UnifiedOrder(reqData));\r\n }\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u8fd4\u56de\u7684xml\u4e3a\uff1a<\/p>\n<pre>&lt;xml&gt;\r\n &lt;return_code&gt;&lt;![CDATA[SUCCESS]]&gt;&lt;\/return_code&gt;\r\n &lt;return_msg&gt;&lt;![CDATA[OK]]&gt;&lt;\/return_msg&gt;\r\n &lt;appid&gt;&lt;![CDATA[wx2421b1c4370ec43b]]&gt;&lt;\/appid&gt;\r\n &lt;mch_id&gt;&lt;![CDATA[10000100]]&gt;&lt;\/mch_id&gt;\r\n &lt;nonce_str&gt;&lt;![CDATA[IITRi8Iabbblz1Jc]]&gt;&lt;\/nonce_str&gt;\r\n &lt;sign&gt;&lt;![CDATA[7921E432F65EB8ED0CE9755F0E86D72F]]&gt;&lt;\/sign&gt;\r\n &lt;result_code&gt;&lt;![CDATA[SUCCESS]]&gt;&lt;\/result_code&gt;\r\n &lt;prepay_id&gt;&lt;![CDATA[wx201411101639507cbf6ffd8b0779950874]]&gt;&lt;\/prepay_id&gt;\r\n &lt;trade_type&gt;&lt;![CDATA[JSAPI]]&gt;&lt;\/trade_type&gt;\r\n&lt;\/xml&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">return_code \u548cresult_code\u90fd\u4e3aSUCCESS\u7684\u65f6\u5019\u4f1a\u8fd4\u56de\u6211\u4eec\u9700\u8981\u7684prepay_id\u3002\u3002\u3002\uff0c\u7136\u540e\u5728jsapi\u4e2d\u4f7f\u7528\u4ed6\u5c31\u53ef\u4ee5\u4e86\u3002\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u8be6\u89e3Java\u901a\u8fc7JsApi\u65b9\u5f0f\u5b9e\u73b0\u5fae\u4fe1\u652f\u4ed8\u65b9\u6cd5\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u8bb2\u89e3\u4e86Java\u5982\u4f55\u5b9e\u73b0JsApi\u65b9\u5f0f\u7684\u5fae\u4fe1\u652f\u4ed8\uff0c\u4ee3\u7801\u5185\u5bb9\u8be6\u7ec6\uff0c\u6587\u7ae0\u601d\u8def\u6e05\u6670,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b \u8981\u4f7f\u7528JsApi\u8fdb\u884c\u5fae\u4fe1\u652f\u4ed8\uff0c\u9996\u5148\u8981\u4ece\u5fae\u4fe1\u83b7\u5f97\u4e00\u4e2aprepay_id\uff0c\u7136\u540e\u901a\u8fc7\u8c03\u7528\u5fae\u4fe1\u7684jsapi\u5b8c\u6210\u652f\u4ed8\uff0cJS API\u7684\u8fd4\u56de\u7ed3\u679cget_brand_wcpay_request:ok\u4ec5\u5728\u7528\u6237\u6210\u529f\u5b8c\u6210\u652f\u4ed8\u65f6\u8fd4\u56de\u3002\u7531\u4e8e\u524d\u7aef\u4ea4\u4e92\u590d\u6742\uff0cget_brand_wcpay_request:cancel\u6216\u8005get_brand_wcpay_request:fail\u53ef\u4ee5\u7edf\u4e00\u5904\u7406\u4e3a\u7528\u6237\u9047\u5230\u9519\u8bef\u6216\u8005\u4e3b\u52a8\u653e\u5f03\uff0c\u4e0d\u5fc5\u7ec6\u5316\u533a\u5206\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a function onBridgeReady(){ WeixinJSBridge.invoke( &#8216;getBrandWCPayRequest&#8217;, { &#8220;appId&#8221; : &#8220;wx2421b1c4370ec43b&#8221;, \/\/\u516c\u4f17\u53f7\u540d\u79f0\uff0c\u7531\u5546\u6237\u4f20\u5165 &#8220;timeStamp&#8221;:&#8221; 1395712654&#8243;, \/\/\u65f6\u95f4\u6233\uff0c\u81ea1970\u5e74\u4ee5\u6765\u7684\u79d2\u6570 &#8220;nonceStr&#8221; : &#8220;e61463f8efa94090b1f366cccfbbb444&#8221;, \/\/\u968f\u673a\u4e32 &#8220;package&#8221; : &#8220;u802345jgfjsdfgsdg888&#8221;, &#8220;signType&#8221; : &#8220;MD5&#8221;, \/\/\u5fae\u4fe1\u7b7e\u540d\u65b9\u5f0f: &#8220;paySign&#8221; : &#8220;70EA570631E4BB79628FBCA90534C63FF7FADD89&#8221; \/\/\u5fae\u4fe1\u7b7e\u540d }, function(res){ if(res.err_msg == &#8220;get_brand_wcpay_request:ok&#8221; ) {} \/\/ \u4f7f\u7528\u4ee5\u4e0a\u65b9\u5f0f\u5224\u65ad\u524d\u7aef\u8fd4\u56de,\u5fae\u4fe1\u56e2\u961f\u90d1\u91cd\u63d0\u793a\uff1ares.err_msg\u5c06\u5728\u7528\u6237\u652f\u4ed8\u6210\u529f\u540e\u8fd4\u56de ok\uff0c\u4f46\u5e76\u4e0d\u4fdd\u8bc1\u5b83\u7edd\u5bf9\u53ef\u9760\u3002 } ); } if (typeof WeixinJSBridge == &#8220;undefined&#8221;){ if( document.addEventListener ){ document.addEventListener(&#8216;WeixinJSBridgeReady&#8217;, onBridgeReady, false); }else [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-29460","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/29460","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=29460"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/29460\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=29460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=29460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=29460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}