|
| 1 | +# Upgrading to 2.4.* from 2.3.* |
| 2 | + |
| 3 | +## Guidelines |
| 4 | + |
| 5 | +### 1. RequestOptions |
| 6 | + |
| 7 | +每个独立的请求可以使用特定的 API Key、App ID、私钥等配置。 |
| 8 | + |
| 9 | +#### 使用方法 |
| 10 | + |
| 11 | +初始化 `RequestOptions` |
| 12 | + |
| 13 | +```java |
| 14 | +RequestOptions options = new RequestOptionsBuilder() |
| 15 | + .setApiKey("sk_test_ibbTe5jLGCi5rzfH4OqPW9KC") |
| 16 | + .setAppId("app_1Gqj58ynP0mHeX1q") |
| 17 | + .setPrivateKey("-----BEGIN PRIVATE KEY-----\n......\n-----END PRIVATE KEY-----") |
| 18 | + .setConnectTimeout(5000) |
| 19 | + .setReadTimeout(20000) |
| 20 | + .setAcceptLanguage("zh-CN") |
| 21 | + .setMaxNetworkRetries(2) |
| 22 | + .build(); |
| 23 | +``` |
| 24 | + |
| 25 | +使用 `RequestOptions` |
| 26 | + |
| 27 | +```java |
| 28 | +Map<String, Object> params = new HashMap<String, Object>(); |
| 29 | +// 填充 params... |
| 30 | +Order order = Order.create(params, options); |
| 31 | +``` |
| 32 | + |
| 33 | +```java |
| 34 | +User user = User.retrieve("USER_ID", options); |
| 35 | +``` |
| 36 | + |
| 37 | +具体使用示例参考 [RequestOptionsTest.java](/src/test/java/com/pingplusplus/RequestOptionsTest.java) |
| 38 | + |
| 39 | +### 2. API Key in method |
| 40 | + |
| 41 | +原有方法中传 `API Key` 的方式都已经废弃,不能使用,请使用 `RequestOptions` 的方式。 |
| 42 | + |
| 43 | +涉及到以下方法: |
| 44 | + |
| 45 | +```java |
| 46 | +Agreement.create(Map<String, Object> params, String apiKey) |
| 47 | +Agreement.retrieve(String id, String apiKey) |
| 48 | +Agreement.list(Map<String, Object> params, String apiKey) |
| 49 | +Agreement.cancel(String id, String apiKey) |
| 50 | + |
| 51 | +BatchRefund.create(String apiKey, Map<String, Object>params) |
| 52 | +BatchRefund.retrieve(String id, String apiKey) |
| 53 | +BatchRefund.list(String apiKey, Map<String, Object> params) |
| 54 | + |
| 55 | +BatchTransfer.create(String apiKey, Map<String, Object>params) |
| 56 | +BatchTransfer.retrieve(String id, String apiKey) |
| 57 | +BatchTransfer.list(String apiKey, Map<String, Object> params) |
| 58 | +BatchTransfer.update(String id, String apiKey, Map<String, Object> params) |
| 59 | + |
| 60 | +CardInfo.query(Map<String, Object> params, String apiKey) |
| 61 | + |
| 62 | +Charge.create(String apiKey, Map<String, Object> params) |
| 63 | +Charge.retrieve(String id, String apiKey) |
| 64 | +Charge.retrieve(String id, String apiKey, Map<String, Object> params) |
| 65 | +Charge.list(String apiKey, Map<String, Object> params) |
| 66 | +Charge.reverse(String id, String apiKey, Map<String, Object> params) |
| 67 | +Charge.reverse(String id, String apiKey) |
| 68 | + |
| 69 | +charge.refunds.list(String apiKey, Map<String, Object> params) |
| 70 | +charge.refunds.retrieve(String id, String apiKey) |
| 71 | +charge.refunds.create(String apiKey, Map<String, Object> params) |
| 72 | + |
| 73 | +Contact.create(Map<String, Object> params, String apiKey) |
| 74 | + |
| 75 | +Customs.create(String apiKey, Map<String, Object> params) |
| 76 | +Customs.retrieve(String id, String apiKey) |
| 77 | +Customs.list(String apiKey, Map<String, Object> params) |
| 78 | + |
| 79 | +Event.retrieve(String id, String apiKey) |
| 80 | +Event.retrieve(String id, String apiKey, Map<String, Object> params) |
| 81 | + |
| 82 | +Identification.identify(String apiKey, Map<String, Object> params) |
| 83 | + |
| 84 | +ProfitTransaction.retrieve(String id, String apiKey) |
| 85 | +ProfitTransaction.list(Map<String, Object> params, String apiKey) |
| 86 | + |
| 87 | +RedEnvelope.create(String apiKey, Map<String, Object> params) |
| 88 | +RedEnvelope.retrieve(String id, String apiKey) |
| 89 | +RedEnvelope.retrieve(String id, String apiKey, Map<String, Object> params) |
| 90 | +RedEnvelope.list(String apiKey, Map<String, Object> params) |
| 91 | + |
| 92 | +Refund.create(String charge, String apiKey, Map<String, Object> params) |
| 93 | +Refund.retrieve(String charge, String id, String apiKey) |
| 94 | +Refund.list(String charge, String apiKey, Map<String, Object>params) |
| 95 | + |
| 96 | +Royalty.createData(String orderId, Map<String, Object> params, String apiKey) |
| 97 | + |
| 98 | +SplitProfit.create(Map<String, Object> params, String apiKey) |
| 99 | +SplitProfit.retrieve(String id, String apiKey) |
| 100 | +SplitProfit.list(Map<String, Object> params, String apiKey) |
| 101 | +SplitReceiver.create(Map<String, Object> params, String apiKey) |
| 102 | +SplitReceiver.retrieve(String id, String apiKey) |
| 103 | +SplitReceiver.list(Map<String, Object> params, String apiKey) |
| 104 | +SplitReceiver.delete(String id, String apiKey) |
| 105 | + |
| 106 | +SubBank.query(Map<String, Object> params, String apiKey) |
| 107 | + |
| 108 | +Transfer.create(String apiKey, Map<String, Object> params) |
| 109 | +Transfer.retrieve(String id, String apiKey) |
| 110 | +Transfer.retrieve(String id, String apiKey, Map<String, Object> params) |
| 111 | +Transfer.list(String apiKey, Map<String, Object> params) |
| 112 | + |
| 113 | +UserPic.upload(Map<String, Object> params, String apiKey) |
| 114 | +``` |
| 115 | + |
| 116 | +#### 替换示例 |
| 117 | + |
| 118 | +##### 示例 1 |
| 119 | + |
| 120 | +```java |
| 121 | +String apiKey = "YOUR_API_KEY"; |
| 122 | +Agreement.create(params, apiKey); |
| 123 | + |
| 124 | +// 替换成 |
| 125 | + |
| 126 | +String apiKey = "YOUR_API_KEY"; |
| 127 | +RequestOptions options = new RequestOptionsBuilder().setApiKey(apiKey).build(); |
| 128 | +Agreement.create(params, options); |
| 129 | +``` |
| 130 | + |
| 131 | +##### 示例 2 |
| 132 | + |
| 133 | +```java |
| 134 | +String apiKey = "YOUR_API_KEY"; |
| 135 | +Charge.create(apiKey, params) |
| 136 | + |
| 137 | +// 替换成 |
| 138 | + |
| 139 | +String apiKey = "YOUR_API_KEY"; |
| 140 | +RequestOptions options = new RequestOptionsBuilder().setApiKey(apiKey).build(); |
| 141 | +Charge.create(params, options); |
| 142 | +``` |
| 143 | + |
| 144 | +### 3. Exceptions |
| 145 | + |
| 146 | +```java |
| 147 | +try { |
| 148 | + Charge.create(params); |
| 149 | +} catch (AuthenticationException e) { |
| 150 | +} catch (InvalidRequestException e) { |
| 151 | +} catch (APIConnectionException e) { |
| 152 | +} catch (APIException e) { |
| 153 | +} catch (ChannelException e) { |
| 154 | +} catch (RateLimitException e) { |
| 155 | +} |
| 156 | +``` |
| 157 | + |
| 158 | +改成 |
| 159 | + |
| 160 | +```java |
| 161 | +try { |
| 162 | + Charge.create(params); |
| 163 | +} catch (PingppException e) { |
| 164 | + // 如果要具体分是哪个 exception |
| 165 | + if (e instanceof InvalidRequestException) { |
| 166 | + InvalidRequestException ex = (InvalidRequestException) e; |
| 167 | + // 获取 error.code 字段(部分报错有) |
| 168 | + System.out.println(ex.getCode()); |
| 169 | + // 获取请求的 HTTP Status Code |
| 170 | + System.out.println(ex.getStatusCode()); |
| 171 | + // 获取 error.type 字段 |
| 172 | + System.out.println(ex.getType)); |
| 173 | + } |
| 174 | + // ... |
| 175 | +} |
| 176 | +``` |
| 177 | + |
| 178 | +### 4. Accept-Language |
| 179 | + |
| 180 | +`Pingpp.AcceptLanguage` 改为 `Pingpp.acceptLanguage`。 |
| 181 | + |
| 182 | +### 5. Private Key |
| 183 | + |
| 184 | +废弃 `PKCS#1` 编码的私钥支持。 |
| 185 | + |
| 186 | +请使用 `PKCS#8` 编码的私钥。openssl 转换命令如下: |
| 187 | + |
| 188 | +```shell |
| 189 | +openssl pkcs8 -topk8 -inform PEM -in pkcs1.pem -outform PEM -nocrypt -out pkcs8.pem |
| 190 | +``` |
| 191 | + |
| 192 | +### 6. getLastResponse |
| 193 | + |
| 194 | +可以获取当前请求的 `response` 对象。 |
| 195 | + |
| 196 | +```java |
| 197 | +Charge charge = Charge.create(params); |
| 198 | +PingppResponse response = charge.getLastResponse(); |
| 199 | + |
| 200 | +// 获取响应的 HTTP Status Code |
| 201 | +int statusCode = response.getResponseCode(); |
| 202 | + |
| 203 | +// 获取响应的 Headers |
| 204 | +HttpHeaders headers = response.getResponseHeaders(); |
| 205 | + |
| 206 | +// 获取响应体 |
| 207 | +String responseBody = response.getResponseBody() |
| 208 | +``` |
0 commit comments