Java SDK使用说明
您可以使用我们提供的开放SDK与CodePay支付网关集成。SDK封装了执行集成的过程,包括添加和验证调用支付网关API的签名。 我们不保证此代码的安全性和实用性。这只是一个示例,演示了调用API的过程。您可以参考此代码来编写更适合您的业务系统的程序。
使用条件
适用于Java语言,JDK 1.8及以上版本的开发环境。
Java SDK集成
- SDK封装了签名验证逻辑,可以使用SDK直接调用API。
- 确定与接口对应的类
例如接口名称:
wisehub.cloud.pay.order
SDK中的类是:将每个单词的第一个字母大写,去掉分隔符(“.”),并在末尾添加Request(或Response)。
示例中接口名称对应的类为:WisehubCloudPayOrderRequest
(Request class)BscancPaySubmitResponse
(Response class)
使用步骤
1. 下载jar并将其添加到您的项目中,请参考github源代码
gateway-api-sdk-java2. 这个JAR依赖于一些开源的第三方JAR。如果这些JAR没有集成到您的项目中,您将需要手动向项目添加依赖项
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.6</version>
</dependency>
3. 请参阅以下示例,并根据 API文档进行开发
// Instantiate the client
OpenApiClient openapiClient = new OpenApiClient(<YOUR_APP_ID>, <GATEWAY_URL>, <YOUR_APP_RSA_PRIVATE_KEY>, <GATEWAY_RSA_PUBLIC_KEY>);
// Build request object and set request parameters
WisehubCloudPayOrderRequest request = new WisehubCloudPayOrderRequest();
request.setMerchant_no("{Your merchant number}");
request.setStore_no("{Your store number}");
request.setTerminal_sn("{Your terminal sn}");
request.setMessage_receiving_application("CodePay Register");
request.setPay_method_category("BANKCARD");
request.setPrice_currency("USD");
request.setOrder_amount(34.50);
request.setTrans_type(1);
request.setMerchant_order_no("mapp" + System.currentTimeMillis());
request.setDescription("IPhone 15 white X2");
request.setExpires(5);
request.setOrder_queue_mode("FIFO");
// Execute API calls and receive response message objects
WisehubCloudPayOrderResponse response = null;
try {
response = openapiClient.execute(request);
} catch (OpenApiException e) {
// TODO
// API request failed, possibly due to network issues, signature issues, or other issues. It is recommended to call the query or close the order API
return;
}
// Process response
if (response.isSuccess()) {
// TODO
// The result of successful API response, saving the transaction number, calling the query interface in the background
// to query the order payment result, and receiving asynchronous payment result notification
} else {
// TODO
// API response business result failed, execute failure handling logic
}