diff --git a/.gitignore b/.gitignore index 6e48406..c5693ed 100644 --- a/.gitignore +++ b/.gitignore @@ -60,3 +60,4 @@ org.eclipse.jdt.core.prefs example/out .vscode .classpath +target/ diff --git a/CHANGELOG.md b/CHANGELOG.md index f969a42..d36bfe4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,54 @@ # ChangeLog +## 2.5.6 + +- 更新: `RequestOptions` 新增`Ping++`验签公钥设置参数 + +## 2.5.5 + +-更新: 退款 refund 新增返回 `app` 参数. + +## 2.5.4 + +- 修正: 修复版本 2.5.3 中存在的 POM 依赖问题。建议用户直接升级到此版本。 + +## 2.5.3 + +- 新增: 签约修改接口 + +## 2.5.2 + +- 修改: 正确 `Map` 以及 `List` 内的元素类型 + +## 2.5.0 + +- 新增: 响应签名验签 +- 新增: 微信扣款预扣费通知实现 +- 更新: `RequestOptions` 新增`Ping++`公钥设置参数 +- 更新: webhook验签方法移动至 `PingppSignature` 类中 +- 更新: `gson` 到`2.10`;`commons-codec` 到 `1.17.0` + +## 2.4.1 + +- 修改: Refund 对象新增`currency` 字段 +- 修改: 更新示例 Main.java 中设置私钥和私钥文件地址写法 +- 修改: build.gradle 镜像仓库使用阿里云源 + +## 2.4.0 + +> 本次更新有较多与旧版本不兼容的地方。 + +- 修改: 支持 JDK 8 及以上; +- 修改: 异常抛出修改,统一声明为 `PingppException`,实际使用可以再做具体判断; +- 废弃: 原接口,支持传 `apiKey` 参数的方法,全部删除,可以用 `RequestOptions` 代替; +- 新增: 每个接口最后一个参数支持 `RequestOptions`; +- 新增: `PingppException` 增加 `type`、`code`、`statusCode`; +- 新增: 对象增加 `getLastResponse()` 方法,可以用于获取当前这次请求的 `response` 对象,包含 `HTTP Status Code`、`Headers`、 + `Response Body` 等; +- 废弃: 取消对 `PKCS1` 格式私钥的支持,请转成 `PKCS8` 再使用; + +--- + ## 2.3.14 - 新增:settle_account.recipient extra 字段 @@ -46,25 +95,34 @@ - 移除 `PingppAccount` ## 2.3.8 (2018-05-22) + #### 新增 + - WxLiteOAuth 接口 - Agreement 接口 #### 修改 + - 合并部分内部类 - WxpubOAuth 接口错误时增加异常 ## 2.3.7 (2018-03-16) + #### 新增 + - BalanceSettlement 接口 - CardInfo 接口 ## 2.3.5 (2018-02-27) + #### 修正 + - 部分对象字段从 Integer 改成 Long ## 2.3.4 (2018-01-10) + #### 修改 + - 移除已废弃接口 - 移除 transfer、batch_transfer 取消接口 - 更新示例 @@ -72,137 +130,190 @@ - settle_account 字段更新 ## 2.3.3 (2017-12-27) + #### 修正 + - 修正 javadoc ## 2.3.2 (2017-12-21) + #### 修正 + - 兼容 JDK 1.7 ## 2.3.1 (2017-12-08) + #### 修正 + - Event 解析修正 ## 2.3.0 (2017-12-01) + #### 更改 + - 合并账户系统相关接口 ## 2.2.5 (2017-10-10) + #### 新增 + - 新增重试机制 #### 修改 + - 优化 SDK ## 2.2.4 (2017-06-19) + #### 修改 + - Charge 增加字段 reversed,表示是否已撤销 - 针对渠道 isv_scan, isv_qr_isv_wap 增加撤销方法,`Charge.reverse(CHARGE_ID)` - Refund 增加 extra 字段 ## 2.2.3 (2017-03-27) + #### 修改 + - BatchTransfer 增加字段 ## 2.2.2 (2017-03-21) + #### 修正 + - alipay_pc_direct/alipay_wap 渠道 credential 字段类型的修正 ## 2.2.1 (2017-01-13) + #### 新增 + - 添加 gradle 相关文件 - 添加部分测试文件 - batch_transfer 更新/取消接口 #### 修改 + - 更改签名私钥获取方式 - Charge.list app[id] 参数改为必传 - 红包去除多余的参数 - batch_refund charges 字段格式修改 #### 修正 + - Webhooks 解析对象 batch_refund、batch_transfer、customs 对应事件错误 ## 2.2.0 + #### 新增 + - 添加 BatchRefund、BatchTransfer、Customs #### 修改 + - 签名生成规则变更 - 列表查询接口换成 `list` 方法,代替 `all` 方法 - 退款接口调用方式变更,直接使用 Refund 对象 - Refund 对象添加 charge_order_no, transaction_no 属性 ## 2.1.9 + #### 修改 -- 添加 RateLimitException + +- 添加 RateLimitException - 兼容 http 地址,仅为方便测试 ## 2.1.8 + #### 新增 + - Identification 身份证银行卡接口 ## 2.1.7 + #### 修正 + - RedEnvelope、Transfer 添加字段 ## 2.1.6 + #### 修正 + - 签名内容编码问题 ## 2.1.5 + #### 修改 + - 补充 RedEnvelope、Transfer 缺少字段 ## 2.1.4 + #### 修改 + - 更改 content-type 为 application/json ## 2.1.3 + - 兼容两种微信公众号接入方式 ## 2.1.2 + - 补全确实字段 - 修正 Event 解析 - 修正部分字符 JSON 序列化问题 - 添加请求签名 ## 2.1.1 + - 修正 extra 类型 - 兼容 jdk 1.6 ## 2.1.0 + - 添加应用内快捷支付对应接口 ## 2.0.10 + - 增加 Refund 对象 status 字段 ## 2.0.8 + - 增加 ChannelException ## 2.0.6 + - 增加企业转账、修改红包接口 ## 2.0.5 + - 增加 event sdk ## 2.0.4 + - 更改 sdk 目录格式,修复wx_pub渠道timeStamp 类型问题。 ## 2.0.3 + - 增加 apple pay ,重写 getcredential 方法。 ## 2.0.2 + - 增加微信红包 ## 2.0.1 + #### 更改 + 修改 wx credential 里的 timeStamp 类型为字符串,防止变成科学计数法 ## 2.0.0 + #### 更改 + 添加新渠道支持:百付宝、百付宝WAP、微信公众平台 ## 1.0.4 + #### 更改: + 添加 `Expanding` 机制 diff --git a/README.md b/README.md index 6e2d358..1a8a82c 100644 --- a/README.md +++ b/README.md @@ -9,64 +9,64 @@ ## 版本要求 -Java SDK 要求 JDK 版本 1.7 及以上 +Java 要求 JDK 8 及以上。 ## 安装 -#### 手动安装 +### 手动安装 -将 libs/ 下面的 jar 包导入工程 +JAR 下载地址: [pingpp-java](https://mvnrepository.com/artifact/com.pingxx/pingpp-java) -#### maven 安装 +请根据版本号下载相应的 JAR 文件并导入至工程。 + +#### 依赖库 + +- com.google.code.gson:gson +- commons-codec:commons-codec + +### maven 安装 maven 远程仓库 ``` xml - - - false - - central - bintray - http://jcenter.bintray.com - +mavenCentral ``` 安装 Ping++ SDK + ``` xml - Pingplusplus + com.pingxx pingpp-java - 2.3.14 + 2.5.6 jar ``` -#### gradle 安装 +### gradle 安装 gradle 远程仓库 ``` repositories { - maven { - url "http://jcenter.bintray.com" - } + mavenCentral() } ``` 安装 Ping++ SDK ``` -compile 'Pingplusplus:pingpp-java:2.3.14' +implementation 'com.pingxx:pingpp-java:2.5.6' ``` -### 初始化 +## 初始化 ``` Pingpp.apiKey = "YOUR_API_KEY"; ``` -### 使用示例 +## 使用示例 - 参考 [example](/example) 示例项目工程。该工程提供了付款、退款、微信公共号付款相关的 demo。 - 以及 [test](/src/test/java/com/pingplusplus) 目录下的示例。 +- 原有旧版本的用户请查看[升级文档](/docs/update/)进行相应调整。 diff --git a/VERSION b/VERSION index 0e80d41..1b03fe6 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.3.14 +2.5.6 \ No newline at end of file diff --git a/build.gradle b/build.gradle index 4838ac1..e7c254d 100644 --- a/build.gradle +++ b/build.gradle @@ -1,85 +1,12 @@ buildscript { repositories { - jcenter() + mavenCentral() } } -plugins { - id "com.jfrog.bintray" version "1.8.0" -} - allprojects { repositories { - jcenter() + mavenCentral() } - apply plugin: 'maven' - apply plugin: 'maven-publish' apply plugin: 'java' } - -sourceCompatibility = 1.7 -targetCompatibility = 1.7 - -dependencies { - compile 'com.google.code.gson:gson:2.8.5' - compile 'commons-codec:commons-codec:1.10' - - testCompile 'junit:junit:4.12' -} - -task sourcesJar(type: Jar, dependsOn: classes) { - classifier = 'sources' - from sourceSets.main.allSource -} - -task javadocJar(type: Jar, dependsOn: javadoc) { - classifier = 'javadoc' - from javadoc.destinationDir -} - -artifacts { - archives sourcesJar, javadocJar -} - -publishing { - publications { - mavenJava(MavenPublication) { - from components.java - artifact sourcesJar - artifact javadocJar - groupId 'Pingplusplus' - artifactId 'pingpp-java' - version releaseVersion - - pom.withXml { - def root = asNode() - root.appendNode('name', 'pingpp-java') - root.appendNode('url', 'https://github.com/PingPlusPlus/pingpp-java') - } - } - } -} - -bintray { - user = bintrayUser - key = bintrayApiKey - publications = ['mavenJava'] - dryRun = false - publish = true - pkg { - repo = 'maven' - name = 'pingplusplus' - licenses = ['MIT'] - labels = ['Ping++', 'pingxx', 'payment'] - vcsUrl = 'https://github.com/PingPlusPlus/pingpp-java.git' - version { - name = releaseVersion - } - } -} - -model { - tasks.generatePomFileForMavenJavaPublication { - destination = file("pom.xml") - } -} diff --git a/docs/update/2.3-to-2.4.md b/docs/update/2.3-to-2.4.md new file mode 100644 index 0000000..f8a39b8 --- /dev/null +++ b/docs/update/2.3-to-2.4.md @@ -0,0 +1,208 @@ +# Upgrading to 2.4.* from 2.3.* + +## Guidelines + +### 1. RequestOptions + +每个独立的请求可以使用特定的 API Key、App ID、私钥等配置。 + +#### 使用方法 + +初始化 `RequestOptions` + +```java +RequestOptions options = new RequestOptionsBuilder() + .setApiKey("sk_test_ibbTe5jLGCi5rzfH4OqPW9KC") + .setAppId("app_1Gqj58ynP0mHeX1q") + .setPrivateKey("-----BEGIN PRIVATE KEY-----\n......\n-----END PRIVATE KEY-----") + .setConnectTimeout(5000) + .setReadTimeout(20000) + .setAcceptLanguage("zh-CN") + .setMaxNetworkRetries(2) + .build(); +``` + +使用 `RequestOptions` + +```java +Map params = new HashMap(); +// 填充 params... +Order order = Order.create(params, options); +``` + +```java +User user = User.retrieve("USER_ID", options); +``` + +具体使用示例参考 [RequestOptionsTest.java](/src/test/java/com/pingplusplus/RequestOptionsTest.java) + +### 2. API Key in method + +原有方法中传 `API Key` 的方式都已经废弃,不能使用,请使用 `RequestOptions` 的方式。 + +涉及到以下方法: + +```java +Agreement.create(Map params, String apiKey) +Agreement.retrieve(String id, String apiKey) +Agreement.list(Map params, String apiKey) +Agreement.cancel(String id, String apiKey) + +BatchRefund.create(String apiKey, Mapparams) +BatchRefund.retrieve(String id, String apiKey) +BatchRefund.list(String apiKey, Map params) + +BatchTransfer.create(String apiKey, Mapparams) +BatchTransfer.retrieve(String id, String apiKey) +BatchTransfer.list(String apiKey, Map params) +BatchTransfer.update(String id, String apiKey, Map params) + +CardInfo.query(Map params, String apiKey) + +Charge.create(String apiKey, Map params) +Charge.retrieve(String id, String apiKey) +Charge.retrieve(String id, String apiKey, Map params) +Charge.list(String apiKey, Map params) +Charge.reverse(String id, String apiKey, Map params) +Charge.reverse(String id, String apiKey) + +charge.refunds.list(String apiKey, Map params) +charge.refunds.retrieve(String id, String apiKey) +charge.refunds.create(String apiKey, Map params) + +Contact.create(Map params, String apiKey) + +Customs.create(String apiKey, Map params) +Customs.retrieve(String id, String apiKey) +Customs.list(String apiKey, Map params) + +Event.retrieve(String id, String apiKey) +Event.retrieve(String id, String apiKey, Map params) + +Identification.identify(String apiKey, Map params) + +ProfitTransaction.retrieve(String id, String apiKey) +ProfitTransaction.list(Map params, String apiKey) + +RedEnvelope.create(String apiKey, Map params) +RedEnvelope.retrieve(String id, String apiKey) +RedEnvelope.retrieve(String id, String apiKey, Map params) +RedEnvelope.list(String apiKey, Map params) + +Refund.create(String charge, String apiKey, Map params) +Refund.retrieve(String charge, String id, String apiKey) +Refund.list(String charge, String apiKey, Mapparams) + +Royalty.createData(String orderId, Map params, String apiKey) + +SplitProfit.create(Map params, String apiKey) +SplitProfit.retrieve(String id, String apiKey) +SplitProfit.list(Map params, String apiKey) +SplitReceiver.create(Map params, String apiKey) +SplitReceiver.retrieve(String id, String apiKey) +SplitReceiver.list(Map params, String apiKey) +SplitReceiver.delete(String id, String apiKey) + +SubBank.query(Map params, String apiKey) + +Transfer.create(String apiKey, Map params) +Transfer.retrieve(String id, String apiKey) +Transfer.retrieve(String id, String apiKey, Map params) +Transfer.list(String apiKey, Map params) + +UserPic.upload(Map params, String apiKey) +``` + +#### 替换示例 + +##### 示例 1 + +```java +String apiKey = "YOUR_API_KEY"; +Agreement.create(params, apiKey); + +// 替换成 + +String apiKey = "YOUR_API_KEY"; +RequestOptions options = new RequestOptionsBuilder().setApiKey(apiKey).build(); +Agreement.create(params, options); +``` + +##### 示例 2 + +```java +String apiKey = "YOUR_API_KEY"; +Charge.create(apiKey, params) + +// 替换成 + +String apiKey = "YOUR_API_KEY"; +RequestOptions options = new RequestOptionsBuilder().setApiKey(apiKey).build(); +Charge.create(params, options); +``` + +### 3. Exceptions + +```java +try { + Charge.create(params); +} catch (AuthenticationException e) { +} catch (InvalidRequestException e) { +} catch (APIConnectionException e) { +} catch (APIException e) { +} catch (ChannelException e) { +} catch (RateLimitException e) { +} +``` + +改成 + +```java +try { + Charge.create(params); +} catch (PingppException e) { + // 如果要具体分是哪个 exception + if (e instanceof InvalidRequestException) { + InvalidRequestException ex = (InvalidRequestException) e; + // 获取 error.code 字段(部分报错有) + System.out.println(ex.getCode()); + // 获取请求的 HTTP Status Code + System.out.println(ex.getStatusCode()); + // 获取 error.type 字段 + System.out.println(ex.getType)); + } + // ... +} +``` + +### 4. Accept-Language + +`Pingpp.AcceptLanguage` 改为 `Pingpp.acceptLanguage`。 + +### 5. Private Key + +废弃 `PKCS#1` 编码的私钥支持。 + +请使用 `PKCS#8` 编码的私钥。openssl 转换命令如下: + +```shell +openssl pkcs8 -topk8 -inform PEM -in pkcs1.pem -outform PEM -nocrypt -out pkcs8.pem +``` + +### 6. getLastResponse + +可以获取当前请求的 `response` 对象。 + +```java +Charge charge = Charge.create(params); +PingppResponse response = charge.getLastResponse(); + +// 获取响应的 HTTP Status Code +int statusCode = response.getResponseCode(); + +// 获取响应的 Headers +HttpHeaders headers = response.getResponseHeaders(); + +// 获取响应体 +String responseBody = response.getResponseBody() +``` diff --git a/example/build.gradle b/example/build.gradle index a9101ed..495f667 100644 --- a/example/build.gradle +++ b/example/build.gradle @@ -1,12 +1,13 @@ apply plugin: 'java' -repositories { - jcenter() -} - dependencies { - compile 'com.google.code.gson:gson:2.8.5' - compile 'commons-codec:commons-codec:1.10' + implementation 'com.google.code.gson:gson:2.10' + implementation 'commons-codec:commons-codec:1.17.0' - compile project(':') + // 方法 1. 本地文件系统依赖 + // implementation fileTree(dir: 'lib', includes: ['*jar']) + // 方法 2. 仓库依赖 + // implementation 'com.pingxx:pingpp-java:2.5.6' + // 方法 3.模块依赖 + implementation project(':pingpp-sdk') } diff --git a/example/res/pingpp_public_key.pem b/example/res/pingpp_public_key.pem deleted file mode 100644 index 7cf6d7c..0000000 --- a/example/res/pingpp_public_key.pem +++ /dev/null @@ -1,9 +0,0 @@ ------BEGIN PUBLIC KEY----- -MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzs8SiPoFQT9K0lWa6WSx -0d6UnA391KM2aFwijY0AK7r+MiAe07ivenopzFL3dqIRhQjuP7d30V85kWydN5UZ -cm/tZgm4K+8wttb988hOrzSjtPOMghHK+bnDwE8FIB+ZbHAZCEVhNfE6i9kLGbHH -Q617+mxUTJ3yEZG9CIgke475o2Blxy4UMsRYjo2gl5aanzmOmoZcbiC/R5hXSQUH -XV9/VzA7U//DIm8Xn7rerd1n8+KWCg4hrIIu/A0FKm8zyS4QwAwQO2wdzGB0h15t -uFLhjVz1W5ZPXjmCRLzTUoAvH12C6YFStvS5kjPcA66P1nSKk5o3koSxOumOs0iC -EQIDAQAB ------END PUBLIC KEY----- diff --git a/example/res/your_rsa_private_key_pkcs8.pem b/example/res/your_rsa_private_key_pkcs8.pem index e4e042c..9da76fb 100644 --- a/example/res/your_rsa_private_key_pkcs8.pem +++ b/example/res/your_rsa_private_key_pkcs8.pem @@ -1,28 +1 @@ ------BEGIN PRIVATE KEY----- -MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDHYyS3FwoESp1h -GLYiBhy6k9Ag3lzGCIEvm50IIEkE0Ftc9qq44TWqyl+EHUpTMdcBOcI42JLO5stw -FOfCLa3PQStEJ4llIRFEKlsrHh67pvWd5RNaSBrvGlnFY40S+SZmjk2WF/h9dE9R -ic79t0YI0alD8dIl9Yu3OaEKo7VonBWFwOYMxjPhtORlq+EUF1XJd//yftQrKWTT -d7KaUonWzBCl4VzFop/OyTWYlTuZz3eYJaNpH5VaQ1vDgBAcPIeBvMf7NgBHMKW6 -LLmFd2LEYQ/6I7hkGTjysSzWEpO8bPWT6OEsJ2R2kFGOrSkr+G2MDcJ7ykXYAmz5 -+A3plS6ZAgMBAAECggEAVrgwR9GlcahiOtDcpn+yDxQq+aC9CQS561LrQZWJLKbS -leRS7IZHKTlLwdJbeUO8F7RfXQoVEBghc2YkRrhHWFUn1ES95VY0hElHzcET7Nn5 -CeuQNzwVOtljIg7iVNY4dXJ/HEDguu/Tb8tYU9FajItj60FJ/WiGk/JksJPzWsOC -VPVniy9fTbTLy1e+dCpCI6OXirtm7hvbodRNDjree0wSEzm7vL0wVzEZFo6kX+AB -GUwaoO7pPyH+hgyI5Iuhc65NHsHzTJpf8yNFl9QGhkxvm2Ff2oEtDt1idOTBrHB6 -tg+ti9Ctb2+2yzBnk14hsSYJnKitR7wM6ZCFPX4eYQKBgQD+JAREeFkodec/SC+G -X+4Q4Y68uMPkfUPrMKXM4cyY5wgXk64RBvRVxIxX7x6Y3tIKn9v8tWAprbsyVr15 -eb4RcAFEVwjuoZixhd9sIPsRhfdNolKn/fSPIsHL4ywcJMSIt7KVKHuQeqBNHy0o -0PxQjNej1ozsmrAWqV55cbKHswKBgQDI2JQRTPIEC/2y6LdmBVhGJW9OKWTYdVNj -q7rX+Yw4uxOtfd5hBqpvgZEklKEk72aazFdEcERlAm9SqoX09qk6zK/wcq4Xn5Q/ -qy8ecmjuyf2AK9X+HUdMerMVxhK9RpeevKYP/RO2F/wIN64anlQVYygVkXXgdOvW -hBE4YABKgwKBgDRtmbPGYB5ItHwJmERQZfx1i8zDESaB8RED6DBsJJkmkDTM8ovw -s1c+RPWfDuDalto6QFfR0xTGEmhAHLaCtwNB6AEBM4aHL8jvpTfZVfI3gN0zL3oY -mestcG1vYBouO504yE6dG2Ci6479b4OMGYFEjPfvuwLUpp8GMcc7/WihAoGANCp8 -mtm/ammq5VMof2kX+nAyrrx1ovsmQ5cRGpOIZhvBCqjMn6rZjci7aCLqj+tWXRKC -ABagzROK0o/T50JBxjHv6KYArcYW/Up7HI9ezdbM7wNzu2LjZ+veo+MkbuDs9J/P -CgwTmJI2NfQwVl2VPVDZ0nBLi5cSwk7fIiNdL/0CgYEAtECmC1QDs53Di2MIsa/F -e4sWfJGSDqEWqhcA/aPwf1skM6VJJXBBMV1qFtwgO1AlLnu9dQYra6ylsUoubVYI -XM9XK7EMhbqi57+Q75jHFTc0DnzOTyho5Gp4Ddi8dztmZGNWdWTGdeMqh+svqMXk -D6VdJeddyGu/Zlgj7Wk6whU= ------END PRIVATE KEY----- +MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDHYyS3FwoESp1hGLYiBhy6k9Ag3lzGCIEvm50IIEkE0Ftc9qq44TWqyl+EHUpTMdcBOcI42JLO5stwFOfCLa3PQStEJ4llIRFEKlsrHh67pvWd5RNaSBrvGlnFY40S+SZmjk2WF/h9dE9Ric79t0YI0alD8dIl9Yu3OaEKo7VonBWFwOYMxjPhtORlq+EUF1XJd//yftQrKWTTd7KaUonWzBCl4VzFop/OyTWYlTuZz3eYJaNpH5VaQ1vDgBAcPIeBvMf7NgBHMKW6LLmFd2LEYQ/6I7hkGTjysSzWEpO8bPWT6OEsJ2R2kFGOrSkr+G2MDcJ7ykXYAmz5+A3plS6ZAgMBAAECggEAVrgwR9GlcahiOtDcpn+yDxQq+aC9CQS561LrQZWJLKbSleRS7IZHKTlLwdJbeUO8F7RfXQoVEBghc2YkRrhHWFUn1ES95VY0hElHzcET7Nn5CeuQNzwVOtljIg7iVNY4dXJ/HEDguu/Tb8tYU9FajItj60FJ/WiGk/JksJPzWsOCVPVniy9fTbTLy1e+dCpCI6OXirtm7hvbodRNDjree0wSEzm7vL0wVzEZFo6kX+ABGUwaoO7pPyH+hgyI5Iuhc65NHsHzTJpf8yNFl9QGhkxvm2Ff2oEtDt1idOTBrHB6tg+ti9Ctb2+2yzBnk14hsSYJnKitR7wM6ZCFPX4eYQKBgQD+JAREeFkodec/SC+GX+4Q4Y68uMPkfUPrMKXM4cyY5wgXk64RBvRVxIxX7x6Y3tIKn9v8tWAprbsyVr15eb4RcAFEVwjuoZixhd9sIPsRhfdNolKn/fSPIsHL4ywcJMSIt7KVKHuQeqBNHy0o0PxQjNej1ozsmrAWqV55cbKHswKBgQDI2JQRTPIEC/2y6LdmBVhGJW9OKWTYdVNjq7rX+Yw4uxOtfd5hBqpvgZEklKEk72aazFdEcERlAm9SqoX09qk6zK/wcq4Xn5Q/qy8ecmjuyf2AK9X+HUdMerMVxhK9RpeevKYP/RO2F/wIN64anlQVYygVkXXgdOvWhBE4YABKgwKBgDRtmbPGYB5ItHwJmERQZfx1i8zDESaB8RED6DBsJJkmkDTM8ovws1c+RPWfDuDalto6QFfR0xTGEmhAHLaCtwNB6AEBM4aHL8jvpTfZVfI3gN0zL3oYmestcG1vYBouO504yE6dG2Ci6479b4OMGYFEjPfvuwLUpp8GMcc7/WihAoGANCp8mtm/ammq5VMof2kX+nAyrrx1ovsmQ5cRGpOIZhvBCqjMn6rZjci7aCLqj+tWXRKCABagzROK0o/T50JBxjHv6KYArcYW/Up7HI9ezdbM7wNzu2LjZ+veo+MkbuDs9J/PCgwTmJI2NfQwVl2VPVDZ0nBLi5cSwk7fIiNdL/0CgYEAtECmC1QDs53Di2MIsa/Fe4sWfJGSDqEWqhcA/aPwf1skM6VJJXBBMV1qFtwgO1AlLnu9dQYra6ylsUoubVYIXM9XK7EMhbqi57+Q75jHFTc0DnzOTyho5Gp4Ddi8dztmZGNWdWTGdeMqh+svqMXkD6VdJeddyGu/Zlgj7Wk6whU= \ No newline at end of file diff --git a/example/src/main/java/com/pingxx/example/AgreementExample.java b/example/src/main/java/com/pingxx/example/AgreementExample.java new file mode 100644 index 0000000..205a51c --- /dev/null +++ b/example/src/main/java/com/pingxx/example/AgreementExample.java @@ -0,0 +1,30 @@ +package com.pingxx.example; + +import com.pingplusplus.exception.PingppException; +import com.pingplusplus.model.Agreement; + +import java.util.HashMap; +import java.util.Map; + +public class AgreementExample { + + private String appId; + + public AgreementExample(String appId) { + this.appId = appId; + } + + public static void runDemos(String appId) throws PingppException { + AgreementExample example = new AgreementExample(appId); + Agreement agrModify = example.modify(); + } + + public Agreement modify() throws PingppException { + String agreementId = "agr_123456"; + Map params = new HashMap<>(); + params.put("deduct_time", "2025-07-10"); + params.put("memo", "扣款失败,需延期扣款时间"); + + return Agreement.modify(agreementId, params); + } +} diff --git a/example/src/main/java/com/pingxx/example/AgreementNotifyExample.java b/example/src/main/java/com/pingxx/example/AgreementNotifyExample.java new file mode 100644 index 0000000..54c9fa5 --- /dev/null +++ b/example/src/main/java/com/pingxx/example/AgreementNotifyExample.java @@ -0,0 +1,33 @@ +package com.pingxx.example; + +import com.pingplusplus.exception.PingppException; +import com.pingplusplus.model.AgreementNotify; + +import java.util.HashMap; +import java.util.Map; + +public class AgreementNotifyExample { + private String appId; + + AgreementNotifyExample(String appId) { + this.appId = appId; + } + + public static void runDemos(String appId) { + AgreementNotifyExample example = new AgreementNotifyExample(appId); + System.out.println("------- 签约扣款-商户预扣费通知 -------"); + example.create("agr_123456"); + } + + public AgreementNotify create(String id) { + AgreementNotify agreementNotify = null; + Map params = new HashMap(); + params.put("amount", 100); + try { + agreementNotify = AgreementNotify.create(id, params); + } catch (PingppException e) { + e.printStackTrace(); + } + return agreementNotify; + } +} diff --git a/example/src/main/java/com/pingxx/example/BatchTransferExample.java b/example/src/main/java/com/pingxx/example/BatchTransferExample.java index bb73d4f..a556df1 100644 --- a/example/src/main/java/com/pingxx/example/BatchTransferExample.java +++ b/example/src/main/java/com/pingxx/example/BatchTransferExample.java @@ -80,17 +80,7 @@ public BatchTransfer create() { try { obj = BatchTransfer.create(params); System.out.println(obj); - } catch (AuthenticationException e) { - e.printStackTrace(); - } catch (InvalidRequestException e) { - e.printStackTrace(); - } catch (APIConnectionException e) { - e.printStackTrace(); - } catch (APIException e) { - e.printStackTrace(); - } catch (ChannelException e) { - e.printStackTrace(); - } catch (RateLimitException e) { + } catch (PingppException e) { e.printStackTrace(); } return obj; @@ -107,17 +97,7 @@ public void retrieve(String id) { try { BatchTransfer obj = BatchTransfer.retrieve(id); System.out.println(obj); - } catch (AuthenticationException e) { - e.printStackTrace(); - } catch (InvalidRequestException e) { - e.printStackTrace(); - } catch (APIConnectionException e) { - e.printStackTrace(); - } catch (APIException e) { - e.printStackTrace(); - } catch (ChannelException e) { - e.printStackTrace(); - } catch (RateLimitException e) { + } catch (PingppException e) { e.printStackTrace(); } } @@ -135,17 +115,7 @@ public void list() { try { BatchTransferCollection objs = BatchTransfer.list(param); System.out.println(objs); - } catch (AuthenticationException e) { - e.printStackTrace(); - } catch (InvalidRequestException e) { - e.printStackTrace(); - } catch (APIConnectionException e) { - e.printStackTrace(); - } catch (APIException e) { - e.printStackTrace(); - } catch (ChannelException e) { - e.printStackTrace(); - } catch (RateLimitException e) { + } catch (PingppException e) { e.printStackTrace(); } } diff --git a/example/src/main/java/com/pingxx/example/ChargeExample.java b/example/src/main/java/com/pingxx/example/ChargeExample.java index f69ebef..88421e1 100644 --- a/example/src/main/java/com/pingxx/example/ChargeExample.java +++ b/example/src/main/java/com/pingxx/example/ChargeExample.java @@ -7,18 +7,12 @@ */ package com.pingxx.example; -import java.util.*; - -import com.pingplusplus.exception.APIConnectionException; -import com.pingplusplus.exception.APIException; -import com.pingplusplus.exception.AuthenticationException; -import com.pingplusplus.exception.ChannelException; -import com.pingplusplus.exception.InvalidRequestException; -import com.pingplusplus.exception.RateLimitException; import com.pingplusplus.exception.PingppException; import com.pingplusplus.model.Charge; import com.pingplusplus.model.ChargeCollection; +import java.util.*; + /** * Charge 对象相关示例 * @@ -84,19 +78,10 @@ public Charge createCharge() { // 传到客户端请先转成字符串 .toString(), 调该方法,会自动转成正确的 JSON 字符串 String chargeString = charge.toString(); System.out.println(chargeString); - } catch (APIConnectionException e) { - e.printStackTrace(); - } catch (ChannelException e) { - e.printStackTrace(); - } catch (RateLimitException e) { - e.printStackTrace(); - } catch (AuthenticationException e) { - e.printStackTrace(); - } catch (APIException e) { - e.printStackTrace(); - } catch (InvalidRequestException e) { + } catch (PingppException e) { e.printStackTrace(); } + return charge; } @@ -159,17 +144,7 @@ public ChargeCollection list() { try { chargeCollection = Charge.list(params); System.out.println(chargeCollection); - } catch (AuthenticationException e) { - e.printStackTrace(); - } catch (InvalidRequestException e) { - e.printStackTrace(); - } catch (APIConnectionException e) { - e.printStackTrace(); - } catch (APIException e) { - e.printStackTrace(); - } catch (ChannelException e) { - e.printStackTrace(); - } catch (RateLimitException e) { + } catch (PingppException e) { e.printStackTrace(); } @@ -292,9 +267,9 @@ private Map alipayExtra() { private Map alipayWapExtra() { Map extra = new HashMap<>(); - // 必须,支付成功的回调地址,在本地测试不要写 localhost ,请写 127.0.0.1。URL 后面不要加自定义参数。 + // 必须,支付成功的回调地址,在本地测试不要写 localhost。URL 后面带参数的情况,可能会被渠道拒绝,建议把参数放到“路径”里,比如:`https://example.com/result/user/12345/order_no/6789`。 extra.put("success_url", "https://example.com/success"); - // 可选,支付取消的回调地址, app_pay 为true时,该字段无效,在本地测试不要写 localhost ,请写 127.0.0.1。URL 后面不要加自定义参数。 + // 可选,支付取消的回调地址, app_pay 为true时,该字段无效,在本地测试不要写 localhost。URL 后面带参数的情况,可能会被渠道拒绝,建议把参数放到“路径”里,比如:`https://example.com/result/user/12345/order_no/6789`。 extra.put("cancel_url", "https://example.com/cancel"); // 可选,2016 年 6 月 16 日之前登录 Ping++ 管理平台填写支付宝手机网站的渠道参数的旧接口商户,需要更新接口时设置此参数值为true,6月16号后接入的新接口商户不需要设置该参数。 @@ -308,7 +283,7 @@ private Map alipayWapExtra() { private Map alipayPcDirectExtra() { Map extra = new HashMap<>(); - // 必须,支付成功的回调地址,在本地测试不要写 localhost ,请写 127.0.0.1。URL 后面不要加自定义参数。 + // 必须,支付成功的回调地址,在本地测试不要写 localhost。URL 后面带参数的情况,可能会被渠道拒绝,建议把参数放到“路径”里,比如:`https://example.com/result/user/12345/order_no/6789`。 extra.put("success_url", "https://example.com/success"); // 可选,是否开启防钓鱼网站的验证参数(如果已申请开通防钓鱼时间戳验证,则此字段必填)。 @@ -398,7 +373,7 @@ private Map bfbExtra() { private Map bfbWapExtra() { Map extra = new HashMap<>(); - // 必须,支付完成的回调地址,在本地测试不要写 localhost ,请写 127.0.0.1。URL 后面不要加自定义参数。 + // 必须,支付完成的回调地址,在本地测试不要写 localhost。URL 后面带参数的情况,可能会被渠道拒绝,建议把参数放到“路径”里,比如:`https://example.com/result/user/12345/order_no/6789`。 extra.put("result_url", "https://example.com/success"); // 必须,是否需要登录百度钱包来进行支付。 @@ -415,7 +390,7 @@ private Map upacpExtra() { private Map upacpWapExtra() { Map extra = new HashMap<>(); - // 必须,支付完成的回调地址,在本地测试不要写 localhost ,请写 127.0.0.1。URL 后面不要加自定义参数。 + // 必须,支付完成的回调地址,在本地测试不要写 localhost。URL 后面带参数的情况,可能会被渠道拒绝,建议把参数放到“路径”里,比如:`https://example.com/result/user/12345/order_no/6789`。 extra.put("result_url", "https://example.com/success"); return extra; @@ -423,7 +398,7 @@ private Map upacpWapExtra() { private Map upacpPcExtra() { Map extra = new HashMap<>(); - // 必须,支付完成的回调地址,在本地测试不要写 localhost ,请写 127.0.0.1。URL 后面不要加自定义参数。 + // 必须,支付完成的回调地址,在本地测试不要写 localhost。URL 后面带参数的情况,可能会被渠道拒绝,建议把参数放到“路径”里,比如:`https://example.com/result/user/12345/order_no/6789`。 extra.put("result_url", "https://example.com/success"); return extra; diff --git a/example/src/main/java/com/pingxx/example/CustomsExample.java b/example/src/main/java/com/pingxx/example/CustomsExample.java index 54e82b2..82c67eb 100644 --- a/example/src/main/java/com/pingxx/example/CustomsExample.java +++ b/example/src/main/java/com/pingxx/example/CustomsExample.java @@ -7,10 +7,7 @@ */ package com.pingxx.example; -import com.pingplusplus.Pingpp; -import com.pingplusplus.exception.*; -import com.pingplusplus.model.Charge; -import com.pingplusplus.model.ChargeCollection; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.model.Customs; import java.util.Date; @@ -75,19 +72,10 @@ public Customs createCustoms() { //发起交易请求 obj = Customs.create(params); System.out.println(obj); - } catch (APIConnectionException e) { - e.printStackTrace(); - } catch (ChannelException e) { - e.printStackTrace(); - } catch (RateLimitException e) { - e.printStackTrace(); - } catch (AuthenticationException e) { - e.printStackTrace(); - } catch (APIException e) { - e.printStackTrace(); - } catch (InvalidRequestException e) { + } catch (PingppException e) { e.printStackTrace(); } + return obj; } diff --git a/example/src/main/java/com/pingxx/example/EventExample.java b/example/src/main/java/com/pingxx/example/EventExample.java index d22baff..ef3e638 100644 --- a/example/src/main/java/com/pingxx/example/EventExample.java +++ b/example/src/main/java/com/pingxx/example/EventExample.java @@ -54,17 +54,7 @@ public void retrieve(String id) { // } else if (obj instanceof Summary) { // System.out.println("webhooks 发送了 Summary"); // } - } catch (AuthenticationException e) { - e.printStackTrace(); - } catch (InvalidRequestException e) { - e.printStackTrace(); - } catch (APIConnectionException e) { - e.printStackTrace(); - } catch (APIException e) { - e.printStackTrace(); - } catch (ChannelException e) { - e.printStackTrace(); - } catch (RateLimitException e) { + } catch (PingppException e) { e.printStackTrace(); } diff --git a/example/src/main/java/com/pingxx/example/IdentificationExample.java b/example/src/main/java/com/pingxx/example/IdentificationExample.java index df758f1..fef7986 100644 --- a/example/src/main/java/com/pingxx/example/IdentificationExample.java +++ b/example/src/main/java/com/pingxx/example/IdentificationExample.java @@ -1,11 +1,8 @@ package com.pingxx.example; -import com.pingplusplus.Pingpp; -import com.pingplusplus.exception.*; -import com.pingplusplus.model.Event; -import com.pingplusplus.model.EventCollection; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.model.Identification; -import java.util.Date; + import java.util.HashMap; import java.util.Map; @@ -55,17 +52,7 @@ public void identifyIdCard() { System.out.println(result.getMessage()); } System.out.println(result); - } catch (AuthenticationException e) { - e.printStackTrace(); - } catch (InvalidRequestException e) { - e.printStackTrace(); - } catch (APIConnectionException e) { - e.printStackTrace(); - } catch (APIException e) { - e.printStackTrace(); - } catch (ChannelException e) { - e.printStackTrace(); - } catch (RateLimitException e) { + } catch (PingppException e) { e.printStackTrace(); } } @@ -93,17 +80,7 @@ public void identifyBankCard() { System.out.println(result.getMessage()); } System.out.println(result); - } catch (AuthenticationException e) { - e.printStackTrace(); - } catch (InvalidRequestException e) { - e.printStackTrace(); - } catch (APIConnectionException e) { - e.printStackTrace(); - } catch (APIException e) { - e.printStackTrace(); - } catch (ChannelException e) { - e.printStackTrace(); - } catch (RateLimitException e) { + } catch (PingppException e) { e.printStackTrace(); } } diff --git a/example/src/main/java/com/pingxx/example/Main.java b/example/src/main/java/com/pingxx/example/Main.java index 21275d3..c38f9f2 100644 --- a/example/src/main/java/com/pingxx/example/Main.java +++ b/example/src/main/java/com/pingxx/example/Main.java @@ -3,6 +3,7 @@ import com.pingplusplus.Pingpp; import java.math.BigInteger; +import java.net.URL; import java.security.SecureRandom; /** @@ -30,6 +31,8 @@ public class Main { // 你生成的私钥路径 private final static String privateKeyFilePath = "res/your_rsa_private_key_pkcs8.pem"; + private final static String verifyKeyFilePath = "pingpp_public_key.pem"; + protected static String projectDir; public static void main(String[] args) throws Exception { @@ -41,43 +44,16 @@ public static void main(String[] args) throws Exception { Pingpp.appId = appId; // 设置私钥路径,用于请求签名 -// Pingpp.privateKeyPath = privateKeyFilePath; + //Pingpp.privateKeyPath = projectDir + privateKeyFilePath; - /** + /* * 或者直接设置私钥内容 - Pingpp.privateKey = "-----BEGIN RSA PRIVATE KEY-----\n" + - "... 私钥内容字符串 ...\n" + - "-----END RSA PRIVATE KEY-----\n"; + * Pingpp.privateKey = "... PKCS8私钥内容字符串 ..."; */ - Pingpp.privateKey = "-----BEGIN PRIVATE KEY-----\n" + - "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDHYyS3FwoESp1h\n" + - "GLYiBhy6k9Ag3lzGCIEvm50IIEkE0Ftc9qq44TWqyl+EHUpTMdcBOcI42JLO5stw\n" + - "FOfCLa3PQStEJ4llIRFEKlsrHh67pvWd5RNaSBrvGlnFY40S+SZmjk2WF/h9dE9R\n" + - "ic79t0YI0alD8dIl9Yu3OaEKo7VonBWFwOYMxjPhtORlq+EUF1XJd//yftQrKWTT\n" + - "d7KaUonWzBCl4VzFop/OyTWYlTuZz3eYJaNpH5VaQ1vDgBAcPIeBvMf7NgBHMKW6\n" + - "LLmFd2LEYQ/6I7hkGTjysSzWEpO8bPWT6OEsJ2R2kFGOrSkr+G2MDcJ7ykXYAmz5\n" + - "+A3plS6ZAgMBAAECggEAVrgwR9GlcahiOtDcpn+yDxQq+aC9CQS561LrQZWJLKbS\n" + - "leRS7IZHKTlLwdJbeUO8F7RfXQoVEBghc2YkRrhHWFUn1ES95VY0hElHzcET7Nn5\n" + - "CeuQNzwVOtljIg7iVNY4dXJ/HEDguu/Tb8tYU9FajItj60FJ/WiGk/JksJPzWsOC\n" + - "VPVniy9fTbTLy1e+dCpCI6OXirtm7hvbodRNDjree0wSEzm7vL0wVzEZFo6kX+AB\n" + - "GUwaoO7pPyH+hgyI5Iuhc65NHsHzTJpf8yNFl9QGhkxvm2Ff2oEtDt1idOTBrHB6\n" + - "tg+ti9Ctb2+2yzBnk14hsSYJnKitR7wM6ZCFPX4eYQKBgQD+JAREeFkodec/SC+G\n" + - "X+4Q4Y68uMPkfUPrMKXM4cyY5wgXk64RBvRVxIxX7x6Y3tIKn9v8tWAprbsyVr15\n" + - "eb4RcAFEVwjuoZixhd9sIPsRhfdNolKn/fSPIsHL4ywcJMSIt7KVKHuQeqBNHy0o\n" + - "0PxQjNej1ozsmrAWqV55cbKHswKBgQDI2JQRTPIEC/2y6LdmBVhGJW9OKWTYdVNj\n" + - "q7rX+Yw4uxOtfd5hBqpvgZEklKEk72aazFdEcERlAm9SqoX09qk6zK/wcq4Xn5Q/\n" + - "qy8ecmjuyf2AK9X+HUdMerMVxhK9RpeevKYP/RO2F/wIN64anlQVYygVkXXgdOvW\n" + - "hBE4YABKgwKBgDRtmbPGYB5ItHwJmERQZfx1i8zDESaB8RED6DBsJJkmkDTM8ovw\n" + - "s1c+RPWfDuDalto6QFfR0xTGEmhAHLaCtwNB6AEBM4aHL8jvpTfZVfI3gN0zL3oY\n" + - "mestcG1vYBouO504yE6dG2Ci6479b4OMGYFEjPfvuwLUpp8GMcc7/WihAoGANCp8\n" + - "mtm/ammq5VMof2kX+nAyrrx1ovsmQ5cRGpOIZhvBCqjMn6rZjci7aCLqj+tWXRKC\n" + - "ABagzROK0o/T50JBxjHv6KYArcYW/Up7HI9ezdbM7wNzu2LjZ+veo+MkbuDs9J/P\n" + - "CgwTmJI2NfQwVl2VPVDZ0nBLi5cSwk7fIiNdL/0CgYEAtECmC1QDs53Di2MIsa/F\n" + - "e4sWfJGSDqEWqhcA/aPwf1skM6VJJXBBMV1qFtwgO1AlLnu9dQYra6ylsUoubVYI\n" + - "XM9XK7EMhbqi57+Q75jHFTc0DnzOTyho5Gp4Ddi8dztmZGNWdWTGdeMqh+svqMXk\n" + - "D6VdJeddyGu/Zlgj7Wk6whU=\n" + - "-----END PRIVATE KEY-----\n"; + Pingpp.privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDHYyS3FwoESp1hGLYiBhy6k9Ag3lzGCIEvm50IIEkE0Ftc9qq44TWqyl+EHUpTMdcBOcI42JLO5stwFOfCLa3PQStEJ4llIRFEKlsrHh67pvWd5RNaSBrvGlnFY40S+SZmjk2WF/h9dE9Ric79t0YI0alD8dIl9Yu3OaEKo7VonBWFwOYMxjPhtORlq+EUF1XJd//yftQrKWTTd7KaUonWzBCl4VzFop/OyTWYlTuZz3eYJaNpH5VaQ1vDgBAcPIeBvMf7NgBHMKW6LLmFd2LEYQ/6I7hkGTjysSzWEpO8bPWT6OEsJ2R2kFGOrSkr+G2MDcJ7ykXYAmz5+A3plS6ZAgMBAAECggEAVrgwR9GlcahiOtDcpn+yDxQq+aC9CQS561LrQZWJLKbSleRS7IZHKTlLwdJbeUO8F7RfXQoVEBghc2YkRrhHWFUn1ES95VY0hElHzcET7Nn5CeuQNzwVOtljIg7iVNY4dXJ/HEDguu/Tb8tYU9FajItj60FJ/WiGk/JksJPzWsOCVPVniy9fTbTLy1e+dCpCI6OXirtm7hvbodRNDjree0wSEzm7vL0wVzEZFo6kX+ABGUwaoO7pPyH+hgyI5Iuhc65NHsHzTJpf8yNFl9QGhkxvm2Ff2oEtDt1idOTBrHB6tg+ti9Ctb2+2yzBnk14hsSYJnKitR7wM6ZCFPX4eYQKBgQD+JAREeFkodec/SC+GX+4Q4Y68uMPkfUPrMKXM4cyY5wgXk64RBvRVxIxX7x6Y3tIKn9v8tWAprbsyVr15eb4RcAFEVwjuoZixhd9sIPsRhfdNolKn/fSPIsHL4ywcJMSIt7KVKHuQeqBNHy0o0PxQjNej1ozsmrAWqV55cbKHswKBgQDI2JQRTPIEC/2y6LdmBVhGJW9OKWTYdVNjq7rX+Yw4uxOtfd5hBqpvgZEklKEk72aazFdEcERlAm9SqoX09qk6zK/wcq4Xn5Q/qy8ecmjuyf2AK9X+HUdMerMVxhK9RpeevKYP/RO2F/wIN64anlQVYygVkXXgdOvWhBE4YABKgwKBgDRtmbPGYB5ItHwJmERQZfx1i8zDESaB8RED6DBsJJkmkDTM8ovws1c+RPWfDuDalto6QFfR0xTGEmhAHLaCtwNB6AEBM4aHL8jvpTfZVfI3gN0zL3oYmestcG1vYBouO504yE6dG2Ci6479b4OMGYFEjPfvuwLUpp8GMcc7/WihAoGANCp8mtm/ammq5VMof2kX+nAyrrx1ovsmQ5cRGpOIZhvBCqjMn6rZjci7aCLqj+tWXRKCABagzROK0o/T50JBxjHv6KYArcYW/Up7HI9ezdbM7wNzu2LjZ+veo+MkbuDs9J/PCgwTmJI2NfQwVl2VPVDZ0nBLi5cSwk7fIiNdL/0CgYEAtECmC1QDs53Di2MIsa/Fe4sWfJGSDqEWqhcA/aPwf1skM6VJJXBBMV1qFtwgO1AlLnu9dQYra6ylsUoubVYIXM9XK7EMhbqi57+Q75jHFTc0DnzOTyho5Gp4Ddi8dztmZGNWdWTGdeMqh+svqMXkD6VdJeddyGu/Zlgj7Wk6whU="; + //(可选,推荐设置)设置响应验证公钥,用于验证响应是否真实有效;不设置或设置密钥错误将不验签 + Pingpp.setVerifyPublicKeyPath(getResPath(verifyKeyFilePath)); // Charge 示例 ChargeExample.runDemos(appId); @@ -110,16 +86,27 @@ public static void main(String[] args) throws Exception { // 报关 // 请使用 live key 调用该接口 CustomsExample.runDemos(appId); + + //签约代扣示例 + AgreementExample.runDemos(appId); } - private static SecureRandom random = new SecureRandom(); + private static final SecureRandom random = new SecureRandom(); public static String randomString(int length) { String str = new BigInteger(130, random).toString(32); return str.substring(0, length); } + public static String getResPath(String subPath) { + if (subPath == null) { + throw new NullPointerException(); + } + URL resource = Thread.currentThread().getContextClassLoader().getResource(subPath); + return resource != null ? resource.getPath() : subPath; + } + public static int currentTimeSeconds() { - return (int)(System.currentTimeMillis() / 1000); + return (int) (System.currentTimeMillis() / 1000); } } diff --git a/example/src/main/java/com/pingxx/example/RedEnvelopeExample.java b/example/src/main/java/com/pingxx/example/RedEnvelopeExample.java index f49ac6e..e741f1a 100644 --- a/example/src/main/java/com/pingxx/example/RedEnvelopeExample.java +++ b/example/src/main/java/com/pingxx/example/RedEnvelopeExample.java @@ -80,17 +80,7 @@ public RedEnvelope create() { try { red = RedEnvelope.create(redenvelope); System.out.println(red); - } catch (AuthenticationException e) { - e.printStackTrace(); - } catch (InvalidRequestException e) { - e.printStackTrace(); - } catch (APIConnectionException e) { - e.printStackTrace(); - } catch (APIException e) { - e.printStackTrace(); - } catch (ChannelException e) { - e.printStackTrace(); - } catch (RateLimitException e) { + } catch (PingppException e) { e.printStackTrace(); } return red; @@ -108,17 +98,7 @@ public void retrieve(String id) { try { RedEnvelope redEnvelope = RedEnvelope.retrieve(id); System.out.println(redEnvelope); - } catch (AuthenticationException e) { - e.printStackTrace(); - } catch (InvalidRequestException e) { - e.printStackTrace(); - } catch (APIConnectionException e) { - e.printStackTrace(); - } catch (APIException e) { - e.printStackTrace(); - } catch (ChannelException e) { - e.printStackTrace(); - } catch (RateLimitException e) { + } catch (PingppException e) { e.printStackTrace(); } } @@ -135,17 +115,7 @@ public void list() { try { redEnvelopeCollection = RedEnvelope.list(chargeParams); System.out.println(redEnvelopeCollection); - } catch (AuthenticationException e) { - e.printStackTrace(); - } catch (InvalidRequestException e) { - e.printStackTrace(); - } catch (APIConnectionException e) { - e.printStackTrace(); - } catch (APIException e) { - e.printStackTrace(); - } catch (ChannelException e) { - e.printStackTrace(); - } catch (RateLimitException e) { + } catch (PingppException e) { e.printStackTrace(); } diff --git a/example/src/main/java/com/pingxx/example/RefundExample.java b/example/src/main/java/com/pingxx/example/RefundExample.java index e6cb351..0c15f5b 100644 --- a/example/src/main/java/com/pingxx/example/RefundExample.java +++ b/example/src/main/java/com/pingxx/example/RefundExample.java @@ -70,17 +70,7 @@ public Refund refund(Integer amount) { try { refund = Refund.create(this.charge, params); System.out.println(refund); - } catch (AuthenticationException e) { - e.printStackTrace(); - } catch (InvalidRequestException e) { - e.printStackTrace(); - } catch (APIConnectionException e) { - e.printStackTrace(); - } catch (APIException e) { - e.printStackTrace(); - } catch (ChannelException e) { - e.printStackTrace(); - } catch (RateLimitException e) { + } catch (PingppException e) { e.printStackTrace(); } return refund; @@ -101,17 +91,7 @@ public void retrieve(String id) { try { Refund refund = Refund.retrieve(this.charge, id); System.out.println(refund); - } catch (AuthenticationException e) { - e.printStackTrace(); - } catch (InvalidRequestException e) { - e.printStackTrace(); - } catch (APIConnectionException e) { - e.printStackTrace(); - } catch (APIException e) { - e.printStackTrace(); - } catch (ChannelException e) { - e.printStackTrace(); - } catch (RateLimitException e) { + } catch (PingppException e) { e.printStackTrace(); } } @@ -132,17 +112,7 @@ public void all() { try { ChargeRefundCollection refunds = Refund.list(this.charge, refundParams); System.out.println(refunds); - } catch (AuthenticationException e) { - e.printStackTrace(); - } catch (InvalidRequestException e) { - e.printStackTrace(); - } catch (APIConnectionException e) { - e.printStackTrace(); - } catch (APIException e) { - e.printStackTrace(); - } catch (ChannelException e) { - e.printStackTrace(); - } catch (RateLimitException e) { + } catch (PingppException e) { e.printStackTrace(); } } diff --git a/example/src/main/java/com/pingxx/example/TransferExample.java b/example/src/main/java/com/pingxx/example/TransferExample.java index 2e51f3d..ba01da1 100644 --- a/example/src/main/java/com/pingxx/example/TransferExample.java +++ b/example/src/main/java/com/pingxx/example/TransferExample.java @@ -104,17 +104,7 @@ public Transfer create() { try { transfer = Transfer.create(transferMap); System.out.println(transfer); - } catch (AuthenticationException e) { - e.printStackTrace(); - } catch (InvalidRequestException e) { - e.printStackTrace(); - } catch (APIConnectionException e) { - e.printStackTrace(); - } catch (APIException e) { - e.printStackTrace(); - } catch (ChannelException e) { - e.printStackTrace(); - } catch (RateLimitException e) { + } catch (PingppException e) { e.printStackTrace(); } return transfer; @@ -132,17 +122,7 @@ public void retrieve(String id) { try { Transfer transfer = Transfer.retrieve(id, param); System.out.println(transfer); - } catch (AuthenticationException e) { - e.printStackTrace(); - } catch (InvalidRequestException e) { - e.printStackTrace(); - } catch (APIConnectionException e) { - e.printStackTrace(); - } catch (APIException e) { - e.printStackTrace(); - } catch (ChannelException e) { - e.printStackTrace(); - } catch (RateLimitException e) { + } catch (PingppException e) { e.printStackTrace(); } @@ -161,17 +141,7 @@ public void list() { try { TransferCollection transferCollection = Transfer.list(param); System.out.println(transferCollection); - } catch (AuthenticationException e) { - e.printStackTrace(); - } catch (InvalidRequestException e) { - e.printStackTrace(); - } catch (APIConnectionException e) { - e.printStackTrace(); - } catch (APIException e) { - e.printStackTrace(); - } catch (ChannelException e) { - e.printStackTrace(); - } catch (RateLimitException e) { + } catch (PingppException e) { e.printStackTrace(); } } diff --git a/example/src/main/java/com/pingxx/example/WebhooksVerifyExample.java b/example/src/main/java/com/pingxx/example/WebhooksVerifyExample.java index 31af3fe..891e1ed 100644 --- a/example/src/main/java/com/pingxx/example/WebhooksVerifyExample.java +++ b/example/src/main/java/com/pingxx/example/WebhooksVerifyExample.java @@ -7,37 +7,25 @@ */ package com.pingxx.example; -import java.io.*; -import java.security.InvalidKeyException; -import java.security.KeyFactory; -import java.security.NoSuchAlgorithmException; -import java.security.PublicKey; -import java.security.Signature; -import java.security.SignatureException; -import java.security.spec.X509EncodedKeySpec; - -import org.apache.commons.codec.binary.Base64; +import com.pingplusplus.Pingpp; +import com.pingplusplus.net.APIResource; +import com.pingplusplus.util.PingppSignature; /** - * Created by sunkai on 15/5/19. webhooks 验证签名示例 - * + * webhooks 验证签名示例 + *

* 该实例演示如何对 Ping++ webhooks 通知进行验证。 * 验证是为了让开发者确认该通知来自 Ping++ ,防止恶意伪造通知。用户如果有别的验证机制,可以不进行验证签名。 - * + *

* 验证签名需要 签名、公钥、验证信息,该实例采用文件存储方式进行演示。 * 实际项目中,需要用户从异步通知的 HTTP header 中读取签名,从 HTTP body 中读取验证信息。公钥的存储方式也需要用户自行设定。 - * - * 该实例仅供演示如何验证签名,请务必不要直接 copy 到实际项目中使用。 - * + *

+ * 该实例仅供演示如何验证签名,请务必不要直接 copy 到实际项目中使用。 */ public class WebhooksVerifyExample { - - private static String pubKeyPath = "res/pingpp_public_key.pem"; - private static String eventPath = "res/webhooks_raw_post_data.json"; - private static String signPath = "res/signature.txt"; - /** * 验证 webhooks 签名,仅供参考 + * * @param args * @throws Exception */ @@ -46,78 +34,17 @@ public static void main(String[] args) throws Exception { } public static void runDemos() throws Exception { + String verifyPublicKey = Pingpp.verifyPublicKey; // 该数据请从 request 中获取原始 POST 请求数据, 以下仅作为示例 - String webhooksRawPostData = getStringFromFile(eventPath); + String webhooksRawPostData = "{\"id\":\"evt_400240816160755820469807\",\"created\":1723795675,\"livemode\":false,\"type\":\"charge.succeeded\",\"data\":{\"object\":{\"id\":\"ch_100240816592822384640019\",\"object\":\"charge\",\"created\":1723795492,\"livemode\":false,\"paid\":true,\"refunded\":false,\"reversed\":false,\"app\":\"app_eTC8yLuj9GSSL0yv\",\"channel\":\"wx_pub\",\"order_no\":\"1723795491985uqclqj7\",\"client_ip\":\"127.0.0.1\",\"amount\":100,\"amount_settle\":100,\"currency\":\"cny\",\"subject\":\"Your Subject\",\"body\":\"Your Body\",\"extra\":{\"open_id\":\"o7xEMsySBFG3MVHI-9VsAJX-j50W\",\"limit_pay\":\"no_credit\",\"bank_type\":\"your bank type\"},\"time_paid\":1723795675,\"time_expire\":1723802692,\"time_settle\":null,\"transaction_no\":\"1290362656202408163243920572\",\"refunds\":{\"object\":\"list\",\"url\":\"/v1/charges/ch_100240816592822384640019/refunds\",\"has_more\":false,\"data\":[]},\"amount_refunded\":0,\"failure_code\":null,\"failure_msg\":null,\"metadata\":{},\"credential\":{},\"description\":null}},\"object\":\"event\",\"request\":\"iar_mrrvj9ivPOaHPOi9aLPWPqvT\",\"pending_webhooks\":0}"; System.out.println("------- POST 原始数据 -------"); System.out.println(webhooksRawPostData); // 签名数据请从 request 的 header 中获取, key 为 X-Pingplusplus-Signature (请忽略大小写, 建议自己做格式化) - String signature = getStringFromFile(signPath); + String signature = "Ju5ItDM9Hblkm6Pbb/r3G9iM58oCtAUJlJDrOud0E23TUtGBxKYJSMcuTFbPgLGLKy5QHYovJJojjMjM1CMGCp82F+SOY1U2zuwzkAk0lVqhfQk+CWGpcyUzOtLOOblKidyuIb+axZDTIg4kK/JoOR2xMH3+cJD9BN6rtfzEDbHqyZIfv6n3y/LdZNhsXfQq+qoIRuLdHrFexk1USgk7SXFvH1pCOIt8o2+dryp/ixlNj1vGq57eE8sbEzPo72vxLtzPazlgR+67cS1bUxjzObh7YFbSSTJxkHDEIi3ipTpoiI7Kc5ng7EzsWgp3cAl7on5HztfFLfT8+Bjpdvtr6w=="; System.out.println("------- 签名 -------"); System.out.println(signature); - boolean result = verifyData(webhooksRawPostData, signature, getPubKey()); + boolean result = PingppSignature.verify(signature, webhooksRawPostData, verifyPublicKey, APIResource.CHARSET.name()); System.out.println("验签结果:" + (result ? "通过" : "失败")); } - - /** - * 读取文件, 部署 web 程序的时候, 签名和验签内容需要从 request 中获得 - * @param filePath - * @return - * @throws Exception - */ - public static String getStringFromFile(String filePath) throws Exception { - FileInputStream in = new FileInputStream(Main.projectDir + filePath); - InputStreamReader inReader = new InputStreamReader(in, "UTF-8"); - BufferedReader bf = new BufferedReader(inReader); - StringBuilder sb = new StringBuilder(); - String line; - do { - line = bf.readLine(); - if (line != null) { - if (sb.length() != 0) { - sb.append("\n"); - } - sb.append(line); - } - } while (line != null); - - return sb.toString(); - } - - /** - * 获得公钥 - * @return - * @throws Exception - */ - public static PublicKey getPubKey() throws Exception { - String pubKeyString = getStringFromFile(pubKeyPath); - pubKeyString = pubKeyString.replaceAll("(-+BEGIN PUBLIC KEY-+\\r?\\n|-+END PUBLIC KEY-+\\r?\\n?)", ""); - byte[] keyBytes = Base64.decodeBase64(pubKeyString.getBytes("UTF-8")); - - // generate public key - X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes); - KeyFactory keyFactory = KeyFactory.getInstance("RSA"); - - return keyFactory.generatePublic(spec); - } - - /** - * 验证签名 - * @param dataString - * @param signatureString - * @param publicKey - * @return - * @throws NoSuchAlgorithmException - * @throws InvalidKeyException - * @throws SignatureException - */ - public static boolean verifyData(String dataString, String signatureString, PublicKey publicKey) - throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, UnsupportedEncodingException { - byte[] signatureBytes = Base64.decodeBase64(signatureString); - Signature signature = Signature.getInstance("SHA256withRSA"); - signature.initVerify(publicKey); - signature.update(dataString.getBytes("UTF-8")); - return signature.verify(signatureBytes); - } - } diff --git a/example/src/main/resources/pingpp_public_key.pem b/example/src/main/resources/pingpp_public_key.pem new file mode 100644 index 0000000..c0f5d19 --- /dev/null +++ b/example/src/main/resources/pingpp_public_key.pem @@ -0,0 +1,6 @@ +-----BEGIN PUBLIC KEY----- +MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDCJqcTaGUab0vBZSkGNE1AaGiY +RcbPd7QdCQuM8EpbEpEQGr9/NjbJHCLd3eaUmYM0vy34Wux+VHnw4ckiDfBuW6/Z +fIo5GBRCI/nZLOnWcOX98ZAXYnn/VVJXuBtHtHL8DWEbYmMp+Gl4k1rFSuQKk52k +YLXeFJZJV8CBV5jA+wIDAQAB +-----END PUBLIC KEY----- \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 7b83409..68566cb 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-3.1-all.zip +distributionUrl=https\://mirrors.aliyun.com/macports/distfiles/gradle/gradle-8.6-all.zip diff --git a/libs/pingpp-java-2.3.14.jar b/libs/pingpp-java-2.3.14.jar deleted file mode 100644 index b15b13e..0000000 Binary files a/libs/pingpp-java-2.3.14.jar and /dev/null differ diff --git a/libs/pingpp-sdk-2.5.3.jar b/libs/pingpp-sdk-2.5.3.jar new file mode 100644 index 0000000..72f00e6 Binary files /dev/null and b/libs/pingpp-sdk-2.5.3.jar differ diff --git a/pingpp-sdk/build.gradle b/pingpp-sdk/build.gradle new file mode 100644 index 0000000..cd44911 --- /dev/null +++ b/pingpp-sdk/build.gradle @@ -0,0 +1,52 @@ +buildscript { + repositories { + maven { + url 'https://maven.aliyun.com/repository/central' + } + mavenLocal() + mavenCentral() + } +} + +plugins { + id "maven-publish" + id "java" +} + +sourceCompatibility = 1.8 +targetCompatibility = 1.8 + +configurations { + compileOnly { + extendsFrom annotationProcessor + } + testCompileOnly { + extendsFrom testAnnotationProcessor + } +} + +dependencies { + implementation 'com.google.code.gson:gson:2.10' + implementation 'commons-codec:commons-codec:1.17.0' + testImplementation 'junit:junit:4.13.2' +} + +java { + withSourcesJar() +} + +publishing { + publications { + mavenJava(MavenPublication) { + from components.java + versionMapping { + usage('java-api') { + fromResolutionOf('runtimeClasspath') + } + usage('java-runtime') { + fromResolutionResult() + } + } + } + } +} \ No newline at end of file diff --git a/pingpp-sdk/pom.xml b/pingpp-sdk/pom.xml new file mode 100644 index 0000000..ab60a11 --- /dev/null +++ b/pingpp-sdk/pom.xml @@ -0,0 +1,123 @@ + + + 4.0.0 + + com.pingxx + pingpp-java + 2.5.6 + jar + + pingpp-java + A Java SDK for Ping++ Payment API. + https://github.com/PingPlusPlus/pingpp-java + + + Apache License 2.0 + http://www.apache.org/licenses/LICENSE-2.0 + repo + + + + + scm:git:https://github.com/PingPlusPlus/pingpp-java.git + + + + afon + Afon + xufeng.weng@pingxx.com + + + + + 1.8 + 1.8 + + + + + com.google.code.gson + gson + 2.10.1 + compile + + + commons-codec + commons-codec + 1.17.0 + compile + + + junit + junit + 4.13.2 + test + + + + + + + + + org.apache.maven.plugins + maven-source-plugin + 3.3.1 + + + attach-sources + + jar-no-fork + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + 3.11.2 + + + attach-javadocs + + jar + + + + + + org.apache.maven.plugins + maven-gpg-plugin + 3.2.8 + + + sign-artifacts + verify + + sign + + + ${gpg.keyname} + ${gpg.passphrase} + + --pinentry-mode + loopback + + + + + + + org.sonatype.central + central-publishing-maven-plugin + 0.8.0 + true + + central + true + + + + + diff --git a/pingpp-sdk/src/main/java/com/pingplusplus/Pingpp.java b/pingpp-sdk/src/main/java/com/pingplusplus/Pingpp.java new file mode 100644 index 0000000..444f8b4 --- /dev/null +++ b/pingpp-sdk/src/main/java/com/pingplusplus/Pingpp.java @@ -0,0 +1,161 @@ +package com.pingplusplus; + +import com.pingplusplus.net.APIResource; +import com.pingplusplus.util.StreamUtils; + +import java.io.FileInputStream; +import java.io.IOException; + +/** + * Ping++ Base class + */ +public abstract class Pingpp { + /** + * Ping++ API BASE URL + */ + public static final String LIVE_API_BASE = "https://api.pingxx.com"; + /** + * version + */ + public static final String VERSION = "2.5.6"; + /** + * api key + */ + public static volatile String apiKey; + + public static volatile String appId; + + public static String acceptLanguage = "zh-CN"; + + private static volatile String apiBase = LIVE_API_BASE; + + public static volatile String privateKey; + public static volatile String verifyPublicKey; + public static volatile String privateKeyPath; + + public static Boolean DEBUG = false; + + public static final int DEFAULT_CONNECT_TIMEOUT = 30 * 1000; + public static final int DEFAULT_READ_TIMEOUT = 80 * 1000; + + private static volatile int connectTimeout = -1; + private static volatile int readTimeout = -1; + + private static volatile int maxNetworkRetries = 1; + + /** + * (FOR TESTING ONLY) + * If you'd like your API requests to hit your own (mocked) server, + * you can set this up here by overriding the base api URL. + * @param overriddenApiBase API 地址 + */ + public static void overrideApiBase(final String overriddenApiBase) { + apiBase = overriddenApiBase; + } + + /** + * get api url + * + * @return String api url + */ + public static String getApiBase() { + return apiBase; + } + + /** + * set api url + * + * @param apiBase apiBase API 地址 + */ + public static void setApiBase(String apiBase) { + Pingpp.apiBase = apiBase; + } + + /** + * 网络连接超时时间 + * + * @return timeout value in milliseconds + */ + public static int getConnectTimeout() { + if (connectTimeout == -1) { + return DEFAULT_CONNECT_TIMEOUT; + } + return connectTimeout; + } + + /** + * 设置网络连接超时时间 (毫秒) + * + * @param timeout timeout value in milliseconds + */ + public static void setConnectTimeout(final int timeout) { + connectTimeout = timeout; + } + + /** + * 数据读取超时时间 + * + * @return timeout value in milliseconds + */ + public static int getReadTimeout() { + if (readTimeout == -1) { + return DEFAULT_READ_TIMEOUT; + } + return readTimeout; + } + + /** + * 设置数据读取超时时间 (毫秒) + * 不同接口的耗时时间不一样,部分接口的耗时可能比较长。 + * + * @param timeout timeout value in milliseconds + */ + public static void setReadTimeout(final int timeout) { + readTimeout = timeout; + } + + /** + * 连接失败时的最大重试次数 + * + * @return the maximum number of times requests will be retried + */ + public static int getMaxNetworkRetries() { + return maxNetworkRetries; + } + + /** + * 设置连接失败时的最大重试次数 + * + * @param numRetries the maximum number of times requests will be retried + */ + public static void setMaxNetworkRetries(final int numRetries) { + maxNetworkRetries = numRetries; + } + + public static String getAcceptLanguage() { + return acceptLanguage; + } + + public static void setAcceptLanguage(String acceptLanguage) { + Pingpp.acceptLanguage = acceptLanguage; + } + + public static void setVerifyPublicKeyPath(String keyFilePath) throws IOException { + Pingpp.verifyPublicKey = readKeyFromFilePath(keyFilePath); + } + + /** + * 读取密钥文件 + * + * @param filePath 文件路径 + * @return 密钥内容 + * @throws IOException IO 异常 + */ + private static String readKeyFromFilePath(String filePath) throws IOException { + FileInputStream inputStream = new FileInputStream(filePath); + String privateKey = StreamUtils.readToEnd(inputStream, APIResource.CHARSET); + inputStream.close(); + + return privateKey; + } +} diff --git a/pingpp-sdk/src/main/java/com/pingplusplus/exception/APIConnectionException.java b/pingpp-sdk/src/main/java/com/pingplusplus/exception/APIConnectionException.java new file mode 100644 index 0000000..556945e --- /dev/null +++ b/pingpp-sdk/src/main/java/com/pingplusplus/exception/APIConnectionException.java @@ -0,0 +1,15 @@ +package com.pingplusplus.exception; + +public class APIConnectionException extends PingppException { + + private static final long serialVersionUID = 2L; + + public APIConnectionException(String message) { + super(message); + } + + public APIConnectionException(String message, Throwable e) { + super(message, e); + } + +} diff --git a/pingpp-sdk/src/main/java/com/pingplusplus/exception/APIException.java b/pingpp-sdk/src/main/java/com/pingplusplus/exception/APIException.java new file mode 100644 index 0000000..1c5095d --- /dev/null +++ b/pingpp-sdk/src/main/java/com/pingplusplus/exception/APIException.java @@ -0,0 +1,10 @@ +package com.pingplusplus.exception; + +public class APIException extends PingppException { + + private static final long serialVersionUID = 2L; + + public APIException(String message, String type, String code, int statusCode, Throwable e) { + super(message, type, code, statusCode, e); + } +} diff --git a/pingpp-sdk/src/main/java/com/pingplusplus/exception/AuthenticationException.java b/pingpp-sdk/src/main/java/com/pingplusplus/exception/AuthenticationException.java new file mode 100644 index 0000000..42681d0 --- /dev/null +++ b/pingpp-sdk/src/main/java/com/pingplusplus/exception/AuthenticationException.java @@ -0,0 +1,16 @@ +package com.pingplusplus.exception; + +public class AuthenticationException extends PingppException { + + + public AuthenticationException(String message) { + super(message); + } + + public AuthenticationException(String message, String type, String code, int statusCode, Throwable e) { + super(message, type, code, statusCode, e); + } + + private static final long serialVersionUID = 2L; + +} diff --git a/pingpp-sdk/src/main/java/com/pingplusplus/exception/ChannelException.java b/pingpp-sdk/src/main/java/com/pingplusplus/exception/ChannelException.java new file mode 100644 index 0000000..a820f85 --- /dev/null +++ b/pingpp-sdk/src/main/java/com/pingplusplus/exception/ChannelException.java @@ -0,0 +1,23 @@ +package com.pingplusplus.exception; + +public class ChannelException extends PingppException { + + private static final long serialVersionUID = 2L; + + private final String param; + + public ChannelException(String message, String type, String code, int statusCode, Throwable e) { + super(message, type, code, statusCode, e); + this.param = null; + } + + public ChannelException(String message, String type, String code, String param, int statusCode, Throwable e) { + super(message, type, code, statusCode, e); + this.param = param; + } + + public String getParam() { + return param; + } + +} diff --git a/pingpp-sdk/src/main/java/com/pingplusplus/exception/InvalidRequestException.java b/pingpp-sdk/src/main/java/com/pingplusplus/exception/InvalidRequestException.java new file mode 100644 index 0000000..da74498 --- /dev/null +++ b/pingpp-sdk/src/main/java/com/pingplusplus/exception/InvalidRequestException.java @@ -0,0 +1,17 @@ +package com.pingplusplus.exception; + +public class InvalidRequestException extends PingppException { + + private static final long serialVersionUID = 2L; + + private final String param; + + public InvalidRequestException(String message, String type, String code, String param, int statusCode, Throwable e) { + super(message, type, code, statusCode, e); + this.param = param; + } + + public String getParam() { + return param; + } +} diff --git a/pingpp-sdk/src/main/java/com/pingplusplus/exception/PingppException.java b/pingpp-sdk/src/main/java/com/pingplusplus/exception/PingppException.java new file mode 100644 index 0000000..129a5cc --- /dev/null +++ b/pingpp-sdk/src/main/java/com/pingplusplus/exception/PingppException.java @@ -0,0 +1,52 @@ +package com.pingplusplus.exception; + +public abstract class PingppException extends Exception { + private String type; + + private String code; + + private int statusCode; + + public PingppException(String message) { + super(message, null); + } + + public PingppException(String message, Throwable e) { + super(message, e); + } + + public PingppException(String message, String type, String code, int statusCode, Throwable e) { + super(message, e); + this.type = type; + this.code = code; + this.statusCode = statusCode; + } + + private static final long serialVersionUID = 2L; + + public String getType() { + return type; + } + + public String getCode() { + return code; + } + + public int getStatusCode() { + return statusCode; + } + + @Override + public String toString() { + StringBuffer sb = new StringBuffer(); + sb.append(super.toString()); + if (type != null) { + sb.append("; type: ").append(type); + } + if (code != null) { + sb.append("; code: ").append(code); + } + + return sb.toString(); + } +} diff --git a/pingpp-sdk/src/main/java/com/pingplusplus/exception/RateLimitException.java b/pingpp-sdk/src/main/java/com/pingplusplus/exception/RateLimitException.java new file mode 100644 index 0000000..080bca4 --- /dev/null +++ b/pingpp-sdk/src/main/java/com/pingplusplus/exception/RateLimitException.java @@ -0,0 +1,14 @@ +package com.pingplusplus.exception; + +public class RateLimitException extends PingppException { + + private static final long serialVersionUID = 2L; + + public RateLimitException(String message, Throwable e) { + super(message, e); + } + + public RateLimitException(String message, String type, String code, int statusCode, Throwable e) { + super(message, type, code, statusCode, e); + } +} diff --git a/src/main/java/com/pingplusplus/model/Agreement.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/Agreement.java similarity index 53% rename from src/main/java/com/pingplusplus/model/Agreement.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/Agreement.java index ac43ad5..ae64880 100644 --- a/src/main/java/com/pingplusplus/model/Agreement.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/Agreement.java @@ -1,7 +1,8 @@ package com.pingplusplus.model; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.net.APIResource; +import com.pingplusplus.net.RequestOptions; import java.util.HashMap; import java.util.Map; @@ -160,15 +161,9 @@ public void setMetadata(Map metadata) { * * @param params 签约参数 * @return Agreement - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException + * @throws PingppException */ - public static Agreement create(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { + public static Agreement create(Map params) throws PingppException { return create(params, null); } @@ -176,19 +171,13 @@ public static Agreement create(Map params) * 创建签约(agreement) * * @param params 签约参数 - * @param apiKey Ping++ APiKey - * @return Transfer - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @param options the specific options + * @return Agreement + * @throws PingppException */ - public static Agreement create(Map params, String apiKey) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(RequestMethod.POST, classURL(Agreement.class), apiKey, params, Agreement.class); + public static Agreement create(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(RequestMethod.POST, classURL(Agreement.class), params, Agreement.class, options); } /** @@ -196,35 +185,24 @@ public static Agreement create(Map params, String apiKey) * * @param id id * @return Agreement - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException + * @throws PingppException */ - public static Agreement retrieve(String id) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { + public static Agreement retrieve(String id) throws PingppException { return retrieve(id, null); } + + /** * 查询签约(agreement) * * @param id id - * @param apiKey Ping++ ApiKey + * @param options the specific options * @return Agreement - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static Agreement retrieve(String id, String apiKey) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return request(RequestMethod.GET, instanceURL(Agreement.class, id), apiKey, null, Agreement.class); + public static Agreement retrieve(String id, RequestOptions options) throws PingppException { + return APIResource.request(RequestMethod.GET, instanceURL(Agreement.class, id), null, Agreement.class, options); } /** @@ -232,35 +210,24 @@ public static Agreement retrieve(String id, String apiKey) throws Authentication * * @param params 分页参数等 * @return AgreementCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException + * @throws PingppException */ public static AgreementCollection list(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { + throws PingppException { return list(params, null); } /** * 查询签约列表 * - * @param apiKey Ping++ APiKey * @param params 分页参数等 + * @param options the specific options * @return AgreementCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static AgreementCollection list(Map params, String apiKey) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(RequestMethod.GET, classURL(Agreement.class), apiKey, params, AgreementCollection.class); + public static AgreementCollection list(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(RequestMethod.GET, classURL(Agreement.class), params, AgreementCollection.class, options); } /** @@ -268,16 +235,9 @@ public static AgreementCollection list(Map params, String apiKey * * @param id id * @return Agreement - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static Agreement cancel(String id) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { + public static Agreement cancel(String id) throws PingppException { return cancel(id, null); } @@ -285,20 +245,36 @@ public static Agreement cancel(String id) throws AuthenticationException, * 解除签约(agreement) * * @param id id - * @param apiKey Ping++ ApiKey + * @param options the specific options * @return Agreement - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static Agreement cancel(String id, String apiKey) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { + public static Agreement cancel(String id, RequestOptions options) throws PingppException { Map params = new HashMap(); params.put("status", "canceled"); - return request(RequestMethod.PUT, instanceURL(Agreement.class, id), apiKey, params, Agreement.class); + return APIResource.request(RequestMethod.PUT, instanceURL(Agreement.class, id), params, Agreement.class, options); + } + + /** + * 签约修改 + * @param id + * @param params + * @param options + * @return + * @throws PingppException + */ + public static Agreement modify(String id,Map params, RequestOptions options) throws PingppException { + return APIResource.request(RequestMethod.POST, String.format("%s/%s/modify/", classURL(Agreement.class), id), params, Agreement.class, options); + } + + /** + * 签约修改 + * @param id + * @param params + * @return + * @throws PingppException + */ + public static Agreement modify(String id, Map params) throws PingppException { + return modify(id, params, null); } } diff --git a/src/main/java/com/pingplusplus/model/AgreementCollection.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/AgreementCollection.java similarity index 100% rename from src/main/java/com/pingplusplus/model/AgreementCollection.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/AgreementCollection.java diff --git a/pingpp-sdk/src/main/java/com/pingplusplus/model/AgreementNotify.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/AgreementNotify.java new file mode 100644 index 0000000..9fd9970 --- /dev/null +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/AgreementNotify.java @@ -0,0 +1,44 @@ +package com.pingplusplus.model; + +import com.pingplusplus.exception.PingppException; +import com.pingplusplus.net.APIResource; +import com.pingplusplus.net.RequestOptions; + +import java.util.Map; + +public class AgreementNotify extends APIResource { + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public Boolean getSucceed() { + return succeed; + } + + public void setSucceed(Boolean succeed) { + this.succeed = succeed; + } + + String id; + Boolean succeed; + + public static AgreementNotify create(String id, Map params) throws PingppException { + return create(id, params, null); + } + + /** + * 签约扣款-商户预扣费通知 + * @param agreementId + * @param params + * @param options + * @return + * @throws PingppException + */ + public static AgreementNotify create(String agreementId, Map params, RequestOptions options) throws PingppException { + return APIResource.request(APIResource.RequestMethod.POST, String.format("%s/notify", instanceURL(Agreement.class, agreementId)), params, AgreementNotify.class, options); + } +} diff --git a/src/main/java/com/pingplusplus/model/App.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/App.java similarity index 100% rename from src/main/java/com/pingplusplus/model/App.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/App.java diff --git a/src/main/java/com/pingplusplus/model/BalanceBonus.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/BalanceBonus.java similarity index 65% rename from src/main/java/com/pingplusplus/model/BalanceBonus.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/BalanceBonus.java index bfb76b3..911abdd 100644 --- a/src/main/java/com/pingplusplus/model/BalanceBonus.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/BalanceBonus.java @@ -1,9 +1,9 @@ package com.pingplusplus.model; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.net.APIResource; import com.pingplusplus.net.AppBasedResource; - +import com.pingplusplus.net.RequestOptions; import java.util.Map; public class BalanceBonus extends AppBasedResource { @@ -148,17 +148,24 @@ public void setMetadata(Map metadata) { * * @param params 请求参数 * @return BalanceBonus - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static BalanceBonus create(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.POST, classURL(BalanceBonus.class), params, BalanceBonus.class); + throws PingppException { + return create(params, null); + } + + /** + * 创建 balance_bonus + * + * @param params 请求参数 + * @param options the specific options + * @return BalanceBonus + * @throws PingppException + */ + public static BalanceBonus create(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.POST, classURL(BalanceBonus.class), params, BalanceBonus.class, options); } /** @@ -166,17 +173,24 @@ public static BalanceBonus create(Map params) * * @param id * @return BalanceBonus - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static BalanceBonus retrieve(String id) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.GET, instanceURL(BalanceBonus.class, id), null, BalanceBonus.class); + throws PingppException { + return retrieve(id, null); + } + + /** + * 查询 balance_bonus + * + * @param id + * @param options the specific options + * @return BalanceBonus + * @throws PingppException + */ + public static BalanceBonus retrieve(String id, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, instanceURL(BalanceBonus.class, id), null, BalanceBonus.class, options); } /** @@ -184,16 +198,23 @@ public static BalanceBonus retrieve(String id) * * @param params * @return BalanceBonusCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static BalanceBonusCollection list(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.GET, classURL(BalanceBonus.class), params, BalanceBonusCollection.class); + throws PingppException { + return list(params, null); + } + + /** + * 查询 balance_bonus 列表 + * + * @param params + * @param options the specific options + * @return BalanceBonusCollection + * @throws PingppException + */ + public static BalanceBonusCollection list(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, classURL(BalanceBonus.class), params, BalanceBonusCollection.class, options); } } diff --git a/src/main/java/com/pingplusplus/model/BalanceBonusCollection.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/BalanceBonusCollection.java similarity index 100% rename from src/main/java/com/pingplusplus/model/BalanceBonusCollection.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/BalanceBonusCollection.java diff --git a/src/main/java/com/pingplusplus/model/BalanceSettlement.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/BalanceSettlement.java similarity index 73% rename from src/main/java/com/pingplusplus/model/BalanceSettlement.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/BalanceSettlement.java index b9f6f85..2a60832 100644 --- a/src/main/java/com/pingplusplus/model/BalanceSettlement.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/BalanceSettlement.java @@ -1,9 +1,9 @@ package com.pingplusplus.model; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.net.APIResource; import com.pingplusplus.net.AppBasedResource; - +import com.pingplusplus.net.RequestOptions; import java.util.Map; public class BalanceSettlement extends AppBasedResource { @@ -166,17 +166,24 @@ public void setUserFee(String userFee) { * * @param id balance_settlement ID * @return BalanceSettlement - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static BalanceSettlement retrieve(String id) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.GET, instanceURL(BalanceSettlement.class, id), null, BalanceSettlement.class); + throws PingppException { + return retrieve(id, null); + } + + /** + * 查询 balance_settlement + * + * @param id balance_settlement ID + * @param options the specific options + * @return BalanceSettlement + * @throws PingppException + */ + public static BalanceSettlement retrieve(String id, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, instanceURL(BalanceSettlement.class, id), null, BalanceSettlement.class, options); } /** @@ -184,16 +191,23 @@ public static BalanceSettlement retrieve(String id) * * @param params 过滤参数 * @return BalanceSettlementCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static BalanceSettlementCollection list(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.GET, classURL(BalanceSettlement.class), params, BalanceSettlementCollection.class); + throws PingppException { + return list(params, null); + } + + /** + * 查询 balance_transaction 列表 + * + * @param params 过滤参数 + * @param options the specific options + * @return BalanceSettlementCollection + * @throws PingppException + */ + public static BalanceSettlementCollection list(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, classURL(BalanceSettlement.class), params, BalanceSettlementCollection.class, options); } } diff --git a/src/main/java/com/pingplusplus/model/BalanceSettlementCollection.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/BalanceSettlementCollection.java similarity index 100% rename from src/main/java/com/pingplusplus/model/BalanceSettlementCollection.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/BalanceSettlementCollection.java diff --git a/src/main/java/com/pingplusplus/model/BalanceTransaction.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/BalanceTransaction.java similarity index 65% rename from src/main/java/com/pingplusplus/model/BalanceTransaction.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/BalanceTransaction.java index 3ee8076..4873833 100644 --- a/src/main/java/com/pingplusplus/model/BalanceTransaction.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/BalanceTransaction.java @@ -1,8 +1,9 @@ package com.pingplusplus.model; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.net.APIResource; import com.pingplusplus.net.AppBasedResource; +import com.pingplusplus.net.RequestOptions; import java.util.Map; @@ -112,17 +113,24 @@ public void setUser(String user) { * * @param id * @return BalanceTransaction - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static BalanceTransaction retrieve(String id) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.GET, instanceURL(BalanceTransaction.class, id), null, BalanceTransaction.class); + throws PingppException { + return retrieve(id, null); + } + + /** + * 查询 balance_transaction + * + * @param id + * @param options the specific options + * @return BalanceTransaction + * @throws PingppException + */ + public static BalanceTransaction retrieve(String id, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, instanceURL(BalanceTransaction.class, id), null, BalanceTransaction.class, options); } /** @@ -130,16 +138,23 @@ public static BalanceTransaction retrieve(String id) * * @param params * @return BalanceTransactionCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static BalanceTransactionCollection list(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.GET, classURL(BalanceTransaction.class), params, BalanceTransactionCollection.class); + throws PingppException { + return list(params, null); + } + + /** + * 查询 balance_transaction 列表 + * + * @param params + * @param options the specific options + * @return BalanceTransactionCollection + * @throws PingppException + */ + public static BalanceTransactionCollection list(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, classURL(BalanceTransaction.class), params, BalanceTransactionCollection.class, options); } } diff --git a/src/main/java/com/pingplusplus/model/BalanceTransactionCollection.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/BalanceTransactionCollection.java similarity index 100% rename from src/main/java/com/pingplusplus/model/BalanceTransactionCollection.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/BalanceTransactionCollection.java diff --git a/src/main/java/com/pingplusplus/model/BalanceTransfer.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/BalanceTransfer.java similarity index 66% rename from src/main/java/com/pingplusplus/model/BalanceTransfer.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/BalanceTransfer.java index 451ce87..61b1cb9 100644 --- a/src/main/java/com/pingplusplus/model/BalanceTransfer.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/BalanceTransfer.java @@ -1,8 +1,9 @@ package com.pingplusplus.model; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.net.APIResource; import com.pingplusplus.net.AppBasedResource; +import com.pingplusplus.net.RequestOptions; import java.util.Map; @@ -148,17 +149,24 @@ public void setMetadata(Map metadata) { * * @param params * @return BalanceTransfer - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static BalanceTransfer create(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.POST, classURL(BalanceTransfer.class), params, BalanceTransfer.class); + throws PingppException { + return create(params, null); + } + + /** + * 创建 balance_transfer + * + * @param params + * @param options the specific options + * @return BalanceTransfer + * @throws PingppException + */ + public static BalanceTransfer create(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.POST, classURL(BalanceTransfer.class), params, BalanceTransfer.class, options); } /** @@ -166,17 +174,24 @@ public static BalanceTransfer create(Map params) * * @param id * @return BalanceTransfer - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static BalanceTransfer retrieve(String id) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.GET, instanceURL(BalanceTransfer.class, id), null, BalanceTransfer.class); + throws PingppException { + return retrieve(id, null); + } + + /** + * 查询 balance_transfer + * + * @param id + * @param options the specific options + * @return BalanceTransfer + * @throws PingppException + */ + public static BalanceTransfer retrieve(String id, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, instanceURL(BalanceTransfer.class, id), null, BalanceTransfer.class, options); } /** @@ -184,16 +199,23 @@ public static BalanceTransfer retrieve(String id) * * @param params * @return BalanceTransferCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static BalanceTransferCollection list(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.GET, classURL(BalanceTransfer.class), params, BalanceTransferCollection.class); + throws PingppException { + return list(params, null); + } + + /** + * 查询 balance_transfer 列表 + * + * @param params + * @param options the specific options + * @return BalanceTransferCollection + * @throws PingppException + */ + public static BalanceTransferCollection list(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, classURL(BalanceTransfer.class), params, BalanceTransferCollection.class, options); } } diff --git a/src/main/java/com/pingplusplus/model/BalanceTransferCollection.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/BalanceTransferCollection.java similarity index 100% rename from src/main/java/com/pingplusplus/model/BalanceTransferCollection.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/BalanceTransferCollection.java diff --git a/src/main/java/com/pingplusplus/model/BatchRefund.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/BatchRefund.java similarity index 54% rename from src/main/java/com/pingplusplus/model/BatchRefund.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/BatchRefund.java index e400e85..be83e92 100644 --- a/src/main/java/com/pingplusplus/model/BatchRefund.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/BatchRefund.java @@ -1,7 +1,8 @@ package com.pingplusplus.model; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.net.APIResource; +import com.pingplusplus.net.RequestOptions; import java.util.List; import java.util.Map; @@ -138,36 +139,24 @@ protected static String classURL(Class clazz) { * * @param params * @return BatchRefund - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static BatchRefund create(Mapparams) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return create(null, params); + throws PingppException { + return create(params, null); } /** * 创建 batch_refund * - * @param apiKey Ping++ ApiKey * @param params + * @param options the specific options * @return BatchRefund - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static BatchRefund create(String apiKey, Mapparams) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(RequestMethod.POST, classURL(BatchRefund.class), apiKey, params, BatchRefund.class); + public static BatchRefund create(Mapparams, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.POST, classURL(BatchRefund.class), params, BatchRefund.class, options); } /** @@ -175,16 +164,10 @@ public static BatchRefund create(String apiKey, Mapparams) * * @param id * @return BatchRefund - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static BatchRefund retrieve(String id) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { + throws PingppException { return retrieve(id, null); } @@ -192,19 +175,13 @@ public static BatchRefund retrieve(String id) * 查询 batch_refund * * @param id - * @param apiKey Ping++ ApiKey + * @param options the specific options * @return BatchRefund - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static BatchRefund retrieve(String id, String apiKey) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(RequestMethod.GET, instanceURL(BatchRefund.class, id), apiKey, null, BatchRefund.class); + public static BatchRefund retrieve(String id, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, instanceURL(BatchRefund.class, id), null, BatchRefund.class, options); } /** @@ -212,35 +189,23 @@ public static BatchRefund retrieve(String id, String apiKey) * * @param params * @return BatchRefundCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static BatchRefundCollection list(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return list(null, params); + throws PingppException { + return list(params, null); } /** * 查询 batch_refund 列表 * - * @param apiKey Ping++ ApiKey * @param params + * @param options the specific options * @return BatchRefundCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static BatchRefundCollection list(String apiKey, Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(RequestMethod.GET, classURL(BatchRefund.class), apiKey, params, BatchRefundCollection.class); + public static BatchRefundCollection list(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, classURL(BatchRefund.class), params, BatchRefundCollection.class, options); } } diff --git a/src/main/java/com/pingplusplus/model/BatchRefundCharges.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/BatchRefundCharges.java similarity index 100% rename from src/main/java/com/pingplusplus/model/BatchRefundCharges.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/BatchRefundCharges.java diff --git a/src/main/java/com/pingplusplus/model/BatchRefundCollection.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/BatchRefundCollection.java similarity index 100% rename from src/main/java/com/pingplusplus/model/BatchRefundCollection.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/BatchRefundCollection.java diff --git a/src/main/java/com/pingplusplus/model/BatchTransfer.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/BatchTransfer.java similarity index 54% rename from src/main/java/com/pingplusplus/model/BatchTransfer.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/BatchTransfer.java index 61bcb3a..90d1114 100644 --- a/src/main/java/com/pingplusplus/model/BatchTransfer.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/BatchTransfer.java @@ -1,7 +1,8 @@ package com.pingplusplus.model; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.net.APIResource; +import com.pingplusplus.net.RequestOptions; import java.util.List; import java.util.Map; @@ -183,36 +184,24 @@ protected static String classURL(Class clazz) { * * @param params * @return BatchTransfer - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static BatchTransfer create(Mapparams) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return create(null, params); + throws PingppException { + return create(params, null); } /** * 创建 batch_transfer * - * @param apiKey Ping++ ApiKey * @param params + * @param options the specific options * @return BatchTransfer - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static BatchTransfer create(String apiKey, Mapparams) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(RequestMethod.POST, classURL(BatchTransfer.class), apiKey, params, BatchTransfer.class); + public static BatchTransfer create(Mapparams, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.POST, classURL(BatchTransfer.class), params, BatchTransfer.class, options); } /** @@ -220,16 +209,10 @@ public static BatchTransfer create(String apiKey, Mapparams) * * @param id * @return BatchTransfer - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static BatchTransfer retrieve(String id) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { + throws PingppException { return retrieve(id, null); } @@ -237,19 +220,13 @@ public static BatchTransfer retrieve(String id) * 查询 batch_transfer * * @param id - * @param apiKey Ping++ ApiKey + * @param options the specific options * @return BatchTransfer - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static BatchTransfer retrieve(String id, String apiKey) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(RequestMethod.GET, instanceURL(BatchTransfer.class, id), apiKey, null, BatchTransfer.class); + public static BatchTransfer retrieve(String id, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, instanceURL(BatchTransfer.class, id), null, BatchTransfer.class, options); } /** @@ -257,36 +234,24 @@ public static BatchTransfer retrieve(String id, String apiKey) * * @param params * @return BatchTransferCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static BatchTransferCollection list(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return list(null, params); + throws PingppException { + return list(params, null); } /** * 查询 batch_transfer 列表 * - * @param apiKey Ping++ ApiKey * @param params + * @param options the specific options * @return BatchTransferCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static BatchTransferCollection list(String apiKey, Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(RequestMethod.GET, classURL(BatchTransfer.class), apiKey, params, BatchTransferCollection.class); + public static BatchTransferCollection list(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, classURL(BatchTransfer.class), params, BatchTransferCollection.class, options); } /** @@ -294,35 +259,24 @@ public static BatchTransferCollection list(String apiKey, Map pa * @param id batch_transfer ID * @param params 更新参数 * @return BatchTransfer - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException + * @throws PingppException */ public static BatchTransfer update(String id, Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return update(id, null, params); + throws PingppException { + return update(id, params, null); } /** * 更新 BatchTransfer * * @param id - * @param apiKey Ping++ ApiKey * @param params + * @param options the specific options * @return BatchTransfer - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static BatchTransfer update(String id, String apiKey, Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(RequestMethod.PUT, instanceURL(BatchTransfer.class, id), apiKey, params, BatchTransfer.class); + public static BatchTransfer update(String id, Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.PUT, instanceURL(BatchTransfer.class, id), params, BatchTransfer.class, options); } } diff --git a/src/main/java/com/pingplusplus/model/BatchTransferCollection.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/BatchTransferCollection.java similarity index 100% rename from src/main/java/com/pingplusplus/model/BatchTransferCollection.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/BatchTransferCollection.java diff --git a/src/main/java/com/pingplusplus/model/BatchTransferRecipient.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/BatchTransferRecipient.java similarity index 100% rename from src/main/java/com/pingplusplus/model/BatchTransferRecipient.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/BatchTransferRecipient.java diff --git a/src/main/java/com/pingplusplus/model/BatchWithdrawal.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/BatchWithdrawal.java similarity index 71% rename from src/main/java/com/pingplusplus/model/BatchWithdrawal.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/BatchWithdrawal.java index a194a6f..33eeba1 100644 --- a/src/main/java/com/pingplusplus/model/BatchWithdrawal.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/BatchWithdrawal.java @@ -1,8 +1,9 @@ package com.pingplusplus.model; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.net.APIResource; import com.pingplusplus.net.AppBasedResource; +import com.pingplusplus.net.RequestOptions; import java.util.Map; @@ -202,17 +203,24 @@ public void setTimeFinished(Long timeFinished) { * * @param params * @return BatchWithdrawal - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static BatchWithdrawal create(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.POST, classURL(BatchWithdrawal.class), params, BatchWithdrawal.class); + throws PingppException { + return create(params, null); + } + + /** + * 创建 batch_withdrawal + * + * @param params + * @param options the specific options + * @return BatchWithdrawal + * @throws PingppException + */ + public static BatchWithdrawal create(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.POST, classURL(BatchWithdrawal.class), params, BatchWithdrawal.class, options); } /** @@ -220,17 +228,24 @@ public static BatchWithdrawal create(Map params) * * @param id * @return BatchWithdrawal - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static BatchWithdrawal retrieve(String id) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.GET, instanceURL(BatchWithdrawal.class, id), null, BatchWithdrawal.class); + throws PingppException { + return retrieve(id, null); + } + + /** + * 查询 batch_withdrawal + * + * @param id + * @param options the specific options + * @return BatchWithdrawal + * @throws PingppException + */ + public static BatchWithdrawal retrieve(String id, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, instanceURL(BatchWithdrawal.class, id), null, BatchWithdrawal.class, options); } /** @@ -238,16 +253,23 @@ public static BatchWithdrawal retrieve(String id) * * @param params * @return BatchWithdrawalCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static BatchWithdrawalCollection list(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.GET, classURL(BatchWithdrawal.class), params, BatchWithdrawalCollection.class); + throws PingppException { + return list(params, null); + } + + /** + * 查询 batch_withdrawal 列表 + * + * @param params + * @param options the specific options + * @return BatchWithdrawalCollection + * @throws PingppException + */ + public static BatchWithdrawalCollection list(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, classURL(BatchWithdrawal.class), params, BatchWithdrawalCollection.class, options); } } diff --git a/src/main/java/com/pingplusplus/model/BatchWithdrawalCollection.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/BatchWithdrawalCollection.java similarity index 100% rename from src/main/java/com/pingplusplus/model/BatchWithdrawalCollection.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/BatchWithdrawalCollection.java diff --git a/src/main/java/com/pingplusplus/model/CardInfo.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/CardInfo.java similarity index 62% rename from src/main/java/com/pingplusplus/model/CardInfo.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/CardInfo.java index 58e9305..7d3d650 100644 --- a/src/main/java/com/pingplusplus/model/CardInfo.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/CardInfo.java @@ -1,7 +1,8 @@ package com.pingplusplus.model; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.net.APIResource; +import com.pingplusplus.net.RequestOptions; import java.util.List; import java.util.Map; @@ -67,15 +68,10 @@ public void setSupportChannels(List supportChannels) { * * @param params 卡号等信息 * @return CardInfo - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException + * @throws PingppException */ public static CardInfo query(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { + throws PingppException { return query(params, null); } @@ -83,18 +79,12 @@ public static CardInfo query(Map params) * 银行卡信息查询 * * @param params 卡号等信息 - * @param apiKey Ping++ ApiKey + * @param options the specific options * @return CardInfo - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static CardInfo query(Map params, String apiKey) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(RequestMethod.POST, singleClassURL(CardInfo.class), apiKey, params, CardInfo.class); + public static CardInfo query(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.POST, singleClassURL(CardInfo.class), params, CardInfo.class, options); } } diff --git a/pingpp-sdk/src/main/java/com/pingplusplus/model/Channel.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/Channel.java new file mode 100644 index 0000000..aea7dd8 --- /dev/null +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/Channel.java @@ -0,0 +1,185 @@ +package com.pingplusplus.model; + +import com.pingplusplus.exception.PingppException; +import com.pingplusplus.net.APIResource; +import com.pingplusplus.net.RequestOptions; +import com.pingplusplus.net.SubAppBasedResource; + +import java.util.Map; + +public class Channel extends SubAppBasedResource { + String object; + Long created; + String channel; + Boolean banned; + String bannedMsg; + String description; + Map params; + + public String getObject() { + return object; + } + + public void setObject(String object) { + this.object = object; + } + + public Long getCreated() { + return created; + } + + public void setCreated(Long created) { + this.created = created; + } + + public String getChannel() { + return channel; + } + + public void setChannel(String channel) { + this.channel = channel; + } + + public Boolean getBanned() { + return banned; + } + + public void setBanned(Boolean banned) { + this.banned = banned; + } + + public String getBannedMsg() { + return bannedMsg; + } + + public void setBannedMsg(String bannedMsg) { + this.bannedMsg = bannedMsg; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public Map getParams() { + return params; + } + + public void setParams(Map params) { + this.params = params; + } + + /** + * 创建渠道参数 + * + * @param subAppId + * @param params + * @return Channel + * @throws PingppException + */ + public static Channel create(String subAppId, Mapparams) + throws PingppException { + return create(subAppId, params, null); + } + + /** + * 创建渠道参数 + * + * @param subAppId + * @param params + * @param options the specific options + * @return Channel + * @throws PingppException + */ + public static Channel create(String subAppId, Mapparams, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.POST, classURL(Channel.class, subAppId), params, Channel.class, options); + } + + /** + * 查询渠道参数 + * + * @param subAppId + * @param channel + * @return Channel + * @throws PingppException + */ + public static Channel retrieve(String subAppId, String channel) + throws PingppException { + return retrieve(subAppId, channel, null); + } + + + /** + * 查询渠道参数 + * + * @param subAppId + * @param channel + * @param options the specific options + * @return Channel + * @throws PingppException + */ + public static Channel retrieve(String subAppId, String channel, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, instanceURL(Channel.class, subAppId, channel), null, Channel.class, options); + } + + /** + * 更新渠道参数 + * + * @param subAppId + * @param channel + * @param params + * @return Channel + * @throws PingppException + */ + public static Channel update(String subAppId, String channel, Mapparams) + throws PingppException { + return update(subAppId, channel, params, null); + } + + /** + * 更新渠道参数 + * + * @param subAppId + * @param channel + * @param params + * @param options the specific options + * @return Channel + * @throws PingppException + */ + public static Channel update(String subAppId, String channel, Mapparams, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.PUT, instanceURL(Channel.class, subAppId, channel), params, Channel.class, options); + } + + /** + * 删除渠道参数 + * + * @param subAppId + * @param channel + * @return DeletedChannel + * @throws PingppException + */ + public static DeletedChannel delete(String subAppId, String channel) + throws PingppException { + return delete(subAppId, channel, null); + } + + /** + * 删除渠道参数 + * + * @param subAppId + * @param channel + * @param options the specific options + * @return DeletedChannel + * @throws PingppException + */ + public static DeletedChannel delete(String subAppId, String channel, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.DELETE, instanceURL(Channel.class, subAppId, channel), null, DeletedChannel.class, options); + } +} diff --git a/src/main/java/com/pingplusplus/model/Charge.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/Charge.java similarity index 55% rename from src/main/java/com/pingplusplus/model/Charge.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/Charge.java index 4e80f4f..d76a27f 100644 --- a/src/main/java/com/pingplusplus/model/Charge.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/Charge.java @@ -1,7 +1,9 @@ package com.pingplusplus.model; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.InvalidRequestException; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.net.APIResource; +import com.pingplusplus.net.RequestOptions; import java.util.HashMap; import java.util.Map; @@ -262,36 +264,24 @@ public void setTimeSettle(Long timeSettle) { * * @param params * @return Charge - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static Charge create(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return create(null, params); + throws PingppException { + return create(params, null); } /** * 创建 charge * * @param params - * @param apiKey Ping++ ApiKey + * @param options the specific options * @return Charge - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static Charge create(String apiKey, Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(RequestMethod.POST, classURL(Charge.class), apiKey, params, Charge.class); + public static Charge create(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.POST, classURL(Charge.class), params, Charge.class, options); } /** @@ -299,16 +289,9 @@ public static Charge create(String apiKey, Map params) * * @param id * @return Charge - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static Charge retrieve(String id) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { + public static Charge retrieve(String id) throws PingppException { return retrieve(id, null, null); } @@ -316,37 +299,24 @@ public static Charge retrieve(String id) throws AuthenticationException, * 查询 charge * * @param id + * @param params * @return Charge - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static Charge retrieve(String id, String apiKey) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return retrieve(id, apiKey, null); + public static Charge retrieve(String id, Map params) throws PingppException { + return retrieve(id, params, null); } /** * 查询 charge * * @param id - * @param params + * @param options the specific options * @return Charge - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static Charge retrieve(String id, Map params) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return retrieve(id, null, params); + public static Charge retrieve(String id, RequestOptions options) throws PingppException { + return retrieve(id, null, options); } /** @@ -354,18 +324,12 @@ public static Charge retrieve(String id, Map params) throws Auth * * @param id * @param params + * @param options the specific options * @return Charge - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static Charge retrieve(String id, String apiKey, Map params) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return request(RequestMethod.GET, instanceURL(Charge.class, id), apiKey, params, Charge.class); + public static Charge retrieve(String id, Map params, RequestOptions options) throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, instanceURL(Charge.class, id), params, Charge.class, options); } /** @@ -373,64 +337,52 @@ public static Charge retrieve(String id, String apiKey, Map para * * @param params * @return ChargeCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static ChargeCollection list(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return list(null, params); + throws PingppException { + return list(params, null); } /** * 查询 charge 列表 * * @param params + * @param options the specific options * @return ChargeCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static ChargeCollection list(String apiKey, Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { + public static ChargeCollection list(Map params, RequestOptions options) + throws PingppException { if (params != null && ((params.containsKey("app") && (params.get("app") instanceof Map) && ((Map) params.get("app")).containsKey("id")) || (params.containsKey("app[id]") && (params.get("app[id]") instanceof String)))) { - return request(RequestMethod.GET, classURL(Charge.class), apiKey, params, ChargeCollection.class); + return request(RequestMethod.GET, classURL(Charge.class), params, ChargeCollection.class, options); } - throw new InvalidRequestException("Please pass app[id] as parameter.", "app[id]", null); + throw new InvalidRequestException( + "Please pass app[id] as parameter.", + "invalid_request_error", + "request_param_error", + "app[id]", + 0, + null); } /** * 撤销 charge * * @param id - * @param apiKey Ping++ ApiKey * @param params + * @param options the specific options * @return Charge - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static Charge reverse(String id, String apiKey, Map params) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { + public static Charge reverse(String id, Map params, RequestOptions options) throws PingppException { String reverseUrl = String.format("%s/reverse", instanceURL(Charge.class, id)); if (params == null) { params = new HashMap(); } - return request(RequestMethod.POST, reverseUrl, apiKey, params, Charge.class); + return APIResource.request(APIResource.RequestMethod.POST, reverseUrl, params, Charge.class, options); } /** @@ -439,17 +391,10 @@ public static Charge reverse(String id, String apiKey, Map param * @param id * @param params * @return Charge - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static Charge reverse(String id, Map params) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return reverse(id, null, params); + public static Charge reverse(String id, Map params) throws PingppException { + return reverse(id, params, null); } /** @@ -457,16 +402,9 @@ public static Charge reverse(String id, Map params) throws Authe * * @param id * @return Charge - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static Charge reverse(String id) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { + public static Charge reverse(String id) throws PingppException { return reverse(id, null, null); } @@ -474,18 +412,11 @@ public static Charge reverse(String id) throws AuthenticationException, * 撤销 charge * * @param id - * @param apiKey Ping++ ApiKey + * @param options the specific options * @return Charge - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static Charge reverse(String id, String apiKey) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return reverse(id, apiKey, null); + public static Charge reverse(String id, RequestOptions options) throws PingppException { + return reverse(id, null, options); } } diff --git a/src/main/java/com/pingplusplus/model/ChargeCollection.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/ChargeCollection.java similarity index 100% rename from src/main/java/com/pingplusplus/model/ChargeCollection.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/ChargeCollection.java diff --git a/src/main/java/com/pingplusplus/model/ChargeEssentials.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/ChargeEssentials.java similarity index 100% rename from src/main/java/com/pingplusplus/model/ChargeEssentials.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/ChargeEssentials.java diff --git a/pingpp-sdk/src/main/java/com/pingplusplus/model/ChargeRefundCollection.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/ChargeRefundCollection.java new file mode 100644 index 0000000..012a9e1 --- /dev/null +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/ChargeRefundCollection.java @@ -0,0 +1,51 @@ +package com.pingplusplus.model; + +import com.pingplusplus.Pingpp; +import com.pingplusplus.exception.PingppException; +import com.pingplusplus.net.APIResource; +import com.pingplusplus.net.RequestOptions; + +import java.util.Map; + +public class ChargeRefundCollection extends PingppCollectionAPIResource { + + public ChargeRefundCollection list(Map params) + throws PingppException { + + return list(params, null); + } + + public ChargeRefundCollection list(Map params, RequestOptions options) + throws PingppException { + + String url = String.format("%s%s", Pingpp.getApiBase(), this.getURL()); + return APIResource.request(APIResource.RequestMethod.GET, url, params, ChargeRefundCollection.class, options); + } + + public Refund retrieve(String id) + throws PingppException { + + return retrieve(id, null); + } + + public Refund retrieve(String id, RequestOptions options) + throws PingppException { + + String url = String.format("%s%s/%s", Pingpp.getApiBase(), this.getURL(), id); + return APIResource.request(APIResource.RequestMethod.GET, url, null, Refund.class, options); + } + + public Refund create(Map params) + throws PingppException { + + return create(params, null); + } + + public Refund create(Map params, RequestOptions options) + throws PingppException { + + String url = String.format("%s%s", Pingpp.getApiBase(), this.getURL()); + return APIResource.request(APIResource.RequestMethod.POST, url, params, Refund.class, options); + } + +} diff --git a/pingpp-sdk/src/main/java/com/pingplusplus/model/Contact.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/Contact.java new file mode 100644 index 0000000..caf2b50 --- /dev/null +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/Contact.java @@ -0,0 +1,72 @@ +package com.pingplusplus.model; + +import com.pingplusplus.exception.PingppException; +import com.pingplusplus.net.APIResource; +import com.pingplusplus.net.AppBasedResource; +import com.pingplusplus.net.RequestOptions; + +import java.util.Map; + +public class Contact extends AppBasedResource { + String user; + Boolean livemode; + String accNo; + String contactNo; + + public String getUser() { + return user; + } + + public void setUser(String user) { + this.user = user; + } + + public String getAccNo() { + return accNo; + } + + public void setAccNo(String accNo) { + this.accNo = accNo; + } + + public Boolean getLivemode() { + return livemode; + } + + public void setLivemode(Boolean livemode) { + this.livemode = livemode; + } + + public String getContactNo() { + return contactNo; + } + + public void setContactNo(String contactNo) { + this.contactNo = contactNo; + } + + /** + * 新增联系人 + * + * @param params 请求参数 + * @return Contact + * @throws PingppException + */ + public static Contact create(Map params) + throws PingppException { + return create(params, null); + } + + /** + * 新增联系人 + * + * @param params 请求参数 + * @param options the specific options + * @return Contact + * @throws PingppException + */ + public static Contact create(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.POST, singleClassURL(Contact.class), params, Contact.class, options); + } +} diff --git a/src/main/java/com/pingplusplus/model/Coupon.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/Coupon.java similarity index 55% rename from src/main/java/com/pingplusplus/model/Coupon.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/Coupon.java index 79c18c3..253984a 100644 --- a/src/main/java/com/pingplusplus/model/Coupon.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/Coupon.java @@ -1,7 +1,8 @@ package com.pingplusplus.model; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.net.APIResource; +import com.pingplusplus.net.RequestOptions; import com.pingplusplus.net.UserBasedResource; import java.util.Map; @@ -149,18 +150,27 @@ public void setUserTimesCirculated(Integer userTimesCirculated) { * @param userId * @param params * @return Coupon - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static Coupon create(String userId, Mapparams) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { + throws PingppException { User.checkUserId(userId); - return request(APIResource.RequestMethod.POST, classURL(Coupon.class, userId), params, Coupon.class); + return create(userId, params, null); + } + + /** + * 创建 coupon + * + * @param userId + * @param params + * @param options the specific options + * @return Coupon + * @throws PingppException + */ + public static Coupon create(String userId, Mapparams, RequestOptions options) + throws PingppException { + User.checkUserId(userId); + return APIResource.request(APIResource.RequestMethod.POST, classURL(Coupon.class, userId), params, Coupon.class, options); } /** @@ -169,18 +179,27 @@ public static Coupon create(String userId, Mapparams) * @param userId * @param id * @return Coupon - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static Coupon retrieve(String userId, String id) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { + throws PingppException { + User.checkUserId(userId); + return retrieve(userId, id, null); + } + + /** + * 查询 coupon + * + * @param userId + * @param id + * @param options the specific options + * @return Coupon + * @throws PingppException + */ + public static Coupon retrieve(String userId, String id, RequestOptions options) + throws PingppException { User.checkUserId(userId); - return request(APIResource.RequestMethod.GET, instanceURL(Coupon.class, userId, id), null, Coupon.class); + return APIResource.request(APIResource.RequestMethod.GET, instanceURL(Coupon.class, userId, id), null, Coupon.class, options); } /** @@ -189,38 +208,58 @@ public static Coupon retrieve(String userId, String id) * @param userId * @param params * @return CouponCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static CouponCollection list(String userId, Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { + throws PingppException { + User.checkUserId(userId); + return list(userId, params, null); + } + + /** + * 查询 coupon 列表 + * + * @param userId + * @param params + * @param options the specific options + * @return CouponCollection + * @throws PingppException + */ + public static CouponCollection list(String userId, Map params, RequestOptions options) + throws PingppException { User.checkUserId(userId); - return request(APIResource.RequestMethod.GET, classURL(Coupon.class, userId), params, CouponCollection.class); + return APIResource.request(APIResource.RequestMethod.GET, classURL(Coupon.class, userId), params, CouponCollection.class, options); } /** * 更新 coupon * * @param userId + * @param id * @param params * @return Coupon - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static Coupon update(String userId, String id, Mapparams) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { + throws PingppException { + User.checkUserId(userId); + return update(userId, id, params, null); + } + + /** + * 更新 coupon + * + * @param userId + * @param id + * @param params + * @param options the specific options + * @return Coupon + * @throws PingppException + */ + public static Coupon update(String userId, String id, Mapparams, RequestOptions options) + throws PingppException { User.checkUserId(userId); - return request(APIResource.RequestMethod.PUT, instanceURL(Coupon.class, userId, id), params, Coupon.class); + return APIResource.request(APIResource.RequestMethod.PUT, instanceURL(Coupon.class, userId, id), params, Coupon.class, options); } /** @@ -229,17 +268,26 @@ public static Coupon update(String userId, String id, Mapparams) * @param userId * @param id * @return DeletedCoupon - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static DeletedCoupon delete(String userId, String id) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { + throws PingppException { + User.checkUserId(userId); + return delete(userId, id, null); + } + + /** + * 删除 coupon + * + * @param userId + * @param id + * @param options the specific options + * @return DeletedCoupon + * @throws PingppException + */ + public static DeletedCoupon delete(String userId, String id, RequestOptions options) + throws PingppException { User.checkUserId(userId); - return request(APIResource.RequestMethod.DELETE, instanceURL(Coupon.class, userId, id), null, DeletedCoupon.class); + return APIResource.request(APIResource.RequestMethod.DELETE, instanceURL(Coupon.class, userId, id), null, DeletedCoupon.class, options); } } diff --git a/src/main/java/com/pingplusplus/model/CouponCollection.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/CouponCollection.java similarity index 100% rename from src/main/java/com/pingplusplus/model/CouponCollection.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/CouponCollection.java diff --git a/src/main/java/com/pingplusplus/model/CouponTemplate.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/CouponTemplate.java similarity index 53% rename from src/main/java/com/pingplusplus/model/CouponTemplate.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/CouponTemplate.java index f805ee4..e33014f 100644 --- a/src/main/java/com/pingplusplus/model/CouponTemplate.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/CouponTemplate.java @@ -1,8 +1,9 @@ package com.pingplusplus.model; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.net.APIResource; import com.pingplusplus.net.AppBasedResource; +import com.pingplusplus.net.RequestOptions; import java.util.Map; @@ -166,17 +167,24 @@ public void setMaxUserCirculation(Integer maxUserCirculation) { * * @param params * @return CouponTemplate - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static CouponTemplate create(Mapparams) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.POST, classURL(CouponTemplate.class), params, CouponTemplate.class); + throws PingppException { + return create(params, null); + } + + /** + * 创建 coupon_template + * + * @param params + * @param options the specific options + * @return CouponTemplate + * @throws PingppException + */ + public static CouponTemplate create(Mapparams, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.POST, classURL(CouponTemplate.class), params, CouponTemplate.class, options); } /** @@ -184,17 +192,24 @@ public static CouponTemplate create(Mapparams) * * @param id * @return CouponTemplate - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static CouponTemplate retrieve(String id) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.GET, instanceURL(CouponTemplate.class, id), null, CouponTemplate.class); + throws PingppException { + return retrieve(id, null); + } + + /** + * 查询 coupon_template + * + * @param id + * @param options the specific options + * @return CouponTemplate + * @throws PingppException + */ + public static CouponTemplate retrieve(String id, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, instanceURL(CouponTemplate.class, id), null, CouponTemplate.class, options); } /** @@ -202,35 +217,51 @@ public static CouponTemplate retrieve(String id) * * @param params * @return CouponTemplateCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static CouponTemplateCollection list(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.GET, classURL(CouponTemplate.class), params, CouponTemplateCollection.class); + throws PingppException { + return list(params, null); + } + + /** + * 查询 coupon_template 列表 + * + * @param params + * @param options the specific options + * @return CouponTemplateCollection + * @throws PingppException + */ + public static CouponTemplateCollection list(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, classURL(CouponTemplate.class), params, CouponTemplateCollection.class, options); } /** * 更新 coupon_template * + * @param id * @param params * @return CouponTemplate - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static CouponTemplate update(String id, Mapparams) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.PUT, instanceURL(CouponTemplate.class, id), params, CouponTemplate.class); + throws PingppException { + return update(id, params, null); + } + + /** + * 更新 coupon_template + * + * @param id + * @param params + * @param options the specific options + * @return CouponTemplate + * @throws PingppException + */ + public static CouponTemplate update(String id, Mapparams, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.PUT, instanceURL(CouponTemplate.class, id), params, CouponTemplate.class, options); } /** @@ -238,20 +269,27 @@ public static CouponTemplate update(String id, Mapparams) * * @param id * @return DeletedCouponTemplate - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static DeletedCouponTemplate delete(String id) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.DELETE, instanceURL(CouponTemplate.class, id), null, DeletedCouponTemplate.class); + throws PingppException { + return delete(id, null); } - public static String couponsURL(String tmplId) throws InvalidRequestException { + /** + * 删除 coupon_template + * + * @param id + * @param options the specific options + * @return DeletedCouponTemplate + * @throws PingppException + */ + public static DeletedCouponTemplate delete(String id, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.DELETE, instanceURL(CouponTemplate.class, id), null, DeletedCouponTemplate.class, options); + } + + public static String couponsURL(String tmplId) { return String.format("%s/coupons", instanceURL(CouponTemplate.class, tmplId)); } @@ -261,17 +299,25 @@ public static String couponsURL(String tmplId) throws InvalidRequestException { * @param id * @param params * @return CouponCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static CouponCollection createCoupons(String id, Mapparams) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.POST, couponsURL(id), params, CouponCollection.class); + throws PingppException { + return createCoupons(id, params, null); + } + + /** + * 批量创建 coupon + * + * @param id + * @param params + * @param options the specific options + * @return CouponCollection + * @throws PingppException + */ + public static CouponCollection createCoupons(String id, Mapparams, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.POST, couponsURL(id), params, CouponCollection.class, options); } /** @@ -280,16 +326,24 @@ public static CouponCollection createCoupons(String id, Mapparam * @param id * @param params * @return CouponCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static CouponCollection listCoupons(String id, Mapparams) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.GET, couponsURL(id), params, CouponCollection.class); + throws PingppException { + return listCoupons(id, params, null); + } + + /** + * 查询 coupon 列表 + * + * @param id + * @param params + * @param options the specific options + * @return CouponCollection + * @throws PingppException + */ + public static CouponCollection listCoupons(String id, Mapparams, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, couponsURL(id), params, CouponCollection.class, options); } } diff --git a/src/main/java/com/pingplusplus/model/CouponTemplateCollection.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/CouponTemplateCollection.java similarity index 100% rename from src/main/java/com/pingplusplus/model/CouponTemplateCollection.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/CouponTemplateCollection.java diff --git a/src/main/java/com/pingplusplus/model/CouponTemplateExpiration.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/CouponTemplateExpiration.java similarity index 100% rename from src/main/java/com/pingplusplus/model/CouponTemplateExpiration.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/CouponTemplateExpiration.java diff --git a/src/main/java/com/pingplusplus/model/Customs.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/Customs.java similarity index 57% rename from src/main/java/com/pingplusplus/model/Customs.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/Customs.java index 6b05c1d..96306af 100644 --- a/src/main/java/com/pingplusplus/model/Customs.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/Customs.java @@ -1,7 +1,8 @@ package com.pingplusplus.model; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.net.APIResource; +import com.pingplusplus.net.RequestOptions; import java.util.Map; @@ -174,36 +175,24 @@ public void setTransactionNo(String transactionNo) { * * @param params * @return Customs - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static Customs create(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return create(null, params); + throws PingppException { + return create(params, null); } /** * 创建 customs * - * @param apiKey Ping++ ApiKey * @param params + * @param options the specific options * @return Customs - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static Customs create(String apiKey, Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(RequestMethod.POST, classURL(Customs.class), apiKey, params, Customs.class); + public static Customs create(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.POST, classURL(Customs.class), params, Customs.class, options); } /** @@ -211,16 +200,9 @@ public static Customs create(String apiKey, Map params) * * @param id * @return Customs - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static Customs retrieve(String id) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { + public static Customs retrieve(String id) throws PingppException { return retrieve(id, null); } @@ -228,19 +210,12 @@ public static Customs retrieve(String id) throws AuthenticationException, * 查询 customs * * @param id - * @param apiKey Ping++ ApiKey + * @param options the specific options * @return Customs - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static Customs retrieve(String id, String apiKey) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return request(RequestMethod.GET, instanceURL(Customs.class, id), apiKey, null, Customs.class); + public static Customs retrieve(String id, RequestOptions options) throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, instanceURL(Customs.class, id), null, Customs.class, options); } /** @@ -248,35 +223,23 @@ public static Customs retrieve(String id, String apiKey) throws AuthenticationEx * * @param params * @return CustomsCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static CustomsCollection list(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return list(null, params); + throws PingppException { + return list(params, null); } /** * 查询 customs 列表 * - * @param apiKey Ping++ ApiKey * @param params + * @param options the specific options * @return CustomsCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static CustomsCollection list(String apiKey, Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(RequestMethod.GET, classURL(Customs.class), apiKey, params, CustomsCollection.class); + public static CustomsCollection list(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, classURL(Customs.class), params, CustomsCollection.class, options); } } diff --git a/src/main/java/com/pingplusplus/model/CustomsCollection.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/CustomsCollection.java similarity index 100% rename from src/main/java/com/pingplusplus/model/CustomsCollection.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/CustomsCollection.java diff --git a/src/main/java/com/pingplusplus/model/DeleteRoyaltyTemplate.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/DeleteRoyaltyTemplate.java similarity index 100% rename from src/main/java/com/pingplusplus/model/DeleteRoyaltyTemplate.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/DeleteRoyaltyTemplate.java diff --git a/src/main/java/com/pingplusplus/model/DeletedChannel.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/DeletedChannel.java similarity index 100% rename from src/main/java/com/pingplusplus/model/DeletedChannel.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/DeletedChannel.java diff --git a/src/main/java/com/pingplusplus/model/DeletedCoupon.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/DeletedCoupon.java similarity index 100% rename from src/main/java/com/pingplusplus/model/DeletedCoupon.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/DeletedCoupon.java diff --git a/src/main/java/com/pingplusplus/model/DeletedCouponTemplate.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/DeletedCouponTemplate.java similarity index 100% rename from src/main/java/com/pingplusplus/model/DeletedCouponTemplate.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/DeletedCouponTemplate.java diff --git a/src/main/java/com/pingplusplus/model/DeletedObjectBase.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/DeletedObjectBase.java similarity index 100% rename from src/main/java/com/pingplusplus/model/DeletedObjectBase.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/DeletedObjectBase.java diff --git a/src/main/java/com/pingplusplus/model/DeletedPingppObject.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/DeletedPingppObject.java similarity index 100% rename from src/main/java/com/pingplusplus/model/DeletedPingppObject.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/DeletedPingppObject.java diff --git a/src/main/java/com/pingplusplus/model/DeletedSettleAccount.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/DeletedSettleAccount.java similarity index 100% rename from src/main/java/com/pingplusplus/model/DeletedSettleAccount.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/DeletedSettleAccount.java diff --git a/src/main/java/com/pingplusplus/model/DeletedSplitReceiver.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/DeletedSplitReceiver.java similarity index 100% rename from src/main/java/com/pingplusplus/model/DeletedSplitReceiver.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/DeletedSplitReceiver.java diff --git a/src/main/java/com/pingplusplus/model/DeletedSubApp.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/DeletedSubApp.java similarity index 100% rename from src/main/java/com/pingplusplus/model/DeletedSubApp.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/DeletedSubApp.java diff --git a/src/main/java/com/pingplusplus/model/Event.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/Event.java similarity index 52% rename from src/main/java/com/pingplusplus/model/Event.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/Event.java index 6a502b0..25a694f 100644 --- a/src/main/java/com/pingplusplus/model/Event.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/Event.java @@ -1,7 +1,8 @@ package com.pingplusplus.model; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.net.APIResource; +import com.pingplusplus.net.RequestOptions; import java.util.Map; @@ -88,15 +89,9 @@ public void setRequest(String request) { * * @param id * @return Event - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException + * @throws PingppException */ - public static Event retrieve(String id) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { + public static Event retrieve(String id) throws PingppException { return retrieve(id, null, null); } @@ -104,19 +99,12 @@ public static Event retrieve(String id) throws AuthenticationException, * 查询 Event * * @param id - * @param apiKey Ping++ ApiKey + * @param options Ping++ ApiKey * @return Event - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static Event retrieve(String id, String apiKey) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return retrieve(id, apiKey, null); + public static Event retrieve(String id, RequestOptions options) throws PingppException { + return retrieve(id, null, options); } /** @@ -125,35 +113,22 @@ public static Event retrieve(String id, String apiKey) throws AuthenticationExce * @param id * @param params * @return Event - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException + * @throws PingppException */ - public static Event retrieve(String id, Map params) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return retrieve(id, null, params); + public static Event retrieve(String id, Map params) throws PingppException { + return retrieve(id, params, null); } /** * 查询 Event * * @param id - * @param apiKey Ping++ ApiKey * @param params + * @param options the specific options * @return Event - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static Event retrieve(String id, String apiKey, Map params) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.GET, instanceURL(Event.class, id), apiKey, params, Event.class); + public static Event retrieve(String id, Map params, RequestOptions options) throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, instanceURL(Event.class, id), params, Event.class, options); } } diff --git a/src/main/java/com/pingplusplus/model/EventCollection.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/EventCollection.java similarity index 100% rename from src/main/java/com/pingplusplus/model/EventCollection.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/EventCollection.java diff --git a/src/main/java/com/pingplusplus/model/EventData.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/EventData.java similarity index 100% rename from src/main/java/com/pingplusplus/model/EventData.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/EventData.java diff --git a/src/main/java/com/pingplusplus/model/Identification.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/Identification.java similarity index 57% rename from src/main/java/com/pingplusplus/model/Identification.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/Identification.java index dd8bf17..ee01014 100644 --- a/src/main/java/com/pingplusplus/model/Identification.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/Identification.java @@ -1,7 +1,8 @@ package com.pingplusplus.model; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.net.APIResource; +import com.pingplusplus.net.RequestOptions; import java.util.Map; @@ -66,34 +67,23 @@ public void setApp(Object app) { * * @param params * @return Identification - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException + * @throws PingppException */ public static Identification identify(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return identify(null, params); + throws PingppException { + return identify(params, null); } /** * 请求认证 * - * @param apiKey Ping++ ApiKey * @param params + * @param options the specific options * @return Identification - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static Identification identify(String apiKey, Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(RequestMethod.POST, singleClassURL(Identification.class), apiKey, params, Identification.class); + public static Identification identify(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.POST, singleClassURL(Identification.class), params, Identification.class, options); } } diff --git a/src/main/java/com/pingplusplus/model/Order.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/Order.java similarity index 55% rename from src/main/java/com/pingplusplus/model/Order.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/Order.java index ff258f1..7d51268 100644 --- a/src/main/java/com/pingplusplus/model/Order.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/Order.java @@ -1,7 +1,8 @@ package com.pingplusplus.model; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.net.APIResource; +import com.pingplusplus.net.RequestOptions; import java.util.HashMap; import java.util.List; @@ -302,17 +303,24 @@ public void setDiscountAmount(Integer discountAmount) { * * @param params * @return Order - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static Order create(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(RequestMethod.POST, classURL(Order.class), params, Order.class); + throws PingppException { + return create(params, null); + } + + /** + * 创建 order + * + * @param params + * @param options the specific options + * @return Order + * @throws PingppException + */ + public static Order create(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.POST, classURL(Order.class), params, Order.class, options); } /** @@ -320,17 +328,47 @@ public static Order create(Map params) * * @param id * @return Order - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static Order retrieve(String id) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return request(RequestMethod.GET, instanceURL(Order.class, id), null, Order.class); + public static Order retrieve(String id) throws PingppException { + return retrieve(id, null, null); + } + + /** + * 查询 order + * + * @param id + * @param params + * @return Order + * @throws PingppException + */ + public static Order retrieve(String id, Map params) throws PingppException { + return retrieve(id, params, null); + } + + /** + * 查询 order + * + * @param id + * @param options the specific options + * @return Order + * @throws PingppException + */ + public static Order retrieve(String id, RequestOptions options) throws PingppException { + return retrieve(id, null, options); + } + + /** + * 查询 order + * + * @param id + * @param params + * @param options the specific options + * @return Order + * @throws PingppException + */ + public static Order retrieve(String id, Map params, RequestOptions options) throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, instanceURL(Order.class, id), params, Order.class, options); } /** @@ -338,54 +376,78 @@ public static Order retrieve(String id) throws AuthenticationException, * * @param params * @return OrderCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static OrderCollection list(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(RequestMethod.GET, classURL(Order.class), params, OrderCollection.class); + throws PingppException { + return list(params, null); + } + + /** + * 查询 order 列表 + * + * @param params + * @param options the specific options + * @return OrderCollection + * @throws PingppException + */ + public static OrderCollection list(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, classURL(Order.class), params, OrderCollection.class, options); } /** * 更新 order * + * @param id * @param params * @return Order - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static Order update(String id, Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(RequestMethod.PUT, instanceURL(Order.class, id), params, Order.class); + throws PingppException { + return update(id, params, null); + } + + /** + * 更新 order + * + * @param id + * @param params + * @param options the specific options + * @return Order + * @throws PingppException + */ + public static Order update(String id, Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.PUT, instanceURL(Order.class, id), params, Order.class, options); } /** * 取消 order * * @return Order - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public Order cancel() - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { + throws PingppException { + Map params = new HashMap(); + params.put("status", "canceled"); + return Order.update(this.getId(), params, null); + } + + /** + * 取消 order + * + * @param options the specific options + * @return Order + * @throws PingppException + */ + public Order cancel(RequestOptions options) + throws PingppException { Map params = new HashMap(); params.put("status", "canceled"); - return update(this.getId(), params); + return Order.update(this.getId(), params, options); } /** @@ -393,38 +455,51 @@ public Order cancel() * * @param id * @return Order - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static Order cancel(String id) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { + throws PingppException { Map params = new HashMap(); params.put("status", "canceled"); return update(id, params); } + /** + * 取消 order + * + * @param id + * @param options the specific options + * @return Order + * @throws PingppException + */ + public static Order cancel(String id, RequestOptions options) + throws PingppException { + Map params = new HashMap(); + params.put("status", "canceled"); + return update(id, params, options); + } + /** * 支付 order * * @param params * @return Order - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public Order pay(Map params) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return request(RequestMethod.POST, String.format("%s/pay", instanceURL(Order.class, id)), - params, Order.class); + public Order pay(Map params) throws PingppException { + return Order.pay(this.getId(), params, null); + } + + /** + * 支付 order + * + * @param params + * @param options the specific options + * @return Order + * @throws PingppException + */ + public Order pay(Map params, RequestOptions options) throws PingppException { + return Order.pay(this.getId(), params, options); } /** @@ -433,18 +508,36 @@ public Order pay(Map params) throws AuthenticationException, * @param id * @param params * @return Order - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static Order pay(String id, Map params) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return request(RequestMethod.POST, String.format("%s/pay", instanceURL(Order.class, id)), - params, Order.class); + public static Order pay(String id, Map params) throws PingppException { + return pay(id, params, null); + } + + /** + * 支付 order + * + * @param id + * @param params + * @param options the specific options + * @return Order + * @throws PingppException + */ + public static Order pay(String id, Map params, RequestOptions options) throws PingppException { + return APIResource.request(APIResource.RequestMethod.POST, String.format("%s/pay", + instanceURL(Order.class, id)), params, Order.class, options); + } + + /** + * 查询订单 Charge 列表 + * + * @param id + * @param params + * @return ChargeCollection + * @throws PingppException + */ + public static ChargeCollection chargeList(String id, Map params) throws PingppException { + return chargeList(id, params, null); } /** @@ -452,19 +545,25 @@ public static Order pay(String id, Map params) throws Authentica * * @param id * @param params + * @param options the specific options * @return ChargeCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException + */ + public static ChargeCollection chargeList(String id, Map params, RequestOptions options) throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, String.format("%s/charges", instanceURL(Order.class, id)), + params, ChargeCollection.class, options); + } + + /** + * 查询订单 Charge + * + * @param orderId + * @param chargeId + * @return Charge + * @throws PingppException */ - public static ChargeCollection chargeList(String id, Map params) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return request(RequestMethod.GET, String.format("%s/charges", instanceURL(Order.class, id)), - params, ChargeCollection.class); + public static Charge retrieveCharge(String orderId, String chargeId) throws PingppException { + return retrieveCharge(orderId, chargeId, null); } /** @@ -472,18 +571,12 @@ public static ChargeCollection chargeList(String id, Map params) * * @param orderId * @param chargeId + * @param options the specific options * @return Charge - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static Charge retrieveCharge(String orderId, String chargeId) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return request(RequestMethod.GET, String.format("%s/charges/%s", instanceURL(Order.class, orderId), chargeId), - null, Charge.class); + public static Charge retrieveCharge(String orderId, String chargeId, RequestOptions options) throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, String.format("%s/charges/%s", instanceURL(Order.class, orderId), chargeId), + null, Charge.class, options); } } diff --git a/src/main/java/com/pingplusplus/model/OrderCollection.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/OrderCollection.java similarity index 100% rename from src/main/java/com/pingplusplus/model/OrderCollection.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/OrderCollection.java diff --git a/pingpp-sdk/src/main/java/com/pingplusplus/model/OrderRefund.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/OrderRefund.java new file mode 100644 index 0000000..8d30fd3 --- /dev/null +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/OrderRefund.java @@ -0,0 +1,119 @@ +package com.pingplusplus.model; + +import com.pingplusplus.exception.PingppException; +import com.pingplusplus.net.APIResource; +import com.pingplusplus.net.RequestOptions; + +import java.util.Map; + +public class OrderRefund extends APIResource { + + /** + * 创建 order_refund + * + * @param orderId + * @param params + * @return OrderRefundCollection + * @throws PingppException + */ + public static OrderRefundCollection create(String orderId, Map params) + throws PingppException { + return create(orderId, params, null); + } + + /** + * 创建 order_refund + * + * @param orderId + * @param params + * @param options the specific options + * @return OrderRefundCollection + * @throws PingppException + */ + public static OrderRefundCollection create(String orderId, Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.POST, String.format("%s/order_refunds", instanceURL(Order.class, orderId)), + params, OrderRefundCollection.class, options); + } + + /** + * 查询 order_refund + * + * @param orderId + * @param refundId + * @return Refund + * @throws PingppException + */ + public static Refund retrieve(String orderId, String refundId) + throws PingppException { + return retrieve(orderId, refundId, null); + } + + /** + * 查询 order_refund + * + * @param orderId + * @param refundId + * @param options the specific options + * @return Refund + * @throws PingppException + */ + public static Refund retrieve(String orderId, String refundId, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, String.format("%s/order_refunds/%s", instanceURL(Order.class, orderId), refundId), + null, Refund.class, options); + } + + /** + * 查询 order_refund 列表 + * + * @param orderId + * @param params + * @return OrderRefundCollection + * @throws PingppException + */ + public static OrderRefundCollection list(String orderId, Mapparams) + throws PingppException { + return list(orderId, params, null); + } + + /** + * 查询 order_refund 列表 + * + * @param orderId + * @return OrderRefundCollection + * @throws PingppException + */ + public static OrderRefundCollection list(String orderId) + throws PingppException { + return list(orderId, null, null); + } + + /** + * 查询 order_refund 列表 + * + * @param orderId + * @param options the specific options + * @return OrderRefundCollection + * @throws PingppException + */ + public static OrderRefundCollection list(String orderId, RequestOptions options) + throws PingppException { + return list(orderId, null, options); + } + + /** + * 查询 order_refund 列表 + * + * @param orderId + * @param params + * @param options the specific options + * @return OrderRefundCollection + * @throws PingppException + */ + public static OrderRefundCollection list(String orderId, Mapparams, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, String.format("%s/order_refunds", instanceURL(Order.class, orderId)), + params, OrderRefundCollection.class, options); + } +} diff --git a/src/main/java/com/pingplusplus/model/OrderRefundCollection.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/OrderRefundCollection.java similarity index 100% rename from src/main/java/com/pingplusplus/model/OrderRefundCollection.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/OrderRefundCollection.java diff --git a/src/main/java/com/pingplusplus/model/PingppCollection.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/PingppCollection.java similarity index 100% rename from src/main/java/com/pingplusplus/model/PingppCollection.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/PingppCollection.java diff --git a/src/main/java/com/pingplusplus/model/PingppCollectionAPIResource.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/PingppCollectionAPIResource.java similarity index 100% rename from src/main/java/com/pingplusplus/model/PingppCollectionAPIResource.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/PingppCollectionAPIResource.java diff --git a/pingpp-sdk/src/main/java/com/pingplusplus/model/PingppError.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/PingppError.java new file mode 100644 index 0000000..49cfa1a --- /dev/null +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/PingppError.java @@ -0,0 +1,27 @@ +package com.pingplusplus.model; + +public class PingppError extends PingppObject { + String type; + + String message; + + String code; + + String param; + + public String getType() { + return type; + } + + public String getMessage() { + return message; + } + + public String getCode() { + return code; + } + + public String getParam() { + return param; + } +} diff --git a/src/main/java/com/pingplusplus/model/PingppObject.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/PingppObject.java similarity index 76% rename from src/main/java/com/pingplusplus/model/PingppObject.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/PingppObject.java index 6f58a7b..ee89588 100644 --- a/src/main/java/com/pingplusplus/model/PingppObject.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/PingppObject.java @@ -3,18 +3,19 @@ import com.google.gson.FieldNamingPolicy; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.pingplusplus.net.PingppResponse; import com.pingplusplus.serializer.*; import java.lang.reflect.Field; -public abstract class PingppObject { +public abstract class PingppObject implements PingppObjectInterface { public static final Gson PRETTY_PRINT_GSON = new GsonBuilder() .setPrettyPrinting() .serializeNulls() .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) .disableHtmlEscaping() - .registerTypeAdapter(BatchTransferRecipientSerializer.class, new BatchTransferRecipientSerializer()) + .registerTypeAdapter(BatchTransferRecipient.class, new BatchTransferRecipientSerializer()) .registerTypeAdapter(BatchRefundCharges.class, new BatchRefundChargesSerializer()) .registerTypeAdapter(Double.class, new DoubleTypeSerializer()) .registerTypeAdapter(ChargeEssentials.class, new ChargeEssentialsSerializer()) @@ -41,4 +42,16 @@ public static Gson getPrettyPrintGson() { public String toString() { return getPrettyPrintGson().toJson(this); } + + private transient PingppResponse lastResponse; + + @Override + public PingppResponse getLastResponse() { + return lastResponse; + } + + @Override + public void setLastResponse(PingppResponse response) { + this.lastResponse = response; + } } diff --git a/pingpp-sdk/src/main/java/com/pingplusplus/model/PingppObjectInterface.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/PingppObjectInterface.java new file mode 100644 index 0000000..3b68826 --- /dev/null +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/PingppObjectInterface.java @@ -0,0 +1,9 @@ +package com.pingplusplus.model; + +import com.pingplusplus.net.PingppResponse; + +public interface PingppObjectInterface { + public PingppResponse getLastResponse(); + + public void setLastResponse(PingppResponse response); +} diff --git a/src/main/java/com/pingplusplus/model/PingppRawJsonObject.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/PingppRawJsonObject.java similarity index 100% rename from src/main/java/com/pingplusplus/model/PingppRawJsonObject.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/PingppRawJsonObject.java diff --git a/src/main/java/com/pingplusplus/model/ProfitTransaction.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/ProfitTransaction.java similarity index 63% rename from src/main/java/com/pingplusplus/model/ProfitTransaction.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/ProfitTransaction.java index 01987e7..cdd5f23 100644 --- a/src/main/java/com/pingplusplus/model/ProfitTransaction.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/ProfitTransaction.java @@ -1,7 +1,8 @@ package com.pingplusplus.model; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.net.APIResource; +import com.pingplusplus.net.RequestOptions; import java.util.Map; @@ -141,15 +142,9 @@ public void setFailureMsg(String failureMsg) { * * @param id id * @return ProfitTransaction - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException + * @throws PingppException */ - public static ProfitTransaction retrieve(String id) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { + public static ProfitTransaction retrieve(String id) throws PingppException { return retrieve(id, null); } @@ -157,19 +152,12 @@ public static ProfitTransaction retrieve(String id) throws AuthenticationExcepti * 查询分账明细 * * @param id id - * @param apiKey Ping++ ApiKey + * @param options the specific options * @return ProfitTransaction - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static ProfitTransaction retrieve(String id, String apiKey) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return request(RequestMethod.GET, instanceURL(ProfitTransaction.class, id), apiKey, null, ProfitTransaction.class); + public static ProfitTransaction retrieve(String id, RequestOptions options) throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, instanceURL(ProfitTransaction.class, id), null, ProfitTransaction.class, options); } /** @@ -177,34 +165,23 @@ public static ProfitTransaction retrieve(String id, String apiKey) throws Authen * * @param params 分页参数等 * @return ProfitTransactionCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException + * @throws PingppException */ public static ProfitTransactionCollection list(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { + throws PingppException { return list(params, null); } /** * 查询分账明细列表 * - * @param apiKey Ping++ APiKey * @param params 分页参数等 + * @param options the specific options * @return ProfitTransactionCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static ProfitTransactionCollection list(Map params, String apiKey) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(RequestMethod.GET, classURL(ProfitTransaction.class), apiKey, params, ProfitTransactionCollection.class); + public static ProfitTransactionCollection list(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, classURL(ProfitTransaction.class), params, ProfitTransactionCollection.class, options); } } diff --git a/src/main/java/com/pingplusplus/model/ProfitTransactionCollection.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/ProfitTransactionCollection.java similarity index 100% rename from src/main/java/com/pingplusplus/model/ProfitTransactionCollection.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/ProfitTransactionCollection.java diff --git a/src/main/java/com/pingplusplus/model/Recharge.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/Recharge.java similarity index 68% rename from src/main/java/com/pingplusplus/model/Recharge.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/Recharge.java index bda72d2..5a08820 100644 --- a/src/main/java/com/pingplusplus/model/Recharge.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/Recharge.java @@ -1,8 +1,9 @@ package com.pingplusplus.model; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.net.APIResource; import com.pingplusplus.net.AppBasedResource; +import com.pingplusplus.net.RequestOptions; import java.util.Map; @@ -166,17 +167,24 @@ public void setMetadata(Map metadata) { * * @param params * @return Recharge - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static Recharge create(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.POST, classURL(Recharge.class), params, Recharge.class); + throws PingppException { + return create(params, null); + } + + /** + * 创建 recharge + * + * @param params + * @param options the specific options + * @return Recharge + * @throws PingppException + */ + public static Recharge create(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.POST, classURL(Recharge.class), params, Recharge.class, options); } /** @@ -184,17 +192,24 @@ public static Recharge create(Map params) * * @param id * @return Recharge - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static Recharge retrieve(String id) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.GET, instanceURL(Recharge.class, id), null, Recharge.class); + throws PingppException { + return retrieve(id, null); + } + + /** + * 查询 recharge + * + * @param id + * @param options the specific options + * @return Recharge + * @throws PingppException + */ + public static Recharge retrieve(String id, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, instanceURL(Recharge.class, id), null, Recharge.class, options); } /** @@ -202,16 +217,23 @@ public static Recharge retrieve(String id) * * @param params * @return RechargeCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static RechargeCollection list(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.GET, classURL(Recharge.class), params, RechargeCollection.class); + throws PingppException { + return list(params, null); + } + + /** + * 查询 recharge 列表 + * + * @param params + * @param options the specific options + * @return RechargeCollection + * @throws PingppException + */ + public static RechargeCollection list(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, classURL(Recharge.class), params, RechargeCollection.class, options); } } diff --git a/src/main/java/com/pingplusplus/model/RechargeCollection.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/RechargeCollection.java similarity index 100% rename from src/main/java/com/pingplusplus/model/RechargeCollection.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/RechargeCollection.java diff --git a/pingpp-sdk/src/main/java/com/pingplusplus/model/RechargeRefund.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/RechargeRefund.java new file mode 100644 index 0000000..99a9f07 --- /dev/null +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/RechargeRefund.java @@ -0,0 +1,120 @@ +package com.pingplusplus.model; + +import com.pingplusplus.exception.PingppException; +import com.pingplusplus.net.APIResource; +import com.pingplusplus.net.AppBasedResource; +import com.pingplusplus.net.RequestOptions; + +import java.util.Map; + +public class RechargeRefund extends AppBasedResource { + /** + * 创建 recharge_refund + * + * @param rechargeId + * @param params + * @return Refund + * @throws PingppException + */ + public static Refund create(String rechargeId, Map params) + throws PingppException { + return create(rechargeId, params, null); + } + + /** + * 创建 recharge_refund + * + * @param rechargeId + * @param params + * @param options the specific options + * @return Refund + * @throws PingppException + */ + public static Refund create(String rechargeId, Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.POST, String.format("%s/refunds", instanceURL(Recharge.class, rechargeId)), + params, Refund.class, options); + } + + /** + * 查询 recharge_refund + * + * @param rechargeId + * @param refundId + * @return Refund + * @throws PingppException + */ + public static Refund retrieve(String rechargeId, String refundId) + throws PingppException { + return retrieve(rechargeId, refundId, null); + } + + /** + * 查询 recharge_refund + * + * @param rechargeId + * @param refundId + * @param options the specific options + * @return Refund + * @throws PingppException + */ + public static Refund retrieve(String rechargeId, String refundId, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, String.format("%s/refunds/%s", instanceURL(Recharge.class, rechargeId), refundId), + null, Refund.class, options); + } + + /** + * 查询 recharge_refund 列表 + * + * @param rechargeId + * @return RechargeRefundCollection + * @throws PingppException + */ + public static RechargeRefundCollection list(String rechargeId) + throws PingppException { + return list(rechargeId, null, null); + } + + /** + * 查询 recharge_refund 列表 + * + * @param rechargeId + * @param options the specific options + * @return RechargeRefundCollection + * @throws PingppException + */ + public static RechargeRefundCollection list(String rechargeId, RequestOptions options) + throws PingppException { + return list(rechargeId, null, options); + + } + + /** + * 查询 recharge_refund 列表 + * + * @param rechargeId + * @param params + * @return RechargeRefundCollection + * @throws PingppException + */ + public static RechargeRefundCollection list(String rechargeId, Mapparams) + throws PingppException { + return list(rechargeId, params, null); + } + + /** + * 查询 recharge_refund 列表 + * + * @param rechargeId + * @param params + * @param options the specific options + * @return RechargeRefundCollection + * @throws PingppException + */ + public static RechargeRefundCollection list(String rechargeId, Mapparams, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, String.format("%s/refunds", instanceURL(Recharge.class, rechargeId)), + params, RechargeRefundCollection.class, options); + } +} diff --git a/src/main/java/com/pingplusplus/model/RechargeRefundCollection.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/RechargeRefundCollection.java similarity index 100% rename from src/main/java/com/pingplusplus/model/RechargeRefundCollection.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/RechargeRefundCollection.java diff --git a/src/main/java/com/pingplusplus/model/RedEnvelope.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/RedEnvelope.java similarity index 57% rename from src/main/java/com/pingplusplus/model/RedEnvelope.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/RedEnvelope.java index 07d4a6b..f433bb9 100644 --- a/src/main/java/com/pingplusplus/model/RedEnvelope.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/RedEnvelope.java @@ -8,8 +8,9 @@ import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializer; import com.google.gson.LongSerializationPolicy; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.net.APIResource; +import com.pingplusplus.net.RequestOptions; import java.lang.reflect.Type; import java.util.Map; @@ -206,41 +207,29 @@ public void setMetadata(Map metadata) { this.metadata = metadata; } - /** * 创建 RedEnvelope * * @param params * @return RedEnvelope - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException + * @throws PingppException */ public static RedEnvelope create(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return create(null, params); + throws PingppException { + return create(params, null); } /** * 创建 RedEnvelope * - * @param apiKey Ping++ ApiKey * @param params + * @param options the specific options * @return RedEnvelope - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static RedEnvelope create(String apiKey, Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(RequestMethod.POST, classURL(RedEnvelope.class), apiKey, params, RedEnvelope.class); + public static RedEnvelope create(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.POST, classURL(RedEnvelope.class), params, RedEnvelope.class, options); } /** @@ -248,15 +237,9 @@ public static RedEnvelope create(String apiKey, Map params) * * @param id * @return RedEnvelope - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException + * @throws PingppException */ - public static RedEnvelope retrieve(String id) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { + public static RedEnvelope retrieve(String id) throws PingppException { return retrieve(id, null, null); } @@ -264,19 +247,12 @@ public static RedEnvelope retrieve(String id) throws AuthenticationException, * 查询 RedEnvelope * * @param id - * @param apiKey Ping++ ApiKey + * @param options the specific options * @return RedEnvelope - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static RedEnvelope retrieve(String id, String apiKey) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return retrieve(id, apiKey, null); + public static RedEnvelope retrieve(String id, RequestOptions options) throws PingppException { + return retrieve(id, null, options); } /** @@ -285,36 +261,23 @@ public static RedEnvelope retrieve(String id, String apiKey) throws Authenticati * @param id * @param params * @return RedEnvelope - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException + * @throws PingppException */ - public static RedEnvelope retrieve(String id, Map params) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return retrieve(id, null, params); + public static RedEnvelope retrieve(String id, Map params) throws PingppException { + return retrieve(id, params, null); } /** * 查询 RedEnvelope * * @param id - * @param apiKey Ping++ ApiKey * @param params + * @param options the specific options * @return RedEnvelope - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static RedEnvelope retrieve(String id, String apiKey, Map params) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return request(RequestMethod.GET, instanceURL(RedEnvelope.class, id), apiKey, params, RedEnvelope.class); + public static RedEnvelope retrieve(String id, Map params, RequestOptions options) throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, instanceURL(RedEnvelope.class, id), params, RedEnvelope.class, options); } /** @@ -322,34 +285,24 @@ public static RedEnvelope retrieve(String id, String apiKey, Map * * @param params * @return RedEnvelopeCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException + * @throws PingppException */ public static RedEnvelopeCollection list(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return list(null, params); + throws PingppException { + return list(params, null); } /** * 查询 RedEnvelope * - * @param apiKey Ping++ ApiKey * @param params + * @param options the specific options * @return RedEnvelopeCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static RedEnvelopeCollection list(String apiKey, Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(RequestMethod.GET, classURL(RedEnvelope.class), apiKey, params, RedEnvelopeCollection.class); + public static RedEnvelopeCollection list(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, classURL(RedEnvelope.class), params, RedEnvelopeCollection.class, options); } + } diff --git a/src/main/java/com/pingplusplus/model/RedEnvelopeCollection.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/RedEnvelopeCollection.java similarity index 100% rename from src/main/java/com/pingplusplus/model/RedEnvelopeCollection.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/RedEnvelopeCollection.java diff --git a/src/main/java/com/pingplusplus/model/Refund.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/Refund.java similarity index 53% rename from src/main/java/com/pingplusplus/model/Refund.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/Refund.java index e6205e4..9120769 100644 --- a/src/main/java/com/pingplusplus/model/Refund.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/Refund.java @@ -1,15 +1,18 @@ package com.pingplusplus.model; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.net.APIResource; +import com.pingplusplus.net.RequestOptions; import java.util.Map; public class Refund extends APIResource { String id; + String app; String object; String orderNo; Integer amount; + String currency; Long created; Boolean succeed; String status; @@ -24,7 +27,7 @@ public class Refund extends APIResource { String fundingSource; Map extra; - public String getInstanceURL() throws InvalidRequestException { + public String getInstanceURL() { if (this.charge != null) { return String.format("%s/%s/refunds/%s", classURL(Charge.class), this.charge, this.getId()); } @@ -166,123 +169,101 @@ public void setExtra(Map extra) { this.extra = extra; } + public String getCurrency() { + return currency; + } + + public void setCurrency(String currency) { + this.currency = currency; + } + + public String getApp() { + return app; + } + + public void setApp(String app) { + this.app = app; + } /** * 创建 refund * - * @param charge + * @param chargeId * @param params * @return Refund - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static Refund create(String charge, Map params) - throws AuthenticationException, InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return create(charge, null, params); + public static Refund create(String chargeId, Map params) + throws PingppException { + return create(chargeId, params, null); } /** * 创建 refund * - * @param charge - * @param apiKey Ping++ ApiKey + * @param chargeId * @param params + * @param options the specific options * @return Refund - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static Refund create(String charge, String apiKey, Map params) - throws AuthenticationException, InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return request(RequestMethod.POST, String.format("%s/refunds", instanceURL(Charge.class, charge)), - apiKey, params, Refund.class); + public static Refund create(String chargeId, Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.POST, String.format("%s/refunds", instanceURL(Charge.class, chargeId)), + params, Refund.class, options); } /** * 查询 refund * - * @param charge + * @param chargeId * @param id * @return Refund - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static Refund retrieve(String charge, String id) - throws AuthenticationException, InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return retrieve(charge, id, null); + public static Refund retrieve(String chargeId, String id) + throws PingppException { + return retrieve(chargeId, id, null); } /** * 查询 refund * - * @param charge + * @param chargeId * @param id - * @param apiKey Ping++ ApiKey + * @param options the specific options * @return Refund - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static Refund retrieve(String charge, String id, String apiKey) - throws AuthenticationException, InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return request(RequestMethod.GET, String.format("%s/refunds/%s", instanceURL(Charge.class, charge), id), - apiKey, null, Refund.class); + public static Refund retrieve(String chargeId, String id, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, String.format("%s/refunds/%s", instanceURL(Charge.class, chargeId), id),null, Refund.class, options); } /** * 查询 refund 列表 * - * @param charge + * @param chargeId * @param params * @return ChargeRefundCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static ChargeRefundCollection list(String charge, Mapparams) - throws AuthenticationException, InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return list(charge, null, params); + public static ChargeRefundCollection list(String chargeId, Mapparams) + throws PingppException { + return list(chargeId, params, null); } /** * 查询 refund 列表 * - * @param charge - * @param apiKey Ping++ ApiKey + * @param chargeId * @param params + * @param options the specific options * @return ChargeRefundCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static ChargeRefundCollection list(String charge, String apiKey, Mapparams) - throws AuthenticationException, InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return request(RequestMethod.GET, String.format("%s/refunds", instanceURL(Charge.class, charge)), - apiKey, params, ChargeRefundCollection.class); + public static ChargeRefundCollection list(String chargeId, Mapparams, RequestOptions options) + throws PingppException { + return request(RequestMethod.GET, String.format("%s/refunds", instanceURL(Charge.class, chargeId)), + params, ChargeRefundCollection.class, options); } } diff --git a/src/main/java/com/pingplusplus/model/Royalty.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/Royalty.java similarity index 63% rename from src/main/java/com/pingplusplus/model/Royalty.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/Royalty.java index 1b85595..c641ec7 100644 --- a/src/main/java/com/pingplusplus/model/Royalty.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/Royalty.java @@ -1,7 +1,8 @@ package com.pingplusplus.model; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.net.APIResource; +import com.pingplusplus.net.RequestOptions; import java.util.Map; @@ -183,17 +184,22 @@ public void setRoyaltySettlement(String royaltySettlement) { * * @param id * @return Royalty - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static Royalty retrieve(String id) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.GET, instanceURL(Royalty.class, id), null, Royalty.class); + public static Royalty retrieve(String id) throws PingppException { + return retrieve(id, null); + } + + /** + * 查询 royalty + * + * @param id + * @param options the specific options + * @return Royalty + * @throws PingppException + */ + public static Royalty retrieve(String id, RequestOptions options) throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, instanceURL(Royalty.class, id), null, Royalty.class, options); } /** @@ -201,17 +207,24 @@ public static Royalty retrieve(String id) throws AuthenticationException, * * @param params * @return RoyaltyCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static RoyaltyCollection list(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.GET, classURL(Royalty.class), params, RoyaltyCollection.class); + throws PingppException { + return list(params, null); + } + + /** + * 查询 royalty 列表 + * + * @param params + * @param options the specific options + * @return RoyaltyCollection + * @throws PingppException + */ + public static RoyaltyCollection list(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, classURL(Royalty.class), params, RoyaltyCollection.class, options); } /** @@ -219,17 +232,24 @@ public static RoyaltyCollection list(Map params) * * @param params * @return RoyaltyCollection - * @throws AuthenticationException 认证异常 - * @throws InvalidRequestException 错误请求 - * @throws APIConnectionException 连接异常 - * @throws APIException 系统异常 - * @throws ChannelException 渠道异常 - * @throws RateLimitException 请求超限 + * @throws PingppException */ public static RoyaltyCollection batchUpdate(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.PUT, classURL(Royalty.class), params, RoyaltyCollection.class); + throws PingppException { + return batchUpdate(params, null); + } + + /** + * 批量更新 royalty + * + * @param params + * @param options the specific options + * @return RoyaltyCollection + * @throws PingppException + */ + public static RoyaltyCollection batchUpdate(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.PUT, classURL(Royalty.class), params, RoyaltyCollection.class, options); } /** @@ -238,16 +258,10 @@ public static RoyaltyCollection batchUpdate(Map params) * @param orderId 订单 ID * @param params 分润信息参数 * @return RoyaltyDataResult - * @throws AuthenticationException 认证异常 - * @throws InvalidRequestException 错误请求 - * @throws APIConnectionException 连接异常 - * @throws APIException 系统异常 - * @throws ChannelException 渠道异常 - * @throws RateLimitException 请求超限 + * @throws PingppException */ public static RoyaltyDataResult createData(String orderId, Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { + throws PingppException { return createData(orderId, params, null); } @@ -256,22 +270,15 @@ public static RoyaltyDataResult createData(String orderId, Map p * * @param orderId 订单 ID * @param params 分润信息参数 - * @param apiKey API Key + * @param options the specific options * @return RoyaltyDataResult - * @throws AuthenticationException 认证异常 - * @throws InvalidRequestException 错误请求 - * @throws APIConnectionException 连接异常 - * @throws APIException 系统异常 - * @throws ChannelException 渠道异常 - * @throws RateLimitException 请求超限 + * @throws PingppException */ - public static RoyaltyDataResult createData(String orderId, Map params, String apiKey) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.POST, + public static RoyaltyDataResult createData(String orderId, Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.POST, String.format("%s/royalty_datas", instanceURL(Order.class, orderId)), - apiKey, params, - RoyaltyDataResult.class); + RoyaltyDataResult.class, options); } } diff --git a/src/main/java/com/pingplusplus/model/RoyaltyCollection.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/RoyaltyCollection.java similarity index 100% rename from src/main/java/com/pingplusplus/model/RoyaltyCollection.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/RoyaltyCollection.java diff --git a/src/main/java/com/pingplusplus/model/RoyaltyDataResult.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/RoyaltyDataResult.java similarity index 100% rename from src/main/java/com/pingplusplus/model/RoyaltyDataResult.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/RoyaltyDataResult.java diff --git a/src/main/java/com/pingplusplus/model/RoyaltySettlement.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/RoyaltySettlement.java similarity index 65% rename from src/main/java/com/pingplusplus/model/RoyaltySettlement.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/RoyaltySettlement.java index 8403002..e095ecc 100644 --- a/src/main/java/com/pingplusplus/model/RoyaltySettlement.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/RoyaltySettlement.java @@ -1,7 +1,8 @@ package com.pingplusplus.model; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.net.APIResource; +import com.pingplusplus.net.RequestOptions; import java.util.Map; @@ -192,17 +193,22 @@ public void setMetadata(Map metadata) { * * @param params * @return RoyaltySettlement - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static RoyaltySettlement create(Map params) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.POST, classURL(RoyaltySettlement.class), params, RoyaltySettlement.class); + public static RoyaltySettlement create(Map params) throws PingppException { + return create(params, null); + } + + /** + * 创建 royalty_settlement + * + * @param params + * @param options the specific options + * @return RoyaltySettlement + * @throws PingppException + */ + public static RoyaltySettlement create(Map params, RequestOptions options) throws PingppException { + return APIResource.request(APIResource.RequestMethod.POST, classURL(RoyaltySettlement.class), params, RoyaltySettlement.class, options); } /** @@ -210,17 +216,22 @@ public static RoyaltySettlement create(Map params) throws Authen * * @param id * @return RoyaltySettlement - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static RoyaltySettlement retrieve(String id) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.GET, instanceURL(RoyaltySettlement.class, id), null, RoyaltySettlement.class); + public static RoyaltySettlement retrieve(String id) throws PingppException { + return retrieve(id, null); + } + + /** + * 查询 royalty_settlement + * + * @param id + * @param options the specific options + * @return RoyaltySettlement + * @throws PingppException + */ + public static RoyaltySettlement retrieve(String id, RequestOptions options) throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, instanceURL(RoyaltySettlement.class, id), null, RoyaltySettlement.class, options); } /** @@ -228,17 +239,24 @@ public static RoyaltySettlement retrieve(String id) throws AuthenticationExcepti * * @param params * @return RoyaltySettlement - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static RoyaltySettlementCollection list(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.GET, classURL(RoyaltySettlement.class), params, RoyaltySettlementCollection.class); + throws PingppException { + return list(params, null); + } + + /** + * 查询 royalty_settlement 列表 + * + * @param params + * @param options the specific options + * @return RoyaltySettlement + * @throws PingppException + */ + public static RoyaltySettlementCollection list(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, classURL(RoyaltySettlement.class), params, RoyaltySettlementCollection.class, options); } /** @@ -247,16 +265,24 @@ public static RoyaltySettlementCollection list(Map params) * @param id * @param params * @return RoyaltySettlement - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static RoyaltySettlement update(String id, Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.PUT, instanceURL(RoyaltySettlement.class, id), params, RoyaltySettlement.class); + throws PingppException { + return update(id, params, null); + } + + /** + * 更新 royalty_settlement + * + * @param id + * @param params + * @param options the specific options + * @return RoyaltySettlement + * @throws PingppException + */ + public static RoyaltySettlement update(String id, Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.PUT, instanceURL(RoyaltySettlement.class, id), params, RoyaltySettlement.class, options); } } diff --git a/src/main/java/com/pingplusplus/model/RoyaltySettlementCollection.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/RoyaltySettlementCollection.java similarity index 100% rename from src/main/java/com/pingplusplus/model/RoyaltySettlementCollection.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/RoyaltySettlementCollection.java diff --git a/pingpp-sdk/src/main/java/com/pingplusplus/model/RoyaltyTemplate.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/RoyaltyTemplate.java new file mode 100644 index 0000000..94f2877 --- /dev/null +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/RoyaltyTemplate.java @@ -0,0 +1,203 @@ +package com.pingplusplus.model; + +import com.pingplusplus.exception.PingppException; +import com.pingplusplus.net.APIResource; +import com.pingplusplus.net.RequestOptions; + +import java.util.Map; + +public class RoyaltyTemplate extends APIResource { + String id; + String object; + Boolean livemode; + String app; + String name; + Long created; + String description; + Map rule; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getApp() { + return app; + } + + public void setApp(String app) { + this.app = app; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Long getCreated() { + return created; + } + + public void setCreated(Long created) { + this.created = created; + } + + public Map getRule() { + return rule; + } + + public void setRule(Map rule) { + this.rule = rule; + } + + public String getObject() { + return object; + } + + public void setObject(String object) { + this.object = object; + } + + public Boolean getLivemode() { + return livemode; + } + + public void setLivemode(Boolean livemode) { + this.livemode = livemode; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + /** + * 创建 royalty_template + * + * @param params + * @return RoyaltyTemplate + * @throws PingppException + */ + public static RoyaltyTemplate create(Map params) throws PingppException { + return create(params, null); + } + + /** + * 创建 royalty_template + * + * @param params + * @param options the specific options + * @return RoyaltyTemplate + * @throws PingppException + */ + public static RoyaltyTemplate create(Map params, RequestOptions options) throws PingppException { + return APIResource.request(APIResource.RequestMethod.POST, classURL(RoyaltyTemplate.class), params, RoyaltyTemplate.class, options); + } + + /** + * 查询 royalty_template + * + * @param id + * @return RoyaltyTemplate + * @throws PingppException + */ + public static RoyaltyTemplate retrieve(String id) throws PingppException { + return retrieve(id, null); + } + + /** + * 查询 royalty_template + * + * @param id + * @param options the specific options + * @return RoyaltyTemplate + * @throws PingppException + */ + public static RoyaltyTemplate retrieve(String id, RequestOptions options) throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, instanceURL(RoyaltyTemplate.class, id), null, RoyaltyTemplate.class, options); + } + + /** + * 查询 royalty_template 列表 + * + * @param params + * @return RoyaltyTemplateCollection + * @throws PingppException + */ + public static RoyaltyTemplateCollection list(Map params) + throws PingppException { + return list(params, null); + } + + /** + * 查询 royalty_template 列表 + * + * @param params + * @param options the specific options + * @return RoyaltyTemplateCollection + * @throws PingppException + */ + public static RoyaltyTemplateCollection list(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, classURL(RoyaltyTemplate.class), params, RoyaltyTemplateCollection.class, options); + } + + /** + * 删除 royalty_template + * + * @param id + * @return DeleteRoyaltyTemplate + * @throws PingppException + */ + public static DeleteRoyaltyTemplate delete(String id) + throws PingppException { + return delete(id, null); + } + + /** + * 删除 royalty_template + * + * @param id + * @param options the specific options + * @return DeleteRoyaltyTemplate + * @throws PingppException + */ + public static DeleteRoyaltyTemplate delete(String id, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.DELETE, instanceURL(RoyaltyTemplate.class, id), null, DeleteRoyaltyTemplate.class, options); + } + + /** + * 更新 royalty_template + * + * @param id + * @return RoyaltyTemplate + * @throws PingppException + */ + public static RoyaltyTemplate update(String id, Map params) + throws PingppException { + return update(id, params, null); + } + + /** + * 更新 royalty_template + * + * @param id + * @param options the specific options + * @return RoyaltyTemplate + * @throws PingppException + */ + public static RoyaltyTemplate update(String id, Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.PUT, instanceURL(RoyaltyTemplate.class, id), params, RoyaltyTemplate.class, options); + } +} diff --git a/src/main/java/com/pingplusplus/model/RoyaltyTemplateCollection.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/RoyaltyTemplateCollection.java similarity index 100% rename from src/main/java/com/pingplusplus/model/RoyaltyTemplateCollection.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/RoyaltyTemplateCollection.java diff --git a/src/main/java/com/pingplusplus/model/RoyaltyTransaction.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/RoyaltyTransaction.java similarity index 67% rename from src/main/java/com/pingplusplus/model/RoyaltyTransaction.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/RoyaltyTransaction.java index a620595..c117952 100644 --- a/src/main/java/com/pingplusplus/model/RoyaltyTransaction.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/RoyaltyTransaction.java @@ -1,7 +1,8 @@ package com.pingplusplus.model; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.net.APIResource; +import com.pingplusplus.net.RequestOptions; import java.util.Map; @@ -111,17 +112,22 @@ public void setTransfer(String transfer) { * * @param id * @return RoyaltyTransaction - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static RoyaltyTransaction retrieve(String id) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.GET, instanceURL(RoyaltyTransaction.class, id), null, RoyaltyTransaction.class); + public static RoyaltyTransaction retrieve(String id) throws PingppException { + return retrieve(id, null); + } + + /** + * 查询 royalty_transaction + * + * @param id + * @param options the specific options + * @return RoyaltyTransaction + * @throws PingppException + */ + public static RoyaltyTransaction retrieve(String id, RequestOptions options) throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, instanceURL(RoyaltyTransaction.class, id), null, RoyaltyTransaction.class, options); } /** @@ -129,16 +135,23 @@ public static RoyaltyTransaction retrieve(String id) throws AuthenticationExcept * * @param params * @return RoyaltyTransactionCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static RoyaltyTransactionCollection list(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.GET, classURL(RoyaltyTransaction.class), params, RoyaltyTransactionCollection.class); + throws PingppException { + return list(params, null); + } + + /** + * 查询 royalty_transaction 列表 + * + * @param params + * @param options the specific options + * @return RoyaltyTransactionCollection + * @throws PingppException + */ + public static RoyaltyTransactionCollection list(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, classURL(RoyaltyTransaction.class), params, RoyaltyTransactionCollection.class, options); } } diff --git a/src/main/java/com/pingplusplus/model/RoyaltyTransactionCollection.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/RoyaltyTransactionCollection.java similarity index 100% rename from src/main/java/com/pingplusplus/model/RoyaltyTransactionCollection.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/RoyaltyTransactionCollection.java diff --git a/pingpp-sdk/src/main/java/com/pingplusplus/model/SettleAccount.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/SettleAccount.java new file mode 100644 index 0000000..5794490 --- /dev/null +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/SettleAccount.java @@ -0,0 +1,268 @@ +package com.pingplusplus.model; + +import com.pingplusplus.exception.PingppException; +import com.pingplusplus.net.APIResource; +import com.pingplusplus.net.RequestOptions; +import com.pingplusplus.net.UserBasedResource; + +import java.util.Map; + +public class SettleAccount extends UserBasedResource { + String id; + String object; + Boolean livemode; + String channel; + Long created; + SettleAccountRecipient recipient; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getObject() { + return object; + } + + public void setObject(String object) { + this.object = object; + } + + public String getChannel() { + return channel; + } + + public void setChannel(String channel) { + this.channel = channel; + } + + public Long getCreated() { + return created; + } + + public void setCreated(Long created) { + this.created = created; + } + + public SettleAccountRecipient getRecipient() { + return recipient; + } + + public void setRecipient(SettleAccountRecipient recipient) { + this.recipient = recipient; + } + + public Boolean getLivemode() { + return livemode; + } + + public void setLivemode(Boolean livemode) { + this.livemode = livemode; + } + + /** + * 创建 settle_account + * + * @param userId + * @param params + * @return SettleAccount + * @throws PingppException + */ + public static SettleAccount create(String userId, Mapparams) + throws PingppException { + User.checkUserId(userId); + return create(userId, params, null); + } + + /** + * 创建 settle_account + * + * @param userId + * @param params + * @param options the specific options + * @return SettleAccount + * @throws PingppException + */ + public static SettleAccount create(String userId, Mapparams, RequestOptions options) + throws PingppException { + User.checkUserId(userId); + return APIResource.request(APIResource.RequestMethod.POST, classURL(SettleAccount.class, userId), params, SettleAccount.class, options); + } + + /** + * 查询 settle_account + * + * @param userId + * @param id + * @return SettleAccount + * @throws PingppException + */ + public static SettleAccount retrieve(String userId, String id) + throws PingppException { + User.checkUserId(userId); + return retrieve(userId, id, null); + } + + /** + * 查询 settle_account + * + * @param userId + * @param id + * @param options the specific options + * @return SettleAccount + * @throws PingppException + */ + public static SettleAccount retrieve(String userId, String id, RequestOptions options) + throws PingppException { + User.checkUserId(userId); + return APIResource.request(APIResource.RequestMethod.GET, instanceURL(SettleAccount.class, userId, id), null, SettleAccount.class, options); + } + + /** + * 查询 settle_account 列表 + * + * @param userId + * @param params + * @return SettleAccountCollection + * @throws PingppException + */ + public static SettleAccountCollection list(String userId, Map params) + throws PingppException { + User.checkUserId(userId); + return list(userId, params, null); + } + + /** + * 查询 settle_account 列表 + * + * @param userId + * @param params + * @param options the specific options + * @return SettleAccountCollection + * @throws PingppException + */ + public static SettleAccountCollection list(String userId, Map params, RequestOptions options) + throws PingppException { + User.checkUserId(userId); + return APIResource.request(APIResource.RequestMethod.GET, classURL(SettleAccount.class, userId), params, SettleAccountCollection.class, options); + } + + /** + * 删除 settle_account + * + * @param userId + * @param id + * @return DeletedSettleAccount + * @throws PingppException + */ + public static DeletedSettleAccount delete(String userId, String id) + throws PingppException { + User.checkUserId(userId); + return delete(userId, id, null); + } + + /** + * 删除 settle_account + * + * @param userId + * @param id + * @param options the specific options + * @return DeletedSettleAccount + * @throws PingppException + */ + public static DeletedSettleAccount delete(String userId, String id, RequestOptions options) + throws PingppException { + User.checkUserId(userId); + return APIResource.request(APIResource.RequestMethod.DELETE, instanceURL(SettleAccount.class, userId, id), null, DeletedSettleAccount.class, options); + } + + /** + * 更新 settle_account + * + * @param userId 用户 ID + * @param id 结算账户 ID + * @param params 更新数据 + * @return SettleAccount + * @throws PingppException + */ + public static SettleAccount update(String userId, String id, Map params) + throws PingppException { + User.checkUserId(userId); + return update(userId, id, params, null); + } + + /** + * 更新 settle_account + * + * @param userId 用户 ID + * @param id 结算账户 ID + * @param params 更新数据 + * @param options the specific options + * @return SettleAccount + * @throws PingppException + */ + public static SettleAccount update(String userId, String id, Map params, RequestOptions options) + throws PingppException { + User.checkUserId(userId); + return APIResource.request(APIResource.RequestMethod.PUT, instanceURL(SettleAccount.class, userId, id), params, SettleAccount.class, options); + } + + /** + * 更新银行手机号 + * + * @param userId 用户 ID + * @param id 结算账号 ID + * @param params 更新参数 + * @return SettleAccount + * @throws PingppException + */ + public static SettleAccount updateMobile(String userId, String id, Map params) throws PingppException { + return updateMobile(userId, id, params, null); + } + + /** + * 更新银行手机号 + * + * @param userId 用户 ID + * @param id 结算账号 ID + * @param params 更新参数 + * @param options the specific options + * @return SettleAccount + * @throws PingppException + */ + public static SettleAccount updateMobile(String userId, String id, Map params, RequestOptions options) throws PingppException { + return APIResource.request(APIResource.RequestMethod.PUT, String.format("%s/mobile", instanceURL(SettleAccount.class, userId, id)), + params, SettleAccount.class, options); + } + + /** + * 打款验证 + * + * @param userId 用户 ID + * @param id 结算账号 ID + * @param params 参数 + * @return SettleAccount + * @throws PingppException + */ + public static SettleAccount verify(String userId, String id, Map params) throws PingppException { + return verify(userId, id, params, null); + } + + /** + * 打款验证 + * + * @param userId 用户 ID + * @param id 结算账号 ID + * @param params 参数 + * @param options the specific options + * @return SettleAccount + * @throws PingppException + */ + public static SettleAccount verify(String userId, String id, Map params, RequestOptions options) throws PingppException { + return APIResource.request(APIResource.RequestMethod.POST, String.format("%s/verify", instanceURL(SettleAccount.class, userId, id)), + params, SettleAccount.class, options); + } +} diff --git a/src/main/java/com/pingplusplus/model/SettleAccountCollection.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/SettleAccountCollection.java similarity index 100% rename from src/main/java/com/pingplusplus/model/SettleAccountCollection.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/SettleAccountCollection.java diff --git a/src/main/java/com/pingplusplus/model/SettleAccountRecipient.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/SettleAccountRecipient.java similarity index 100% rename from src/main/java/com/pingplusplus/model/SettleAccountRecipient.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/SettleAccountRecipient.java diff --git a/src/main/java/com/pingplusplus/model/SplitProfit.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/SplitProfit.java similarity index 58% rename from src/main/java/com/pingplusplus/model/SplitProfit.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/SplitProfit.java index 6ed5c10..f128a2e 100644 --- a/src/main/java/com/pingplusplus/model/SplitProfit.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/SplitProfit.java @@ -1,7 +1,8 @@ package com.pingplusplus.model; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.net.APIResource; +import com.pingplusplus.net.RequestOptions; import java.util.List; import java.util.Map; @@ -151,15 +152,10 @@ public void setMetadata(Map metadata) { * * @param params 参数 * @return SplitProfit - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException + * @throws PingppException */ public static SplitProfit create(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { + throws PingppException { return create(params, null); } @@ -167,19 +163,13 @@ public static SplitProfit create(Map params) * 创建分账 * * @param params 参数 - * @param apiKey Ping++ APiKey + * @param options the specific options * @return SplitProfit - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static SplitProfit create(Map params, String apiKey) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(RequestMethod.POST, classURL(SplitProfit.class), apiKey, params, SplitProfit.class); + public static SplitProfit create(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.POST, classURL(SplitProfit.class), params, SplitProfit.class, options); } /** @@ -187,15 +177,9 @@ public static SplitProfit create(Map params, String apiKey) * * @param id id * @return SplitProfit - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException + * @throws PingppException */ - public static SplitProfit retrieve(String id) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { + public static SplitProfit retrieve(String id) throws PingppException { return retrieve(id, null); } @@ -203,19 +187,12 @@ public static SplitProfit retrieve(String id) throws AuthenticationException, * 查询分账 * * @param id id - * @param apiKey Ping++ ApiKey + * @param options the specific options * @return SplitProfit - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static SplitProfit retrieve(String id, String apiKey) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return request(RequestMethod.GET, instanceURL(SplitProfit.class, id), apiKey, null, SplitProfit.class); + public static SplitProfit retrieve(String id, RequestOptions options) throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, instanceURL(SplitProfit.class, id), null, SplitProfit.class, options); } /** @@ -223,34 +200,23 @@ public static SplitProfit retrieve(String id, String apiKey) throws Authenticati * * @param params 分页参数等 * @return SplitProfitCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException + * @throws PingppException */ public static SplitProfitCollection list(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { + throws PingppException { return list(params, null); } /** * 查询分账列表 * - * @param apiKey Ping++ APiKey * @param params 分页参数等 + * @param options the specific options * @return SplitProfitCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static SplitProfitCollection list(Map params, String apiKey) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(RequestMethod.GET, classURL(SplitProfit.class), apiKey, params, SplitProfitCollection.class); + public static SplitProfitCollection list(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, classURL(SplitProfit.class), params, SplitProfitCollection.class, options); } } diff --git a/src/main/java/com/pingplusplus/model/SplitProfitCollection.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/SplitProfitCollection.java similarity index 100% rename from src/main/java/com/pingplusplus/model/SplitProfitCollection.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/SplitProfitCollection.java diff --git a/src/main/java/com/pingplusplus/model/SplitProfitRecipient.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/SplitProfitRecipient.java similarity index 100% rename from src/main/java/com/pingplusplus/model/SplitProfitRecipient.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/SplitProfitRecipient.java diff --git a/src/main/java/com/pingplusplus/model/SplitReceiver.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/SplitReceiver.java similarity index 51% rename from src/main/java/com/pingplusplus/model/SplitReceiver.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/SplitReceiver.java index c07e04e..9ca98b3 100644 --- a/src/main/java/com/pingplusplus/model/SplitReceiver.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/SplitReceiver.java @@ -1,7 +1,8 @@ package com.pingplusplus.model; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.net.APIResource; +import com.pingplusplus.net.RequestOptions; import java.util.HashMap; import java.util.Map; @@ -115,15 +116,10 @@ public void setName(String name) { * * @param params 参数 * @return SplitReceiver - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException + * @throws PingppException */ public static SplitReceiver create(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { + throws PingppException { return create(params, null); } @@ -131,19 +127,13 @@ public static SplitReceiver create(Map params) * 创建分账接收方 * * @param params 参数 - * @param apiKey Ping++ APiKey + * @param options the specific options * @return SplitReceiver - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static SplitReceiver create(Map params, String apiKey) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(RequestMethod.POST, classURL(SplitReceiver.class), apiKey, params, SplitReceiver.class); + public static SplitReceiver create(Map params, RequestOptions options) + throws PingppException { + return request(RequestMethod.POST, classURL(SplitReceiver.class), params, SplitReceiver.class, options); } /** @@ -151,15 +141,9 @@ public static SplitReceiver create(Map params, String apiKey) * * @param id id * @return SplitReceiver - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException + * @throws PingppException */ - public static SplitReceiver retrieve(String id) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { + public static SplitReceiver retrieve(String id) throws PingppException { return retrieve(id, null); } @@ -167,19 +151,12 @@ public static SplitReceiver retrieve(String id) throws AuthenticationException, * 查询分账接收方 * * @param id id - * @param apiKey Ping++ ApiKey + * @param options the specific options * @return SplitReceiver - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static SplitReceiver retrieve(String id, String apiKey) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return request(RequestMethod.GET, instanceURL(SplitReceiver.class, id), apiKey, null, SplitReceiver.class); + public static SplitReceiver retrieve(String id, RequestOptions options) throws PingppException { + return request(RequestMethod.GET, instanceURL(SplitReceiver.class, id), null, SplitReceiver.class, options); } /** @@ -187,35 +164,24 @@ public static SplitReceiver retrieve(String id, String apiKey) throws Authentica * * @param params 分页参数等 * @return SplitReceiverCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException + * @throws PingppException */ public static SplitReceiverCollection list(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { + throws PingppException { return list(params, null); } /** * 查询分账接收方列表 * - * @param apiKey Ping++ APiKey * @param params 分页参数等 + * @param options the specific options * @return SplitReceiverCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static SplitReceiverCollection list(Map params, String apiKey) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(RequestMethod.GET, classURL(SplitReceiver.class), apiKey, params, SplitReceiverCollection.class); + public static SplitReceiverCollection list(Map params, RequestOptions options) + throws PingppException { + return request(RequestMethod.GET, classURL(SplitReceiver.class), params, SplitReceiverCollection.class, options); } /** @@ -223,16 +189,9 @@ public static SplitReceiverCollection list(Map params, String ap * * @param id id * @return DeletedSplitReceiver - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static DeletedSplitReceiver delete(String id) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { + public static DeletedSplitReceiver delete(String id) throws PingppException { return delete(id, null); } @@ -240,18 +199,11 @@ public static DeletedSplitReceiver delete(String id) throws AuthenticationExcept * 删除分账接收方 * * @param id id - * @param apiKey Ping++ ApiKey + * @param options the specific options * @return DeletedSplitReceiver - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static DeletedSplitReceiver delete(String id, String apiKey) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return request(RequestMethod.DELETE, instanceURL(SplitReceiver.class, id), apiKey, null, DeletedSplitReceiver.class); + public static DeletedSplitReceiver delete(String id, RequestOptions options) throws PingppException { + return request(RequestMethod.DELETE, instanceURL(SplitReceiver.class, id), null, DeletedSplitReceiver.class, options); } } diff --git a/src/main/java/com/pingplusplus/model/SplitReceiverCollection.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/SplitReceiverCollection.java similarity index 100% rename from src/main/java/com/pingplusplus/model/SplitReceiverCollection.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/SplitReceiverCollection.java diff --git a/src/main/java/com/pingplusplus/model/SubApp.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/SubApp.java similarity index 51% rename from src/main/java/com/pingplusplus/model/SubApp.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/SubApp.java index c285335..6692898 100644 --- a/src/main/java/com/pingplusplus/model/SubApp.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/SubApp.java @@ -1,8 +1,9 @@ package com.pingplusplus.model; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.net.APIResource; import com.pingplusplus.net.AppBasedResource; +import com.pingplusplus.net.RequestOptions; import java.util.List; import java.util.Map; @@ -122,17 +123,24 @@ public void setExtra(Map extra) { * * @param params * @return SubApp - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static SubApp create(Mapparams) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.POST, classURL(SubApp.class), params, SubApp.class); + throws PingppException { + return create(params, null); + } + + /** + * 创建 sub_app + * + * @param params + * @param options the specific options + * @return SubApp + * @throws PingppException + */ + public static SubApp create(Mapparams, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.POST, classURL(SubApp.class), params, SubApp.class, options); } /** @@ -140,17 +148,11 @@ public static SubApp create(Mapparams) * * @param id * @return SubApp - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static SubApp retrieve(String id) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return retrieve(id, null); + throws PingppException { + return retrieve(id, null, null); } /** @@ -159,17 +161,38 @@ public static SubApp retrieve(String id) * @param id SubApp ID * @param params 参数 * @return SubApp - * @throws AuthenticationException 认证异常 - * @throws InvalidRequestException 错误请求 - * @throws APIConnectionException 连接异常 - * @throws APIException 系统异常 - * @throws ChannelException 渠道异常 - * @throws RateLimitException 请求超限 + * @throws PingppException */ public static SubApp retrieve(String id, Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.GET, instanceURL(SubApp.class, id), params, SubApp.class); + throws PingppException { + return retrieve(id, params, null); + } + + /** + * 查询 sub_app + * + * @param id + * @param options the specific options + * @return SubApp + * @throws PingppException + */ + public static SubApp retrieve(String id, RequestOptions options) + throws PingppException { + return retrieve(id, null, options); + } + + /** + * 查询 sub_app + * + * @param id SubApp ID + * @param params 参数 + * @param options the specific options + * @return SubApp + * @throws PingppException + */ + public static SubApp retrieve(String id, Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, instanceURL(SubApp.class, id), params, SubApp.class, options); } /** @@ -177,17 +200,24 @@ public static SubApp retrieve(String id, Map params) * * @param params * @return SubApp - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static SubAppCollection list(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.GET, classURL(SubApp.class), params, SubAppCollection.class); + throws PingppException { + return list(params, null); + } + + /** + * 查询 sub_app 列表 + * + * @param params + * @param options the specific options + * @return SubApp + * @throws PingppException + */ + public static SubAppCollection list(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, classURL(SubApp.class), params, SubAppCollection.class, options); } /** @@ -196,17 +226,25 @@ public static SubAppCollection list(Map params) * @param id * @param params * @return SubApp - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static SubApp update(String id, Mapparams) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.PUT, instanceURL(SubApp.class, id), params, SubApp.class); + throws PingppException { + return update(id, params, null); + } + + /** + * 更新 sub_app + * + * @param id + * @param params + * @param options the specific options + * @return SubApp + * @throws PingppException + */ + public static SubApp update(String id, Mapparams, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.PUT, instanceURL(SubApp.class, id), params, SubApp.class, options); } /** @@ -214,16 +252,23 @@ public static SubApp update(String id, Mapparams) * * @param id * @return DeletedSubApp - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static DeletedSubApp delete(String id) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.DELETE, instanceURL(SubApp.class, id), null, DeletedSubApp.class); + throws PingppException { + return delete(id, null); + } + + /** + * 删除 sub_app + * + * @param id + * @param options the specific options + * @return DeletedSubApp + * @throws PingppException + */ + public static DeletedSubApp delete(String id, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.DELETE, instanceURL(SubApp.class, id), null, DeletedSubApp.class, options); } } diff --git a/src/main/java/com/pingplusplus/model/SubAppCollection.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/SubAppCollection.java similarity index 100% rename from src/main/java/com/pingplusplus/model/SubAppCollection.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/SubAppCollection.java diff --git a/src/main/java/com/pingplusplus/model/SubBank.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/SubBank.java similarity index 61% rename from src/main/java/com/pingplusplus/model/SubBank.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/SubBank.java index b2a36d2..1f93941 100644 --- a/src/main/java/com/pingplusplus/model/SubBank.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/SubBank.java @@ -1,7 +1,8 @@ package com.pingplusplus.model; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.net.APIResource; +import com.pingplusplus.net.RequestOptions; import java.util.Map; @@ -57,15 +58,10 @@ public void setCity(String city) { * * @param params 银行编号和省市信息 * @return SubBankCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException + * @throws PingppException */ public static SubBankCollection query(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { + throws PingppException { return query(params, null); } @@ -73,18 +69,12 @@ public static SubBankCollection query(Map params) * 银行支行列表查询 * * @param params 银行编号和省市信息 - * @param apiKey Ping++ ApiKey + * @param options the specific options * @return SubBankCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static SubBankCollection query(Map params, String apiKey) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(RequestMethod.GET, classURL(SubBank.class), apiKey, params, SubBankCollection.class); + public static SubBankCollection query(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, classURL(SubBank.class), params, SubBankCollection.class, options); } } diff --git a/src/main/java/com/pingplusplus/model/SubBankCollection.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/SubBankCollection.java similarity index 100% rename from src/main/java/com/pingplusplus/model/SubBankCollection.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/SubBankCollection.java diff --git a/src/main/java/com/pingplusplus/model/Summary.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/Summary.java similarity index 100% rename from src/main/java/com/pingplusplus/model/Summary.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/Summary.java diff --git a/src/main/java/com/pingplusplus/model/Transfer.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/Transfer.java similarity index 53% rename from src/main/java/com/pingplusplus/model/Transfer.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/Transfer.java index 3ae6dbf..79da899 100644 --- a/src/main/java/com/pingplusplus/model/Transfer.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/Transfer.java @@ -4,8 +4,9 @@ * Created by sunkai on 15/5/11. */ -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.net.APIResource; +import com.pingplusplus.net.RequestOptions; import java.util.HashMap; import java.util.Map; @@ -31,7 +32,7 @@ public class Transfer extends APIResource { String description; String failureMsg; String transaction_no; - Map extra; + Map extra; Map metadata; public String getId() { @@ -170,11 +171,11 @@ public void setTransaction_no(String transaction_no) { this.transaction_no = transaction_no; } - public Map getExtra() { + public Map getExtra() { return extra; } - public void setExtra(Map extra) { + public void setExtra(Map extra) { this.extra = extra; } @@ -191,50 +192,33 @@ public void setMetadata(Map metadata) { * 创建 Transfer * @param params * @return Transfer - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException + * @throws PingppException */ public static Transfer create(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return create(null, params); + throws PingppException { + return create(params, null); } /** * 创建 Transfer * - * @param apiKey Ping++ APiKey * @param params + * @param options the specific options * @return Transfer - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static Transfer create(String apiKey, Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(RequestMethod.POST, classURL(Transfer.class), apiKey, params, Transfer.class); + public static Transfer create(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.POST, classURL(Transfer.class), params, Transfer.class, options); } /** * 查询 Transfer * @param id * @return Transfer - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException + * @throws PingppException */ - public static Transfer retrieve(String id) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { + public static Transfer retrieve(String id) throws PingppException { return retrieve(id, null, null); } @@ -242,19 +226,12 @@ public static Transfer retrieve(String id) throws AuthenticationException, * 查询 Transfer * * @param id - * @param apiKey Ping++ ApiKey + * @param options the specific options * @return Transfer - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static Transfer retrieve(String id, String apiKey) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return retrieve(id, apiKey, null); + public static Transfer retrieve(String id, RequestOptions options) throws PingppException { + return retrieve(id, null, options); } /** @@ -262,70 +239,46 @@ public static Transfer retrieve(String id, String apiKey) throws AuthenticationE * @param id * @param params * @return Transfer - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException + * @throws PingppException */ - public static Transfer retrieve(String id, Map params) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return retrieve(id, null, params); + public static Transfer retrieve(String id, Map params) throws PingppException { + return retrieve(id, params, null); } /** * 查询 Transfer * * @param id - * @param apiKey Ping++ ApiKey * @param params + * @param options the specific options * @return Transfer - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static Transfer retrieve(String id, String apiKey, Map params) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return request(RequestMethod.GET, instanceURL(Transfer.class, id), apiKey, params, Transfer.class); + public static Transfer retrieve(String id, Map params, RequestOptions options) throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, instanceURL(Transfer.class, id), params, Transfer.class, options); } /** * 查询 Transfer * @param params 分页参数等 * @return TransferCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException + * @throws PingppException */ public static TransferCollection list(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return list(null, params); + throws PingppException { + return list(params, null); } /** * 查询 Transfer * - * @param apiKey Ping++ APiKey * @param params 分页参数等 + * @param options the specific options * @return TransferCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ - public static TransferCollection list(String apiKey, Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(RequestMethod.GET, classURL(Transfer.class), apiKey, params, TransferCollection.class); + public static TransferCollection list(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, classURL(Transfer.class), params, TransferCollection.class, options); } } diff --git a/src/main/java/com/pingplusplus/model/TransferCollection.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/TransferCollection.java similarity index 100% rename from src/main/java/com/pingplusplus/model/TransferCollection.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/TransferCollection.java diff --git a/src/main/java/com/pingplusplus/model/User.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/User.java similarity index 68% rename from src/main/java/com/pingplusplus/model/User.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/User.java index 43e3c97..8c3f1af 100644 --- a/src/main/java/com/pingplusplus/model/User.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/User.java @@ -1,8 +1,10 @@ package com.pingplusplus.model; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.InvalidRequestException; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.net.APIResource; import com.pingplusplus.net.AppBasedResource; +import com.pingplusplus.net.RequestOptions; import java.util.List; import java.util.Map; @@ -230,17 +232,24 @@ public void setParentUserId(String parentUserId) { * * @param params * @return User - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static User create(Mapparams) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.POST, classURL(User.class), params, User.class); + throws PingppException { + return create(params, null); + } + + /** + * 创建 user + * + * @param params + * @param options the specific options + * @return User + * @throws PingppException + */ + public static User create(Mapparams, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.POST, classURL(User.class), params, User.class, options); } /** @@ -248,18 +257,26 @@ public static User create(Mapparams) * * @param id * @return User - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static User retrieve(String id) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { + throws PingppException { + User.checkUserId(id); + return retrieve(id, null); + } + + /** + * 查询 user + * + * @param id + * @param options the specific options + * @return User + * @throws PingppException + */ + public static User retrieve(String id, RequestOptions options) + throws PingppException { User.checkUserId(id); - return request(APIResource.RequestMethod.GET, instanceURL(User.class, id), null, User.class); + return APIResource.request(APIResource.RequestMethod.GET, instanceURL(User.class, id), null, User.class, options); } /** @@ -267,17 +284,24 @@ public static User retrieve(String id) * * @param params * @return UserCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static UserCollection list(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.GET, classURL(User.class), params, UserCollection.class); + throws PingppException { + return list(params, null); + } + + /** + * 查询 user 列表 + * + * @param params + * @param options the specific options + * @return UserCollection + * @throws PingppException + */ + public static UserCollection list(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, classURL(User.class), params, UserCollection.class, options); } /** @@ -286,23 +310,38 @@ public static UserCollection list(Map params) * @param id * @param params * @return User - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static User update(String id, Mapparams) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { + throws PingppException { + User.checkUserId(id); + return update(id, params, null); + } + + /** + * 更新 user + * + * @param id + * @param params + * @param options the specific options + * @return User + * @throws PingppException + */ + public static User update(String id, Mapparams, RequestOptions options) + throws PingppException { User.checkUserId(id); - return request(APIResource.RequestMethod.PUT, instanceURL(User.class, id), params, User.class); + return APIResource.request(APIResource.RequestMethod.PUT, instanceURL(User.class, id), params, User.class, options); } public static void checkUserId(String userId) throws InvalidRequestException { if (userId == null || userId.trim().length() == 0) { - throw new InvalidRequestException("ID should not be null or empty.", "user_id", null); + throw new InvalidRequestException( + "ID should not be null or empty.", + "invalid_request_error", + "request_param_error", + "user_id", + 0, + null); } } } diff --git a/src/main/java/com/pingplusplus/model/UserCollection.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/UserCollection.java similarity index 100% rename from src/main/java/com/pingplusplus/model/UserCollection.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/UserCollection.java diff --git a/src/main/java/com/pingplusplus/model/UserPic.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/UserPic.java similarity index 60% rename from src/main/java/com/pingplusplus/model/UserPic.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/UserPic.java index 91efec6..b1e5ed1 100644 --- a/src/main/java/com/pingplusplus/model/UserPic.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/UserPic.java @@ -1,7 +1,9 @@ package com.pingplusplus.model; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; +import com.pingplusplus.net.APIResource; import com.pingplusplus.net.AppBasedResource; +import com.pingplusplus.net.RequestOptions; import java.util.Map; @@ -83,17 +85,11 @@ public void setPicExternalId(String picExternalId) { * 证件上传 * * @param params 请求参数 - * @return Contact - * @throws AuthenticationException 认证异常 - * @throws InvalidRequestException 错误请求 - * @throws APIConnectionException 连接异常 - * @throws APIException 系统异常 - * @throws ChannelException 渠道异常 - * @throws RateLimitException 请求超限 + * @return UserPic + * @throws PingppException */ public static UserPic upload(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { + throws PingppException { return upload(params, null); } @@ -101,18 +97,12 @@ public static UserPic upload(Map params) * 证件上传 * * @param params 请求参数 - * @param apiKey API key - * @return Contact - * @throws AuthenticationException 认证异常 - * @throws InvalidRequestException 错误请求 - * @throws APIConnectionException 连接异常 - * @throws APIException 系统异常 - * @throws ChannelException 渠道异常 - * @throws RateLimitException 请求超限 + * @param options the specific options + * @return UserPic + * @throws PingppException */ - public static UserPic upload(Map params, String apiKey) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(RequestMethod.POST, singleClassURL(UserPic.class), apiKey, params, UserPic.class); + public static UserPic upload(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.POST, singleClassURL(UserPic.class), params, UserPic.class, options); } } diff --git a/src/main/java/com/pingplusplus/model/Webhooks.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/Webhooks.java similarity index 100% rename from src/main/java/com/pingplusplus/model/Webhooks.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/Webhooks.java diff --git a/src/main/java/com/pingplusplus/model/Withdrawal.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/Withdrawal.java similarity index 62% rename from src/main/java/com/pingplusplus/model/Withdrawal.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/Withdrawal.java index 1deb594..164e0d0 100644 --- a/src/main/java/com/pingplusplus/model/Withdrawal.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/model/Withdrawal.java @@ -1,8 +1,9 @@ package com.pingplusplus.model; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.net.APIResource; import com.pingplusplus.net.AppBasedResource; +import com.pingplusplus.net.RequestOptions; import java.util.HashMap; import java.util.Map; @@ -221,17 +222,24 @@ public void setSettleAccount(String settleAccount) { * * @param params * @return Withdrawal - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static Withdrawal create(Mapparams) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.POST, classURL(Withdrawal.class), params, Withdrawal.class); + throws PingppException { + return create(params, null); + } + + /** + * 创建 withdrawal + * + * @param params + * @param options the specific options + * @return Withdrawal + * @throws PingppException + */ + public static Withdrawal create(Mapparams, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.POST, classURL(Withdrawal.class), params, Withdrawal.class, options); } /** @@ -239,17 +247,24 @@ public static Withdrawal create(Mapparams) * * @param id * @return Withdrawal - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static Withdrawal retrieve(String id) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.GET, instanceURL(Withdrawal.class, id), null, Withdrawal.class); + throws PingppException { + return retrieve(id, null); + } + + /** + * 查询 withdrawal + * + * @param id + * @param options the specific options + * @return Withdrawal + * @throws PingppException + */ + public static Withdrawal retrieve(String id, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, instanceURL(Withdrawal.class, id), null, Withdrawal.class, options); } /** @@ -257,17 +272,24 @@ public static Withdrawal retrieve(String id) * * @param params * @return WithdrawalCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static WithdrawalCollection list(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.GET, classURL(Withdrawal.class), params, WithdrawalCollection.class); + throws PingppException { + return list(params, null); + } + + /** + * 查询 withdrawal 列表 + * + * @param params + * @param options the specific options + * @return WithdrawalCollection + * @throws PingppException + */ + public static WithdrawalCollection list(Map params, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.GET, classURL(Withdrawal.class), params, WithdrawalCollection.class, options); } /** @@ -276,17 +298,25 @@ public static WithdrawalCollection list(Map params) * @param id * @param params * @return Withdrawal - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static Withdrawal update(String id, Mapparams) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.PUT, instanceURL(Withdrawal.class, id), params, Withdrawal.class); + throws PingppException { + return update(id, params, null); + } + + /** + * 更新 withdrawal + * + * @param id + * @param params + * @param options the specific options + * @return Withdrawal + * @throws PingppException + */ + public static Withdrawal update(String id, Mapparams, RequestOptions options) + throws PingppException { + return APIResource.request(APIResource.RequestMethod.PUT, instanceURL(Withdrawal.class, id), params, Withdrawal.class, options); } /** @@ -294,19 +324,28 @@ public static Withdrawal update(String id, Mapparams) * * @param id * @return Withdrawal - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static Withdrawal cancel(String id) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { + throws PingppException { + Map params = new HashMap(); + params.put("status", "canceled"); + return update(id, params, null); + } + + /** + * 取消 withdrawal + * + * @param id + * @param options the specific options + * @return Withdrawal + * @throws PingppException + */ + public static Withdrawal cancel(String id, RequestOptions options) + throws PingppException { Map params = new HashMap(); params.put("status", "canceled"); - return update(id, params); + return update(id, params, options); } /** @@ -314,18 +353,27 @@ public static Withdrawal cancel(String id) * * @param id * @return Withdrawal - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException + * @throws PingppException */ public static Withdrawal confirm(String id) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { + throws PingppException { + Map params = new HashMap(); + params.put("status", "pending"); + return update(id, params, null); + } + + /** + * 确认 withdrawal + * + * @param id + * @param options the specific options + * @return Withdrawal + * @throws PingppException + */ + public static Withdrawal confirm(String id, RequestOptions options) + throws PingppException { Map params = new HashMap(); params.put("status", "pending"); - return update(id, params); + return update(id, params, options); } } diff --git a/src/main/java/com/pingplusplus/model/WithdrawalCollection.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/WithdrawalCollection.java similarity index 100% rename from src/main/java/com/pingplusplus/model/WithdrawalCollection.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/WithdrawalCollection.java diff --git a/src/main/java/com/pingplusplus/model/WithdrawalCollectionBase.java b/pingpp-sdk/src/main/java/com/pingplusplus/model/WithdrawalCollectionBase.java similarity index 100% rename from src/main/java/com/pingplusplus/model/WithdrawalCollectionBase.java rename to pingpp-sdk/src/main/java/com/pingplusplus/model/WithdrawalCollectionBase.java diff --git a/pingpp-sdk/src/main/java/com/pingplusplus/net/APIResource.java b/pingpp-sdk/src/main/java/com/pingplusplus/net/APIResource.java new file mode 100644 index 0000000..10bcb6c --- /dev/null +++ b/pingpp-sdk/src/main/java/com/pingplusplus/net/APIResource.java @@ -0,0 +1,273 @@ +package com.pingplusplus.net; + +import com.google.gson.*; +import com.pingplusplus.Pingpp; +import com.pingplusplus.exception.*; +import com.pingplusplus.model.*; +import com.pingplusplus.serializer.*; +import com.pingplusplus.util.GsonUtils; + +import java.io.*; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.net.URLEncoder; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.*; + +/** + * extends the abstract class when you need requset anything from ping++ + */ +public abstract class APIResource extends PingppObject { + /** + * URLEncoder charset + */ + public static final Charset CHARSET = StandardCharsets.UTF_8; + + private static HttpClient httpClient = new HttpURLConnectionClient(); + + public static final String URIAppIdHolder = ""; + + /** + * Http requset method + */ + protected enum RequestMethod { + GET, + POST, + DELETE, + PUT + } + + /** + * Gson object use to transform json string to resource object + */ + public static final Gson GSON = GsonUtils.baseGsonBuilder() + .registerTypeAdapter(Charge.class, new ChargeDeserializer()) + .registerTypeAdapter(RedEnvelope.class, new RedEnvelopeDeserializer()) + .registerTypeAdapter(Transfer.class, new TransferDeserializer()) + .registerTypeAdapter(ChargeRefundCollection.class, new ChargeRefundCollectionDeserializer()) + .registerTypeAdapter(EventData.class, new EventDataDeserializer()) + .registerTypeAdapter(PingppRawJsonObject.class, new PingppRawJsonObjectDeserializer()) + .registerTypeAdapter(SubApp.class, new SubAppDeserializer()) + .create(); + + public static Gson getGson() { + return GSON; + } + + public static Class getSelfClass() { + return APIResource.class; + } + + /** + * @param clazz the class + * @return className + */ + protected static String className(Class clazz) { + String className = clazz.getSimpleName().toLowerCase().replace("$", " "); + + switch (className) { + case "redenvelope": + return "red_envelope"; + case "batchrefund": + return "batch_refund"; + case "batchtransfer": + return "batch_transfer"; + case "customs": + return "custom"; + case "cardinfo": + return "card_info"; + case "assettransaction": + return "asset_transaction"; + case "balancebonus": + return "balance_bonuse"; + case "balancetransfer": + return "balance_transfer"; + case "balancetransaction": + return "balance_transaction"; + case "coupontemplate": + return "coupon_template"; + case "batchwithdrawal": + return "batch_withdrawal"; + case "transactionstatistics": + return "transaction_statistics"; + case "settleaccount": + return "settle_account"; + case "subapp": + return "sub_app"; + case "royalty": + return "royaltie"; + case "royaltysettlement": + return "royalty_settlement"; + case "royaltytransaction": + return "royalty_transaction"; + case "royaltytemplate": + return "royalty_template"; + case "balancesettlement": + return "balance_settlement"; + case "subbank": + return "sub_bank"; + case "splitreceiver": + return "split_receiver"; + case "splitprofit": + return "split_profit"; + case "profittransaction": + return "profit_transaction"; + case "userpic": + return "users/upload_pic"; + case "contact": + return "sub_apps/contact"; + default: + return className; + } + } + + /** + * @param clazz the object class + * @return singleClassURL + */ + protected static String singleClassURL(Class clazz) { + String className = null; + Class klass = getSelfClass(); + if (!klass.getSimpleName().equalsIgnoreCase("APIResource")) { + try { + Method method = klass.getMethod("className", Class.class); + className = (String)method.invoke(klass, clazz); + } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + } + } + + if (className == null) { + className = className(clazz); + } + + return String.format("%s/v1/%s", Pingpp.getApiBase(), className); + } + + /** + * @param clazz the object class + * @return classURL + */ + protected static String classURL(Class clazz) { + return String.format("%ss", singleClassURL(clazz)); + } + + /** + * @param clazz the object class + * @param id the id of the object + * @return instanceURL + */ + protected static String instanceURL(Class clazz, String id){ + return String.format("%s/%s", classURL(clazz), urlEncode(id)); + } + + protected static String apiBasePrefixedURL(String url) { + return String.format("%s%s", Pingpp.getApiBase(), url); + } + + /** + * @param str the string to encode + * @return urlEncodedString + */ + protected static String urlEncode(String str) { + if (str == null) { + return null; + } + + try { + return URLEncoder.encode(str, CHARSET.name()); + } catch (UnsupportedEncodingException e) { + throw new AssertionError("UTF-8 is unknown"); + } + } + + /** + * @param method the method of request + * @param url the URL of request + * @param params the parameters + * @param clazz the class + * @param type + * @param options the specific options + * @return PingppObject + * @throws PingppException if some error occurs + */ + protected static T request( + APIResource.RequestMethod method, + String url, + Map params, + Class clazz, + RequestOptions options) throws PingppException { + PingppRequest request = new PingppRequest(method, url, params, options); + PingppResponse response = httpClient.requestWithRetries(request); + + int responseCode = response.getResponseCode(); + String responseBody = response.getResponseBody(); + + if (responseCode < 200 || responseCode >= 300) { + handleAPIError(response); + } + + T resource = null; + try { + resource = APIResource.getGson().fromJson(responseBody, clazz); + } catch (JsonSyntaxException e) { + raiseMalformedJsonError(responseBody, responseCode); + } + + if (resource instanceof PingppObject) { + PingppObject obj = (PingppObject) resource; + obj.setLastResponse(response); + } + + return resource; + } + + /** + * 错误处理 + * + * @param response the response + * @throws PingppException + */ + private static void handleAPIError(PingppResponse response) + throws PingppException { + PingppError error = null; + String rBody = response.getResponseBody(); + int rCode = response.getResponseCode(); + try { + JsonObject jsonObject = APIResource.GSON.fromJson(rBody, JsonObject.class).getAsJsonObject("error"); + error = APIResource.GSON.fromJson(jsonObject, PingppError.class); + } catch (JsonSyntaxException e) { + raiseMalformedJsonError(rBody, rCode); + } + + switch (rCode) { + case 400: + case 404: + throw new InvalidRequestException(error.getMessage(), error.getType(), error.getCode(), error.getParam(), rCode, null); + case 403: + case 429: + throw new RateLimitException(error.getMessage(), error.getType(), error.getCode(), rCode, null); + case 402: + throw new ChannelException(error.getMessage(), error.getType(), error.getCode(), error.getParam(), rCode, null); + case 401: + throw new AuthenticationException(error.getMessage(), error.getType(), error.getCode(), rCode, null); + case 502: + throw new APIConnectionException(error.getMessage()); + default: + throw new APIException(error.getMessage(), error.getType(), error.getCode(), rCode, null); + } + } + + private static void raiseMalformedJsonError( + String responseBody, int responseCode) throws APIException { + throw new APIException( + String.format( + "Invalid response object from API: %s. (HTTP response code was %d)", + responseBody, responseCode), + null, + null, + responseCode, + null); + } +} diff --git a/pingpp-sdk/src/main/java/com/pingplusplus/net/AppBasedResource.java b/pingpp-sdk/src/main/java/com/pingplusplus/net/AppBasedResource.java new file mode 100755 index 0000000..eb4cc02 --- /dev/null +++ b/pingpp-sdk/src/main/java/com/pingplusplus/net/AppBasedResource.java @@ -0,0 +1,30 @@ +package com.pingplusplus.net; + +import com.pingplusplus.Pingpp; + +public abstract class AppBasedResource extends APIResource { + /** + * @param clazz the class of object + * @return singleClassURL + */ + protected static String singleClassURL(Class clazz) { + return String.format("%s/v1/apps/%s/%s", Pingpp.getApiBase(), APIResource.URIAppIdHolder, className(clazz)); + } + + /** + * @param clazz the class of object + * @return classURL + */ + protected static String classURL(Class clazz) { + return String.format("%ss", singleClassURL(clazz)); + } + + /** + * @param clazz the class of object + * @param id the id of object + * @return instanceURL + */ + protected static String instanceURL(Class clazz, String id) { + return String.format("%s/%s", classURL(clazz), urlEncode(id)); + } +} diff --git a/pingpp-sdk/src/main/java/com/pingplusplus/net/HttpClient.java b/pingpp-sdk/src/main/java/com/pingplusplus/net/HttpClient.java new file mode 100644 index 0000000..6c007ab --- /dev/null +++ b/pingpp-sdk/src/main/java/com/pingplusplus/net/HttpClient.java @@ -0,0 +1,205 @@ +package com.pingplusplus.net; + +import com.pingplusplus.Pingpp; +import com.pingplusplus.exception.APIConnectionException; +import com.pingplusplus.exception.PingppException; +import com.pingplusplus.util.PingppSignature; +import com.pingplusplus.util.StreamUtils; + +import java.io.FileInputStream; +import java.io.IOException; +import java.net.ConnectException; +import java.net.URL; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ThreadLocalRandom; + +public abstract class HttpClient { + /** Maximum sleep time between tries to send HTTP requests after network failure. */ + public static final long maxNetworkRetriesDelay = 5000; + + /** Minimum sleep time between tries to send HTTP requests after network failure. */ + public static final long minNetworkRetriesDelay = 500; + + /** A value indicating whether the client should sleep between automatic request retries. */ + boolean networkRetriesSleep = true; + + /** Initializes a new instance of the {@link HttpClient} class. */ + public HttpClient() {} + + /** + * Sends the given request to Pingpp's API. + * + * @param request the request + * @return the response + * @throws PingppException If the request fails for any reason + */ + public abstract PingppResponse request(PingppRequest request) throws PingppException; + + /** + * Sends the given request to Pingpp's API, retrying the request in cases of intermittent + * problems. + * + * @param request the request + * @return the response + * @throws PingppException If the request fails for any reason + */ + public PingppResponse requestWithRetries(PingppRequest request) throws PingppException { + APIConnectionException requestException = null; + PingppResponse response = null; + int retry = 0; + + while (true) { + requestException = null; + + try { + response = this.request(request); + } catch (APIConnectionException e) { + requestException = e; + } + + if (!this.shouldRetry(retry, requestException, request, response)) { + break; + } + + retry += 1; + + try { + Thread.sleep(this.sleepTime(retry)); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + + if (requestException != null) { + throw requestException; + } + + response.setNumRetries(retry); + + return response; + } + + /** + * Builds the value of the {@code User-Agent} header. + * + * @return a string containing the value of the {@code User-Agent} header + */ + protected static String buildUserAgentString() { + return String.format("Pingpp/v1 JavaBindings/%s", Pingpp.VERSION); + } + + /** + * Builds the value of the {@code X-Pingpp-Client-User-Agent} header. + * + * @return a string containing the value of the {@code X-Pingpp-Client-User-Agent} header + */ + protected static String buildXPingppClientUserAgentString() { + String[] propertyNames = { + "os.name", + "os.version", + "os.arch", + "java.version", + "java.vendor", + "java.vm.version", + "java.vm.vendor" + }; + + Map propertyMap = new HashMap<>(); + for (String propertyName : propertyNames) { + propertyMap.put(propertyName, System.getProperty(propertyName)); + } + propertyMap.put("bindings.version", Pingpp.VERSION); + propertyMap.put("lang", "Java"); + propertyMap.put("publisher", "Pingpp"); + + return APIResource.GSON.toJson(propertyMap); + } + + protected static String buildPingppSignature(PingppRequest request, String currentTime) + throws IOException { + StringBuilder sb = new StringBuilder(); + if (request.content != null) { + sb.append(request.content.stringContent()); + } + sb.append(getRequestURIFromURL(request.url)); + sb.append(currentTime); + + String stringToBeSigned = sb.toString(); + + return generateSign(stringToBeSigned, request.options); + } + + private boolean shouldRetry( + int numRetries, PingppException exception, PingppRequest request, PingppResponse response) { + // Do not retry if we are out of retries. + if (numRetries >= request.options.getMaxNetworkRetries()) { + return false; + } + + // Retry on connection error. + if ((exception != null) + && (exception.getCause() != null) + && (exception.getCause() instanceof ConnectException)) { + return true; + } + + // Retry on 500, 503, and other internal errors. + if ((response != null) && (response.getResponseCode() >= 500)) { + return true; + } + + return false; + } + + private long sleepTime(int numRetries) { + if (!networkRetriesSleep) { + return 0; + } + + long delay = (long) (minNetworkRetriesDelay * Math.pow(2, numRetries - 1)); + + if (delay > maxNetworkRetriesDelay) { + delay = maxNetworkRetriesDelay; + } + + double jitter = ThreadLocalRandom.current().nextDouble(0.75, 1.0); + delay = (long) (delay * jitter); + + if (delay < minNetworkRetriesDelay) { + delay = minNetworkRetriesDelay; + } + + return delay; + } + + protected static String currentTimeString() { + int requestTime = (int) (System.currentTimeMillis() / 1000); + return Integer.toString(requestTime); + } + + private static String getRequestURIFromURL(URL url) { + String path = url.getPath(); + String query = url.getQuery(); + if (query == null) { + return path; + } + return path + "?" + query; + } + + private static String generateSign(String data, RequestOptions options) + throws IOException { + String privatekey = options.getPrivateKey(); + if (privatekey == null) { + if (Pingpp.privateKeyPath == null) { + return null; + } + FileInputStream inputStream = new FileInputStream(Pingpp.privateKeyPath); + privatekey = StreamUtils.readToEnd(inputStream, APIResource.CHARSET); + inputStream.close(); + } + + return PingppSignature.sign(data, privatekey, APIResource.CHARSET.name()); + } + +} diff --git a/pingpp-sdk/src/main/java/com/pingplusplus/net/HttpContent.java b/pingpp-sdk/src/main/java/com/pingplusplus/net/HttpContent.java new file mode 100644 index 0000000..28987d6 --- /dev/null +++ b/pingpp-sdk/src/main/java/com/pingplusplus/net/HttpContent.java @@ -0,0 +1,44 @@ +package com.pingplusplus.net; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import java.util.Map; +import static java.util.Objects.requireNonNull; + +public class HttpContent { + /** The request's content, as a byte array. */ + byte[] byteArrayContent; + + /** The value of the {@code Content-Type} header. */ + String contentType; + + private HttpContent(byte[] byteArrayContent, String contentType) { + this.byteArrayContent = byteArrayContent; + this.contentType = contentType; + } + + /** + * The request's content, as a string. + * @return the string content + */ + public String stringContent() { + return new String(this.byteArrayContent, APIResource.CHARSET); + } + + public static HttpContent buildJSONContent(Map params) { + requireNonNull(params); + + return new HttpContent( + createJSONString(params).getBytes(APIResource.CHARSET), + String.format("application/json; charset=%s", APIResource.CHARSET)); + } + + /** + * @param params the parameters + * @return JSONString + */ + private static String createJSONString(Map params) { + Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create(); + return gson.toJson(params); + } +} diff --git a/pingpp-sdk/src/main/java/com/pingplusplus/net/HttpHeaders.java b/pingpp-sdk/src/main/java/com/pingplusplus/net/HttpHeaders.java new file mode 100644 index 0000000..6247b90 --- /dev/null +++ b/pingpp-sdk/src/main/java/com/pingplusplus/net/HttpHeaders.java @@ -0,0 +1,133 @@ +package com.pingplusplus.net; + +import com.pingplusplus.util.CaseInsensitiveMap; + +import java.util.*; + +import static java.util.Objects.requireNonNull; + +public class HttpHeaders { + private CaseInsensitiveMap> headerMap; + + private HttpHeaders(CaseInsensitiveMap> headerMap) { + this.headerMap = headerMap; + } + + /** + * Returns an {@link HttpHeaders} instance initialized from the given map. + * + * @param headerMap the map containing the header names and values + * @return an {@link HttpHeaders} instance containing the given headers + * @throws NullPointerException if {@code headerMap} is {@code null} + */ + public static HttpHeaders of(Map> headerMap) { + requireNonNull(headerMap); + return new HttpHeaders(CaseInsensitiveMap.of(headerMap)); + } + + /** + * Returns a new {@link HttpHeaders} instance containing the headers of the current instance plus + * the provided header. + * + * @param name the name of the header to add + * @param value the value of the header to add + * @return the new {@link HttpHeaders} instance + * @throws NullPointerException if {@code name} or {@code value} is {@code null} + */ + public HttpHeaders withAdditionalHeader(String name, String value) { + requireNonNull(name); + requireNonNull(value); + return this.withAdditionalHeader(name, Arrays.asList(value)); + } + + /** + * Returns a new {@link HttpHeaders} instance containing the headers of the current instance plus + * the provided header. + * + * @param name the name of the header to add + * @param values the values of the header to add + * @return the new {@link HttpHeaders} instance + * @throws NullPointerException if {@code name} or {@code values} is {@code null} + */ + public HttpHeaders withAdditionalHeader(String name, List values) { + requireNonNull(name); + requireNonNull(values); + Map> headerMap = new HashMap<>(); + headerMap.put(name, values); + return this.withAdditionalHeaders(headerMap); + } + + /** + * Returns a new {@link HttpHeaders} instance containing the headers of the current instance plus + * the provided headers. + * + * @param headerMap the map containing the headers to add + * @return the new {@link HttpHeaders} instance + * @throws NullPointerException if {@code headerMap} is {@code null} + */ + public HttpHeaders withAdditionalHeaders(Map> headerMap) { + requireNonNull(headerMap); + Map> newHeaderMap = new HashMap<>(this.map()); + newHeaderMap.putAll(headerMap); + return HttpHeaders.of(newHeaderMap); + } + + /** + * Returns an unmodifiable List of all of the header string values of the given named header. + * Always returns a List, which may be empty if the header is not present. + * + * @param name the header name + * @return a List of headers string values + */ + public List allValues(String name) { + if (this.headerMap.containsKey(name)) { + List values = this.headerMap.get(name); + if ((values != null) && (values.size() > 0)) { + return Collections.unmodifiableList(values); + } + } + return Collections.emptyList(); + } + + /** + * Returns an {@link Optional} containing the first header string value of the given named (and + * possibly multi-valued) header. If the header is not present, then the returned {@code Optional} + * is empty. + * + * @param name the header name + * @return an {@code Optional} containing the first named header string value, if present + */ + public Optional firstValue(String name) { + if (this.headerMap.containsKey(name)) { + List values = this.headerMap.get(name); + if ((values != null) && (values.size() > 0)) { + return Optional.of(values.get(0)); + } + } + return Optional.empty(); + } + + /** + * Returns an unmodifiable Map view of this HttpHeaders. + * + * @return the Map + */ + public Map> map() { + return Collections.unmodifiableMap(this.headerMap); + } + + /** + * Returns this {@link HttpHeaders} as a string. + * + * @return a string describing the HTTP headers + */ + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(super.toString()); + sb.append(" { "); + sb.append(map()); + sb.append(" }"); + return sb.toString(); + } +} diff --git a/pingpp-sdk/src/main/java/com/pingplusplus/net/HttpURLConnectionClient.java b/pingpp-sdk/src/main/java/com/pingplusplus/net/HttpURLConnectionClient.java new file mode 100644 index 0000000..41d2bcd --- /dev/null +++ b/pingpp-sdk/src/main/java/com/pingplusplus/net/HttpURLConnectionClient.java @@ -0,0 +1,108 @@ +package com.pingplusplus.net; + +import com.pingplusplus.Pingpp; +import com.pingplusplus.exception.APIConnectionException; +import com.pingplusplus.exception.AuthenticationException; +import com.pingplusplus.util.PingppSignature; +import com.pingplusplus.util.StreamUtils; +import com.pingplusplus.util.StringUtils; + +import java.io.IOException; +import java.io.OutputStream; +import java.net.HttpURLConnection; +import java.util.*; + +public class HttpURLConnectionClient extends HttpClient { + /** Initializes a new instance of the {@link HttpURLConnectionClient}. */ + public HttpURLConnectionClient() { + super(); + } + + /** + * Sends the given request to Pingpp's API. + * + * @param request the request + * @return the response + * @throws APIConnectionException if an error occurs when sending or receiving + */ + @Override + public PingppResponse request(PingppRequest request) throws APIConnectionException { + HttpURLConnection conn = null; + + try { + conn = createPingppConnection(request); + + // trigger the request + int responseCode = conn.getResponseCode(); + HttpHeaders headers = HttpHeaders.of(conn.getHeaderFields()); + String responseBody; + + if (responseCode >= 200 && responseCode < 300) { + responseBody = StreamUtils.readToEnd(conn.getInputStream(), APIResource.CHARSET); + boolean verified = PingppSignature.verify(headers, responseBody, request.options.getVerifyPublicKey(), APIResource.CHARSET.name()); + if (!verified) { + throw new AuthenticationException("响应签名验证失败,请检查验签公钥是否正确"); + } + } else { + responseBody = StreamUtils.readToEnd(conn.getErrorStream(), APIResource.CHARSET); + } + + return new PingppResponse(responseCode, responseBody, headers); + } catch (IOException e) { + throw new APIConnectionException( + String.format( + "IOException during API request to Pingpp (%s): %s " + + "Please check your internet connection and try again.", + Pingpp.getApiBase(), e.getMessage()), + e); + } catch (AuthenticationException e) { + throw new RuntimeException(e); + } finally { + if (conn != null) { + conn.disconnect(); + } + } + } + + static HttpHeaders getHeaders(PingppRequest request) { + Map> userAgentHeadersMap = new HashMap<>(); + + userAgentHeadersMap.put("User-Agent", Collections.singletonList(buildUserAgentString())); + userAgentHeadersMap.put( + "X-Pingpp-Client-User-Agent", Collections.singletonList(buildXPingppClientUserAgentString())); + + return request.getHeaders().withAdditionalHeaders(userAgentHeadersMap); + } + + private static HttpURLConnection createPingppConnection(PingppRequest request) + throws IOException { + HttpURLConnection conn = (HttpURLConnection) request.url.openConnection(); + + conn.setConnectTimeout(request.options.getConnectTimeout()); + conn.setReadTimeout(request.options.getReadTimeout()); + conn.setUseCaches(false); + for (Map.Entry> entry : getHeaders(request).map().entrySet()) { + conn.setRequestProperty(entry.getKey(), StringUtils.join(",", entry.getValue())); + } + + conn.setRequestMethod(request.method.name()); + + String requestTime = currentTimeString(); + conn.setRequestProperty("Pingplusplus-Request-Timestamp", requestTime); + String signature = buildPingppSignature(request, requestTime); + if (signature != null) { + conn.setRequestProperty("Pingplusplus-Signature", signature); + } + + if (request.content != null) { + conn.setDoOutput(true); + conn.setRequestProperty("Content-Type", request.content.contentType); + + try (OutputStream output = conn.getOutputStream()) { + output.write(request.content.byteArrayContent); + } + } + + return conn; + } +} diff --git a/pingpp-sdk/src/main/java/com/pingplusplus/net/PingppRequest.java b/pingpp-sdk/src/main/java/com/pingplusplus/net/PingppRequest.java new file mode 100644 index 0000000..91bab2a --- /dev/null +++ b/pingpp-sdk/src/main/java/com/pingplusplus/net/PingppRequest.java @@ -0,0 +1,260 @@ +package com.pingplusplus.net; + +import com.pingplusplus.Pingpp; +import com.pingplusplus.exception.APIConnectionException; +import com.pingplusplus.exception.AuthenticationException; +import com.pingplusplus.exception.InvalidRequestException; +import com.pingplusplus.exception.PingppException; +import com.pingplusplus.util.StringUtils; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.URL; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; +import java.util.*; + +public class PingppRequest { + /** + * The HTTP method for the request (GET, POST, DELETE or PUT). + * */ + APIResource.RequestMethod method; + + /** + * The URL for the request. + */ + URL url; + + /** + * The body of the request. + */ + HttpContent content; + + /** + * The HTTP headers of the request + */ + HttpHeaders headers; + + /** + * The parameters of the request. + */ + Map params; + + /** + * The options of the request. + */ + RequestOptions options; + + /** + * Initializes a new instance of the {@link PingppRequest} class. + * + * @param method the HTTP method + * @param url the URL + * @param params the parameters + * @param options the options + * @throws PingppException if the request cannot be initialized for any reason + */ + public PingppRequest( + APIResource.RequestMethod method, + String url, + Map params, + RequestOptions options) + throws PingppException { + try { + this.params = (params != null) ? Collections.unmodifiableMap(params) : null; + this.options = (options != null) ? options : RequestOptions.getDefault(); + this.method = method; + this.url = buildURL(method, url, params, this.options); + this.content = buildContent(method, params); + this.headers = buildHeaders(method, this.options); + } catch (IOException e) { + throw new APIConnectionException( + String.format( + "IOException during API request to Pingpp (%s): %s " + + "Please check your internet connection and try again.", + Pingpp.getApiBase(), e.getMessage()), + e); + } + } + + private static URL buildURL( + APIResource.RequestMethod method, String spec, Map params, RequestOptions options) + throws IOException, InvalidRequestException { + StringBuilder sb = new StringBuilder(); + + if (spec.contains(APIResource.URIAppIdHolder)) { + if (options.getAppId() == null) { + throw new InvalidRequestException( + "Please set app_id using Pingpp.appId = or ReqquestOptions", + "invalid_request_error", + "request_param_error", + "app_id", + 0, + null); + } + spec = spec.replaceFirst(APIResource.URIAppIdHolder, options.getAppId()); + } + sb.append(spec); + + if ((method != APIResource.RequestMethod.POST && method != APIResource.RequestMethod.PUT) && (params != null)) { + String queryString = createQuery(params); + if (!queryString.isEmpty()) { + sb.append("?"); + sb.append(queryString); + } + } + + return new URL(sb.toString()); + } + + private static HttpContent buildContent( + APIResource.RequestMethod method, Map params) { + if (method != APIResource.RequestMethod.POST && method != APIResource.RequestMethod.PUT) { + return null; + } + + if (params == null) { + return null; + } + + return HttpContent.buildJSONContent(params); + } + + /** + * @param params the parameters + * @return queryString + */ + private static String createQuery(Map params) { + if (params == null) { + return ""; + } + + Map flatParams = flattenParams(params); + StringBuilder queryStringBuffer = new StringBuilder(); + for (Map.Entry entry : flatParams.entrySet()) { + if (queryStringBuffer.length() > 0) { + queryStringBuffer.append("&"); + } + queryStringBuffer.append(urlEncodePair(entry.getKey(), + entry.getValue())); + } + return queryStringBuffer.toString(); + } + + /** + * @param k the key + * @param v the value + * @return urlEncodedString + */ + private static String urlEncodePair(String k, String v) { + return String.format("%s=%s", urlEncode(k), urlEncode(v)); + } + + /** + * @param str the string to encode + * @return urlEncodedString + */ + protected static String urlEncode(String str) { + if (str == null) { + return null; + } + + try { + return URLEncoder.encode(str, StandardCharsets.UTF_8.name()); + } catch (UnsupportedEncodingException e) { + throw new AssertionError("UTF-8 is unknown"); + } + } + + /** + * @param params the parameters + * @return flattenParams + */ + private static Map flattenParams(Map params) { + if (params == null) { + return new HashMap(); + } + Map flatParams = new HashMap(); + for (Map.Entry entry : params.entrySet()) { + String key = entry.getKey(); + Object value = entry.getValue(); + if (value instanceof Map) { + Map flatNestedMap = new HashMap(); + Map nestedMap = (Map) value; + for (Map.Entry nestedEntry : nestedMap.entrySet()) { + flatNestedMap.put( + String.format("%s[%s]", key, nestedEntry.getKey()), + nestedEntry.getValue()); + } + flatParams.putAll(flattenParams(flatNestedMap)); + } else if (value instanceof ArrayList) { + ArrayList ar = (ArrayList) value; + Map flatNestedMap = new HashMap(); + int size = ar.size(); + for (int i = 0; i < size; i++) { + flatNestedMap.put(String.format("%s[%d]", key, i), ar.get(i)); + } + flatParams.putAll(flattenParams(flatNestedMap)); + } else if (value == null) { + flatParams.put(key, ""); + } else { + flatParams.put(key, value.toString()); + } + } + return flatParams; + } + + private static HttpHeaders buildHeaders(APIResource.RequestMethod method, RequestOptions options) + throws AuthenticationException { + Map> headerMap = new HashMap>(); + + // Accept + headerMap.put("Accept", Collections.singletonList("application/json")); + + // Accept-Charset + headerMap.put("Accept-Charset", Collections.singletonList(APIResource.CHARSET.name())); + + // Accept-Language + headerMap.put("Accept-Language", Collections.singletonList(options.getAcceptLanguage())); + + // Authorization + String apiKey = options.getApiKey(); + if (apiKey == null) { + throw new AuthenticationException( + "No API key provided. Set your API key using `Pingpp.apiKey = \"\"`."); + } else if (apiKey.isEmpty()) { + throw new AuthenticationException( + "Your API key is invalid, as it is an empty string."); + } else if (StringUtils.containsWhitespace(apiKey)) { + throw new AuthenticationException( + "Your API key is invalid, as it contains whitespace."); + } + headerMap.put("Authorization", Collections.singletonList(String.format("Bearer %s", apiKey))); + + return HttpHeaders.of(headerMap); + } + + public APIResource.RequestMethod getMethod() { + return method; + } + + public URL getUrl() { + return url; + } + + public HttpContent getContent() { + return content; + } + + public HttpHeaders getHeaders() { + return headers; + } + + public Map getParams() { + return params; + } + + public RequestOptions getOptions() { + return options; + } +} diff --git a/src/main/java/com/pingplusplus/net/PingppResponse.java b/pingpp-sdk/src/main/java/com/pingplusplus/net/PingppResponse.java similarity index 66% rename from src/main/java/com/pingplusplus/net/PingppResponse.java rename to pingpp-sdk/src/main/java/com/pingplusplus/net/PingppResponse.java index f27e983..78ed7dd 100644 --- a/src/main/java/com/pingplusplus/net/PingppResponse.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/net/PingppResponse.java @@ -1,8 +1,5 @@ package com.pingplusplus.net; -import java.util.List; -import java.util.Map; - /** * Handler Pingpp response when you request charge from pingxx */ @@ -10,11 +7,13 @@ public class PingppResponse { private int responseCode; private String responseBody; - private Map> responseHeaders; + private HttpHeaders responseHeaders; + + private int numRetries; /** - * @param responseCode - * @param responseBody + * @param responseCode the HTTP Status Code + * @param responseBody the response body */ public PingppResponse(int responseCode, String responseBody) { this.responseCode = responseCode; @@ -23,11 +22,11 @@ public PingppResponse(int responseCode, String responseBody) { } /** - * @param responseCode - * @param responseBody - * @param responseHeaders + * @param responseCode the HTTP Status Code + * @param responseBody the response body + * @param responseHeaders the response headers */ - public PingppResponse(int responseCode, String responseBody, Map> responseHeaders) { + public PingppResponse(int responseCode, String responseBody, HttpHeaders responseHeaders) { this.responseCode = responseCode; this.responseBody = responseBody; this.responseHeaders = responseHeaders; @@ -43,7 +42,7 @@ public int getResponseCode() { /** * - * @param responseCode + * @param responseCode the HTTP Status Code */ public void setResponseCode(int responseCode) { this.responseCode = responseCode; @@ -59,7 +58,7 @@ public String getResponseBody() { /** * - * @param responseBody + * @param responseBody the response body */ public void setResponseBody(String responseBody) { this.responseBody = responseBody; @@ -69,7 +68,15 @@ public void setResponseBody(String responseBody) { * * @return responseHeaders */ - public Map> getResponseHeaders() { + public HttpHeaders getResponseHeaders() { return responseHeaders; } + + public int getNumRetries() { + return numRetries; + } + + public void setNumRetries(int numRetries) { + this.numRetries = numRetries; + } } diff --git a/pingpp-sdk/src/main/java/com/pingplusplus/net/RequestOptions.java b/pingpp-sdk/src/main/java/com/pingplusplus/net/RequestOptions.java new file mode 100644 index 0000000..7a2b165 --- /dev/null +++ b/pingpp-sdk/src/main/java/com/pingplusplus/net/RequestOptions.java @@ -0,0 +1,271 @@ +package com.pingplusplus.net; + +import com.pingplusplus.Pingpp; + +public class RequestOptions { + private final String apiKey; + private final String appId; + private final String privateKey; + private final String verifyPublicKey; + + private final int connectTimeout; + private final int readTimeout; + + private final int maxNetworkRetries; + + private final String acceptLanguage; + + public static RequestOptions getDefault() { + return new RequestOptions( + Pingpp.apiKey, + Pingpp.appId, + Pingpp.privateKey, + Pingpp.verifyPublicKey, + Pingpp.getConnectTimeout(), + Pingpp.getReadTimeout(), + Pingpp.getMaxNetworkRetries(), + Pingpp.getAcceptLanguage()); + } + + private RequestOptions( + String apiKey, + String appId, + String privateKey, + String verifyPublicKey, + int connectTimeout, + int readTimeout, + int maxNetworkRetries, + String acceptLanguage) { + this.apiKey = apiKey; + this.appId = appId; + this.privateKey = privateKey; + this.verifyPublicKey = verifyPublicKey; + this.connectTimeout = connectTimeout; + this.readTimeout = readTimeout; + this.maxNetworkRetries = maxNetworkRetries; + this.acceptLanguage = acceptLanguage; + } + + public String getApiKey() { + return apiKey; + } + + public String getAppId() { + return appId; + } + + public String getPrivateKey() { + return privateKey; + } + + public int getConnectTimeout() { + return connectTimeout; + } + + public int getReadTimeout() { + return readTimeout; + } + + public int getMaxNetworkRetries() { + return maxNetworkRetries; + } + + public String getAcceptLanguage() { + return acceptLanguage; + } + + public String getVerifyPublicKey() {return verifyPublicKey;} + + public static RequestOptionsBuilder builder() { + return new RequestOptionsBuilder(); + } + + public static final class RequestOptionsBuilder { + private String apiKey; + private String appId; + private String privateKey; + private int connectTimeout; + private int readTimeout; + private int maxNetworkRetries; + private String acceptLanguage; + + public String getVerifyPublicKey() { + return verifyPublicKey; + } + + public RequestOptionsBuilder setVerifyPublicKey(String verifyPublicKey) { + this.verifyPublicKey = normalizePublicVerifyKey(verifyPublicKey); + return this; + } + + private String verifyPublicKey; + + public RequestOptionsBuilder() { + this.apiKey = Pingpp.apiKey; + this.appId = Pingpp.appId; + this.privateKey = Pingpp.privateKey; + this.verifyPublicKey = Pingpp.verifyPublicKey; + this.connectTimeout = Pingpp.getConnectTimeout(); + this.readTimeout = Pingpp.getReadTimeout(); + this.maxNetworkRetries = Pingpp.getMaxNetworkRetries(); + this.acceptLanguage = Pingpp.getAcceptLanguage(); + } + + public String getApiKey() { + return apiKey; + } + + public RequestOptionsBuilder setApiKey(String apiKey) { + this.apiKey = normalizeApiKey(apiKey); + return this; + } + + public RequestOptionsBuilder clearApiKey() { + this.apiKey = null; + return this; + } + + public String getAppId() { + return appId; + } + + public RequestOptionsBuilder setAppId(String appId) { + this.appId = normalizeAppId(appId); + return this; + } + + public String getPrivateKey() { + return privateKey; + } + + public RequestOptionsBuilder setPrivateKey(String privateKey) { + this.privateKey = privateKey; + return this; + } + + public RequestOptionsBuilder clearVerifyPublicKey() { + this.verifyPublicKey = null; + return this; + } + + public RequestOptionsBuilder clearAppId() { + this.appId = null; + return this; + } + + public int getConnectTimeout() { + return connectTimeout; + } + + public RequestOptionsBuilder setConnectTimeout(int connectTimeout) { + this.connectTimeout = connectTimeout; + return this; + } + + public int getReadTimeout() { + return readTimeout; + } + + public RequestOptionsBuilder setReadTimeout(int readTimeout) { + this.readTimeout = readTimeout; + return this; + } + + public int getMaxNetworkRetries() { + return maxNetworkRetries; + } + + public RequestOptionsBuilder setMaxNetworkRetries(int maxNetworkRetries) { + this.maxNetworkRetries = maxNetworkRetries; + return this; + } + + public String getAcceptLanguage() { + return acceptLanguage; + } + + public RequestOptionsBuilder setAcceptLanguage(String acceptLanguage) { + this.acceptLanguage = normalizeAcceptLanguage(acceptLanguage); + return this; + } + + public RequestOptions build() { + return new RequestOptions( + normalizeApiKey(this.apiKey), + normalizeAppId(this.appId), + normalizePrivateKey(this.privateKey), + normalizePublicVerifyKey(this.verifyPublicKey), + connectTimeout, + readTimeout, + maxNetworkRetries, + acceptLanguage); + } + } + + private static String normalizeApiKey(String apiKey) { + // null apiKeys are considered "valid" + if (apiKey == null) { + return null; + } + String normalized = apiKey.trim(); + if (normalized.isEmpty()) { + throw new InvalidRequestOptionsException("Empty API key specified!"); + } + return normalized; + } + + private static String normalizeAppId(String appId) { + // null app_ids are considered "valid" + if (appId == null) { + return null; + } + String normalized = appId.trim(); + if (normalized.isEmpty()) { + throw new InvalidRequestOptionsException("Empty app_id specified!"); + } + return normalized; + } + + private static String normalizePrivateKey(String privateKey) { + // null privateKey are considered "valid" + if (privateKey == null) { + return null; + } + String normalized = privateKey.trim(); + if (normalized.isEmpty()) { + throw new InvalidRequestOptionsException("Empty privateKey specified!"); + } + return normalized; + } + + private static String normalizePublicVerifyKey(String verifyPublicKey) { + if (verifyPublicKey == null) { + return null; + } + String normalized = verifyPublicKey.trim(); + if (normalized.isEmpty()) { + throw new InvalidRequestOptionsException("Empty publicVerifyKey specified!"); + } + return normalized; + } + + private static String normalizeAcceptLanguage(String acceptLanguage) { + // null acceptLanguage are considered "valid" + if (acceptLanguage == null) { + return null; + } + String normalized = acceptLanguage.trim(); + if (normalized.isEmpty()) { + throw new InvalidRequestOptionsException("Empty Accept-Language specified!"); + } + return normalized; + } + + public static class InvalidRequestOptionsException extends RuntimeException { + private static final long serialVersionUID = 1L; + + public InvalidRequestOptionsException(String message) { + super(message); + } + } +} diff --git a/pingpp-sdk/src/main/java/com/pingplusplus/net/SubAppBasedResource.java b/pingpp-sdk/src/main/java/com/pingplusplus/net/SubAppBasedResource.java new file mode 100644 index 0000000..c2f20ec --- /dev/null +++ b/pingpp-sdk/src/main/java/com/pingplusplus/net/SubAppBasedResource.java @@ -0,0 +1,34 @@ +package com.pingplusplus.net; + +import com.pingplusplus.Pingpp; + +public abstract class SubAppBasedResource extends AppBasedResource { + + /** + * @param clazz the class of object + * @param subAppId the ID of sub_app + * @return singleClassURL + */ + protected static String singleClassURL(Class clazz, String subAppId) { + return String.format("%s/v1/apps/%s/sub_apps/%s/%s", Pingpp.getApiBase(), APIResource.URIAppIdHolder, subAppId, className(clazz)); + } + + /** + * @param clazz the class of object + * @param subAppId the ID of sub_app + * @return classURL + */ + protected static String classURL(Class clazz, String subAppId) { + return String.format("%ss", singleClassURL(clazz, subAppId)); + } + + /** + * @param clazz the class of object + * @param subAppId the ID of sub_app + * @param id the id of object + * @return instanceURL + */ + protected static String instanceURL(Class clazz, String subAppId, String id) { + return String.format("%s/%s", classURL(clazz, subAppId), urlEncode(id)); + } +} diff --git a/pingpp-sdk/src/main/java/com/pingplusplus/net/UserBasedResource.java b/pingpp-sdk/src/main/java/com/pingplusplus/net/UserBasedResource.java new file mode 100644 index 0000000..48d7ebc --- /dev/null +++ b/pingpp-sdk/src/main/java/com/pingplusplus/net/UserBasedResource.java @@ -0,0 +1,34 @@ +package com.pingplusplus.net; + +import com.pingplusplus.Pingpp; + +public abstract class UserBasedResource extends AppBasedResource { + + /** + * @param clazz the class of object + * @param userId the id of user + * @return singleClassURL + */ + protected static String singleClassURL(Class clazz, String userId) { + return String.format("%s/v1/apps/%s/users/%s/%s", Pingpp.getApiBase(), APIResource.URIAppIdHolder, userId, className(clazz)); + } + + /** + * @param clazz the class of object + * @param userId the id of user + * @return classURL + */ + protected static String classURL(Class clazz, String userId) { + return String.format("%ss", singleClassURL(clazz, userId)); + } + + /** + * @param clazz the class of object + * @param userId the id of user + * @param id the id of object + * @return instanceURL + */ + protected static String instanceURL(Class clazz, String userId, String id) { + return String.format("%s/%s", classURL(clazz, userId), urlEncode(id)); + } +} diff --git a/src/main/java/com/pingplusplus/serializer/BatchRefundChargesSerializer.java b/pingpp-sdk/src/main/java/com/pingplusplus/serializer/BatchRefundChargesSerializer.java similarity index 100% rename from src/main/java/com/pingplusplus/serializer/BatchRefundChargesSerializer.java rename to pingpp-sdk/src/main/java/com/pingplusplus/serializer/BatchRefundChargesSerializer.java diff --git a/src/main/java/com/pingplusplus/serializer/BatchTransferRecipientSerializer.java b/pingpp-sdk/src/main/java/com/pingplusplus/serializer/BatchTransferRecipientSerializer.java similarity index 100% rename from src/main/java/com/pingplusplus/serializer/BatchTransferRecipientSerializer.java rename to pingpp-sdk/src/main/java/com/pingplusplus/serializer/BatchTransferRecipientSerializer.java diff --git a/src/main/java/com/pingplusplus/serializer/ChargeDeserializer.java b/pingpp-sdk/src/main/java/com/pingplusplus/serializer/ChargeDeserializer.java similarity index 78% rename from src/main/java/com/pingplusplus/serializer/ChargeDeserializer.java rename to pingpp-sdk/src/main/java/com/pingplusplus/serializer/ChargeDeserializer.java index 6043e2c..15ee2e3 100644 --- a/src/main/java/com/pingplusplus/serializer/ChargeDeserializer.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/serializer/ChargeDeserializer.java @@ -1,8 +1,6 @@ package com.pingplusplus.serializer; -import com.google.gson.FieldNamingPolicy; import com.google.gson.Gson; -import com.google.gson.GsonBuilder; import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; @@ -11,6 +9,7 @@ import com.pingplusplus.model.App; import com.pingplusplus.model.Charge; import com.pingplusplus.model.ChargeRefundCollection; +import com.pingplusplus.util.GsonUtils; import java.lang.reflect.Type; @@ -20,40 +19,41 @@ public class ChargeDeserializer implements JsonDeserializer { @Override - public Charge deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { - + public Charge deserialize(JsonElement jsonElement, + Type type, + JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { JsonObject chargeJson = jsonElement.getAsJsonObject(); if (null != chargeJson.getAsJsonObject("credential")) { JsonObject credentialJson = chargeJson.getAsJsonObject("credential"); JsonObject channelCredential; if (credentialJson.getAsJsonObject("wx") != null) { JsonObject wx = credentialJson.getAsJsonObject("wx"); - Long timeStamp = wx.get("timeStamp").getAsLong(); + long timeStamp = wx.get("timeStamp").getAsLong(); wx.addProperty("timeStamp", Long.toString(timeStamp)); } else if (credentialJson.getAsJsonObject("wx_pub") != null) { JsonObject wxPub = credentialJson.getAsJsonObject("wx_pub"); if (null == wxPub.get("signed_data") && wxPub.get("timeStamp") != null) { - Long timeStamp = wxPub.get("timeStamp").getAsLong(); + long timeStamp = wxPub.get("timeStamp").getAsLong(); wxPub.addProperty("timeStamp", Long.toString(timeStamp)); } } else if ((channelCredential = credentialJson.getAsJsonObject("bfb")) != null || (channelCredential = credentialJson.getAsJsonObject("bfb_wap")) != null) { if (channelCredential.has("total_amount")) { - Long total_amount = channelCredential.get("total_amount").getAsLong(); + long total_amount = channelCredential.get("total_amount").getAsLong(); channelCredential.addProperty("total_amount", Long.toString(total_amount)); } } else if ((channelCredential = credentialJson.getAsJsonObject("alipay")) != null || (channelCredential = credentialJson.getAsJsonObject("alipay_wap")) != null || (channelCredential = credentialJson.getAsJsonObject("alipay_pc_direct")) != null) { if (channelCredential.has("payment_type")) { - Long paymentType = channelCredential.get("payment_type").getAsLong(); + long paymentType = channelCredential.get("payment_type").getAsLong(); channelCredential.addProperty("payment_type", Long.toString(paymentType)); } } } - Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES). - registerTypeAdapter(ChargeRefundCollection.class, new ChargeRefundCollectionDeserializer()) + Gson gson = GsonUtils.baseGsonBuilder() + .registerTypeAdapter(ChargeRefundCollection.class, new ChargeRefundCollectionDeserializer()) .create(); JsonElement appElement = chargeJson.get("app"); Charge charge = gson.fromJson(jsonElement, Charge.class); diff --git a/src/main/java/com/pingplusplus/serializer/ChargeEssentialsSerializer.java b/pingpp-sdk/src/main/java/com/pingplusplus/serializer/ChargeEssentialsSerializer.java similarity index 100% rename from src/main/java/com/pingplusplus/serializer/ChargeEssentialsSerializer.java rename to pingpp-sdk/src/main/java/com/pingplusplus/serializer/ChargeEssentialsSerializer.java diff --git a/src/main/java/com/pingplusplus/serializer/ChargeRefundCollectionDeserializer.java b/pingpp-sdk/src/main/java/com/pingplusplus/serializer/ChargeRefundCollectionDeserializer.java similarity index 65% rename from src/main/java/com/pingplusplus/serializer/ChargeRefundCollectionDeserializer.java rename to pingpp-sdk/src/main/java/com/pingplusplus/serializer/ChargeRefundCollectionDeserializer.java index 8b1231a..48616ac 100644 --- a/src/main/java/com/pingplusplus/serializer/ChargeRefundCollectionDeserializer.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/serializer/ChargeRefundCollectionDeserializer.java @@ -1,26 +1,20 @@ package com.pingplusplus.serializer; -import com.google.gson.FieldNamingPolicy; import com.google.gson.Gson; -import com.google.gson.GsonBuilder; import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; import com.google.gson.JsonParseException; -import com.google.gson.reflect.TypeToken; import com.pingplusplus.model.ChargeRefundCollection; -import com.pingplusplus.model.Refund; +import com.pingplusplus.util.GsonUtils; import java.lang.reflect.Type; -import java.util.List; public class ChargeRefundCollectionDeserializer implements JsonDeserializer { public ChargeRefundCollection deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { - Gson gson = new GsonBuilder() - .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) - .create(); + Gson gson = GsonUtils.baseGsonBuilder().create(); return gson.fromJson(json, typeOfT); } diff --git a/src/main/java/com/pingplusplus/serializer/CouponTemplateExpirationSerializer.java b/pingpp-sdk/src/main/java/com/pingplusplus/serializer/CouponTemplateExpirationSerializer.java similarity index 100% rename from src/main/java/com/pingplusplus/serializer/CouponTemplateExpirationSerializer.java rename to pingpp-sdk/src/main/java/com/pingplusplus/serializer/CouponTemplateExpirationSerializer.java diff --git a/src/main/java/com/pingplusplus/serializer/DoubleTypeSerializer.java b/pingpp-sdk/src/main/java/com/pingplusplus/serializer/DoubleTypeSerializer.java similarity index 100% rename from src/main/java/com/pingplusplus/serializer/DoubleTypeSerializer.java rename to pingpp-sdk/src/main/java/com/pingplusplus/serializer/DoubleTypeSerializer.java diff --git a/src/main/java/com/pingplusplus/serializer/EventDataDeserializer.java b/pingpp-sdk/src/main/java/com/pingplusplus/serializer/EventDataDeserializer.java similarity index 100% rename from src/main/java/com/pingplusplus/serializer/EventDataDeserializer.java rename to pingpp-sdk/src/main/java/com/pingplusplus/serializer/EventDataDeserializer.java diff --git a/pingpp-sdk/src/main/java/com/pingplusplus/serializer/ObjectListDeserializer.java b/pingpp-sdk/src/main/java/com/pingplusplus/serializer/ObjectListDeserializer.java new file mode 100644 index 0000000..134ba36 --- /dev/null +++ b/pingpp-sdk/src/main/java/com/pingplusplus/serializer/ObjectListDeserializer.java @@ -0,0 +1,49 @@ +package com.pingplusplus.serializer; + +import com.google.gson.*; +import com.pingplusplus.util.GsonUtils; + +import java.lang.reflect.Type; +import java.util.ArrayList; +import java.util.List; + +public class ObjectListDeserializer implements JsonDeserializer> { + + @Override + public List deserialize(JsonElement elem, + Type type, + JsonDeserializationContext context) { + List list = new ArrayList<>(); + JsonArray jsonArray = elem.getAsJsonArray(); + + for (JsonElement element : jsonArray) { + if (element.isJsonNull()) { + list.add(null); + } else if (element.isJsonPrimitive()) { + JsonPrimitive primitive = element.getAsJsonPrimitive(); + if (primitive.isNumber()) { + String numStr = primitive.getAsString(); + if (numStr.contains(".")) { + list.add(primitive.getAsDouble()); + } else { + try { + list.add(primitive.getAsLong()); + } catch (NumberFormatException e) { + list.add(primitive.getAsBigDecimal()); + } + } + } else if (primitive.isString()) { + list.add(primitive.getAsString()); + } else if (primitive.isBoolean()) { + list.add(primitive.getAsBoolean()); + } + } else if (element.isJsonArray()) { + list.add(context.deserialize(element, GsonUtils.LIST_OBJ)); + } else if (element.isJsonObject()) { + list.add(context.deserialize(element, GsonUtils.MAP_STR_OBJ)); + } + } + + return list; + } +} \ No newline at end of file diff --git a/src/main/java/com/pingplusplus/serializer/PingppRawJsonObjectDeserializer.java b/pingpp-sdk/src/main/java/com/pingplusplus/serializer/PingppRawJsonObjectDeserializer.java similarity index 100% rename from src/main/java/com/pingplusplus/serializer/PingppRawJsonObjectDeserializer.java rename to pingpp-sdk/src/main/java/com/pingplusplus/serializer/PingppRawJsonObjectDeserializer.java diff --git a/src/main/java/com/pingplusplus/serializer/RedEnvelopeDeserializer.java b/pingpp-sdk/src/main/java/com/pingplusplus/serializer/RedEnvelopeDeserializer.java similarity index 79% rename from src/main/java/com/pingplusplus/serializer/RedEnvelopeDeserializer.java rename to pingpp-sdk/src/main/java/com/pingplusplus/serializer/RedEnvelopeDeserializer.java index e2b6ee9..251279b 100644 --- a/src/main/java/com/pingplusplus/serializer/RedEnvelopeDeserializer.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/serializer/RedEnvelopeDeserializer.java @@ -1,8 +1,6 @@ package com.pingplusplus.serializer; -import com.google.gson.FieldNamingPolicy; import com.google.gson.Gson; -import com.google.gson.GsonBuilder; import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; @@ -11,6 +9,7 @@ import com.pingplusplus.model.App; import com.pingplusplus.model.ChargeRefundCollection; import com.pingplusplus.model.RedEnvelope; +import com.pingplusplus.util.GsonUtils; import java.lang.reflect.Type; @@ -21,8 +20,8 @@ public class RedEnvelopeDeserializer implements JsonDeserializer { @Override public RedEnvelope deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject transFerJson = json.getAsJsonObject(); - Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES). - registerTypeAdapter(ChargeRefundCollection.class, new ChargeRefundCollectionDeserializer()) + Gson gson = GsonUtils.baseGsonBuilder() + .registerTypeAdapter(ChargeRefundCollection.class, new ChargeRefundCollectionDeserializer()) .create(); JsonElement appElement = transFerJson.get("app"); RedEnvelope redEnvelope = gson.fromJson(json, RedEnvelope.class); diff --git a/src/main/java/com/pingplusplus/serializer/SettleAccountRecipientSerializer.java b/pingpp-sdk/src/main/java/com/pingplusplus/serializer/SettleAccountRecipientSerializer.java similarity index 100% rename from src/main/java/com/pingplusplus/serializer/SettleAccountRecipientSerializer.java rename to pingpp-sdk/src/main/java/com/pingplusplus/serializer/SettleAccountRecipientSerializer.java diff --git a/pingpp-sdk/src/main/java/com/pingplusplus/serializer/StringObjectMapDeserializer.java b/pingpp-sdk/src/main/java/com/pingplusplus/serializer/StringObjectMapDeserializer.java new file mode 100644 index 0000000..42ad230 --- /dev/null +++ b/pingpp-sdk/src/main/java/com/pingplusplus/serializer/StringObjectMapDeserializer.java @@ -0,0 +1,52 @@ +package com.pingplusplus.serializer; + +import com.google.gson.*; +import com.pingplusplus.util.GsonUtils; + +import java.lang.reflect.Type; +import java.util.HashMap; +import java.util.Map; + +public class StringObjectMapDeserializer implements JsonDeserializer> { + + @Override + public Map deserialize(JsonElement elem, + Type type, + JsonDeserializationContext context) { + Map map = new HashMap<>(); + JsonObject jsonObject = elem.getAsJsonObject(); + + for (Map.Entry entry : jsonObject.entrySet()) { + String key = entry.getKey(); + JsonElement value = entry.getValue(); + + if (value.isJsonNull()) { + map.put(key, null); + } else if (value.isJsonPrimitive()) { + JsonPrimitive primitive = value.getAsJsonPrimitive(); + if (primitive.isNumber()) { + String numStr = primitive.getAsString(); + if (numStr.contains(".")) { + map.put(key, primitive.getAsDouble()); + } else { + try { + map.put(key, primitive.getAsLong()); + } catch (NumberFormatException e) { + map.put(key, primitive.getAsBigDecimal()); + } + } + } else if (primitive.isString()) { + map.put(key, primitive.getAsString()); + } else if (primitive.isBoolean()) { + map.put(key, primitive.getAsBoolean()); + } + } else if (value.isJsonArray()) { + map.put(key, context.deserialize(value, GsonUtils.LIST_OBJ)); + } else if (value.isJsonObject()) { + map.put(key, context.deserialize(value, GsonUtils.MAP_STR_OBJ)); + } + } + + return map; + } +} \ No newline at end of file diff --git a/src/main/java/com/pingplusplus/serializer/SubAppDeserializer.java b/pingpp-sdk/src/main/java/com/pingplusplus/serializer/SubAppDeserializer.java similarity index 84% rename from src/main/java/com/pingplusplus/serializer/SubAppDeserializer.java rename to pingpp-sdk/src/main/java/com/pingplusplus/serializer/SubAppDeserializer.java index e3aa75a..a137464 100644 --- a/src/main/java/com/pingplusplus/serializer/SubAppDeserializer.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/serializer/SubAppDeserializer.java @@ -2,6 +2,7 @@ import com.google.gson.*; import com.pingplusplus.model.*; +import com.pingplusplus.util.GsonUtils; import java.lang.reflect.Type; @@ -16,9 +17,7 @@ public SubApp deserialize(JsonElement jsonElement, Type type, JsonDeserializatio JsonObject subAppJson = jsonElement.getAsJsonObject(); - Gson gson = new GsonBuilder() - .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) - .create(); + Gson gson = GsonUtils.baseGsonBuilder().create(); JsonElement userElement = subAppJson.get("user"); SubApp subApp = gson.fromJson(jsonElement, SubApp.class); diff --git a/src/main/java/com/pingplusplus/serializer/TransferDeserializer.java b/pingpp-sdk/src/main/java/com/pingplusplus/serializer/TransferDeserializer.java similarity index 86% rename from src/main/java/com/pingplusplus/serializer/TransferDeserializer.java rename to pingpp-sdk/src/main/java/com/pingplusplus/serializer/TransferDeserializer.java index cc7812f..c0ed8d9 100644 --- a/src/main/java/com/pingplusplus/serializer/TransferDeserializer.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/serializer/TransferDeserializer.java @@ -1,8 +1,6 @@ package com.pingplusplus.serializer; -import com.google.gson.FieldNamingPolicy; import com.google.gson.Gson; -import com.google.gson.GsonBuilder; import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; @@ -11,6 +9,7 @@ import com.pingplusplus.model.App; import com.pingplusplus.model.ChargeRefundCollection; import com.pingplusplus.model.Transfer; +import com.pingplusplus.util.GsonUtils; import java.lang.reflect.Type; @@ -22,7 +21,7 @@ public class TransferDeserializer implements JsonDeserializer { public Transfer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject transFerJson = json.getAsJsonObject(); - Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) + Gson gson = GsonUtils.baseGsonBuilder() .registerTypeAdapter(ChargeRefundCollection.class, new ChargeRefundCollectionDeserializer()) .create(); JsonElement appElement = transFerJson.get("app"); diff --git a/pingpp-sdk/src/main/java/com/pingplusplus/util/CaseInsensitiveMap.java b/pingpp-sdk/src/main/java/com/pingplusplus/util/CaseInsensitiveMap.java new file mode 100644 index 0000000..15f13cb --- /dev/null +++ b/pingpp-sdk/src/main/java/com/pingplusplus/util/CaseInsensitiveMap.java @@ -0,0 +1,108 @@ +package com.pingplusplus.util; + +import java.io.Serializable; +import java.util.*; +import java.util.stream.Collectors; + +public class CaseInsensitiveMap extends AbstractMap + implements Map, Cloneable, Serializable { + private static final long serialVersionUID = 8157337634531724657L; + private Map> store; + + /** Instantiates a new instance of the {@link CaseInsensitiveMap} class. */ + public CaseInsensitiveMap() { + this.store = new HashMap>(); + } + + /** + * Returns an instance of {@link CaseInsensitiveMap} using the contents of another map. + * + * @param map the map to create the {@link CaseInsensitiveMap} from + * @return the {@link CaseInsensitiveMap} + */ + public static CaseInsensitiveMap of(Map map) { + if (map == null) { + return null; + } + CaseInsensitiveMap ciMap = new CaseInsensitiveMap<>(); + ciMap.putAll(map); + return ciMap; + } + + // Query Operations + + @Override + public boolean containsKey(Object key) { + String keyLower = convertKey(key); + return this.store.containsKey(keyLower); + } + + @Override + public boolean containsValue(Object value) { + return this.values().contains(value); + } + + @Override + public V get(Object key) { + String keyLower = convertKey(key); + Entry entry = this.store.get(keyLower); + if (entry == null) { + return null; + } + return entry.getValue(); + } + + // Modification Operations + + @Override + public V put(String key, V value) { + String keyLower = convertKey(key); + this.store.put(keyLower, new AbstractMap.SimpleEntry(key, value)); + return value; + } + + @Override + public V remove(Object key) { + String keyLower = convertKey(key); + Entry entry = this.store.remove(keyLower); + if (entry == null) { + return null; + } + return entry.getValue(); + } + + // Bulk Operations + + @Override + public void clear() { + this.store.clear(); + } + + // Views + + @Override + public Set keySet() { + return this.store.values().stream().map(Entry::getKey).collect(Collectors.toSet()); + } + + @Override + public Collection values() { + return this.store.values().stream().map(Entry::getValue).collect(Collectors.toList()); + } + + @Override + public Set> entrySet() { + return new HashSet<>(this.store.values()); + } + + // Utility + + private static String convertKey(Object key) { + if (key == null) { + return null; + } else if (key instanceof String) { + return ((String) key).toLowerCase(); + } + throw new IllegalArgumentException("key must be a String"); + } +} diff --git a/pingpp-sdk/src/main/java/com/pingplusplus/util/GsonUtils.java b/pingpp-sdk/src/main/java/com/pingplusplus/util/GsonUtils.java new file mode 100644 index 0000000..6187708 --- /dev/null +++ b/pingpp-sdk/src/main/java/com/pingplusplus/util/GsonUtils.java @@ -0,0 +1,25 @@ +package com.pingplusplus.util; + +import java.lang.reflect.Type; +import java.util.List; +import java.util.Map; + +import com.google.gson.FieldNamingPolicy; +import com.google.gson.GsonBuilder; +import com.google.gson.reflect.TypeToken; +import com.pingplusplus.serializer.ObjectListDeserializer; +import com.pingplusplus.serializer.StringObjectMapDeserializer; + +public final class GsonUtils { + public static final Type MAP_STR_OBJ = new TypeToken>() {}.getType(); + public static final Type LIST_OBJ = new TypeToken>() {}.getType(); + + private GsonUtils() {} + + public static GsonBuilder baseGsonBuilder() { + return new GsonBuilder() + .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) + .registerTypeAdapter(GsonUtils.MAP_STR_OBJ, new StringObjectMapDeserializer()) + .registerTypeAdapter(GsonUtils.LIST_OBJ, new ObjectListDeserializer()); + } +} diff --git a/pingpp-sdk/src/main/java/com/pingplusplus/util/PingppSignature.java b/pingpp-sdk/src/main/java/com/pingplusplus/util/PingppSignature.java new file mode 100644 index 0000000..a1db312 --- /dev/null +++ b/pingpp-sdk/src/main/java/com/pingplusplus/util/PingppSignature.java @@ -0,0 +1,150 @@ +package com.pingplusplus.util; + +import com.pingplusplus.Pingpp; +import com.pingplusplus.exception.AuthenticationException; +import com.pingplusplus.net.HttpHeaders; +import org.apache.commons.codec.binary.Base64; + +import java.io.UnsupportedEncodingException; +import java.security.*; +import java.security.spec.InvalidKeySpecException; +import java.security.spec.PKCS8EncodedKeySpec; +import java.security.spec.X509EncodedKeySpec; + +/** + * Created by Afon on 2016/12/16. + */ +public class PingppSignature { + public static String sign(String data, String PEMEncodedPrivateKey, String charset) { + PrivateKey privateKey = getPrivateKeyFromPEM(PEMEncodedPrivateKey); + if (privateKey == null) { + return null; + } + + try { + Signature signature = Signature.getInstance("SHA256withRSA"); + signature.initSign(privateKey); + signature.update(data.getBytes(charset)); + byte[] signBytes = signature.sign(); + + return Base64.encodeBase64String(signBytes).replaceAll("[\n\r]", ""); + } catch (NoSuchAlgorithmException | InvalidKeyException | SignatureException | UnsupportedEncodingException e) { + e.printStackTrace(); + } + + return null; + } + + /** + * webhook verify + * @param encodedSign + * @param data + * @param PEMEncodedPublicKey + * @param charset + * @return + * @throws AuthenticationException + */ + public static boolean verify(String encodedSign, String data, String PEMEncodedPublicKey, String charset) throws AuthenticationException { + if (StringUtils.isBlank(PEMEncodedPublicKey)) { + return true; + } + PublicKey publicKey = getPublicKeyFromPEM(PEMEncodedPublicKey); + if (publicKey == null) { + return true; + } + if (StringUtils.isBlank(encodedSign)) { + throw new AuthenticationException("响应签名 (X-Pingplusplus-Signature) 为空"); + } + try { + Signature signature = Signature.getInstance("SHA256withRSA"); + signature.initVerify(publicKey); + signature.update(data.getBytes(charset)); + return signature.verify(Base64.decodeBase64(encodedSign)); + } catch (InvalidKeyException | UnsupportedEncodingException | SignatureException | NoSuchAlgorithmException e) { + if (Pingpp.DEBUG) { + e.printStackTrace(); + } + return false; + } + } + + public static boolean verify(HttpHeaders headers, String data, String PEMEncodedPublicKey, String charset) throws AuthenticationException { + if (StringUtils.isBlank(PEMEncodedPublicKey)) { + return true; + } + PublicKey publicKey = getPublicKeyFromPEM(PEMEncodedPublicKey); + if (publicKey == null) { + return true; + } + if (!headers.firstValue("X-Pingplusplus-Signature").isPresent()) { + throw new AuthenticationException("响应签名 (X-Pingplusplus-Signature) 为空"); + } + String encodedSign = headers.firstValue("X-Pingplusplus-Signature").get(); + try { + Signature signature = Signature.getInstance("SHA256withRSA"); + signature.initVerify(publicKey); + signature.update(data.getBytes(charset)); + return signature.verify(Base64.decodeBase64(encodedSign)); + } catch (InvalidKeyException | UnsupportedEncodingException | SignatureException | NoSuchAlgorithmException e) { + if (Pingpp.DEBUG) { + e.printStackTrace(); + } + return false; + } + } + + + public static PrivateKey getPrivateKeyFromPEM(String PEMEncodedPrivateKey) { + PEMEncodedPrivateKey = PEMEncodedPrivateKey + .replaceAll("(-+BEGIN (RSA )?PRIVATE KEY-+\\r?\\n|-+END (RSA )?PRIVATE KEY-+\\r?\\n?)", ""); + byte[] privateKeyBytes = Base64.decodeBase64(PEMEncodedPrivateKey); + + try { + return generatePrivateKeyWithPKCS8(privateKeyBytes); + } catch (InvalidKeySpecException e) { + if (Pingpp.DEBUG) { + e.printStackTrace(); + } + return null; + } + } + + public static PrivateKey generatePrivateKeyWithPKCS8(byte[] privateKeyBytes) + throws InvalidKeySpecException { + try { + PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes); + KeyFactory kf = KeyFactory.getInstance("RSA"); + return kf.generatePrivate(keySpec); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } + return null; + } + + public static PublicKey getPublicKeyFromPEM(String PEMEncodedPublicKey) { + PEMEncodedPublicKey = PEMEncodedPublicKey + .replaceAll("(-+BEGIN (RSA )?PUBLIC KEY-+\\r?\\n|-+END (RSA )?PUBLIC KEY-+\\r?\\n?)", ""); + byte[] publicKeyBytes = Base64.decodeBase64(PEMEncodedPublicKey); + + try { + return generatePublicKey(publicKeyBytes); + } catch (InvalidKeySpecException e) { + if (Pingpp.DEBUG) { + e.printStackTrace(); + } + return null; + } + } + + public static PublicKey generatePublicKey(byte[] publicKeyBytes) + throws InvalidKeySpecException { + try { + X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes); + KeyFactory kf = KeyFactory.getInstance("RSA"); + return kf.generatePublic(keySpec); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } + return null; + } +} diff --git a/pingpp-sdk/src/main/java/com/pingplusplus/util/StreamUtils.java b/pingpp-sdk/src/main/java/com/pingplusplus/util/StreamUtils.java new file mode 100644 index 0000000..84de632 --- /dev/null +++ b/pingpp-sdk/src/main/java/com/pingplusplus/util/StreamUtils.java @@ -0,0 +1,39 @@ +package com.pingplusplus.util; + +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.nio.charset.Charset; + +import static java.util.Objects.requireNonNull; + +public class StreamUtils { + private static final int DEFAULT_BUF_SIZE = 1024; + + /** + * Reads the provided stream until the end and returns a string encoded with the provided charset. + * + * @param stream the stream to read + * @param charset the charset to use + * @return a string with the contents of the input stream + * @throws NullPointerException if {@code stream} or {@code charset} is {@code null} + * @throws IOException if an I/O error occurs + */ + public static String readToEnd(InputStream stream, Charset charset) throws IOException { + requireNonNull(stream); + requireNonNull(charset); + + final StringBuilder sb = new StringBuilder(); + final char[] buffer = new char[DEFAULT_BUF_SIZE]; + + try (Reader in = new InputStreamReader(stream, charset)) { + int charsRead = 0; + while ((charsRead = in.read(buffer, 0, buffer.length)) > 0) { + sb.append(buffer, 0, charsRead); + } + } + + return sb.toString(); + } +} diff --git a/pingpp-sdk/src/main/java/com/pingplusplus/util/StringUtils.java b/pingpp-sdk/src/main/java/com/pingplusplus/util/StringUtils.java new file mode 100644 index 0000000..ef6ce39 --- /dev/null +++ b/pingpp-sdk/src/main/java/com/pingplusplus/util/StringUtils.java @@ -0,0 +1,59 @@ +package com.pingplusplus.util; + +import java.util.List; +import java.util.regex.Pattern; + +import static java.util.Objects.requireNonNull; + +public class StringUtils { + private static Pattern whitespacePattern = Pattern.compile("\\s"); + + /** + * Checks whether a string contains any whitespace characters or not. + * + * @param str the string to check. + * @return {@code true} if the string contains any whitespace characters; otherwise, {@code + * false}. + */ + public static boolean containsWhitespace(String str) { + requireNonNull(str); + return whitespacePattern.matcher(str).find(); + } + + public static String join(String separator, List input) { + if (input == null || input.size() <= 0) return ""; + + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < input.size(); i++) { + sb.append(input.get(i)); + + // if not the last item + if (i != input.size() - 1) { + sb.append(separator); + } + } + + return sb.toString(); + } + + /** + * 是否为空 + * + * @param cs 字符串 + * @return 空 {@code true}; 非空 {@code false}. + */ + public static boolean isBlank(final CharSequence cs) { + if (cs == null) { + return true; + } + int l = cs.length(); + if (l > 0) { + for (int i = 0; i < l; i++) { + if (!Character.isWhitespace(cs.charAt(i))) { + return false; + } + } + } + return true; + } +} diff --git a/src/main/java/com/pingplusplus/util/WxLiteOAuth.java b/pingpp-sdk/src/main/java/com/pingplusplus/util/WxLiteOAuth.java similarity index 85% rename from src/main/java/com/pingplusplus/util/WxLiteOAuth.java rename to pingpp-sdk/src/main/java/com/pingplusplus/util/WxLiteOAuth.java index ff4fd04..d7f612b 100644 --- a/src/main/java/com/pingplusplus/util/WxLiteOAuth.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/util/WxLiteOAuth.java @@ -21,14 +21,20 @@ public class WxLiteOAuth extends WxpubOAuth { * @param appSecret 微信小程序应用密钥(注意保密) * @param code 授权 code, 登录时获取的 code * @return openid 微信小程序授权用户唯一标识 - * @throws UnsupportedEncodingException + * @throws UnsupportedEncodingException if the encoding is not supported */ public static String getOpenId(String appId, String appSecret, String code) throws UnsupportedEncodingException, ChannelException { AuthResult authResult = getSession(appId, appSecret, code); if (authResult.getErrmsg() != null) { - throw new ChannelException(authResult.getErrmsg(), authResult.getErrcode().toString(), null); + throw new ChannelException( + authResult.getErrmsg(), + null, + null, + authResult.getErrcode().toString(), + 0, + null); } return authResult.getOpenid(); @@ -41,7 +47,7 @@ public static String getOpenId(String appId, String appSecret, String code) * @param appSecret 微信小程序应用密钥(注意保密) * @param code 授权 code, 登录时获取的 code * @return openid 微信小程序授权用户唯一标识 - * @throws UnsupportedEncodingException + * @throws UnsupportedEncodingException if the encoding is not supported */ public static AuthResult getSession(String appId, String appSecret, String code) throws UnsupportedEncodingException { @@ -53,14 +59,13 @@ public static AuthResult getSession(String appId, String appSecret, String code) String url = "https://api.weixin.qq.com/sns/jscode2session?" + httpBuildQuery(params); String ret = httpGet(url); - AuthResult authResult = new GsonBuilder() + + return new GsonBuilder() .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) .create().fromJson(ret, AuthResult.class); - - return authResult; } - public class AuthResult { + public static class AuthResult { String sessionKey; String openid; String unionid; diff --git a/src/main/java/com/pingplusplus/util/WxpubOAuth.java b/pingpp-sdk/src/main/java/com/pingplusplus/util/WxpubOAuth.java similarity index 89% rename from src/main/java/com/pingplusplus/util/WxpubOAuth.java rename to pingpp-sdk/src/main/java/com/pingplusplus/util/WxpubOAuth.java index ae1faff..d6a7815 100644 --- a/src/main/java/com/pingplusplus/util/WxpubOAuth.java +++ b/pingpp-sdk/src/main/java/com/pingplusplus/util/WxpubOAuth.java @@ -1,10 +1,12 @@ package com.pingplusplus.util; -import com.google.gson.*; +import com.google.gson.FieldNamingPolicy; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import com.pingplusplus.exception.ChannelException; import java.io.BufferedReader; -import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; @@ -35,7 +37,7 @@ public class WxpubOAuth { * @param appSecret 微信公众号应用密钥(注意保密) * @param code 授权code, 通过调用WxpubOAuth.createOauthUrlForCode来获取 * @return openid 微信公众号授权用户唯一标识, 可用于微信网页内支付 - * @throws UnsupportedEncodingException + * @throws UnsupportedEncodingException if the encoding is not supported */ public static String getOpenId(String appId, String appSecret, String code) throws UnsupportedEncodingException, ChannelException { @@ -47,7 +49,13 @@ public static String getOpenId(String appId, String appSecret, String code) .create().fromJson(ret, OAuthResult.class); if (oAuthResult.getErrmsg() != null) { - throw new ChannelException(oAuthResult.getErrmsg(), oAuthResult.getErrcode().toString(), null); + throw new ChannelException( + oAuthResult.getErrmsg(), + null, + null, + oAuthResult.getErrcode().toString(), + 0, + null); } return oAuthResult.getOpenid(); @@ -63,7 +71,7 @@ public static String getOpenId(String appId, String appSecret, String code) * @param moreInfo FALSE 不弹出授权页面,直接跳转,这个只能拿到用户openid * TRUE 弹出授权页面,这个可以通过 openid 拿到昵称、性别、所在地, * @return 用于获取授权code的URL地址 - * @throws UnsupportedEncodingException + * @throws UnsupportedEncodingException if the encoding is not supported */ public static String createOauthUrlForCode(String appId, String redirectUrl, boolean moreInfo) throws UnsupportedEncodingException { @@ -86,7 +94,7 @@ public static String createOauthUrlForCode(String appId, String redirectUrl, boo * @param appSecret 微信公众号应用密钥(注意保密) * @param code 授权code, 通过调用WxpubOAuth.createOauthUrlForCode来获取 * @return 获取openid的URL地址 - * @throws UnsupportedEncodingException + * @throws UnsupportedEncodingException if the encoding is not supported */ private static String createOauthUrlForOpenid(String appId, String appSecret, String code) throws UnsupportedEncodingException { @@ -129,11 +137,10 @@ protected static String httpGet(String urlString) { result += line; } rd.close(); - } catch (IOException e) { - e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } + return result; } @@ -142,7 +149,7 @@ protected static String httpGet(String urlString) { * @param appId * @param appSecret * @return JsapiTicket - * @throws UnsupportedEncodingException + * @throws UnsupportedEncodingException if the encoding is not supported */ public static String getJsapiTicket(String appId, String appSecret) throws UnsupportedEncodingException { Map data = new HashMap(); @@ -152,8 +159,7 @@ public static String getJsapiTicket(String appId, String appSecret) throws Unsup String queryString = WxpubOAuth.httpBuildQuery(data); String accessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?" + queryString; String resp = httpGet(accessTokenUrl); - JsonParser jp = new JsonParser(); - JsonObject respJson = jp.parse(resp).getAsJsonObject(); + JsonObject respJson = JsonParser.parseString(resp).getAsJsonObject(); if (respJson.has("errcode")) { return respJson.toString(); } @@ -164,7 +170,7 @@ public static String getJsapiTicket(String appId, String appSecret) throws Unsup queryString = WxpubOAuth.httpBuildQuery(data); String jsapiTicketUrl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?" + queryString; resp = httpGet(jsapiTicketUrl); - JsonObject ticket = jp.parse(resp).getAsJsonObject(); + JsonObject ticket = JsonParser.parseString(resp).getAsJsonObject(); return ticket.get("ticket").getAsString(); } @@ -178,8 +184,7 @@ public static String getJsapiTicket(String appId, String appSecret) throws Unsup public static String getSignature(String charge, String jsapiTicket, String url) { if (null == charge || null == jsapiTicket || charge.isEmpty() || jsapiTicket.isEmpty()) return null; - JsonParser jp = new JsonParser(); - JsonObject chargeJson = jp.parse(charge).getAsJsonObject(); + JsonObject chargeJson = JsonParser.parseString(charge).getAsJsonObject(); if (!chargeJson.has("credential")) { return null; } @@ -201,9 +206,7 @@ public static String getSignature(String charge, String jsapiTicket, String url) crypt.reset(); crypt.update(string1.getBytes(CHARSET)); signature = byteToHex(crypt.digest()); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } catch (UnsupportedEncodingException e) { + } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) { e.printStackTrace(); } diff --git a/src/test/java/com/pingplusplus/CardInfoTest.java b/pingpp-sdk/src/test/java/com/pingplusplus/CardInfoTest.java similarity index 77% rename from src/test/java/com/pingplusplus/CardInfoTest.java rename to pingpp-sdk/src/test/java/com/pingplusplus/CardInfoTest.java index f23e763..f2c383f 100644 --- a/src/test/java/com/pingplusplus/CardInfoTest.java +++ b/pingpp-sdk/src/test/java/com/pingplusplus/CardInfoTest.java @@ -1,6 +1,6 @@ package com.pingplusplus; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.model.CardInfo; import org.junit.Test; @@ -13,7 +13,7 @@ public class CardInfoTest extends PingppTestBase { /** * 通过卡号查询卡信息。 */ - @Test public void testCardInfoQuery() throws RateLimitException, APIException, ChannelException, InvalidRequestException, APIConnectionException, AuthenticationException { + @Test public void testCardInfoQuery() throws PingppException { Map params = new HashMap(); params.put("app", PingppTestData.getAppID()); params.put("bank_account", "6222280012469823"); diff --git a/src/test/java/com/pingplusplus/PingppTest.java b/pingpp-sdk/src/test/java/com/pingplusplus/PingppTest.java similarity index 75% rename from src/test/java/com/pingplusplus/PingppTest.java rename to pingpp-sdk/src/test/java/com/pingplusplus/PingppTest.java index 745d506..368100a 100644 --- a/src/test/java/com/pingplusplus/PingppTest.java +++ b/pingpp-sdk/src/test/java/com/pingplusplus/PingppTest.java @@ -1,7 +1,8 @@ package com.pingplusplus; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.model.*; +import com.pingplusplus.net.APIResource; import org.junit.BeforeClass; import org.junit.Test; @@ -25,12 +26,55 @@ public class PingppTest { Pingpp.DEBUG = true; } + @Test public void testDeserialize() { + String jsonStr = "{\n" + + " \"id\": \"ch_1234567890\",\n" + + " \"object\": \"charge\",\n" + + " \"livemode\": true,\n" + + " \"paid\": true,\n" + + " \"refunded\": true,\n" + + " \"extra\": {\n" + + " \"discount_code\": \"ABCD\",\n" + + " \"discount_amount\": 20,\n" + + " \"score\": 60.12\n" + + " },\n" + + " \"time_paid\": 1732609210,\n" + + " \"time_expire\": 1732610502,\n" + + " \"time_settle\": null,\n" + + " \"transaction_no\": \"6523236536624\",\n" + + " \"amount_refunded\": 0,\n" + + " \"failure_code\": null,\n" + + " \"failure_msg\": null,\n" + + " \"metadata\": {\n" + + " \"code\": \"10000\",\n" + + " \"type\": \"PAYMENT\",\n" + + " \"list\": [10, 100, 1000]" + + " },\n" + + " \"credential\": {},\n" + + " \"description\": \"DESC\"\n" + + "}"; + Charge ch = APIResource.getGson().fromJson(jsonStr, Charge.class); + Object discountAmount = ch.getExtra().get("discount_amount"); + assertEquals(Long.class, discountAmount.getClass()); + assertEquals(20L, discountAmount); + Object score = ch.getExtra().get("score"); + assertEquals(Double.class, score.getClass()); + System.out.println("score value: " + score); + + Object metaCode = ch.getMetadata().get("code"); + assertEquals(String.class, metaCode.getClass()); + Object metaList = ch.getMetadata().get("list"); + assertEquals(ArrayList.class, metaList.getClass()); + Object metaListEle = ((ArrayList) metaList).get(0); + assertEquals(Long.class, metaListEle.getClass()); + } + @Test public void testSetApiKey() { assertEquals("apiKey should be set", "sk_test_ibbTe5jLGCi5rzfH4OqPW9KC", Pingpp.apiKey); } @Test public void testVerifyVersions() { - assertEquals("Pingpp.VERSION should match", "2.3.14", Pingpp.VERSION); + assertEquals("Pingpp.VERSION should match", "2.5.6", Pingpp.VERSION); } @Test public void testCreateCharge() { @@ -59,20 +103,11 @@ public class PingppTest { // 传到客户端请先转成字符串 .toString(), 调该方法,会自动转成正确的 JSON 字符串 String chargeString = charge.toString(); System.out.println(chargeString); - } catch (APIConnectionException e) { - e.printStackTrace(); - } catch (ChannelException e) { - e.printStackTrace(); - } catch (RateLimitException e) { - e.printStackTrace(); - } catch (AuthenticationException e) { - e.printStackTrace(); - } catch (APIException e) { - e.printStackTrace(); - } catch (InvalidRequestException e) { + } catch (PingppException e) { e.printStackTrace(); } + assertNotNull(charge); assertEquals("charge object should be charge", "charge", charge.getObject()); assertEquals("charge order_no", orderNo, charge.getOrderNo()); } @@ -106,17 +141,7 @@ public class PingppTest { System.out.println(chs); assertEquals("object should be list", "list", chs.getObject()); assertEquals("data count should be same with limit", limit.intValue(), chs.getData().size()); - } catch (AuthenticationException e) { - e.printStackTrace(); - } catch (InvalidRequestException e) { - e.printStackTrace(); - } catch (APIConnectionException e) { - e.printStackTrace(); - } catch (APIException e) { - e.printStackTrace(); - } catch (ChannelException e) { - e.printStackTrace(); - } catch (RateLimitException e) { + } catch (PingppException e) { e.printStackTrace(); } } @@ -130,17 +155,7 @@ public class PingppTest { assertEquals("object should be list", "list", objs.getObject()); assertEquals("data count should be same with per_page", limit.intValue(), objs.getData().size()); - } catch (AuthenticationException e) { - e.printStackTrace(); - } catch (InvalidRequestException e) { - e.printStackTrace(); - } catch (APIConnectionException e) { - e.printStackTrace(); - } catch (APIException e) { - e.printStackTrace(); - } catch (ChannelException e) { - e.printStackTrace(); - } catch (RateLimitException e) { + } catch (PingppException e) { e.printStackTrace(); } } @@ -174,24 +189,12 @@ public class PingppTest { assertEquals("order_no should be same", params.get("order_no"), obj.getOrderNo()); assertEquals("description should be same", params.get("description"), obj.getDescription()); assertEquals("channel should be same", params.get("channel"), obj.getChannel()); - } catch (AuthenticationException e) { - e.printStackTrace(); - } catch (InvalidRequestException e) { - e.printStackTrace(); - } catch (APIConnectionException e) { - e.printStackTrace(); - } catch (APIException e) { - e.printStackTrace(); - } catch (ChannelException e) { - e.printStackTrace(); - } catch (RateLimitException e) { + } catch (PingppException e) { e.printStackTrace(); } } - @Test public void testCreateBatchTransfer() throws - RateLimitException, APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testCreateBatchTransfer() throws PingppException { String batchNo = "2017" + new Date().getTime(); Map params = new HashMap(); @@ -245,22 +248,12 @@ public class PingppTest { // 发起 charge 撤销请求 charge = Charge.reverse(chargeId); System.out.println(charge); - } catch (APIConnectionException e) { - e.printStackTrace(); - } catch (ChannelException e) { - e.printStackTrace(); - } catch (RateLimitException e) { - e.printStackTrace(); - } catch (AuthenticationException e) { - e.printStackTrace(); - } catch (APIException e) { - e.printStackTrace(); - } catch (InvalidRequestException e) { + + assertEquals("charge object should be charge", "charge", charge.getObject()); + assertNotNull("charge reversed not null", charge.getReversed()); + } catch (PingppException e) { e.printStackTrace(); } - - assertEquals("charge object should be charge", "charge", charge.getObject()); - assertNotNull("charge reversed not null", charge.getReversed()); } @Test public void testRetrieveRefund() { @@ -271,17 +264,7 @@ public class PingppTest { try { refund = Refund.retrieve(chargeId, refundId); System.out.println(refund); - } catch (APIConnectionException e) { - e.printStackTrace(); - } catch (ChannelException e) { - e.printStackTrace(); - } catch (RateLimitException e) { - e.printStackTrace(); - } catch (AuthenticationException e) { - e.printStackTrace(); - } catch (APIException e) { - e.printStackTrace(); - } catch (InvalidRequestException e) { + } catch (PingppException e) { e.printStackTrace(); } diff --git a/src/test/java/com/pingplusplus/PingppTestBase.java b/pingpp-sdk/src/test/java/com/pingplusplus/PingppTestBase.java similarity index 100% rename from src/test/java/com/pingplusplus/PingppTestBase.java rename to pingpp-sdk/src/test/java/com/pingplusplus/PingppTestBase.java diff --git a/src/test/java/com/pingplusplus/PingppTestData.java b/pingpp-sdk/src/test/java/com/pingplusplus/PingppTestData.java similarity index 63% rename from src/test/java/com/pingplusplus/PingppTestData.java rename to pingpp-sdk/src/test/java/com/pingplusplus/PingppTestData.java index 597fd80..815aa81 100644 --- a/src/test/java/com/pingplusplus/PingppTestData.java +++ b/pingpp-sdk/src/test/java/com/pingplusplus/PingppTestData.java @@ -18,64 +18,7 @@ public static String getAppID() { } public static String getPKCS8PrivateKey() { - return "-----BEGIN PRIVATE KEY-----\n" + - "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDHYyS3FwoESp1h\n" + - "GLYiBhy6k9Ag3lzGCIEvm50IIEkE0Ftc9qq44TWqyl+EHUpTMdcBOcI42JLO5stw\n" + - "FOfCLa3PQStEJ4llIRFEKlsrHh67pvWd5RNaSBrvGlnFY40S+SZmjk2WF/h9dE9R\n" + - "ic79t0YI0alD8dIl9Yu3OaEKo7VonBWFwOYMxjPhtORlq+EUF1XJd//yftQrKWTT\n" + - "d7KaUonWzBCl4VzFop/OyTWYlTuZz3eYJaNpH5VaQ1vDgBAcPIeBvMf7NgBHMKW6\n" + - "LLmFd2LEYQ/6I7hkGTjysSzWEpO8bPWT6OEsJ2R2kFGOrSkr+G2MDcJ7ykXYAmz5\n" + - "+A3plS6ZAgMBAAECggEAVrgwR9GlcahiOtDcpn+yDxQq+aC9CQS561LrQZWJLKbS\n" + - "leRS7IZHKTlLwdJbeUO8F7RfXQoVEBghc2YkRrhHWFUn1ES95VY0hElHzcET7Nn5\n" + - "CeuQNzwVOtljIg7iVNY4dXJ/HEDguu/Tb8tYU9FajItj60FJ/WiGk/JksJPzWsOC\n" + - "VPVniy9fTbTLy1e+dCpCI6OXirtm7hvbodRNDjree0wSEzm7vL0wVzEZFo6kX+AB\n" + - "GUwaoO7pPyH+hgyI5Iuhc65NHsHzTJpf8yNFl9QGhkxvm2Ff2oEtDt1idOTBrHB6\n" + - "tg+ti9Ctb2+2yzBnk14hsSYJnKitR7wM6ZCFPX4eYQKBgQD+JAREeFkodec/SC+G\n" + - "X+4Q4Y68uMPkfUPrMKXM4cyY5wgXk64RBvRVxIxX7x6Y3tIKn9v8tWAprbsyVr15\n" + - "eb4RcAFEVwjuoZixhd9sIPsRhfdNolKn/fSPIsHL4ywcJMSIt7KVKHuQeqBNHy0o\n" + - "0PxQjNej1ozsmrAWqV55cbKHswKBgQDI2JQRTPIEC/2y6LdmBVhGJW9OKWTYdVNj\n" + - "q7rX+Yw4uxOtfd5hBqpvgZEklKEk72aazFdEcERlAm9SqoX09qk6zK/wcq4Xn5Q/\n" + - "qy8ecmjuyf2AK9X+HUdMerMVxhK9RpeevKYP/RO2F/wIN64anlQVYygVkXXgdOvW\n" + - "hBE4YABKgwKBgDRtmbPGYB5ItHwJmERQZfx1i8zDESaB8RED6DBsJJkmkDTM8ovw\n" + - "s1c+RPWfDuDalto6QFfR0xTGEmhAHLaCtwNB6AEBM4aHL8jvpTfZVfI3gN0zL3oY\n" + - "mestcG1vYBouO504yE6dG2Ci6479b4OMGYFEjPfvuwLUpp8GMcc7/WihAoGANCp8\n" + - "mtm/ammq5VMof2kX+nAyrrx1ovsmQ5cRGpOIZhvBCqjMn6rZjci7aCLqj+tWXRKC\n" + - "ABagzROK0o/T50JBxjHv6KYArcYW/Up7HI9ezdbM7wNzu2LjZ+veo+MkbuDs9J/P\n" + - "CgwTmJI2NfQwVl2VPVDZ0nBLi5cSwk7fIiNdL/0CgYEAtECmC1QDs53Di2MIsa/F\n" + - "e4sWfJGSDqEWqhcA/aPwf1skM6VJJXBBMV1qFtwgO1AlLnu9dQYra6ylsUoubVYI\n" + - "XM9XK7EMhbqi57+Q75jHFTc0DnzOTyho5Gp4Ddi8dztmZGNWdWTGdeMqh+svqMXk\n" + - "D6VdJeddyGu/Zlgj7Wk6whU=\n" + - "-----END PRIVATE KEY-----\n"; - } - - public static String getPKCS1PrivateKey() { - return "-----BEGIN RSA PRIVATE KEY-----\n" + - "MIIEowIBAAKCAQEAx2MktxcKBEqdYRi2IgYcupPQIN5cxgiBL5udCCBJBNBbXPaq\n" + - "uOE1qspfhB1KUzHXATnCONiSzubLcBTnwi2tz0ErRCeJZSERRCpbKx4eu6b1neUT\n" + - "Wkga7xpZxWONEvkmZo5Nlhf4fXRPUYnO/bdGCNGpQ/HSJfWLtzmhCqO1aJwVhcDm\n" + - "DMYz4bTkZavhFBdVyXf/8n7UKylk03eymlKJ1swQpeFcxaKfzsk1mJU7mc93mCWj\n" + - "aR+VWkNbw4AQHDyHgbzH+zYARzCluiy5hXdixGEP+iO4ZBk48rEs1hKTvGz1k+jh\n" + - "LCdkdpBRjq0pK/htjA3Ce8pF2AJs+fgN6ZUumQIDAQABAoIBAFa4MEfRpXGoYjrQ\n" + - "3KZ/sg8UKvmgvQkEuetS60GViSym0pXkUuyGRyk5S8HSW3lDvBe0X10KFRAYIXNm\n" + - "JEa4R1hVJ9REveVWNIRJR83BE+zZ+QnrkDc8FTrZYyIO4lTWOHVyfxxA4Lrv02/L\n" + - "WFPRWoyLY+tBSf1ohpPyZLCT81rDglT1Z4svX020y8tXvnQqQiOjl4q7Zu4b26HU\n" + - "TQ463ntMEhM5u7y9MFcxGRaOpF/gARlMGqDu6T8h/oYMiOSLoXOuTR7B80yaX/Mj\n" + - "RZfUBoZMb5thX9qBLQ7dYnTkwaxwerYPrYvQrW9vtsswZ5NeIbEmCZyorUe8DOmQ\n" + - "hT1+HmECgYEA/iQERHhZKHXnP0gvhl/uEOGOvLjD5H1D6zClzOHMmOcIF5OuEQb0\n" + - "VcSMV+8emN7SCp/b/LVgKa27Mla9eXm+EXABRFcI7qGYsYXfbCD7EYX3TaJSp/30\n" + - "jyLBy+MsHCTEiLeylSh7kHqgTR8tKND8UIzXo9aM7JqwFqleeXGyh7MCgYEAyNiU\n" + - "EUzyBAv9sui3ZgVYRiVvTilk2HVTY6u61/mMOLsTrX3eYQaqb4GRJJShJO9mmsxX\n" + - "RHBEZQJvUqqF9PapOsyv8HKuF5+UP6svHnJo7sn9gCvV/h1HTHqzFcYSvUaXnrym\n" + - "D/0Tthf8CDeuGp5UFWMoFZF14HTr1oQROGAASoMCgYA0bZmzxmAeSLR8CZhEUGX8\n" + - "dYvMwxEmgfERA+gwbCSZJpA0zPKL8LNXPkT1nw7g2pbaOkBX0dMUxhJoQBy2grcD\n" + - "QegBATOGhy/I76U32VXyN4DdMy96GJnrLXBtb2AaLjudOMhOnRtgouuO/W+DjBmB\n" + - "RIz377sC1KafBjHHO/1ooQKBgDQqfJrZv2ppquVTKH9pF/pwMq68daL7JkOXERqT\n" + - "iGYbwQqozJ+q2Y3Iu2gi6o/rVl0SggAWoM0TitKP0+dCQcYx7+imAK3GFv1KexyP\n" + - "Xs3WzO8Dc7ti42fr3qPjJG7g7PSfzwoME5iSNjX0MFZdlT1Q2dJwS4uXEsJO3yIj\n" + - "XS/9AoGBALRApgtUA7Odw4tjCLGvxXuLFnyRkg6hFqoXAP2j8H9bJDOlSSVwQTFd\n" + - "ahbcIDtQJS57vXUGK2uspbFKLm1WCFzPVyuxDIW6oue/kO+YxxU3NA58zk8oaORq\n" + - "eA3YvHc7ZmRjVnVkxnXjKofrL6jF5A+lXSXnXchrv2ZYI+1pOsIV\n" + - "-----END RSA PRIVATE KEY-----\n"; + return "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDHYyS3FwoESp1hGLYiBhy6k9Ag3lzGCIEvm50IIEkE0Ftc9qq44TWqyl+EHUpTMdcBOcI42JLO5stwFOfCLa3PQStEJ4llIRFEKlsrHh67pvWd5RNaSBrvGlnFY40S+SZmjk2WF/h9dE9Ric79t0YI0alD8dIl9Yu3OaEKo7VonBWFwOYMxjPhtORlq+EUF1XJd//yftQrKWTTd7KaUonWzBCl4VzFop/OyTWYlTuZz3eYJaNpH5VaQ1vDgBAcPIeBvMf7NgBHMKW6LLmFd2LEYQ/6I7hkGTjysSzWEpO8bPWT6OEsJ2R2kFGOrSkr+G2MDcJ7ykXYAmz5+A3plS6ZAgMBAAECggEAVrgwR9GlcahiOtDcpn+yDxQq+aC9CQS561LrQZWJLKbSleRS7IZHKTlLwdJbeUO8F7RfXQoVEBghc2YkRrhHWFUn1ES95VY0hElHzcET7Nn5CeuQNzwVOtljIg7iVNY4dXJ/HEDguu/Tb8tYU9FajItj60FJ/WiGk/JksJPzWsOCVPVniy9fTbTLy1e+dCpCI6OXirtm7hvbodRNDjree0wSEzm7vL0wVzEZFo6kX+ABGUwaoO7pPyH+hgyI5Iuhc65NHsHzTJpf8yNFl9QGhkxvm2Ff2oEtDt1idOTBrHB6tg+ti9Ctb2+2yzBnk14hsSYJnKitR7wM6ZCFPX4eYQKBgQD+JAREeFkodec/SC+GX+4Q4Y68uMPkfUPrMKXM4cyY5wgXk64RBvRVxIxX7x6Y3tIKn9v8tWAprbsyVr15eb4RcAFEVwjuoZixhd9sIPsRhfdNolKn/fSPIsHL4ywcJMSIt7KVKHuQeqBNHy0o0PxQjNej1ozsmrAWqV55cbKHswKBgQDI2JQRTPIEC/2y6LdmBVhGJW9OKWTYdVNjq7rX+Yw4uxOtfd5hBqpvgZEklKEk72aazFdEcERlAm9SqoX09qk6zK/wcq4Xn5Q/qy8ecmjuyf2AK9X+HUdMerMVxhK9RpeevKYP/RO2F/wIN64anlQVYygVkXXgdOvWhBE4YABKgwKBgDRtmbPGYB5ItHwJmERQZfx1i8zDESaB8RED6DBsJJkmkDTM8ovws1c+RPWfDuDalto6QFfR0xTGEmhAHLaCtwNB6AEBM4aHL8jvpTfZVfI3gN0zL3oYmestcG1vYBouO504yE6dG2Ci6479b4OMGYFEjPfvuwLUpp8GMcc7/WihAoGANCp8mtm/ammq5VMof2kX+nAyrrx1ovsmQ5cRGpOIZhvBCqjMn6rZjci7aCLqj+tWXRKCABagzROK0o/T50JBxjHv6KYArcYW/Up7HI9ezdbM7wNzu2LjZ+veo+MkbuDs9J/PCgwTmJI2NfQwVl2VPVDZ0nBLi5cSwk7fIiNdL/0CgYEAtECmC1QDs53Di2MIsa/Fe4sWfJGSDqEWqhcA/aPwf1skM6VJJXBBMV1qFtwgO1AlLnu9dQYra6ylsUoubVYIXM9XK7EMhbqi57+Q75jHFTc0DnzOTyho5Gp4Ddi8dztmZGNWdWTGdeMqh+svqMXkD6VdJeddyGu/Zlgj7Wk6whU="; } public static String getChargeWebhooksData() { diff --git a/pingpp-sdk/src/test/java/com/pingplusplus/RequestOptionsTest.java b/pingpp-sdk/src/test/java/com/pingplusplus/RequestOptionsTest.java new file mode 100644 index 0000000..8f9a135 --- /dev/null +++ b/pingpp-sdk/src/test/java/com/pingplusplus/RequestOptionsTest.java @@ -0,0 +1,87 @@ +package com.pingplusplus; + +import com.pingplusplus.exception.PingppException; +import com.pingplusplus.model.User; +import com.pingplusplus.net.RequestOptions; +import com.pingplusplus.net.RequestOptions.RequestOptionsBuilder; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +public class RequestOptionsTest { + + @Before + public void clearConfigs() { + Pingpp.apiKey = null; + Pingpp.appId = null; + Pingpp.privateKey = null; + } + + @Test + public void testRequestOptionsBuild() { + String apiKey = "sk_test_ibbTe5jLGCi5rzfH4OqnzhGs"; + String appId = "app_1Gqj58ynP0mNgKsc"; + String privateKey = "-----BEGIN......END-----".trim(); + RequestOptions options = new RequestOptionsBuilder() + .setApiKey(apiKey) + .setAppId(appId) + .setPrivateKey(privateKey) + .setConnectTimeout(5000) + .setReadTimeout(20000) + .setAcceptLanguage("zh-CN") + .setMaxNetworkRetries(2) + .build(); + + assertEquals("API Key should be", apiKey, options.getApiKey()); + assertEquals("App ID should be", appId, options.getAppId()); + assertEquals("Private key should be", privateKey, options.getPrivateKey()); + assertEquals("Connect timeout should be", 5000, options.getConnectTimeout()); + assertEquals("Read timeout should be", 20000, options.getReadTimeout()); + } + + @Test + public void testDefaultRequestOptionsBuilder() { + Pingpp.apiKey = PingppTestData.getApiKey(); + Pingpp.appId = PingppTestData.getAppID(); + Pingpp.privateKey = PingppTestData.getPKCS8PrivateKey(); + + RequestOptions options = new RequestOptionsBuilder().build(); + + assertEquals("API Key should be", PingppTestData.getApiKey(), options.getApiKey()); + assertEquals("App ID should be", PingppTestData.getAppID(), options.getAppId()); + assertEquals("Private key should be", PingppTestData.getPKCS8PrivateKey(), options.getPrivateKey()); + } + + @Test + public void testDefaultRequestOptions() { + Pingpp.apiKey = PingppTestData.getApiKey(); + Pingpp.appId = PingppTestData.getAppID(); + Pingpp.privateKey = PingppTestData.getPKCS8PrivateKey(); + + RequestOptions options = RequestOptions.getDefault(); + + assertEquals("API Key should be", PingppTestData.getApiKey(), options.getApiKey()); + assertEquals("App ID should be", PingppTestData.getAppID(), options.getAppId()); + assertEquals("Private key should be", PingppTestData.getPKCS8PrivateKey(), options.getPrivateKey()); + } + + @Test + public void testRequestOptionsRequest() throws PingppException { + RequestOptions options = new RequestOptionsBuilder() + .setApiKey(PingppTestData.getApiKey()) + .setAppId(PingppTestData.getAppID()) + .setPrivateKey(PingppTestData.getPKCS8PrivateKey()) + .build(); + + String userId = "test_user_001"; + User obj = User.retrieve(userId, options); + + assertNull("Pingpp.apiKey should be null", Pingpp.apiKey); + assertNull("Pingpp.appId should be null", Pingpp.appId); + assertEquals("object should be user", "user", obj.getObject()); + assertEquals("id", userId, obj.getId()); + assertEquals("app", PingppTestData.getAppID(), obj.getApp()); + } +} diff --git a/src/test/java/com/pingplusplus/SubBankTest.java b/pingpp-sdk/src/test/java/com/pingplusplus/SubBankTest.java similarity index 81% rename from src/test/java/com/pingplusplus/SubBankTest.java rename to pingpp-sdk/src/test/java/com/pingplusplus/SubBankTest.java index 0470429..1094a0e 100644 --- a/src/test/java/com/pingplusplus/SubBankTest.java +++ b/pingpp-sdk/src/test/java/com/pingplusplus/SubBankTest.java @@ -1,6 +1,6 @@ package com.pingplusplus; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.model.SubBank; import com.pingplusplus.model.SubBankCollection; import org.junit.Test; @@ -14,9 +14,7 @@ public class SubBankTest extends PingppTestBase { /** * 银行支行列表查询 */ - @Test public void testSubBankQuery() throws RateLimitException, APIException, - ChannelException, InvalidRequestException, APIConnectionException, - AuthenticationException { + @Test public void testSubBankQuery() throws PingppException { Map params = new HashMap<>(); params.put("app", PingppTestData.getAppID()); params.put("open_bank_code", "0308"); // 银行开户行编号 diff --git a/src/test/java/com/pingplusplus/WebhookTest.java b/pingpp-sdk/src/test/java/com/pingplusplus/WebhookTest.java similarity index 100% rename from src/test/java/com/pingplusplus/WebhookTest.java rename to pingpp-sdk/src/test/java/com/pingplusplus/WebhookTest.java diff --git a/src/test/java/com/pingplusplus/WeixinOpenidTest.java b/pingpp-sdk/src/test/java/com/pingplusplus/WeixinOpenidTest.java similarity index 100% rename from src/test/java/com/pingplusplus/WeixinOpenidTest.java rename to pingpp-sdk/src/test/java/com/pingplusplus/WeixinOpenidTest.java diff --git a/src/test/java/com/pingplusplus/agreement/AgreenmentTest.java b/pingpp-sdk/src/test/java/com/pingplusplus/agreement/AgreenmentTest.java similarity index 78% rename from src/test/java/com/pingplusplus/agreement/AgreenmentTest.java rename to pingpp-sdk/src/test/java/com/pingplusplus/agreement/AgreenmentTest.java index 309e8f3..b92c8c7 100644 --- a/src/test/java/com/pingplusplus/agreement/AgreenmentTest.java +++ b/pingpp-sdk/src/test/java/com/pingplusplus/agreement/AgreenmentTest.java @@ -2,7 +2,8 @@ import com.pingplusplus.PingppTestBase; import com.pingplusplus.PingppTestData; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.InvalidRequestException; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.model.Agreement; import com.pingplusplus.model.AgreementCollection; import org.junit.Test; @@ -16,9 +17,7 @@ public class AgreenmentTest extends PingppTestBase { @Test - public void testCreate() throws RateLimitException, - APIException, ChannelException, - APIConnectionException, AuthenticationException { + public void testCreate() throws PingppException { Map params = new HashMap<>(); params.put("app", PingppTestData.getAppID()); // App ID params.put("contract_no", "2018051700001"); // 签约协议号 @@ -40,9 +39,7 @@ public void testCreate() throws RateLimitException, } @Test - public void testRetrieve() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + public void testRetrieve() throws PingppException { String id = "agr_19EEE7QdgGMCoY"; Agreement agreement = Agreement.retrieve(id); @@ -51,9 +48,7 @@ public void testRetrieve() throws RateLimitException, } @Test - public void testList() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + public void testList() throws PingppException { Map params = new HashMap<>(); params.put("per_page", 3); // 每页数量 params.put("app", PingppTestData.getAppID()); // App ID 此参数必填 @@ -64,9 +59,7 @@ public void testList() throws RateLimitException, } @Test - public void testCancel() throws RateLimitException, - APIException, ChannelException, - APIConnectionException, AuthenticationException { + public void testCancel() throws PingppException { String id = "agr_19EEE7QdgGMCoY"; Agreement agreement; diff --git a/src/test/java/com/pingplusplus/balance/BalanceBonusTest.java b/pingpp-sdk/src/test/java/com/pingplusplus/balance/BalanceBonusTest.java similarity index 73% rename from src/test/java/com/pingplusplus/balance/BalanceBonusTest.java rename to pingpp-sdk/src/test/java/com/pingplusplus/balance/BalanceBonusTest.java index cf62d42..bad9bb2 100644 --- a/src/test/java/com/pingplusplus/balance/BalanceBonusTest.java +++ b/pingpp-sdk/src/test/java/com/pingplusplus/balance/BalanceBonusTest.java @@ -1,7 +1,7 @@ package com.pingplusplus.balance; import com.pingplusplus.PingppTestBase; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.model.BalanceBonus; import com.pingplusplus.model.BalanceBonusCollection; import org.junit.Test; @@ -16,9 +16,7 @@ public class BalanceBonusTest extends PingppTestBase { * 余额赠送 balance-bonus */ @Test - public void testBalanceBonusCreate() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + public void testBalanceBonusCreate() throws PingppException { Map params = new HashMap<>(); params.put("user", "user_test_02"); // 受赠的用户 ID, 必传 params.put("amount", 10); // 支付受赠余额,单位:分, 必传 @@ -34,9 +32,7 @@ public void testBalanceBonusCreate() throws RateLimitException, /** * 查询单个 balance-bonus */ - @Test public void testBalanceBonusRetrieve() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testBalanceBonusRetrieve() throws PingppException { // 查询单个 balance-bonus 方法 // 参数: balance-bonus id BalanceBonus obj = BalanceBonus.retrieve("650170821521710018560001"); @@ -47,9 +43,7 @@ public void testBalanceBonusCreate() throws RateLimitException, /** * 查询 balance-bonus 列表 */ - @Test public void testBalanceBonusList() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testBalanceBonusList() throws PingppException { Map params = new HashMap<>(); params.put("page", 1); params.put("per_page", 10); diff --git a/src/test/java/com/pingplusplus/balance/BalanceSettlementTest.java b/pingpp-sdk/src/test/java/com/pingplusplus/balance/BalanceSettlementTest.java similarity index 73% rename from src/test/java/com/pingplusplus/balance/BalanceSettlementTest.java rename to pingpp-sdk/src/test/java/com/pingplusplus/balance/BalanceSettlementTest.java index 169fed6..8a74b4e 100644 --- a/src/test/java/com/pingplusplus/balance/BalanceSettlementTest.java +++ b/pingpp-sdk/src/test/java/com/pingplusplus/balance/BalanceSettlementTest.java @@ -1,7 +1,7 @@ package com.pingplusplus.balance; import com.pingplusplus.PingppTestBase; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.model.BalanceSettlement; import com.pingplusplus.model.BalanceSettlementCollection; import org.junit.Test; @@ -16,9 +16,7 @@ public class BalanceSettlementTest extends PingppTestBase { * 查询单个结算到余额 */ @Test - public void testRetrieve() throws APIException, - RateLimitException, InvalidRequestException, - ChannelException, AuthenticationException, APIConnectionException { + public void testRetrieve() throws PingppException { // 查询单个 balance_settlement 方法 // 参数: balance_settlement id @@ -31,9 +29,7 @@ public void testRetrieve() throws APIException, * 查询结算到余额列表 */ @Test - public void testList() throws APIException, - RateLimitException, InvalidRequestException, - ChannelException, AuthenticationException, APIConnectionException { + public void testList() throws PingppException { Map params = new HashMap<>(); params.put("page", 1); params.put("per_page", 3); diff --git a/src/test/java/com/pingplusplus/balance/BalanceTransactionTest.java b/pingpp-sdk/src/test/java/com/pingplusplus/balance/BalanceTransactionTest.java similarity index 72% rename from src/test/java/com/pingplusplus/balance/BalanceTransactionTest.java rename to pingpp-sdk/src/test/java/com/pingplusplus/balance/BalanceTransactionTest.java index 93866c7..cb31df0 100644 --- a/src/test/java/com/pingplusplus/balance/BalanceTransactionTest.java +++ b/pingpp-sdk/src/test/java/com/pingplusplus/balance/BalanceTransactionTest.java @@ -1,7 +1,7 @@ package com.pingplusplus.balance; import com.pingplusplus.PingppTestBase; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.model.BalanceTransaction; import com.pingplusplus.model.BalanceTransactionCollection; import org.junit.Test; @@ -16,9 +16,7 @@ public class BalanceTransactionTest extends PingppTestBase { * 查询单个余额明细 (balance_transaction) */ @Test - public void testBalanceTransactionRetrieve() throws APIException, - RateLimitException, InvalidRequestException, - ChannelException, AuthenticationException, APIConnectionException { + public void testBalanceTransactionRetrieve() throws PingppException { // 查询单个 balance_transaction 方法 // 参数: balance_transaction id @@ -30,9 +28,7 @@ public void testBalanceTransactionRetrieve() throws APIException, /** * 查询余额明细 (balance_transaction) 列表 */ - @Test public void testBalanceTransactionList() throws APIException, - RateLimitException, InvalidRequestException, - ChannelException, AuthenticationException, APIConnectionException { + @Test public void testBalanceTransactionList() throws PingppException { Map params = new HashMap<>(); params.put("page", 1); params.put("per_page", 3); diff --git a/src/test/java/com/pingplusplus/balance/BalanceTransferTest.java b/pingpp-sdk/src/test/java/com/pingplusplus/balance/BalanceTransferTest.java similarity index 72% rename from src/test/java/com/pingplusplus/balance/BalanceTransferTest.java rename to pingpp-sdk/src/test/java/com/pingplusplus/balance/BalanceTransferTest.java index a823e35..f6277c9 100644 --- a/src/test/java/com/pingplusplus/balance/BalanceTransferTest.java +++ b/pingpp-sdk/src/test/java/com/pingplusplus/balance/BalanceTransferTest.java @@ -1,7 +1,7 @@ package com.pingplusplus.balance; import com.pingplusplus.PingppTestBase; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.model.BalanceTransfer; import com.pingplusplus.model.BalanceTransferCollection; import org.junit.Test; @@ -16,9 +16,7 @@ public class BalanceTransferTest extends PingppTestBase { * 创建 balance_transfer */ @Test - public void testBalanceTransfer() throws APIException, - RateLimitException, InvalidRequestException, - ChannelException, AuthenticationException, APIConnectionException { + public void testBalanceTransfer() throws PingppException { Map params = new HashMap(); params.put("user", "test_user_001"); params.put("recipient", "test_user_003"); @@ -36,9 +34,7 @@ public void testBalanceTransfer() throws APIException, /** * 查询单个 balance_transfer */ - @Test public void testBalanceTransferRetrieve() throws APIException, - RateLimitException, InvalidRequestException, - ChannelException, AuthenticationException, APIConnectionException { + @Test public void testBalanceTransferRetrieve() throws PingppException { // 查询单个 balance_transfer 方法 // 参数: balance_transfer id @@ -50,9 +46,7 @@ public void testBalanceTransfer() throws APIException, /** * 查询 balance_transfer 列表 */ - @Test public void testBalanceTransferList() throws APIException, - RateLimitException, InvalidRequestException, - ChannelException, AuthenticationException, APIConnectionException { + @Test public void testBalanceTransferList() throws PingppException { Map params = new HashMap<>(); params.put("page", 1); params.put("per_page", 10); diff --git a/src/test/java/com/pingplusplus/coupon/CouponTemplateTest.java b/pingpp-sdk/src/test/java/com/pingplusplus/coupon/CouponTemplateTest.java similarity index 79% rename from src/test/java/com/pingplusplus/coupon/CouponTemplateTest.java rename to pingpp-sdk/src/test/java/com/pingplusplus/coupon/CouponTemplateTest.java index 1a66df2..ccacace 100644 --- a/src/test/java/com/pingplusplus/coupon/CouponTemplateTest.java +++ b/pingpp-sdk/src/test/java/com/pingplusplus/coupon/CouponTemplateTest.java @@ -1,7 +1,7 @@ package com.pingplusplus.coupon; import com.pingplusplus.PingppTestBase; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.model.CouponTemplate; import com.pingplusplus.model.CouponTemplateCollection; import com.pingplusplus.model.DeletedCouponTemplate; @@ -17,9 +17,7 @@ public class CouponTemplateTest extends PingppTestBase { * 创建优惠券模板 */ @Test - public void testCouponTemplateCreate() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + public void testCouponTemplateCreate() throws PingppException { Map params = new HashMap(); params.put("name", "25%OFF"); // 优惠券模板名称, 可选 params.put("type", 2); // 优惠券模板的类型 1:现金券;2:折扣券, 必传 @@ -50,9 +48,7 @@ public void testCouponTemplateCreate() throws RateLimitException, /** * 查询优惠券模板 */ - @Test public void testCouponTemplateRetrieve() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testCouponTemplateRetrieve() throws PingppException { String couponTemplateId = "300117082315262900016402"; // 查询优惠券模板方法 // 参数: couponTemplateId (优惠券模板 id) @@ -64,9 +60,7 @@ public void testCouponTemplateCreate() throws RateLimitException, /** * 查询优惠券模板列表 */ - @Test public void testCouponTemplateList() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testCouponTemplateList() throws PingppException { Map params = new HashMap<>(); params.put("page", 1); params.put("per_page", 3); @@ -80,9 +74,7 @@ public void testCouponTemplateCreate() throws RateLimitException, /** * 更新优惠券模板 */ - @Test public void testCouponTemplateUpdate() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testCouponTemplateUpdate() throws PingppException { String couponTemplateId = "300117082315262900016402"; Map params = new HashMap<>(); Map metadata = new HashMap<>(); @@ -98,9 +90,7 @@ public void testCouponTemplateCreate() throws RateLimitException, /** * 删除优惠券模板 */ - @Test public void testCouponTemplateDelete() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testCouponTemplateDelete() throws PingppException { String couponTemplateId = "300117082315262900016402"; // 删除优惠券模板方法 // 参数: couponTemplateId (优惠券模板 id) diff --git a/src/test/java/com/pingplusplus/coupon/CouponTest.java b/pingpp-sdk/src/test/java/com/pingplusplus/coupon/CouponTest.java similarity index 72% rename from src/test/java/com/pingplusplus/coupon/CouponTest.java rename to pingpp-sdk/src/test/java/com/pingplusplus/coupon/CouponTest.java index bc46701..63d9423 100644 --- a/src/test/java/com/pingplusplus/coupon/CouponTest.java +++ b/pingpp-sdk/src/test/java/com/pingplusplus/coupon/CouponTest.java @@ -1,7 +1,7 @@ package com.pingplusplus.coupon; import com.pingplusplus.PingppTestBase; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.model.Coupon; import com.pingplusplus.model.CouponCollection; import com.pingplusplus.model.CouponTemplate; @@ -20,9 +20,7 @@ public class CouponTest extends PingppTestBase { * 创建单个优惠券 Coupon */ @Test - public void testCouponCreate() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + public void testCouponCreate() throws PingppException { String userId = "test_user_001"; // 用户 ID, 必传 Map params = new HashMap<>(); params.put("coupon_template", "300117082315262900016402"); // 优惠券模板 id, 必传 @@ -37,9 +35,7 @@ public void testCouponCreate() throws RateLimitException, /** * 批量创建优惠券 */ - @Test public void testCouponBatchCreate() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testCouponBatchCreate() throws PingppException { String couponId = "300117082315262900016402"; // 优惠券模板 id Map params = new HashMap<>(); List users = new ArrayList<>(); // 用户 ID 列表 @@ -53,9 +49,7 @@ public void testCouponCreate() throws RateLimitException, /** * 查询单个优惠券 Coupon */ - @Test public void testCouponRetrieve() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testCouponRetrieve() throws PingppException { String userId = "test_user_001"; String couponId = "300317082315265100025202"; // 查询单个优惠券 Coupon @@ -69,9 +63,7 @@ public void testCouponCreate() throws RateLimitException, /** * 查询用户优惠券 Coupon 列表 */ - @Test public void testUserCouponList() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testUserCouponList() throws PingppException { String userId = "test_user_001"; Map params = new HashMap<>(); params.put("page", 1); @@ -87,9 +79,7 @@ public void testCouponCreate() throws RateLimitException, /** * 查询优惠券模板下的优惠券列表 */ - @Test public void testCouponList() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testCouponList() throws PingppException { String couponId = "300117082315262900016402"; Map params = new HashMap<>(); params.put("page", 1); @@ -105,9 +95,7 @@ public void testCouponCreate() throws RateLimitException, /** * 更新优惠券 Coupon */ - @Test public void testCouponUpdate() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testCouponUpdate() throws PingppException { String userId = "test_user_001"; String couponId = "300317082315265100025202"; Map params = new HashMap<>(); @@ -127,9 +115,7 @@ public void testCouponCreate() throws RateLimitException, /** * 删除优惠券 Coupon */ - @Test public void testCouponDelete() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testCouponDelete() throws PingppException { String userId = "test_user_001"; String couponId = "300317022810293600038701"; // 删除优惠券 Coupon diff --git a/src/test/java/com/pingplusplus/order/OrderTest.java b/pingpp-sdk/src/test/java/com/pingplusplus/order/OrderTest.java similarity index 69% rename from src/test/java/com/pingplusplus/order/OrderTest.java rename to pingpp-sdk/src/test/java/com/pingplusplus/order/OrderTest.java index 6b6bccd..a5a9f58 100644 --- a/src/test/java/com/pingplusplus/order/OrderTest.java +++ b/pingpp-sdk/src/test/java/com/pingplusplus/order/OrderTest.java @@ -2,7 +2,7 @@ import com.pingplusplus.PingppTestBase; import com.pingplusplus.PingppTestData; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.model.*; import org.junit.Test; @@ -20,9 +20,7 @@ public class OrderTest extends PingppTestBase { /** * 创建 order */ - @Test public void testCreateOrder() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testCreateOrder() throws PingppException { Map params = new HashMap(); params.put("uid", "test_user_001"); // 用户在当前 app 下的 User ID, 可选 params.put("app", PingppTestData.getAppID()); // App ID, 必传 @@ -47,26 +45,26 @@ public class OrderTest extends PingppTestBase { /** * 支付 order */ - @Test public void testPayOrder() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testPayOrder() { Map params = new HashMap<>(); params.put("channel", "alipay_wap"); params.put("charge_amount", 100); Map extra = new HashMap<>(); // extra: 根据各个渠道传入相应的参数 extra.put("success_url", "http://www.pingxx.com"); params.put("extra", extra); - Order order = Order.pay("2001708220000281981", params); // 创建支付 Order 对象 方法 - - assertEquals("object should be order", "order", order.getObject()); + Order order; // 创建支付 Order 对象 方法 + try { + order = Order.pay("2001708220000281981", params); + assertEquals("object should be order", "order", order.getObject()); + } catch (PingppException e) { + System.out.println(e.toString()); + } } /** * 取消 order */ - @Test public void testCancelOrder() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testCancelOrder() throws PingppException { Order order = Order.cancel("2001708220000280391"); // 取消 Order 对象方法 assertEquals("object should be order", "order", order.getObject()); @@ -75,9 +73,7 @@ public class OrderTest extends PingppTestBase { /** * 查询单个 order */ - @Test public void testOrderRetrieve() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testOrderRetrieve() throws PingppException { Order obj = Order.retrieve("2001708220000281981"); // 查询单个 order 方法 参数: orderId assertNotNull(obj); @@ -87,9 +83,7 @@ public class OrderTest extends PingppTestBase { /** * 获取 order 列表 */ - @Test public void testGetOrderList() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testGetOrderList() throws PingppException { Map params = new HashMap(); params.put("page", 1); params.put("per_page", 3); @@ -106,9 +100,7 @@ public class OrderTest extends PingppTestBase { /** * 查询订单中 Charge 对象 */ - @Test public void testOrderChargeRetrieve() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testOrderChargeRetrieve() throws PingppException { // 查询订单中 Charge 对象 // 参数一: order id // 参数二: charge id @@ -119,9 +111,7 @@ public class OrderTest extends PingppTestBase { /** * 查询订单中 Charge 列表 */ - @Test public void testOrderChargeList() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testOrderChargeList() throws PingppException { Map params = new HashMap<>(); params.put("page", 1); params.put("per_page", 10); @@ -136,9 +126,7 @@ public class OrderTest extends PingppTestBase { /** * 创建 order 退款 */ - @Test public void testOrderRefundCreate() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testOrderRefundCreate() throws PingppException { Map params = new HashMap(); params.put("description", "Order refund test."); // 必传 @@ -155,9 +143,7 @@ public class OrderTest extends PingppTestBase { /** * 查询 order 退款 */ - @Test public void testOrderRefundRetrieve() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testOrderRefundRetrieve() throws PingppException { // 查询 order 退款方法 // 参数一: orderId // 参数二: refundId @@ -168,9 +154,7 @@ public class OrderTest extends PingppTestBase { /** * 查询 order 退款列表 */ - @Test public void testOrderRefundList() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testOrderRefundList() throws PingppException { // 查询 order 退款列表 // 参数: orderId diff --git a/src/test/java/com/pingplusplus/order/RoyaltySettlementTest.java b/pingpp-sdk/src/test/java/com/pingplusplus/order/RoyaltySettlementTest.java similarity index 73% rename from src/test/java/com/pingplusplus/order/RoyaltySettlementTest.java rename to pingpp-sdk/src/test/java/com/pingplusplus/order/RoyaltySettlementTest.java index df70ce7..9730b03 100644 --- a/src/test/java/com/pingplusplus/order/RoyaltySettlementTest.java +++ b/pingpp-sdk/src/test/java/com/pingplusplus/order/RoyaltySettlementTest.java @@ -2,7 +2,7 @@ import com.pingplusplus.PingppTestBase; import com.pingplusplus.PingppTestData; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.model.RoyaltySettlement; import com.pingplusplus.model.RoyaltySettlementCollection; import org.junit.Test; @@ -17,9 +17,7 @@ public class RoyaltySettlementTest extends PingppTestBase { * 创建 royalty_settlement */ @Test - public void testRoyaltySettlementCreate() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + public void testRoyaltySettlementCreate() throws PingppException { Map params = new HashMap<>(); params.put("payer_app", PingppTestData.getAppID()); // 分润发起方所在应用的 id, 必传 params.put("method", "alipay"); // 分润的方式,余额 balance 或渠道名称,例如 alipay, 必传 @@ -35,9 +33,7 @@ public void testRoyaltySettlementCreate() throws RateLimitException, /** * 查询单个 royalty_settlement */ - @Test public void testRoyaltySettlementRetrieve() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testRoyaltySettlementRetrieve() throws PingppException { // 查询单个 royalty_settlement 方法 // 参数: royalty_settlement id RoyaltySettlement obj = RoyaltySettlement.retrieve("170302171104000011"); @@ -48,9 +44,7 @@ public void testRoyaltySettlementCreate() throws RateLimitException, /** * 查询 royalty_settlement list */ - @Test public void testRoyaltySettlementList() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testRoyaltySettlementList() throws PingppException { Map params = new HashMap(); params.put("payer_app", PingppTestData.getAppID()); params.put("per_page", 3); @@ -65,9 +59,7 @@ public void testRoyaltySettlementCreate() throws RateLimitException, /** * 更新 royalty_settlement */ - @Test public void testRoyaltySettlementUpdate() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testRoyaltySettlementUpdate() throws PingppException { Map params = new HashMap<>(); params.put("status", "canceled"); // 需要更新的状态 [pending, canceled] // 更新 royalty_settlement 方法 diff --git a/src/test/java/com/pingplusplus/order/RoyaltyTemplateTest.java b/pingpp-sdk/src/test/java/com/pingplusplus/order/RoyaltyTemplateTest.java similarity index 80% rename from src/test/java/com/pingplusplus/order/RoyaltyTemplateTest.java rename to pingpp-sdk/src/test/java/com/pingplusplus/order/RoyaltyTemplateTest.java index e8d6218..a78101a 100644 --- a/src/test/java/com/pingplusplus/order/RoyaltyTemplateTest.java +++ b/pingpp-sdk/src/test/java/com/pingplusplus/order/RoyaltyTemplateTest.java @@ -2,7 +2,7 @@ import com.pingplusplus.PingppTestBase; import com.pingplusplus.PingppTestData; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.model.DeleteRoyaltyTemplate; import com.pingplusplus.model.RoyaltyTemplate; import com.pingplusplus.model.RoyaltyTemplateCollection; @@ -20,9 +20,7 @@ public class RoyaltyTemplateTest extends PingppTestBase { * 创建 royalty_template */ @Test - public void testRoyaltyTemplateCreate() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + public void testRoyaltyTemplateCreate() throws PingppException { Map params = new HashMap<>(); params.put("app", PingppTestData.getAppID()); // App ID, 必传 params.put("name", "royalty_templates name"); // 模板名称,允许中英文等常用字符, 可选 @@ -58,9 +56,7 @@ public void testRoyaltyTemplateCreate() throws RateLimitException, /** * 查询单个 royalty_template */ - @Test public void testRoyaltyTemplateRetrieve() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testRoyaltyTemplateRetrieve() throws PingppException { String id = "450170822152200001"; // 查询单个 royalty_template 方法 // 参数: royalty_template id @@ -73,9 +69,7 @@ public void testRoyaltyTemplateCreate() throws RateLimitException, /** * 查询 royalty_template 列表 */ - @Test public void testRoyaltyTemplateList() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testRoyaltyTemplateList() throws PingppException { Map params = new HashMap<>(); params.put("per_page", 3); params.put("page", 1); @@ -89,9 +83,7 @@ public void testRoyaltyTemplateCreate() throws RateLimitException, /** * 删除 royalty_template */ - @Test public void testRoyaltyTemplateDelete() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testRoyaltyTemplateDelete() throws PingppException { String id = "450170822185800001"; // 删除 royalty_template 方法 // 参数: royalty_template id @@ -103,9 +95,7 @@ public void testRoyaltyTemplateCreate() throws RateLimitException, /** * 更新 royalty_template */ - @Test public void testRoyaltyTemplateUpdate() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testRoyaltyTemplateUpdate() throws PingppException { Map params = new HashMap<>(); params.put("name", "royalty_templates name new"); diff --git a/src/test/java/com/pingplusplus/order/RoyaltyTest.java b/pingpp-sdk/src/test/java/com/pingplusplus/order/RoyaltyTest.java similarity index 78% rename from src/test/java/com/pingplusplus/order/RoyaltyTest.java rename to pingpp-sdk/src/test/java/com/pingplusplus/order/RoyaltyTest.java index 11df73e..8d33932 100644 --- a/src/test/java/com/pingplusplus/order/RoyaltyTest.java +++ b/pingpp-sdk/src/test/java/com/pingplusplus/order/RoyaltyTest.java @@ -2,7 +2,7 @@ import com.pingplusplus.PingppTestBase; import com.pingplusplus.PingppTestData; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.model.Royalty; import com.pingplusplus.model.RoyaltyCollection; import com.pingplusplus.model.RoyaltyDataResult; @@ -21,9 +21,7 @@ public class RoyaltyTest extends PingppTestBase { * 批量更新 royalty */ @Test - public void testRoyaltyBatchUpdate() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + public void testRoyaltyBatchUpdate() throws PingppException { Map params = new HashMap<>(); List ids = new ArrayList<>(); // 分润 ID 列表, 必传 ids.add("170301124238000111"); @@ -40,9 +38,7 @@ public void testRoyaltyBatchUpdate() throws RateLimitException, /** * 查询 royalty */ - @Test public void testRoyaltyRetrieve() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testRoyaltyRetrieve() throws PingppException { // 查询 royalty 方法 // 参数: royalty id Royalty obj = Royalty.retrieve("421170321093600003"); @@ -53,9 +49,7 @@ public void testRoyaltyBatchUpdate() throws RateLimitException, /** * 查询 royalty list */ - @Test public void testRoyaltyList() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testRoyaltyList() throws PingppException { Map params = new HashMap(); params.put("per_page", 3); // 可选 params.put("page", 1); // 可选 @@ -71,9 +65,7 @@ public void testRoyaltyBatchUpdate() throws RateLimitException, /** * 创建分润 */ - @Test public void testRoyaltyDataCreate() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testRoyaltyDataCreate() throws PingppException { Map params = new HashMap(); params.put("app", PingppTestData.getAppID()); // 必传 // params.put("charge", ""); // 条件可选,对于已经成功的 order 必传该字段 diff --git a/src/test/java/com/pingplusplus/order/RoyaltyTransactionTest.java b/pingpp-sdk/src/test/java/com/pingplusplus/order/RoyaltyTransactionTest.java similarity index 71% rename from src/test/java/com/pingplusplus/order/RoyaltyTransactionTest.java rename to pingpp-sdk/src/test/java/com/pingplusplus/order/RoyaltyTransactionTest.java index 3a8fac5..5a2d0f4 100644 --- a/src/test/java/com/pingplusplus/order/RoyaltyTransactionTest.java +++ b/pingpp-sdk/src/test/java/com/pingplusplus/order/RoyaltyTransactionTest.java @@ -1,7 +1,7 @@ package com.pingplusplus.order; import com.pingplusplus.PingppTestBase; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.model.RoyaltyTransaction; import com.pingplusplus.model.RoyaltyTransactionCollection; import org.junit.Test; @@ -16,9 +16,7 @@ public class RoyaltyTransactionTest extends PingppTestBase { * 查询单个 royalty_transaction */ @Test - public void testRoyaltyTransactionRetrieve() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + public void testRoyaltyTransactionRetrieve() throws PingppException { // 查询单个 royalty_transaction 方法 // 参数: royalty_transaction id RoyaltyTransaction obj = RoyaltyTransaction.retrieve("170302171104000011"); @@ -29,9 +27,7 @@ public void testRoyaltyTransactionRetrieve() throws RateLimitException, /** * 查询 royalty_transaction list */ - @Test public void testRoyaltyTransactionList() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testRoyaltyTransactionList() throws PingppException { Map params = new HashMap(); params.put("per_page", 3); params.put("page", 1); diff --git a/src/test/java/com/pingplusplus/recharge/RechargeTest.java b/pingpp-sdk/src/test/java/com/pingplusplus/recharge/RechargeTest.java similarity index 75% rename from src/test/java/com/pingplusplus/recharge/RechargeTest.java rename to pingpp-sdk/src/test/java/com/pingplusplus/recharge/RechargeTest.java index e72ca89..0cf376d 100644 --- a/src/test/java/com/pingplusplus/recharge/RechargeTest.java +++ b/pingpp-sdk/src/test/java/com/pingplusplus/recharge/RechargeTest.java @@ -1,7 +1,7 @@ package com.pingplusplus.recharge; import com.pingplusplus.PingppTestBase; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.model.*; import org.junit.Test; @@ -15,9 +15,7 @@ public class RechargeTest extends PingppTestBase { * 创建 recharge */ @Test - public void testRechargeCreate() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + public void testRechargeCreate() throws PingppException { Map params = new HashMap<>(); params.put("user", "user_test_02"); // 充值目标用户 ID, 必传 params.put("user_fee", 10); // 用户充值收取的手续费,单位分,不得大于 amount,不可和 balance_bonus[amount] 同时传,默认 0。可选 @@ -40,9 +38,7 @@ public void testRechargeCreate() throws RateLimitException, /** * 查询单个 recharge */ - @Test public void testRechargeRetrieve() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testRechargeRetrieve() throws PingppException { // 查询单个 recharge 方法 // 参数: rechargeId Recharge obj = Recharge.retrieve("220170822678080532480001"); @@ -53,9 +49,7 @@ public void testRechargeCreate() throws RateLimitException, /** * 查询 recharge 列表 */ - @Test public void testRechargeList() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testRechargeList() throws PingppException { Map params = new HashMap<>(); params.put("page", 1); params.put("per_page", 10); @@ -71,9 +65,7 @@ public void testRechargeCreate() throws RateLimitException, /** * 创建 recharge_refund */ - @Test public void testRechargeRefundCreate() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testRechargeRefundCreate() throws PingppException { Map params = new HashMap<>(); params.put("description", "Recharge Refund Description"); // 必传 // 创建 recharge_refund 方法 @@ -87,9 +79,7 @@ public void testRechargeCreate() throws RateLimitException, /** * 查询单个 recharge_refund */ - @Test public void testRechargeRefundRetrieve() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testRechargeRefundRetrieve() throws PingppException { // 查询单个 recharge_refund 方法 // 参数一: rechargeId // 参数二: refundId @@ -101,9 +91,7 @@ public void testRechargeCreate() throws RateLimitException, /** * 查询 recharge_refund 列表 */ - @Test public void testRechargeRefundList() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testRechargeRefundList() throws PingppException { Map params = new HashMap<>(); params.put("page", 1); params.put("per_page", 3); diff --git a/src/test/java/com/pingplusplus/settle_account/SettleAccountTest.java b/pingpp-sdk/src/test/java/com/pingplusplus/settle_account/SettleAccountTest.java similarity index 81% rename from src/test/java/com/pingplusplus/settle_account/SettleAccountTest.java rename to pingpp-sdk/src/test/java/com/pingplusplus/settle_account/SettleAccountTest.java index cfef596..f4a0e4e 100644 --- a/src/test/java/com/pingplusplus/settle_account/SettleAccountTest.java +++ b/pingpp-sdk/src/test/java/com/pingplusplus/settle_account/SettleAccountTest.java @@ -1,7 +1,7 @@ package com.pingplusplus.settle_account; import com.pingplusplus.PingppTestBase; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.model.DeletedSettleAccount; import com.pingplusplus.model.SettleAccount; import com.pingplusplus.model.SettleAccountCollection; @@ -18,9 +18,7 @@ public class SettleAccountTest extends PingppTestBase { * 创建结算账户 SettleAccount */ @Test - public void testSettleAccountCreate() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + public void testSettleAccountCreate() throws PingppException { String userId = "U2019053010510001"; Map params = new HashMap(); params.put("channel", "bank_account"); // [wx_pub, wx, alipay, bank_account] 其中一种 @@ -51,9 +49,7 @@ public void testSettleAccountCreate() throws RateLimitException, /** * 查询结算账户 */ - @Test public void testSettleAccountRetrieve() throws - RateLimitException, APIException, ChannelException, - InvalidRequestException, APIConnectionException, AuthenticationException { + @Test public void testSettleAccountRetrieve() throws PingppException { String userId = "U2019053010510001"; Map params = new HashMap<>(); params.put("page", 1); @@ -74,9 +70,7 @@ public void testSettleAccountCreate() throws RateLimitException, /** * 查询结算账户列表 */ - @Test public void testSettleAccountList() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testSettleAccountList() throws PingppException { String userId = "U2019053010510001"; Map params = new HashMap<>(); params.put("page", 1); @@ -90,9 +84,7 @@ public void testSettleAccountCreate() throws RateLimitException, /** * 删除结算账户 */ - @Test public void testSettleAccountDelete() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testSettleAccountDelete() throws PingppException { String userId = "U2019053010510001"; Map params = new HashMap<>(); params.put("page", 1); @@ -113,9 +105,7 @@ public void testSettleAccountCreate() throws RateLimitException, /** * 结算账户更新(存管相关) */ - @Test public void testSettleAccountUpdate() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testSettleAccountUpdate() throws PingppException { String userId = "U2019053010510001"; String id = "320119071220443300000101"; Map params = new HashMap<>(); @@ -142,9 +132,7 @@ public void testSettleAccountCreate() throws RateLimitException, /** * 结算账户更新手机号(存管相关) */ - @Test public void testSettleAccountUpdateMobile() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testSettleAccountUpdateMobile() throws PingppException { String userId = "U2019053010510001"; String id = "320119071220443300000101"; Map params = new HashMap<>(); @@ -158,9 +146,7 @@ public void testSettleAccountCreate() throws RateLimitException, /** * 结算账号打款验证接口(存管相关) */ - @Test public void testSettleAccountVerify() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testSettleAccountVerify() throws PingppException { String userId = "U2019053010510001"; String id = "320119071220443300000101"; Map params = new HashMap<>(); diff --git a/src/test/java/com/pingplusplus/split/ProfitTransactionTest.java b/pingpp-sdk/src/test/java/com/pingplusplus/split/ProfitTransactionTest.java similarity index 71% rename from src/test/java/com/pingplusplus/split/ProfitTransactionTest.java rename to pingpp-sdk/src/test/java/com/pingplusplus/split/ProfitTransactionTest.java index a7e535f..4b82e2e 100644 --- a/src/test/java/com/pingplusplus/split/ProfitTransactionTest.java +++ b/pingpp-sdk/src/test/java/com/pingplusplus/split/ProfitTransactionTest.java @@ -2,7 +2,7 @@ import com.pingplusplus.PingppTestBase; import com.pingplusplus.PingppTestData; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.model.ProfitTransaction; import com.pingplusplus.model.ProfitTransactionCollection; import org.junit.Test; @@ -14,8 +14,7 @@ public class ProfitTransactionTest extends PingppTestBase { - @Test public void testListAll() throws RateLimitException, APIException, ChannelException, - InvalidRequestException, APIConnectionException, AuthenticationException { + @Test public void testListAll() throws PingppException { Map params = new HashMap<>(); params.put("app", PingppTestData.getAppID()); params.put("page", 1); @@ -28,8 +27,7 @@ public class ProfitTransactionTest extends PingppTestBase { assertEquals("profit_transaction", obj.getData().get(0).getObject()); } - @Test public void testRetrieve() throws RateLimitException, APIException, ChannelException, - InvalidRequestException, APIConnectionException, AuthenticationException { + @Test public void testRetrieve() throws PingppException { ProfitTransaction obj = ProfitTransaction.retrieve("ptxn_1m3xtoBMRqu2qC"); System.out.println(obj); diff --git a/src/test/java/com/pingplusplus/split/SplitProfitTest.java b/pingpp-sdk/src/test/java/com/pingplusplus/split/SplitProfitTest.java similarity index 77% rename from src/test/java/com/pingplusplus/split/SplitProfitTest.java rename to pingpp-sdk/src/test/java/com/pingplusplus/split/SplitProfitTest.java index 4796fc8..14ed828 100644 --- a/src/test/java/com/pingplusplus/split/SplitProfitTest.java +++ b/pingpp-sdk/src/test/java/com/pingplusplus/split/SplitProfitTest.java @@ -2,7 +2,7 @@ import com.pingplusplus.PingppTestBase; import com.pingplusplus.PingppTestData; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.model.*; import org.junit.Test; @@ -14,8 +14,7 @@ import static org.junit.Assert.assertEquals; public class SplitProfitTest extends PingppTestBase { - @Test public void testCreate() throws RateLimitException, APIException, ChannelException, - InvalidRequestException, APIConnectionException, AuthenticationException { + @Test public void testCreate() throws PingppException { Map params = new HashMap<>(); params.put("app", PingppTestData.getAppID()); params.put("charge", "ch_KiT4ePn9SyXDfvfPyDevn1G0"); // 交易成功的 Ping++ Charge ID @@ -36,8 +35,7 @@ public class SplitProfitTest extends PingppTestBase { assertEquals("split_profit", obj.getObject()); } - @Test public void testListAll() throws RateLimitException, APIException, ChannelException, - InvalidRequestException, APIConnectionException, AuthenticationException { + @Test public void testListAll() throws PingppException { Map params = new HashMap<>(); params.put("app", PingppTestData.getAppID()); params.put("page", 1); @@ -50,8 +48,7 @@ public class SplitProfitTest extends PingppTestBase { assertEquals("split_profit", obj.getData().get(0).getObject()); } - @Test public void testRetrieve() throws RateLimitException, APIException, ChannelException, - InvalidRequestException, APIConnectionException, AuthenticationException { + @Test public void testRetrieve() throws PingppException { SplitProfit obj = SplitProfit.retrieve("sp_1iXmM0w3VaE77Y"); System.out.println(obj); diff --git a/src/test/java/com/pingplusplus/split/SplitReceiverTest.java b/pingpp-sdk/src/test/java/com/pingplusplus/split/SplitReceiverTest.java similarity index 71% rename from src/test/java/com/pingplusplus/split/SplitReceiverTest.java rename to pingpp-sdk/src/test/java/com/pingplusplus/split/SplitReceiverTest.java index 6104e15..377931a 100644 --- a/src/test/java/com/pingplusplus/split/SplitReceiverTest.java +++ b/pingpp-sdk/src/test/java/com/pingplusplus/split/SplitReceiverTest.java @@ -14,8 +14,7 @@ import static org.junit.Assert.assertEquals; public class SplitReceiverTest extends PingppTestBase { - @Test public void testCreate() throws RateLimitException, APIException, ChannelException, - InvalidRequestException, APIConnectionException, AuthenticationException { + @Test public void testCreate() throws PingppException { Map params = new HashMap<>(); params.put("app", PingppTestData.getAppID()); params.put("type", "MERCHANT_ID"); // 分账接收方类型 @@ -29,8 +28,7 @@ public class SplitReceiverTest extends PingppTestBase { assertEquals("split_receiver", obj.getObject()); } - @Test public void testListAll() throws RateLimitException, APIException, ChannelException, - InvalidRequestException, APIConnectionException, AuthenticationException { + @Test public void testListAll() throws PingppException { Map params = new HashMap<>(); params.put("app", PingppTestData.getAppID()); params.put("page", 1); @@ -43,8 +41,7 @@ public class SplitReceiverTest extends PingppTestBase { assertEquals("split_receiver", obj.getData().get(0).getObject()); } - @Test public void testRetrieve() throws RateLimitException, APIException, ChannelException, - InvalidRequestException, APIConnectionException, AuthenticationException { + @Test public void testRetrieve() throws PingppException { SplitReceiver obj = SplitReceiver.retrieve("recv_1fRc57XpIehmFI"); System.out.println(obj); @@ -52,8 +49,7 @@ public class SplitReceiverTest extends PingppTestBase { assertEquals("split_receiver", obj.getObject()); } - @Test public void testDelete() throws RateLimitException, APIException, ChannelException, - InvalidRequestException, APIConnectionException, AuthenticationException { + @Test public void testDelete() throws PingppException { DeletedSplitReceiver obj = SplitReceiver.delete("recv_1fRc57XpIehmFI"); System.out.println(obj); diff --git a/src/test/java/com/pingplusplus/sub_app/ContactTest.java b/pingpp-sdk/src/test/java/com/pingplusplus/sub_app/ContactTest.java similarity index 89% rename from src/test/java/com/pingplusplus/sub_app/ContactTest.java rename to pingpp-sdk/src/test/java/com/pingplusplus/sub_app/ContactTest.java index f3ec779..174a0a4 100644 --- a/src/test/java/com/pingplusplus/sub_app/ContactTest.java +++ b/pingpp-sdk/src/test/java/com/pingplusplus/sub_app/ContactTest.java @@ -1,7 +1,7 @@ package com.pingplusplus.sub_app; import com.pingplusplus.PingppTestBase; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.model.Contact; import org.junit.Test; @@ -18,9 +18,7 @@ public class ContactTest extends PingppTestBase { /** * 证件上传 */ - @Test public void testAddContact() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testAddContact() throws PingppException { Map params = new HashMap<>(); params.put("user", "test_user_001"); // 用户 ID params.put("acc_no", "2019057929311601000631"); // 壹账通用户编号。此编号可在证件上传接口的返回获得 diff --git a/src/test/java/com/pingplusplus/sub_app/SubAppChannelTest.java b/pingpp-sdk/src/test/java/com/pingplusplus/sub_app/SubAppChannelTest.java similarity index 77% rename from src/test/java/com/pingplusplus/sub_app/SubAppChannelTest.java rename to pingpp-sdk/src/test/java/com/pingplusplus/sub_app/SubAppChannelTest.java index cd554ad..c209f4d 100644 --- a/src/test/java/com/pingplusplus/sub_app/SubAppChannelTest.java +++ b/pingpp-sdk/src/test/java/com/pingplusplus/sub_app/SubAppChannelTest.java @@ -1,7 +1,7 @@ package com.pingplusplus.sub_app; import com.pingplusplus.PingppTestBase; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.model.Channel; import com.pingplusplus.model.DeletedChannel; import org.junit.Test; @@ -16,9 +16,7 @@ public class SubAppChannelTest extends PingppTestBase { * 配置子商户 app 渠道参数 */ @Test - public void testSubAppChannelCreate() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + public void testSubAppChannelCreate() throws PingppException { String subAppId = "app_rbDmXLHmLqbTLKm9"; Map params = new HashMap<>(); @@ -42,9 +40,7 @@ public void testSubAppChannelCreate() throws RateLimitException, /** * 查询子商户 app 渠道参数 */ - @Test public void testSubAppChannelRetrieve() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testSubAppChannelRetrieve() throws PingppException { String subAppId = "app_rbDmXLHmLqbTLKm9"; String channel = "bfb"; @@ -59,9 +55,7 @@ public void testSubAppChannelCreate() throws RateLimitException, /** * 更新子商户 app 渠道参数 */ - @Test public void testSubAppChannelUpdate() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testSubAppChannelUpdate() throws PingppException { String subAppId = "app_rbDmXLHmLqbTLKm9"; String channel = "bfb"; @@ -85,9 +79,7 @@ public void testSubAppChannelCreate() throws RateLimitException, /** * 删除子商户 app 渠道参数 */ - @Test public void testSubAppChannelDelete() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testSubAppChannelDelete() throws PingppException { String subAppId = "app_rbDmXLHmLqbTLKm9"; String channel = "bfb"; diff --git a/src/test/java/com/pingplusplus/sub_app/SubAppTest.java b/pingpp-sdk/src/test/java/com/pingplusplus/sub_app/SubAppTest.java similarity index 71% rename from src/test/java/com/pingplusplus/sub_app/SubAppTest.java rename to pingpp-sdk/src/test/java/com/pingplusplus/sub_app/SubAppTest.java index c6219ee..430aee2 100644 --- a/src/test/java/com/pingplusplus/sub_app/SubAppTest.java +++ b/pingpp-sdk/src/test/java/com/pingplusplus/sub_app/SubAppTest.java @@ -2,7 +2,7 @@ import com.pingplusplus.PingppTestBase; import com.pingplusplus.PingppTestData; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.model.DeletedSubApp; import com.pingplusplus.model.SubApp; import com.pingplusplus.model.SubAppCollection; @@ -18,9 +18,7 @@ public class SubAppTest extends PingppTestBase { * 创建子商户 app */ @Test - public void testSubAppCreate() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + public void testSubAppCreate() throws PingppException { String userId = "sub_app_user_" + System.currentTimeMillis(); String displayName = "sub_app_" + System.currentTimeMillis(); @@ -41,9 +39,7 @@ public void testSubAppCreate() throws RateLimitException, /** * 查询子商户 app */ - @Test public void testSubAppRetrieve() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testSubAppRetrieve() throws PingppException { String subAppId = "app_rbDmXLHmLqbTLKm9"; // 查询子商户 app // 参数: SubApp id @@ -56,9 +52,7 @@ public void testSubAppCreate() throws RateLimitException, /** * 查询子商户 app 列表 */ - @Test public void testSubAppList() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testSubAppList() throws PingppException { Map params = new HashMap<>(); params.put("page", 1); params.put("per_page", 3); @@ -72,9 +66,7 @@ public void testSubAppCreate() throws RateLimitException, /** * 更新子商户 app */ - @Test public void testSubAppUpdate() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testSubAppUpdate() throws PingppException { String subAppId = "app_rbDmXLHmLqbTLKm9"; Map params = new HashMap<>(); // 更新子商户 app 方法 @@ -88,9 +80,7 @@ public void testSubAppCreate() throws RateLimitException, /** * 删除子商户 app */ - @Test public void testSubAppDelete() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testSubAppDelete() throws PingppException { String subAppId = "app_rbDmXLHmLqbTLKm9"; // 删除子商户 app 方法 // 参数: SubApp id diff --git a/src/test/java/com/pingplusplus/transfer/TransferTest.java b/pingpp-sdk/src/test/java/com/pingplusplus/transfer/TransferTest.java similarity index 88% rename from src/test/java/com/pingplusplus/transfer/TransferTest.java rename to pingpp-sdk/src/test/java/com/pingplusplus/transfer/TransferTest.java index 3f22efc..8818ea2 100644 --- a/src/test/java/com/pingplusplus/transfer/TransferTest.java +++ b/pingpp-sdk/src/test/java/com/pingplusplus/transfer/TransferTest.java @@ -2,7 +2,7 @@ import com.pingplusplus.PingppTestBase; import com.pingplusplus.PingppTestData; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.model.Transfer; import com.pingplusplus.model.TransferCollection; import org.junit.Test; @@ -19,8 +19,7 @@ public class TransferTest extends PingppTestBase { /** * 创建 Transfer 对象- alipay 渠道 */ - @Test public void testAlipayTransferCreate() throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { + @Test public void testAlipayTransferCreate() throws PingppException { Map params = new HashMap<>(); // 付款使用的商户内部订单号。 alipay 为 1 ~ 64 位不能重复的数字字母组合; @@ -56,8 +55,7 @@ public class TransferTest extends PingppTestBase { /** * 创建 Transfer 对象- allinpay 渠道 */ - @Test public void testAllinpayTransferCreate() throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { + @Test public void testAllinpayTransferCreate() throws PingppException { Map params = new HashMap<>(); // 付款使用的商户内部订单号。 allinpay 限长20-40位不能重复的数字字母组合,必须以签约的通联的商户号开头(建议组合格式:通联商户号 + 时间戳 + 固定位数顺序流水号,不包含+号) params.put("order_no", "099002017" + System.currentTimeMillis()); @@ -95,8 +93,7 @@ public class TransferTest extends PingppTestBase { /** * 创建 Transfer 对象- jdpay 渠道 */ - @Test public void testJdpayTransferCreate() throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { + @Test public void testJdpayTransferCreate() throws PingppException { Map params = new HashMap<>(); // 付款使用的商户内部订单号。 jdpay 限长1-64位不能重复的数字字母组合; params.put("order_no", "2017" + System.currentTimeMillis()); @@ -129,8 +126,7 @@ public class TransferTest extends PingppTestBase { /** * 创建 Transfer 对象- unionpay 渠道 */ - @Test public void testUnionpayTransferCreate() throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { + @Test public void testUnionpayTransferCreate() throws PingppException { Map params = new HashMap<>(); // 付款使用的商户内部订单号。 unionpay 为1~16位的纯数字。 params.put("order_no", "17" + System.currentTimeMillis()); @@ -175,8 +171,7 @@ public class TransferTest extends PingppTestBase { /** * 创建 Transfer 对象- wx_pub 渠道 */ - @Test public void testWxPubTransferCreate() throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { + @Test public void testWxPubTransferCreate() throws PingppException { Map params = new HashMap<>(); // 付款使用的商户内部订单号。 wx_pub 规定为 1 ~ 50 位不能重复的数字字母组合; params.put("order_no", "2017" + System.currentTimeMillis()); @@ -210,8 +205,7 @@ public class TransferTest extends PingppTestBase { /** * 创建 Transfer 对象- balance 渠道 */ - @Test public void testBalanceTransferCreate() throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { + @Test public void testBalanceTransferCreate() throws PingppException { Map params = new HashMap<>(); // 付款使用的商户内部订单号。 wx_pub 规定为 1 ~ 50 位不能重复的数字字母组合; params.put("order_no", "2017" + System.currentTimeMillis()); @@ -237,8 +231,7 @@ public class TransferTest extends PingppTestBase { /** * 查询指定 Transfer 对象 */ - @Test public void testTransferRetrieve() throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { + @Test public void testTransferRetrieve() throws PingppException { // 参数: transfer id Transfer obj = Transfer.retrieve("tr_WTarz1Ga9q90X1O4q91Oevv1"); assertEquals("object should be transfer", "transfer", obj.getObject()); @@ -247,8 +240,7 @@ public class TransferTest extends PingppTestBase { /** * 查询 Transfer 对象列表 */ - @Test public void testTransferList() throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { + @Test public void testTransferList() throws PingppException { Map params = new HashMap<>(); params.put("limit", 5); Map app = new HashMap<>(); diff --git a/src/test/java/com/pingplusplus/user/UserPicTest.java b/pingpp-sdk/src/test/java/com/pingplusplus/user/UserPicTest.java similarity index 99% rename from src/test/java/com/pingplusplus/user/UserPicTest.java rename to pingpp-sdk/src/test/java/com/pingplusplus/user/UserPicTest.java index a4f8cc3..38ba083 100644 --- a/src/test/java/com/pingplusplus/user/UserPicTest.java +++ b/pingpp-sdk/src/test/java/com/pingplusplus/user/UserPicTest.java @@ -1,7 +1,7 @@ package com.pingplusplus.user; import com.pingplusplus.PingppTestBase; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.model.UserPic; import org.junit.Test; @@ -18,9 +18,7 @@ public class UserPicTest extends PingppTestBase { /** * 证件上传 */ - @Test public void testUploadPic() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testUploadPic() throws PingppException { Map params = new HashMap(); params.put("user", "test_user_001"); // 用户 ID,首字母必须是英文数字或者 _-@, 必传 params.put("type", "customer"); // 用户类型,customer: 对私,business: 对公 diff --git a/src/test/java/com/pingplusplus/user/UserTest.java b/pingpp-sdk/src/test/java/com/pingplusplus/user/UserTest.java similarity index 83% rename from src/test/java/com/pingplusplus/user/UserTest.java rename to pingpp-sdk/src/test/java/com/pingplusplus/user/UserTest.java index e6f0b19..f27a4f9 100644 --- a/src/test/java/com/pingplusplus/user/UserTest.java +++ b/pingpp-sdk/src/test/java/com/pingplusplus/user/UserTest.java @@ -2,7 +2,7 @@ import com.pingplusplus.PingppTestBase; import com.pingplusplus.PingppTestData; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.model.User; import com.pingplusplus.model.UserCollection; import org.junit.Test; @@ -20,9 +20,7 @@ public class UserTest extends PingppTestBase { /** * 创建用户 (User) */ - @Test public void testUserCreate() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testUserCreate() throws PingppException { Map params = new HashMap(); params.put("id", "test_user_" + System.currentTimeMillis()); // 用户 ID,首字母必须是英文数字或者 _-@, 必传 params.put("address", "Shanghai, China"); // 用户地址, 可选 @@ -53,9 +51,7 @@ public class UserTest extends PingppTestBase { /** * 查询单个用户 (User) */ - @Test public void testUserRetrieve() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testUserRetrieve() throws PingppException { String userId = "test_user_001"; User obj = User.retrieve(userId); //查询 User 方法 @@ -67,9 +63,7 @@ public class UserTest extends PingppTestBase { /** * 查询用户列表 (User) */ - @Test public void testUserList() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testUserList() throws PingppException { Map params = new HashMap<>(); UserCollection objs = User.list(params); //查询 User 列表方法 @@ -79,9 +73,7 @@ public class UserTest extends PingppTestBase { /** * 更新用户信息 (User) */ - @Test public void testUserUpdate() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testUserUpdate() throws PingppException { String userId = "test_user_001"; Map params = new HashMap(); params.put("address", "Shanghai, China"); diff --git a/src/test/java/com/pingplusplus/withdrawal/BatchWithdrawalTest.java b/pingpp-sdk/src/test/java/com/pingplusplus/withdrawal/BatchWithdrawalTest.java similarity index 77% rename from src/test/java/com/pingplusplus/withdrawal/BatchWithdrawalTest.java rename to pingpp-sdk/src/test/java/com/pingplusplus/withdrawal/BatchWithdrawalTest.java index 0d1097a..bcb9b48 100644 --- a/src/test/java/com/pingplusplus/withdrawal/BatchWithdrawalTest.java +++ b/pingpp-sdk/src/test/java/com/pingplusplus/withdrawal/BatchWithdrawalTest.java @@ -2,7 +2,7 @@ import com.pingplusplus.PingppTestBase; import com.pingplusplus.PingppTestData; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.model.BatchWithdrawal; import com.pingplusplus.model.BatchWithdrawalCollection; import org.junit.Test; @@ -18,9 +18,7 @@ public class BatchWithdrawalTest extends PingppTestBase { * 批量提现确认 */ @Test - public void testBatchWithdrawalCreate() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + public void testBatchWithdrawalCreate() throws PingppException { Map params = new HashMap(); ArrayList withdrawals = new ArrayList<>(); // withdrawal id 列表 withdrawals.add("1701708221834035593"); @@ -43,9 +41,7 @@ public void testBatchWithdrawalCreate() throws RateLimitException, /** * 查询批量提现对象 */ - @Test public void testBatchWithdrawalRetrieve() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testBatchWithdrawalRetrieve() throws PingppException { // 查询 BatchWithdrawal 对象方法 // 参数: batch_withdrawal id @@ -57,9 +53,7 @@ public void testBatchWithdrawalCreate() throws RateLimitException, /** * 查询批量提现列表 */ - @Test public void testBatchWithdrawalList() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testBatchWithdrawalList() throws PingppException { Map params = new HashMap(); params.put("per_page", 3); diff --git a/src/test/java/com/pingplusplus/withdrawal/WithdrawalTest.java b/pingpp-sdk/src/test/java/com/pingplusplus/withdrawal/WithdrawalTest.java similarity index 83% rename from src/test/java/com/pingplusplus/withdrawal/WithdrawalTest.java rename to pingpp-sdk/src/test/java/com/pingplusplus/withdrawal/WithdrawalTest.java index b03959d..87b807b 100644 --- a/src/test/java/com/pingplusplus/withdrawal/WithdrawalTest.java +++ b/pingpp-sdk/src/test/java/com/pingplusplus/withdrawal/WithdrawalTest.java @@ -1,7 +1,7 @@ package com.pingplusplus.withdrawal; import com.pingplusplus.PingppTestBase; -import com.pingplusplus.exception.*; +import com.pingplusplus.exception.PingppException; import com.pingplusplus.model.Withdrawal; import com.pingplusplus.model.WithdrawalCollection; import org.junit.Test; @@ -16,9 +16,7 @@ public class WithdrawalTest extends PingppTestBase { * 创建提现申请 */ @Test - public void testCreateWithdrawal() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + public void testCreateWithdrawal() throws PingppException { Map params = new HashMap(); params.put("user", "test_user_001"); // 用户 ID, 必传 params.put("channel", "alipay"); // 提现使用渠道。银联:unionpay,支付宝:alipay,微信:wx_pub,通联:allinpay,京东:jdpay, 可选 @@ -52,9 +50,7 @@ public void testCreateWithdrawal() throws RateLimitException, /** * 获取提现对象 */ - @Test public void testWithdrawalRetrieve() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testWithdrawalRetrieve() throws PingppException { // 查询 Withdrawal // 参数: withdrawal id @@ -66,9 +62,7 @@ public void testCreateWithdrawal() throws RateLimitException, /** * 获取提现列表 */ - @Test public void testGetWithdrawalList() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testGetWithdrawalList() throws PingppException { Map params = new HashMap(); params.put("page", 1); params.put("per_page", 3); @@ -83,9 +77,7 @@ public void testCreateWithdrawal() throws RateLimitException, /** * 更新提现对象 */ - @Test public void testWithdrawalUpdate() throws RateLimitException, - APIException, ChannelException, InvalidRequestException, - APIConnectionException, AuthenticationException { + @Test public void testWithdrawalUpdate() throws PingppException { Map params = new HashMap(); params.put("status", "canceled"); // 更新状态。确认:pending,取消:canceled。 // 更新 Withdrawal 方法 diff --git a/pom.xml b/pom.xml index 69c82b3..71f3e19 100644 --- a/pom.xml +++ b/pom.xml @@ -1,23 +1,63 @@ - + 4.0.0 - Pingplusplus - pingpp-java - 2.3.14 + + com.pingxx + pingpp-java-parent + 2.5.6 + pom + + A Java SDK for Ping++ Payment API. + + + Apache License 2.0 + http://www.apache.org/licenses/LICENSE-2.0 + repo + + + + + scm:git:https://github.com/PingPlusPlus/pingpp-java.git + + + + afon + Afon + xufeng.weng@pingxx.com + + + + + pingpp-sdk + + + + 1.8 + 1.8 + + - - com.google.code.gson - gson - 2.8.5 - runtime - - - commons-codec - commons-codec - 1.10 - runtime - - pingpp-java - https://github.com/PingPlusPlus/pingpp-java + + + + + org.apache.maven.plugins + maven-install-plugin + 2.5.2 + + true + + + + org.apache.maven.plugins + maven-deploy-plugin + 2.7 + + true + + + + diff --git a/settings.gradle b/settings.gradle index 05ca5f7..c5a152d 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,2 +1,3 @@ rootProject.name = 'pingpp-java' +include 'pingpp-sdk' include 'example' diff --git a/src/main/java/com/pingplusplus/Pingpp.java b/src/main/java/com/pingplusplus/Pingpp.java deleted file mode 100644 index b8fddad..0000000 --- a/src/main/java/com/pingplusplus/Pingpp.java +++ /dev/null @@ -1,80 +0,0 @@ -package com.pingplusplus; - -/** - * Ping++ Base class - */ -public abstract class Pingpp { - /** - * Ping++ API BASE URL - */ - public static final String LIVE_API_BASE = "https://api.pingxx.com"; - /** - * version - */ - public static final String VERSION = "2.3.14"; - /** - * api key - */ - public static volatile String apiKey; - - public static volatile String apiVersion; - - public static volatile String appId; - - public static String AcceptLanguage = "zh-CN"; - - private static volatile boolean verifySSL = true; - private static volatile String apiBase = LIVE_API_BASE; - - public static volatile String privateKey; - public static volatile String privateKeyPath; - - public static Boolean DEBUG = false; - - /** - * (FOR TESTING ONLY) - * If you'd like your API requests to hit your own (mocked) server, - * you can set this up here by overriding the base api URL. - * @param overriddenApiBase API 地址 - */ - public static void overrideApiBase(final String overriddenApiBase) { - apiBase = overriddenApiBase; - } - - /** - * (FOR TESTING ONLY) - * Only disable SSL verification if you're using your own (mocked) server. - * Disabling verification on pingxx.com is not supported - * @param verify 是否验证 ssl - */ - public static void setVerifySSL(boolean verify) { - verifySSL = verify; - } - - /** - * get SSL state - * - * @return true is set SSL ,false is not set SSL - */ - public static boolean getVerifySSL() { - return verifySSL; - } - - /** - * get api url - * - * @return String api url - */ - public static String getApiBase() { - return apiBase; - } - - /** - * set api url - * - * @param apiBase apiBase API 地址 - */ - public static void setApiBase(String apiBase) { - Pingpp.apiBase = apiBase; - } -} diff --git a/src/main/java/com/pingplusplus/exception/APIConnectionException.java b/src/main/java/com/pingplusplus/exception/APIConnectionException.java deleted file mode 100644 index 38af6c7..0000000 --- a/src/main/java/com/pingplusplus/exception/APIConnectionException.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.pingplusplus.exception; - -public class APIConnectionException extends PingppException { - - private static final long serialVersionUID = 1L; - - public APIConnectionException(String message) { - super(message); - } - - public APIConnectionException(String message, Throwable e) { - super(message, e); - } - -} diff --git a/src/main/java/com/pingplusplus/exception/APIException.java b/src/main/java/com/pingplusplus/exception/APIException.java deleted file mode 100644 index 50b9db0..0000000 --- a/src/main/java/com/pingplusplus/exception/APIException.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.pingplusplus.exception; - -public class APIException extends PingppException { - - private static final long serialVersionUID = 1L; - - public APIException(String message, Throwable e) { - super(message, e); - } - -} diff --git a/src/main/java/com/pingplusplus/exception/AuthenticationException.java b/src/main/java/com/pingplusplus/exception/AuthenticationException.java deleted file mode 100644 index 40b9c9c..0000000 --- a/src/main/java/com/pingplusplus/exception/AuthenticationException.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.pingplusplus.exception; - -public class AuthenticationException extends PingppException { - - - public AuthenticationException(String message) { - super(message); - } - - private static final long serialVersionUID = 1L; - -} diff --git a/src/main/java/com/pingplusplus/exception/ChannelException.java b/src/main/java/com/pingplusplus/exception/ChannelException.java deleted file mode 100644 index 8f42eb4..0000000 --- a/src/main/java/com/pingplusplus/exception/ChannelException.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.pingplusplus.exception; - -public class ChannelException extends PingppException { - - private static final long serialVersionUID = 1L; - - private final String param; - - public ChannelException(String message, String param, Throwable e) { - super(message, e); - this.param = param; - } - - public String getParam() { - return param; - } - -} diff --git a/src/main/java/com/pingplusplus/exception/InvalidRequestException.java b/src/main/java/com/pingplusplus/exception/InvalidRequestException.java deleted file mode 100644 index f0464e2..0000000 --- a/src/main/java/com/pingplusplus/exception/InvalidRequestException.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.pingplusplus.exception; - -public class InvalidRequestException extends PingppException { - - private static final long serialVersionUID = 1L; - - private final String param; - - public InvalidRequestException(String message, String param, Throwable e) { - super(message, e); - this.param = param; - } - - public String getParam() { - return param; - } - -} diff --git a/src/main/java/com/pingplusplus/exception/PingppException.java b/src/main/java/com/pingplusplus/exception/PingppException.java deleted file mode 100644 index 48865be..0000000 --- a/src/main/java/com/pingplusplus/exception/PingppException.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.pingplusplus.exception; - -public abstract class PingppException extends Exception { - - public PingppException(String message) { - super(message, null); - } - - public PingppException(String message, Throwable e) { - super(message, e); - } - - private static final long serialVersionUID = 1L; - -} diff --git a/src/main/java/com/pingplusplus/exception/RateLimitException.java b/src/main/java/com/pingplusplus/exception/RateLimitException.java deleted file mode 100644 index 3ce1429..0000000 --- a/src/main/java/com/pingplusplus/exception/RateLimitException.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.pingplusplus.exception; - -public class RateLimitException extends PingppException { - - private static final long serialVersionUID = 1L; - - public RateLimitException(String message, Throwable e) { - super(message, e); - } - -} diff --git a/src/main/java/com/pingplusplus/model/Channel.java b/src/main/java/com/pingplusplus/model/Channel.java deleted file mode 100644 index 96e187c..0000000 --- a/src/main/java/com/pingplusplus/model/Channel.java +++ /dev/null @@ -1,150 +0,0 @@ -package com.pingplusplus.model; - -import com.pingplusplus.exception.*; -import com.pingplusplus.net.APIResource; -import com.pingplusplus.net.SubAppBasedResource; - -import java.util.Map; - -public class Channel extends SubAppBasedResource { - String object; - Long created; - String channel; - Boolean banned; - String bannedMsg; - String description; - Map params; - - public String getObject() { - return object; - } - - public void setObject(String object) { - this.object = object; - } - - public Long getCreated() { - return created; - } - - public void setCreated(Long created) { - this.created = created; - } - - public String getChannel() { - return channel; - } - - public void setChannel(String channel) { - this.channel = channel; - } - - public Boolean getBanned() { - return banned; - } - - public void setBanned(Boolean banned) { - this.banned = banned; - } - - public String getBannedMsg() { - return bannedMsg; - } - - public void setBannedMsg(String bannedMsg) { - this.bannedMsg = bannedMsg; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public Map getParams() { - return params; - } - - public void setParams(Map params) { - this.params = params; - } - - /** - * 创建渠道参数 - * - * @param subAppId - * @param params - * @return Channel - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException - */ - public static Channel create(String subAppId, Mapparams) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.POST, classURL(Channel.class, subAppId), params, Channel.class); - } - - /** - * 查询渠道参数 - * - * @param subAppId - * @param channel - * @return Channel - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException - */ - public static Channel retrieve(String subAppId, String channel) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.GET, instanceURL(Channel.class, subAppId, channel), null, Channel.class); - } - - /** - * 更新渠道参数 - * - * @param subAppId - * @param channel - * @param params - * @return Channel - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException - */ - public static Channel update(String subAppId, String channel, Mapparams) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.PUT, instanceURL(Channel.class, subAppId, channel), params, Channel.class); - } - - /** - * 删除渠道参数 - * - * @param subAppId - * @param channel - * @return DeletedChannel - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException - */ - public static DeletedChannel delete(String subAppId, String channel) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.DELETE, instanceURL(Channel.class, subAppId, channel), null, DeletedChannel.class); - } -} diff --git a/src/main/java/com/pingplusplus/model/ChargeRefundCollection.java b/src/main/java/com/pingplusplus/model/ChargeRefundCollection.java deleted file mode 100644 index c73046f..0000000 --- a/src/main/java/com/pingplusplus/model/ChargeRefundCollection.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.pingplusplus.model; - -import com.pingplusplus.Pingpp; -import com.pingplusplus.exception.*; - -import java.util.Map; - -public class ChargeRefundCollection extends PingppCollectionAPIResource { - - public ChargeRefundCollection list(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - - return list(null, params); - } - - public ChargeRefundCollection list(String apiKey, Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - - String url = String.format("%s%s", Pingpp.getApiBase(), this.getURL()); - return request(RequestMethod.GET, url, apiKey, params, ChargeRefundCollection.class); - } - - public Refund retrieve(String id) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - - return retrieve(id, null); - } - - public Refund retrieve(String id, String apiKey) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - - String url = String.format("%s%s/%s", Pingpp.getApiBase(), this.getURL(), id); - return request(RequestMethod.GET, url, apiKey, null, Refund.class); - } - - public Refund create(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - - return create(null, params); - } - - public Refund create(String apiKey, Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - - String url = String.format("%s%s", Pingpp.getApiBase(), this.getURL()); - return request(RequestMethod.POST, url, apiKey, params, Refund.class); - } - -} diff --git a/src/main/java/com/pingplusplus/model/Contact.java b/src/main/java/com/pingplusplus/model/Contact.java deleted file mode 100644 index bdde423..0000000 --- a/src/main/java/com/pingplusplus/model/Contact.java +++ /dev/null @@ -1,82 +0,0 @@ -package com.pingplusplus.model; - -import com.pingplusplus.exception.*; -import com.pingplusplus.net.AppBasedResource; - -import java.util.Map; - -public class Contact extends AppBasedResource { - String user; - Boolean livemode; - String accNo; - String contactNo; - - public String getUser() { - return user; - } - - public void setUser(String user) { - this.user = user; - } - - public String getAccNo() { - return accNo; - } - - public void setAccNo(String accNo) { - this.accNo = accNo; - } - - public Boolean getLivemode() { - return livemode; - } - - public void setLivemode(Boolean livemode) { - this.livemode = livemode; - } - - public String getContactNo() { - return contactNo; - } - - public void setContactNo(String contactNo) { - this.contactNo = contactNo; - } - - /** - * 新增联系人 - * - * @param params 请求参数 - * @return Contact - * @throws AuthenticationException 认证异常 - * @throws InvalidRequestException 错误请求 - * @throws APIConnectionException 连接异常 - * @throws APIException 系统异常 - * @throws ChannelException 渠道异常 - * @throws RateLimitException 请求超限 - */ - public static Contact create(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return create(params, null); - } - - /** - * 新增联系人 - * - * @param params 请求参数 - * @param apiKey API key - * @return Contact - * @throws AuthenticationException 认证异常 - * @throws InvalidRequestException 错误请求 - * @throws APIConnectionException 连接异常 - * @throws APIException 系统异常 - * @throws ChannelException 渠道异常 - * @throws RateLimitException 请求超限 - */ - public static Contact create(Map params, String apiKey) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(RequestMethod.POST, singleClassURL(Contact.class), apiKey, params, Contact.class); - } -} diff --git a/src/main/java/com/pingplusplus/model/OrderRefund.java b/src/main/java/com/pingplusplus/model/OrderRefund.java deleted file mode 100644 index b551e94..0000000 --- a/src/main/java/com/pingplusplus/model/OrderRefund.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.pingplusplus.model; - -import com.pingplusplus.exception.*; -import com.pingplusplus.net.APIResource; - -import java.util.Map; - -public class OrderRefund extends APIResource { - - /** - * 创建 order_refund - * - * @param order - * @param params - * @return OrderRefundCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException - */ - public static OrderRefundCollection create(String order, Map params) - throws AuthenticationException, InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return request(RequestMethod.POST, String.format("%s/order_refunds", instanceURL(Order.class, order)), - params, OrderRefundCollection.class); - } - - /** - * 查询 order_refund - * - * @param order - * @param refund - * @return Refund - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException - */ - public static Refund retrieve(String order, String refund) - throws AuthenticationException, InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return request(RequestMethod.GET, String.format("%s/order_refunds/%s", instanceURL(Order.class, order), refund), - null, Refund.class); - } - - /** - * 查询 order_refund 列表 - * - * @param order - * @param params - * @return OrderRefundCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException - */ - public static OrderRefundCollection list(String order, Mapparams) - throws AuthenticationException, InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return request(RequestMethod.GET, String.format("%s/order_refunds", instanceURL(Order.class, order)), - params, OrderRefundCollection.class); - } - - /** - * 查询 order_refund 列表 - * - * @param order - * @return OrderRefundCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException - */ - public static OrderRefundCollection list(String order) - throws AuthenticationException, InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return list(order, null); - } -} diff --git a/src/main/java/com/pingplusplus/model/RechargeRefund.java b/src/main/java/com/pingplusplus/model/RechargeRefund.java deleted file mode 100644 index 74c1f3a..0000000 --- a/src/main/java/com/pingplusplus/model/RechargeRefund.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.pingplusplus.model; - -import com.pingplusplus.exception.*; -import com.pingplusplus.net.APIResource; -import com.pingplusplus.net.AppBasedResource; - -import java.util.Map; - -public class RechargeRefund extends AppBasedResource { - /** - * 创建 recharge_refund - * - * @param rechargeId - * @param params - * @return Refund - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException - */ - public static Refund create(String rechargeId, Map params) - throws AuthenticationException, InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.POST, String.format("%s/refunds", instanceURL(Recharge.class, rechargeId)), - params, Refund.class); - } - - /** - * 查询 recharge_refund - * - * @param rechargeId - * @param refundId - * @return Refund - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException - */ - public static Refund retrieve(String rechargeId, String refundId) - throws AuthenticationException, InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.GET, String.format("%s/refunds/%s", instanceURL(Recharge.class, rechargeId), refundId), - null, Refund.class); - } - - /** - * 查询 recharge_refund 列表 - * - * @param rechargeId - * @param params - * @return RechargeRefundCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException - */ - public static RechargeRefundCollection list(String rechargeId, Mapparams) - throws AuthenticationException, InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.GET, String.format("%s/refunds", instanceURL(Recharge.class, rechargeId)), - params, RechargeRefundCollection.class); - } - - /** - * 查询 recharge_refund 列表 - * - * @param rechargeId - * @return RechargeRefundCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException - */ - public static RechargeRefundCollection list(String rechargeId) - throws AuthenticationException, InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return list(rechargeId, null); - } -} diff --git a/src/main/java/com/pingplusplus/model/RoyaltyTemplate.java b/src/main/java/com/pingplusplus/model/RoyaltyTemplate.java deleted file mode 100644 index 6d216ca..0000000 --- a/src/main/java/com/pingplusplus/model/RoyaltyTemplate.java +++ /dev/null @@ -1,171 +0,0 @@ -package com.pingplusplus.model; - -import com.pingplusplus.exception.*; -import com.pingplusplus.net.APIResource; - -import java.util.Map; - -public class RoyaltyTemplate extends APIResource { - String id; - String object; - Boolean livemode; - String app; - String name; - Long created; - String description; - Map rule; - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getApp() { - return app; - } - - public void setApp(String app) { - this.app = app; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public Long getCreated() { - return created; - } - - public void setCreated(Long created) { - this.created = created; - } - - public Map getRule() { - return rule; - } - - public void setRule(Map rule) { - this.rule = rule; - } - - public String getObject() { - return object; - } - - public void setObject(String object) { - this.object = object; - } - - public Boolean getLivemode() { - return livemode; - } - - public void setLivemode(Boolean livemode) { - this.livemode = livemode; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - /** - * 创建 royalty_template - * - * @param params - * @return RoyaltyTemplate - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException - */ - public static RoyaltyTemplate create(Map params) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.POST, classURL(RoyaltyTemplate.class), params, RoyaltyTemplate.class); - } - - /** - * 查询 royalty_template - * - * @param id - * @return RoyaltyTemplate - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException - */ - public static RoyaltyTemplate retrieve(String id) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.GET, instanceURL(RoyaltyTemplate.class, id), null, RoyaltyTemplate.class); - } - - /** - * 查询 royalty_template 列表 - * - * @param params - * @return RoyaltyTemplateCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException - */ - public static RoyaltyTemplateCollection list(Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.GET, classURL(RoyaltyTemplate.class), params, RoyaltyTemplateCollection.class); - } - - /** - * 删除 royalty_template - * - * @param id - * @return DeleteRoyaltyTemplate - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException - */ - public static DeleteRoyaltyTemplate delete(String id) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.DELETE, instanceURL(RoyaltyTemplate.class, id), null, DeleteRoyaltyTemplate.class); - } - - /** - * 更新 royalty_template - * - * @param id - * @return RoyaltyTemplate - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException - */ - public static RoyaltyTemplate update(String id, Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - return request(APIResource.RequestMethod.PUT, instanceURL(RoyaltyTemplate.class, id), params, RoyaltyTemplate.class); - } -} diff --git a/src/main/java/com/pingplusplus/model/SettleAccount.java b/src/main/java/com/pingplusplus/model/SettleAccount.java deleted file mode 100644 index d60a571..0000000 --- a/src/main/java/com/pingplusplus/model/SettleAccount.java +++ /dev/null @@ -1,207 +0,0 @@ -package com.pingplusplus.model; - -import com.pingplusplus.exception.*; -import com.pingplusplus.net.APIResource; -import com.pingplusplus.net.UserBasedResource; - -import java.util.Map; - -public class SettleAccount extends UserBasedResource { - String id; - String object; - Boolean livemode; - String channel; - Long created; - SettleAccountRecipient recipient; - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getObject() { - return object; - } - - public void setObject(String object) { - this.object = object; - } - - public String getChannel() { - return channel; - } - - public void setChannel(String channel) { - this.channel = channel; - } - - public Long getCreated() { - return created; - } - - public void setCreated(Long created) { - this.created = created; - } - - public SettleAccountRecipient getRecipient() { - return recipient; - } - - public void setRecipient(SettleAccountRecipient recipient) { - this.recipient = recipient; - } - - public Boolean getLivemode() { - return livemode; - } - - public void setLivemode(Boolean livemode) { - this.livemode = livemode; - } - - /** - * 创建 settle_account - * - * @param userId - * @param params - * @return SettleAccount - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException - */ - public static SettleAccount create(String userId, Mapparams) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - User.checkUserId(userId); - return request(APIResource.RequestMethod.POST, classURL(SettleAccount.class, userId), params, SettleAccount.class); - } - - /** - * 查询 settle_account - * - * @param userId - * @param id - * @return SettleAccount - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException - */ - public static SettleAccount retrieve(String userId, String id) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - User.checkUserId(userId); - return request(APIResource.RequestMethod.GET, instanceURL(SettleAccount.class, userId, id), null, SettleAccount.class); - } - - /** - * 查询 settle_account 列表 - * - * @param userId - * @param params - * @return SettleAccountCollection - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException - */ - public static SettleAccountCollection list(String userId, Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - User.checkUserId(userId); - return request(APIResource.RequestMethod.GET, classURL(SettleAccount.class, userId), params, SettleAccountCollection.class); - } - - /** - * 删除 settle_account - * - * @param userId - * @param id - * @return DeletedSettleAccount - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException - */ - public static DeletedSettleAccount delete(String userId, String id) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - User.checkUserId(userId); - return request(APIResource.RequestMethod.DELETE, instanceURL(SettleAccount.class, userId, id), null, DeletedSettleAccount.class); - } - - /** - * 更新 settle_account - * - * @param userId 用户 ID - * @param id 结算账户 ID - * @param params 更新数据 - * @return SettleAccount - * @throws AuthenticationException 认证异常 - * @throws InvalidRequestException 错误请求 - * @throws APIConnectionException 连接异常 - * @throws APIException 系统异常 - * @throws ChannelException 渠道异常 - * @throws RateLimitException 请求超限 - */ - public static SettleAccount update(String userId, String id, Map params) - throws AuthenticationException, InvalidRequestException, - APIConnectionException, APIException, ChannelException, RateLimitException { - User.checkUserId(userId); - return request(APIResource.RequestMethod.PUT, instanceURL(SettleAccount.class, userId, id), params, SettleAccount.class); - } - - /** - * 更新银行手机号 - * - * @param userId 用户 ID - * @param id 结算账号 ID - * @param params 更新参数 - * @return SettleAccount - * @throws AuthenticationException 认证异常 - * @throws InvalidRequestException 错误请求 - * @throws APIConnectionException 连接异常 - * @throws APIException 系统异常 - * @throws ChannelException 渠道异常 - * @throws RateLimitException 请求超限 - */ - public static SettleAccount updateMobile(String userId, String id, Map params) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return request(RequestMethod.PUT, String.format("%s/mobile", instanceURL(SettleAccount.class, userId, id)), - params, SettleAccount.class); - } - - /** - * 打款验证 - * - * @param userId 用户 ID - * @param id 结算账号 ID - * @param params 参数 - * @return SettleAccount - * @throws AuthenticationException 认证异常 - * @throws InvalidRequestException 错误请求 - * @throws APIConnectionException 连接异常 - * @throws APIException 系统异常 - * @throws ChannelException 渠道异常 - * @throws RateLimitException 请求超限 - */ - public static SettleAccount verify(String userId, String id, Map params) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return request(RequestMethod.POST, String.format("%s/verify", instanceURL(SettleAccount.class, userId, id)), - params, SettleAccount.class); - } -} diff --git a/src/main/java/com/pingplusplus/net/APIResource.java b/src/main/java/com/pingplusplus/net/APIResource.java deleted file mode 100644 index 796e0c7..0000000 --- a/src/main/java/com/pingplusplus/net/APIResource.java +++ /dev/null @@ -1,778 +0,0 @@ -package com.pingplusplus.net; - -import com.google.gson.FieldNamingPolicy; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.google.gson.JsonSyntaxException; -import com.pingplusplus.Pingpp; -import com.pingplusplus.exception.*; -import com.pingplusplus.exception.InvalidRequestException; -import com.pingplusplus.model.*; -import com.pingplusplus.serializer.*; -import com.pingplusplus.util.PingppSignature; - -import java.io.*; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.net.ConnectException; -import java.net.HttpURLConnection; -import java.net.URL; -import java.net.URLEncoder; -import java.util.*; - -import javax.net.ssl.HttpsURLConnection; - -/** - * extends the abstract class when you need requset anything from ping++ - */ -public abstract class APIResource extends PingppObject { - /** - * URLEncoder charset - */ - public static final String CHARSET = "UTF-8"; - - private static final String REQUEST_TIME_KEY = "Pingplusplus-Request-Timestamp"; - - public static int CONNECT_TIMEOUT = 30; - public static int READ_TIMEOUT = 80; - public static int RETRY_MAX = 1; - - /** - * Http requset method - */ - protected enum RequestMethod { - GET, POST, DELETE, PUT - } - - /** - * Gson object use to transform json string to resource object - */ - public static final Gson GSON = new GsonBuilder() - .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) - .registerTypeAdapter(Charge.class, new ChargeDeserializer()) - .registerTypeAdapter(RedEnvelope.class, new RedEnvelopeDeserializer()) - .registerTypeAdapter(Transfer.class, new TransferDeserializer()) - .registerTypeAdapter(ChargeRefundCollection.class, new ChargeRefundCollectionDeserializer()) - .registerTypeAdapter(EventData.class, new EventDataDeserializer()) - .registerTypeAdapter(PingppRawJsonObject.class, new PingppRawJsonObjectDeserializer()) - .registerTypeAdapter(SubApp.class, new SubAppDeserializer()) - .create(); - - public static Gson getGson() { - try { - Class klass = Class.forName("com.pingplusplus.net.AppBasedResource"); - Field field = klass.getField("GSON"); - return (Gson) field.get(klass); - } catch (ClassNotFoundException e) { - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (NoSuchFieldException e) { - e.printStackTrace(); - } - - return GSON; - } - - public static Class getSelfClass() { - return APIResource.class; - } - - /** - * @param clazz - * @return className - */ - protected static String className(Class clazz) { - String className = clazz.getSimpleName().toLowerCase().replace("$", " "); - - switch (className) { - case "redenvelope": - return "red_envelope"; - case "batchrefund": - return "batch_refund"; - case "batchtransfer": - return "batch_transfer"; - case "customs": - return "custom"; - case "cardinfo": - return "card_info"; - case "assettransaction": - return "asset_transaction"; - case "balancebonus": - return "balance_bonuse"; - case "balancetransfer": - return "balance_transfer"; - case "balancetransaction": - return "balance_transaction"; - case "coupontemplate": - return "coupon_template"; - case "batchwithdrawal": - return "batch_withdrawal"; - case "transactionstatistics": - return "transaction_statistics"; - case "settleaccount": - return "settle_account"; - case "subapp": - return "sub_app"; - case "royalty": - return "royaltie"; - case "royaltysettlement": - return "royalty_settlement"; - case "royaltytransaction": - return "royalty_transaction"; - case "royaltytemplate": - return "royalty_template"; - case "balancesettlement": - return "balance_settlement"; - case "subbank": - return "sub_bank"; - case "splitreceiver": - return "split_receiver"; - case "splitprofit": - return "split_profit"; - case "profittransaction": - return "profit_transaction"; - case "userpic": - return "users/upload_pic"; - case "contact": - return "sub_apps/contact"; - default: - return className; - } - } - - /** - * @param clazz - * @return singleClassURL - */ - protected static String singleClassURL(Class clazz) throws InvalidRequestException { - String className = null; - Class klass = getSelfClass(); - if (!klass.getSimpleName().equalsIgnoreCase("APIResource")) { - try { - Method method = klass.getMethod("className", Class.class); - className = (String)method.invoke(klass, clazz); - } catch (NoSuchMethodException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } - } - - if (className == null) { - className = className(clazz); - } - - return String.format("%s/v1/%s", Pingpp.getApiBase(), className); - } - - /** - * @param clazz - * @return classURL - */ - protected static String classURL(Class clazz) throws InvalidRequestException { - return String.format("%ss", singleClassURL(clazz)); - } - - /** - * @param clazz - * @param id - * @return instanceURL - * @throws InvalidRequestException - */ - protected static String instanceURL(Class clazz, String id) throws InvalidRequestException { - try { - return String.format("%s/%s", classURL(clazz), urlEncode(id)); - } catch (UnsupportedEncodingException e) { - throw new InvalidRequestException("Unable to encode parameters to " + CHARSET, null, e); - } - } - - protected static String apiBasePrefixedURL(String url) { - return String.format("%s%s", Pingpp.getApiBase(), url); - } - - /** - * @param str - * @return urlEncodedString - * @throws UnsupportedEncodingException - */ - protected static String urlEncode(String str) throws UnsupportedEncodingException { - if (str == null) { - return null; - } else { - return URLEncoder.encode(str, CHARSET); - } - } - - /** - * @param k - * @param v - * @return urlEncodedString - * @throws UnsupportedEncodingException - */ - private static String urlEncodePair(String k, String v) - throws UnsupportedEncodingException { - return String.format("%s=%s", urlEncode(k), urlEncode(v)); - } - - /** - * @param apiKey - * @return headers - */ - static Map getHeaders(String apiKey) { - Map headers = new HashMap(); - headers.put("Accept-Charset", CHARSET); - headers.put("User-Agent", - String.format("Pingpp/v1 JavaBindings/%s", Pingpp.VERSION)); - - if (apiKey == null) { - apiKey = Pingpp.apiKey; - } - - headers.put("Authorization", String.format("Bearer %s", apiKey)); - headers.put("Accept-Language", Pingpp.AcceptLanguage); - - // debug headers - String[] propertyNames = {"os.name", "os.version", "os.arch", - "java.version", "java.vendor", "java.vm.version", - "java.vm.vendor"}; - Map propertyMap = new HashMap(); - for (String propertyName : propertyNames) { - propertyMap.put(propertyName, System.getProperty(propertyName)); - } - propertyMap.put("bindings.version", Pingpp.VERSION); - propertyMap.put("lang", "Java"); - propertyMap.put("publisher", "Pingpp"); - headers.put("X-Pingpp-Client-User-Agent", getGson().toJson(propertyMap)); - if (Pingpp.apiVersion != null) { - headers.put("Pingplusplus-Version", Pingpp.apiVersion); - } - return headers; - } - - /** - * @param url - * @param apiKey - * @return HttpURLConnection - * @throws IOException - */ - private static java.net.HttpURLConnection createPingppConnection( - String url, String apiKey) throws IOException { - URL pingppURL = new URL(url); - HttpURLConnection conn; - if (pingppURL.getProtocol().equals("https")) { - conn = (HttpsURLConnection) pingppURL.openConnection(); - } else { - conn = (HttpURLConnection) pingppURL.openConnection(); - } - - conn.setConnectTimeout(CONNECT_TIMEOUT * 1000); - conn.setReadTimeout(READ_TIMEOUT * 1000); - conn.setUseCaches(false); - for (Map.Entry header : getHeaders(apiKey).entrySet()) { - conn.setRequestProperty(header.getKey(), header.getValue()); - } - - return conn; - } - - /** - * @throws APIConnectionException - */ - private static void throwInvalidCertificateException() throws APIConnectionException { - throw new APIConnectionException("Invalid server certificate. You tried to connect to a server that has a revoked SSL certificate, which means we cannot securely send data to that server. "); - } - - /** - * @param url - * @param query - * @return formatedURL - */ - private static String formatURL(String url, String query) { - if (query == null || query.isEmpty()) { - return url; - } else { - // In some cases, URL can already contain a question mark (eg, upcoming invoice lines) - String separator = url.contains("?") ? "&" : "?"; - return String.format("%s%s%s", url, separator, query); - } - } - - /** - * @param url - * @param query - * @param apiKey - * @return HttpURLConnection - * @throws IOException - * @throws APIConnectionException - */ - private static java.net.HttpURLConnection createGetConnection( - String url, String query, String apiKey) throws IOException, APIConnectionException { - String getURL = formatURL(url, query); - - java.net.HttpURLConnection conn = createPingppConnection(getURL, - apiKey); - conn.setRequestMethod("GET"); - - String requestTime = currentTimeString(); - String stringToBeSigned = getRequestURIFromURL(conn.getURL()) + requestTime; - conn.setRequestProperty(REQUEST_TIME_KEY, requestTime); - String signature = generateSign(stringToBeSigned); - if (signature != null) { - conn.setRequestProperty("Pingplusplus-Signature", signature); - } - - return conn; - } - - private static java.net.HttpURLConnection createDeleteConnection( - String url, String query, String apiKey) throws IOException, APIConnectionException { - String getURL = formatURL(url, query); - java.net.HttpURLConnection conn = createPingppConnection(getURL, - apiKey); - conn.setRequestMethod("DELETE"); - - String requestTime = currentTimeString(); - String stringToBeSigned = getRequestURIFromURL(conn.getURL()) + requestTime; - conn.setRequestProperty(REQUEST_TIME_KEY, requestTime); - String signature = generateSign(stringToBeSigned); - if (signature != null) { - conn.setRequestProperty("Pingplusplus-Signature", signature); - } - - return conn; - } - - /** - * @param url - * @param query - * @param apiKey - * @return HttpURLConnection - * @throws IOException - * @throws APIConnectionException - */ - private static java.net.HttpURLConnection createPostConnection( - String url, String query, String apiKey) throws IOException, APIConnectionException { - java.net.HttpURLConnection conn = createPingppConnection(url, - apiKey); - - conn.setDoOutput(true); - conn.setRequestMethod("POST"); - conn.setRequestProperty("Content-Type", String.format( - "application/json;charset=%s", CHARSET)); - - String stringToBeSigned = query; - stringToBeSigned += getRequestURIFromURL(conn.getURL()); - String requestTime = currentTimeString(); - stringToBeSigned += requestTime; - - String signature = generateSign(stringToBeSigned); - if (signature != null) { - conn.setRequestProperty("Pingplusplus-Signature", signature); - } - conn.setRequestProperty(REQUEST_TIME_KEY, requestTime); - - OutputStream output = null; - try { - output = conn.getOutputStream(); - output.write(query.getBytes(CHARSET)); - } finally { - if (output != null) { - output.close(); - } - } - return conn; - } - - /** - * @param url - * @param query - * @param apiKey - * @return HttpURLConnection - * @throws IOException - * @throws APIConnectionException - */ - private static java.net.HttpURLConnection createPutConnection( - String url, String query, String apiKey) throws IOException, APIConnectionException { - java.net.HttpURLConnection conn = createPingppConnection(url, - apiKey); - - conn.setDoOutput(true); - conn.setRequestMethod("PUT"); - conn.setRequestProperty("Content-Type", String.format( - "application/json;charset=%s", CHARSET)); - - String stringToBeSigned = query; - stringToBeSigned += getRequestURIFromURL(conn.getURL()); - String requestTime = currentTimeString(); - stringToBeSigned += requestTime; - - String signature = generateSign(stringToBeSigned); - if (signature != null) { - conn.setRequestProperty("Pingplusplus-Signature", signature); - } - conn.setRequestProperty(REQUEST_TIME_KEY, requestTime); - - OutputStream output = null; - try { - output = conn.getOutputStream(); - output.write(query.getBytes(CHARSET)); - } finally { - if (output != null) { - output.close(); - } - } - return conn; - } - - /** - * @param params - * @return queryString - * @throws UnsupportedEncodingException - * @throws InvalidRequestException - */ - private static String createQuery(Map params) - throws UnsupportedEncodingException, InvalidRequestException { - Map flatParams = flattenParams(params); - StringBuilder queryStringBuffer = new StringBuilder(); - for (Map.Entry entry : flatParams.entrySet()) { - if (queryStringBuffer.length() > 0) { - queryStringBuffer.append("&"); - } - queryStringBuffer.append(urlEncodePair(entry.getKey(), - entry.getValue())); - } - return queryStringBuffer.toString(); - } - - /** - * @param params - * @return JSONString - */ - private static String createJSONString(Map params) { - Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create(); - return gson.toJson(params); - } - - /** - * @param params - * @return flattenParams - * @throws InvalidRequestException - */ - private static Map flattenParams(Map params) - throws InvalidRequestException { - if (params == null) { - return new HashMap(); - } - Map flatParams = new HashMap(); - for (Map.Entry entry : params.entrySet()) { - String key = entry.getKey(); - Object value = entry.getValue(); - if (value instanceof Map) { - Map flatNestedMap = new HashMap(); - Map nestedMap = (Map) value; - for (Map.Entry nestedEntry : nestedMap.entrySet()) { - flatNestedMap.put( - String.format("%s[%s]", key, nestedEntry.getKey()), - nestedEntry.getValue()); - } - flatParams.putAll(flattenParams(flatNestedMap)); - } else if (value instanceof ArrayList) { - ArrayList ar = (ArrayList) value; - Map flatNestedMap = new HashMap(); - int size = ar.size(); - for (int i = 0; i < size; i++) { - flatNestedMap.put(String.format("%s[%d]", key, i), ar.get(i)); - } - flatParams.putAll(flattenParams(flatNestedMap)); - } else if ("".equals(value)) { - throw new InvalidRequestException("You cannot set '" + key + "' to an empty string. " + - "We interpret empty strings as null in requests. " + - "You may set '" + key + "' to null to delete the property.", - key, null); - } else if (value == null) { - flatParams.put(key, ""); - } else { - flatParams.put(key, value.toString()); - } - } - return flatParams; - } - - - // represents Errors returned as JSON - private static class ErrorContainer { - private APIResource.Error error; - } - - /** - * - */ - private static class Error { - @SuppressWarnings("unused") - String type; - - String message; - - String code; - - String param; - - @Override - public String toString() { - StringBuffer sb = new StringBuffer(); - if (null != type && !type.isEmpty()) { - sb.append("Error type: " + type + "\n"); - } - if (null != message && !message.isEmpty()) { - sb.append("\t Error message: " + message + "\n"); - } - if (null != code && !code.isEmpty()) { - sb.append("\t Error code: " + code + "\n"); - } - - return sb.toString(); - } - } - - /** - * @param responseStream - * @return responseString - * @throws IOException - */ - private static String getResponseBody(InputStream responseStream) - throws IOException { - //\A is the beginning of - // the stream boundary - String rBody = new Scanner(responseStream, CHARSET) - .useDelimiter("\\A") - .next(); // - responseStream.close(); - return rBody; - } - - /** - * @param method - * @param url - * @param query - * @param apiKey - * @return PingppResponse - * @throws APIConnectionException - */ - private static PingppResponse makeURLConnectionRequest( - APIResource.RequestMethod method, String url, String query, - String apiKey) throws APIConnectionException { - java.net.HttpURLConnection conn = null; - try { - switch (method) { - case GET: - conn = createGetConnection(url, query, apiKey); - break; - case POST: - conn = createPostConnection(url, query, apiKey); - break; - case DELETE: - conn = createDeleteConnection(url, query, apiKey); - break; - case PUT: - conn = createPutConnection(url, query, apiKey); - break; - default: - throw new APIConnectionException( - String.format("Unrecognized HTTP method %s. ", method)); - } - // trigger the request - int rCode = conn.getResponseCode(); - String rBody = null; - Map> headers; - - if (rCode >= 200 && rCode < 300) { - rBody = getResponseBody(conn.getInputStream()); - } else { - rBody = getResponseBody(conn.getErrorStream()); - } - headers = conn.getHeaderFields(); - return new PingppResponse(rCode, rBody, headers); - - } catch (IOException e) { - throw new APIConnectionException( - String.format( - "IOException during API request to Pingpp (%s): %s " - + "Please check your internet connection and try again. If this problem persists," - + "you should check Pingpp's service status at https://pingxx.com/status.", - Pingpp.getApiBase(), e.getMessage()), e); - } finally { - if (conn != null) { - conn.disconnect(); - } - } - } - - /** - * @param method - * @param url - * @param params - * @param clazz - * @param - * @return PingppObject - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - */ - protected static T request(APIResource.RequestMethod method, String url, Map params, Class clazz) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - return request(method, url, null, params, clazz); - } - - /** - * @param method - * @param url - * @param apiKey - * @param params - * @param clazz - * @param - * @return PingppObject - * @throws AuthenticationException - * @throws InvalidRequestException - * @throws APIConnectionException - * @throws APIException - * @throws ChannelException - * @throws RateLimitException - */ - protected static T request(APIResource.RequestMethod method, String url, String apiKey, Map params, Class clazz) throws AuthenticationException, - InvalidRequestException, APIConnectionException, - APIException, ChannelException, RateLimitException { - apiKey = apiKey != null ? apiKey : Pingpp.apiKey; - if ((apiKey == null || apiKey.length() == 0)) { - throw new AuthenticationException( - "No API key provided. (HINT: set your API key using 'Pingpp.apiKey = '. " - + "You can generate API keys from the Pingpp web interface. " - + "See https://pingxx.com for details."); - } - - String query = null; - switch (method) { - case GET: - case DELETE: - try { - query = createQuery(params); - } catch (UnsupportedEncodingException e) { - throw new InvalidRequestException("Unable to encode parameters to " + CHARSET, null, e); - } - break; - case POST: - case PUT: - query = createJSONString(params); - break; - } - - PingppResponse response; - int retryCount = 0; - while(true) { - try { - // HTTPSURLConnection verifies SSL cert by default - response = makeURLConnectionRequest(method, url, query, apiKey); - if (Pingpp.DEBUG) { - System.out.println(getGson().toJson(response)); - } - - int rCode = response.getResponseCode(); - String rBody = response.getResponseBody(); - if (rCode < 200 || rCode >= 300) { - handleAPIError(rBody, rCode); - } - return getGson().fromJson(rBody, clazz); - } catch (ClassCastException ce) { - throw ce; - } catch (ConnectException e) { - if(retryCount < RETRY_MAX) { - retryCount++; - } else { - throw new APIConnectionException(e.getMessage(), e); - } - } - } - } - - /** - * 错误处理 - * - * @param rBody - * @param rCode - * @throws InvalidRequestException - * @throws AuthenticationException - * @throws APIException - */ - private static void handleAPIError(String rBody, int rCode) - throws InvalidRequestException, AuthenticationException, - APIException, ChannelException, RateLimitException, ConnectException { - Error error = null; - try { - error = getGson().fromJson(rBody, - ErrorContainer.class).error; - } catch (JsonSyntaxException e) { - error = new Error(); - error.message = rBody; - error.code = String.valueOf(rCode); - } - - switch (rCode) { - case 400: - throw new InvalidRequestException(error.toString(), error.param, null); - case 404: - throw new InvalidRequestException(error.toString(), error.param, null); - case 403: - case 429: - throw new RateLimitException(error.toString(), null); - case 402: - throw new ChannelException(error.toString(), error.param, null); - case 401: - throw new AuthenticationException(error.toString()); - case 502: - throw new ConnectException(error.toString()); - default: - throw new APIException(error.toString(), null); - } - } - - /** - * 生成请求签名 - * - * @param data - */ - - private static String generateSign(String data) - throws IOException { - if (Pingpp.privateKey == null) { - if (Pingpp.privateKeyPath == null) { - return null; - } - FileInputStream inputStream = new FileInputStream(Pingpp.privateKeyPath); - byte[] keyBytes = new byte[inputStream.available()]; - inputStream.read(keyBytes); - inputStream.close(); - Pingpp.privateKey = new String(keyBytes, CHARSET); - } - - return PingppSignature.sign(data, Pingpp.privateKey, CHARSET); - } - - private static String currentTimeString() { - Integer requestTime = (int) (System.currentTimeMillis() / 1000); - return requestTime.toString(); - } - - private static String getRequestURIFromURL(URL url) { - String path = url.getPath(); - String query = url.getQuery(); - if (query == null) { - return path; - } - return path + "?" + query; - } -} diff --git a/src/main/java/com/pingplusplus/net/AppBasedResource.java b/src/main/java/com/pingplusplus/net/AppBasedResource.java deleted file mode 100755 index 3e68be7..0000000 --- a/src/main/java/com/pingplusplus/net/AppBasedResource.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.pingplusplus.net; - -import com.google.gson.FieldNamingPolicy; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import com.pingplusplus.Pingpp; -import com.pingplusplus.exception.InvalidRequestException; -import com.pingplusplus.model.*; -import com.pingplusplus.serializer.*; - -import java.io.UnsupportedEncodingException; - -public abstract class AppBasedResource extends APIResource { - /** - * @param clazz - * @return singleClassURL - * @throws InvalidRequestException - */ - protected static String singleClassURL(Class clazz) throws InvalidRequestException { - if (Pingpp.appId == null) { - throw new InvalidRequestException("Please set app_id using Pingpp.appId = ", "app_id", null); - } - return String.format("%s/v1/apps/%s/%s", Pingpp.getApiBase(), Pingpp.appId, className(clazz)); - } - - /** - * @param clazz - * @return classURL - * @throws InvalidRequestException - */ - protected static String classURL(Class clazz) throws InvalidRequestException { - return String.format("%ss", singleClassURL(clazz)); - } - - /** - * @param clazz - * @param id - * @return instanceURL - * @throws InvalidRequestException - */ - protected static String instanceURL(Class clazz, String id) throws InvalidRequestException { - try { - return String.format("%s/%s", classURL(clazz), urlEncode(id)); - } catch (UnsupportedEncodingException e) { - throw new InvalidRequestException("Unable to encode parameters to " + CHARSET, null, e); - } - } - - /** - * @param objectName - * @return customURL - * @throws InvalidRequestException - */ - protected static String customURL(String objectName) throws InvalidRequestException { - if (Pingpp.appId == null) { - throw new InvalidRequestException("Please set app_id using Pingpp.appId = ", "app_id", null); - } - return String.format("%s/v1/apps/%s/%s", Pingpp.getApiBase(), Pingpp.appId, objectName); - } -} diff --git a/src/main/java/com/pingplusplus/net/SubAppBasedResource.java b/src/main/java/com/pingplusplus/net/SubAppBasedResource.java deleted file mode 100644 index 2acb9b8..0000000 --- a/src/main/java/com/pingplusplus/net/SubAppBasedResource.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.pingplusplus.net; - -import com.pingplusplus.Pingpp; -import com.pingplusplus.exception.InvalidRequestException; - -import java.io.UnsupportedEncodingException; - -public abstract class SubAppBasedResource extends AppBasedResource { - - /** - * @param clazz - * @param subAppId - * @return singleClassURL - * @throws InvalidRequestException - */ - protected static String singleClassURL(Class clazz, String subAppId) throws InvalidRequestException { - if (Pingpp.appId == null) { - throw new InvalidRequestException("Please set app_id using Pingpp.appId = ", "app_id", null); - } - return String.format("%s/v1/apps/%s/sub_apps/%s/%s", Pingpp.getApiBase(), Pingpp.appId, subAppId, className(clazz)); - } - - /** - * @param clazz - * @param subAppId - * @return classURL - * @throws InvalidRequestException - */ - protected static String classURL(Class clazz, String subAppId) throws InvalidRequestException { - return String.format("%ss", singleClassURL(clazz, subAppId)); - } - - /** - * @param clazz - * @param id - * @param subAppId - * @return instanceURL - * @throws InvalidRequestException - */ - protected static String instanceURL(Class clazz, String subAppId, String id) throws InvalidRequestException { - try { - return String.format("%s/%s", classURL(clazz, subAppId), urlEncode(id)); - } catch (UnsupportedEncodingException e) { - throw new InvalidRequestException("Unable to encode parameters to " + CHARSET, null, e); - } - } -} diff --git a/src/main/java/com/pingplusplus/net/UserBasedResource.java b/src/main/java/com/pingplusplus/net/UserBasedResource.java deleted file mode 100644 index 636e837..0000000 --- a/src/main/java/com/pingplusplus/net/UserBasedResource.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.pingplusplus.net; - -import com.pingplusplus.Pingpp; -import com.pingplusplus.exception.InvalidRequestException; - -import java.io.UnsupportedEncodingException; - -public abstract class UserBasedResource extends AppBasedResource { - - /** - * @param clazz - * @param userId - * @return singleClassURL - * @throws InvalidRequestException - */ - protected static String singleClassURL(Class clazz, String userId) throws InvalidRequestException { - if (Pingpp.appId == null) { - throw new InvalidRequestException("Please set app_id using Pingpp.appId = ", "app_id", null); - } - return String.format("%s/v1/apps/%s/users/%s/%s", Pingpp.getApiBase(), Pingpp.appId, userId, className(clazz)); - } - - /** - * @param clazz - * @param userId - * @return classURL - * @throws InvalidRequestException - */ - protected static String classURL(Class clazz, String userId) throws InvalidRequestException { - return String.format("%ss", singleClassURL(clazz, userId)); - } - - /** - * @param clazz - * @param id - * @param userId - * @return instanceURL - * @throws InvalidRequestException - */ - protected static String instanceURL(Class clazz, String userId, String id) throws InvalidRequestException { - try { - return String.format("%s/%s", classURL(clazz, userId), urlEncode(id)); - } catch (UnsupportedEncodingException e) { - throw new InvalidRequestException("Unable to encode parameters to " + CHARSET, null, e); - } - } -} diff --git a/src/main/java/com/pingplusplus/util/PingppSignature.java b/src/main/java/com/pingplusplus/util/PingppSignature.java deleted file mode 100644 index 3b5f0a9..0000000 --- a/src/main/java/com/pingplusplus/util/PingppSignature.java +++ /dev/null @@ -1,106 +0,0 @@ -package com.pingplusplus.util; - -import com.pingplusplus.Pingpp; -import org.apache.commons.codec.binary.Base64; - -import sun.security.util.DerInputStream; -import sun.security.util.DerValue; - -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.math.BigInteger; -import java.security.*; -import java.security.spec.InvalidKeySpecException; -import java.security.spec.PKCS8EncodedKeySpec; -import java.security.spec.RSAPrivateCrtKeySpec; - -/** - * Created by Afon on 2016/12/16. - */ -public class PingppSignature { - public static String sign(String data, String PEMEncodedPrivateKey, String charset) { - PrivateKey privateKey = getPrivateKeyFromPEM(PEMEncodedPrivateKey); - if (privateKey == null) { - return null; - } - - try { - Signature signature = Signature.getInstance("SHA256withRSA"); - signature.initSign(privateKey); - signature.update(data.getBytes(charset)); - byte[] signBytes = signature.sign(); - - return Base64.encodeBase64String(signBytes).replaceAll("\n|\r", ""); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } catch (InvalidKeyException e) { - e.printStackTrace(); - } catch (SignatureException e) { - e.printStackTrace(); - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - } - - return null; - } - - public static PrivateKey getPrivateKeyFromPEM(String PEMEncodedPrivateKey) { - PEMEncodedPrivateKey = PEMEncodedPrivateKey - .replaceAll("(-+BEGIN (RSA )?PRIVATE KEY-+\\r?\\n|-+END (RSA )?PRIVATE KEY-+\\r?\\n?)", ""); - byte[] privateKeyBytes = Base64.decodeBase64(PEMEncodedPrivateKey); - - try { - return generatePrivateKeyWithPKCS8(privateKeyBytes); - } catch (InvalidKeySpecException e) { - if (Pingpp.DEBUG) { - e.printStackTrace(); - } - return generatePrivateKeyWithPKCS1(privateKeyBytes); - } - } - - public static PrivateKey generatePrivateKeyWithPKCS8(byte[] privateKeyBytes) - throws InvalidKeySpecException { - try { - PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes); - KeyFactory kf = KeyFactory.getInstance("RSA"); - return kf.generatePrivate(keySpec); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } - return null; - } - - public static PrivateKey generatePrivateKeyWithPKCS1(byte[] privateKeyBytes) { - try { - DerInputStream derReader = new DerInputStream(privateKeyBytes); - DerValue[] seq = derReader.getSequence(0); - if (seq.length < 9) { - System.out.println("Could not parse a PKCS1 private key."); - return null; - } - // skip version seq[0]; - BigInteger modulus = seq[1].getBigInteger(); - BigInteger publicExp = seq[2].getBigInteger(); - BigInteger privateExp = seq[3].getBigInteger(); - BigInteger prime1 = seq[4].getBigInteger(); - BigInteger prime2 = seq[5].getBigInteger(); - BigInteger exp1 = seq[6].getBigInteger(); - BigInteger exp2 = seq[7].getBigInteger(); - BigInteger crtCoef = seq[8].getBigInteger(); - RSAPrivateCrtKeySpec spec = new RSAPrivateCrtKeySpec( - modulus, publicExp, privateExp, prime1, prime2, exp1, exp2, crtCoef); - KeyFactory keyFactory = KeyFactory.getInstance("RSA"); - - return keyFactory.generatePrivate(spec); - } catch (IOException e) { - e.printStackTrace(); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } catch (InvalidKeySpecException e) { - e.printStackTrace(); - } - - return null; - } -}