Skip to content

Commit a72e3db

Browse files
authored
Readme增加一些详细示例 (#48)
* Update README.md,补充示例
1 parent c4c44e7 commit a72e3db

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

README.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,98 @@ HttpClient httpClient = builder.build();
5757
HttpResponse response = httpClient.execute(...);
5858
```
5959

60+
参数说明:
61+
62+
+ `merchantId`商户号。
63+
+ `merchantSerialNumber`商户证书的证书序列号,请参考[什么是证书序列号](https://wechatpay-api.gitbook.io/wechatpay-api-v3/chang-jian-wen-ti/zheng-shu-xiang-guan#shen-me-shi-zheng-shu-xu-lie-hao)[如何查看证书序列号](https://wechatpay-api.gitbook.io/wechatpay-api-v3/chang-jian-wen-ti/zheng-shu-xiang-guan#ru-he-cha-kan-zheng-shu-xu-lie-hao)
64+
+ `merchantPrivateKey`字符串格式的商户私钥,也就是通过证书工具得到的`apiclient_key.pem`文件中的内容。
65+
+ `wechatpayCertificates`微信支付平台证书的实例列表,用于应答签名的验证。你也可以使用后面章节提到的“自动更新证书功能”。
66+
67+
### 示例:获取平台证书
68+
69+
你可以使用`WechatPayHttpClientBuilder`构造的`HttpClient`发送请求和应答了。
70+
71+
```java
72+
URIBuilder uriBuilder = new URIBuilder("https://api.mch.weixin.qq.com/v3/certificates");
73+
HttpGet httpGet = new HttpGet(uriBuilder.build());
74+
httpGet.addHeader("Accept", "application/json");
75+
76+
CloseableHttpResponse response = httpClient.execute(httpGet);
77+
78+
String bodyAsString = EntityUtils.toString(response.getEntity());
79+
System.out.println(bodyAsString);
80+
```
81+
82+
### 示例:JSAPI下单
83+
84+
注:
85+
86+
+ 我们使用了 jackson-databind 演示拼装 Json,你也可以使用自己熟悉的 Json 库
87+
+ 请使用你自己的测试商户号、appid 以及对应的 openid
88+
89+
```java
90+
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi");
91+
httpPost.addHeader("Accept", "application/json");
92+
httpPost.addHeader("Content-type","application/json; charset=utf-8");
93+
94+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
95+
ObjectMapper objectMapper = new ObjectMapper();
96+
97+
ObjectNode rootNode = mapper.createObjectNode();
98+
rootNode.put("mchid","1900009191")
99+
.put("appid", "wxd678efh567hg6787")
100+
.put("description", "Image形象店-深圳腾大-QQ公仔")
101+
.put("notify_url", "https://www.weixin.qq.com/wxpay/pay.php")
102+
.put("out_trade_no", "1217752501201407033233368018");
103+
rootNode.putObject("amount")
104+
.put("total", 1);
105+
rootNode.putObject("payer")
106+
.put("openid", "oUpF8uMuAJO_M2pxb1Q9zNjWeS6o");
107+
108+
mapper.writeValue(bos, rootNode);
109+
110+
httpPost.setEntity(new StringEntity(bos.toString("UTF-8")));
111+
CloseableHttpResponse response = httpClient.execute(httpPost);
112+
113+
String bodyAsString = EntityUtils.toString(response.getEntity());
114+
System.out.println(bodyAsString);
115+
```
116+
117+
### 示例:查单
118+
119+
```java
120+
URIBuilder uriBuilder = new URIBuilder("https://api.mch.weixin.qq.com/v3/pay/transactions/id/4200000889202103303311396384?mchid=1230000109");
121+
HttpGet httpGet = new HttpGet(uriBuilder.build());
122+
httpGet.addHeader("Accept", "application/json");
123+
124+
CloseableHttpResponse response = httpClient.execute(httpGet);
125+
126+
String bodyAsString = EntityUtils.toString(response.getEntity());
127+
System.out.println(bodyAsString);
128+
```
129+
130+
### 示例:关单
131+
132+
```java
133+
HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/1217752501201407033233368018/close");
134+
httpPost.addHeader("Accept", "application/json");
135+
httpPost.addHeader("Content-type","application/json; charset=utf-8");
136+
137+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
138+
ObjectMapper objectMapper = new ObjectMapper();
139+
140+
ObjectNode rootNode = mapper.createObjectNode();
141+
rootNode.put("mchid","1900009191");
142+
143+
mapper.writeValue(bos, rootNode);
144+
145+
httpPost.setEntity(new StringEntity(bos.toString("UTF-8")));
146+
CloseableHttpResponse response = httpClient.execute(httpPost);
147+
148+
String bodyAsString = EntityUtils.toString(response.getEntity());
149+
System.out.println(bodyAsString);
150+
```
151+
60152
## 定制
61153

62154
当默认的本地签名和验签方式不适合你的系统时,你可以通过实现`Signer`或者`Verifier`来定制签名和验签。比如,你的系统把商户私钥集中存储,业务系统需通过远程调用进行签名,你可以这样做。

0 commit comments

Comments
 (0)