本教程详细介绍如何使用Alibaba Cloud SDK for Java进行订单查询。

前提条件

在使用本教程前,请确保已完成以下操作:
  • 使用Alibaba Cloud SDK for Java,您需要一个阿里云账号和访问密钥(AccessKey)。 请在阿里云控制台中的AccessKey管理页面上创建和查看您的AccessKey。
  • 确保您已经安装了Alibaba Cloud SDK for Java,准确的SDK版本号,请参见阿里云开发工具包(SDK)
    <dependencies>
         <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-core -->
         <dependency>
             <groupId>com.aliyun</groupId>
             <artifactId>aliyun-java-sdk-core</artifactId>
             <version>4.4.3</version>
         </dependency>
         <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-bssopenapi -->
         <dependency>
             <groupId>com.aliyun</groupId>
             <artifactId>aliyun-java-sdk-bssopenapi</artifactId>
             <version>1.4.2</version>
         </dependency>
    </dependencies>

代码示例

本文操作示例主要以代码形式体现,具体代码如下:
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.bssopenapi.model.v20171214.GetOrderDetailRequest;
import com.aliyuncs.bssopenapi.model.v20171214.GetOrderDetailResponse;
import com.aliyuncs.bssopenapi.model.v20171214.QueryOrdersRequest;
import com.aliyuncs.bssopenapi.model.v20171214.QueryOrdersResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;

/**
 * 查询订单列表以及单个订单详细信息
 */
public class QueryOrdersDemo {

    /**
     * 查询订单列表
     */
    private QueryOrdersResponse queryOrders(QueryOrdersRequest request, IAcsClient client){
        try {
            // 调用SDK发送请求
            return client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
            // 发生调用错误,抛出运行时异常
            throw new RuntimeException();
        }
    }

    /**
     * 查询订单详情
     */
    private GetOrderDetailResponse getOrderDetail(GetOrderDetailRequest request,IAcsClient client){
        try {
            // 调用SDK发送请求
            return client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
            // 发生调用错误,抛出运行时异常
            throw new RuntimeException();
        }
    }

    private static void log_print(String functionName, Object result) {
        Gson gson = new Gson();
        System.out.println("-------------------------------" + functionName + "-------------------------------");
        System.out.println(gson.toJson(result));
    }

    public static void main(String[] args) {
        // 设置鉴权参数,初始化客户端
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-qingdao",// 地域ID
                "your-access-key-id",// 您的AccessKey ID
                "your-access-key-secret");// 您的AccessKey Secret
        IAcsClient client = new DefaultAcsClient(profile);

        QueryOrdersDemo queryOrdersDemo = new QueryOrdersDemo();

        // 查询订单列表
        QueryOrdersRequest queryOrdersRequest = new QueryOrdersRequest();
        // 订单类型:续费
        queryOrdersRequest.setOrderType("Renew");
        // 支付状态:已支付
        queryOrdersRequest.setPaymentStatus("Paid");
        QueryOrdersResponse queryOrdersResponse = queryOrdersDemo.queryOrders(queryOrdersRequest, client);
        log_print("queryOrders",queryOrdersResponse);

        // 查询订单详细信息
        GetOrderDetailRequest getOrderDetailRequest = new GetOrderDetailRequest();
        // 上面查到的订单ID
        getOrderDetailRequest.setOrderId("******");
        GetOrderDetailResponse getOrderDetailResponse = queryOrdersDemo.getOrderDetail(getOrderDetailRequest, client);
        log_print("getOrderDetail",getOrderDetailResponse);
    }
}

运行结果

正确运行结果类似如下:
-------------------------------queryOrders-------------------------------
{
    "requestId": "8BE7D2A8-42DA-4C14-9DB2-0FD621FC8054",
    "success": true,
    "code": "Success",
    "message": "Successful!",
    "data": {
        "pageNum": 1,
        "pageSize": 20,
        "totalCount": 1,
        "orderList": [
            {
                "orderId": "2045xxxx",
                "productCode": "rds",
                "subscriptionType": "Subscription",
                "orderType": "Renew",
                "createTime": "2019-10-09T01:06:35Z",
                "paymentTime": "2019-10-09T01:06:35Z",
                "paymentStatus": "Paid",
                "pretaxGrossAmount": "504.00",
                "pretaxAmount": "0.00",
                "currency": "CNY"
            }
        ]
    }
}
-------------------------------getOrderDetail-------------------------------
{
    "requestId": "1DDA06F2-418A-42E9-93FC-A180545F0090",
    "success": true,
    "code": "Success",
    "message": "Successful!",
    "data": {
        "orderList": [
            {
                "orderId": "20459891594xxxx",
                "subOrderId": "20459891594xxxx",
                "productCode": "rds",
                "subscriptionType": "Subscription",
                "orderType": "Renew",
                "createTime": "2019-10-09T01:06:35Z",
                "paymentTime": "2019-10-09T01:06:35Z",
                "paymentStatus": "Paid",
                "region": "cn-qingdao",
                "config": "DBInstanceClass:[DBInstanceClass:rds.mysql.s2.large;EngineVersion:5.7;Region:cn-qingdao;]DBInstanceStorage:[Engine:mysql;EngineVersion:5.7;Series:HighAvailability;Region:cn-qingdao;DBInstanceStorageType:local_ssd;]",
                "quantity": "1",
                "usageStartTime": "2019-10-17T16:00:00Z",
                "usageEndTime": "2019-11-17T16:00:00Z",
                "instanceIDs": "[\"rm-m5e4x50c91q14xxxx\"]",
                "pretaxGrossAmount": "504.00",
                "pretaxAmount": "0.00",
                "currency": "CNY",
                "orderSubType": "ProductSubOrder",
                "originalConfig": "rds_class:[rds_class:rds.mysql.s2.large;]rds_dbtype:[rds_dbtype:mysql;]rds_dbversion:[rds_dbversion:5.7;]rds_flow_out:[rds_flow_out:1;]rds_iz:[rds_iz:cn-qingdao-b;]rds_nodetype:[rds_nodetype:HighAvailability;]rds_region:[rds_region:cn-qingdao;]rds_storage:[rds_storage:5;]rds_storagetype:[rds_storagetype:local_ssd;]rds_web_type:[rds_web_type:1;]"
            }
        ]
    }
}