本教程详细介绍如何使用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.*;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;

/**
 * 查询账单总览、月账单以及账单明细等
 */
public class QueryBillDemo {

    /**
     * 账单总览查询
     */
    private QueryBillOverviewResponse queryBillOverview(QueryBillOverviewRequest request, IAcsClient client){
        try {
            // 调用SDK发送请求
            return client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
            // 发生调用错误,抛出运行时异常
            throw new RuntimeException();
        }
    }

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

    /**
     * 实例账单查询
     */
    private QueryInstanceBillResponse queryInstanceBill(QueryInstanceBillRequest request, IAcsClient client){
        try {
            // 调用SDK发送请求
            return client.getAcsResponse(request);
        } catch (ClientException e) {
            e.printStackTrace();
            // 发生调用错误,抛出运行时异常
            throw new RuntimeException();
        }
    }

    /**
     * 月费用分摊查询
     */
    private QueryInstanceGaapCostResponse queryInstanceGaapCost(QueryInstanceGaapCostRequest 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);

        QueryBillDemo queryBillDemo = new QueryBillDemo();
        // 账期,YYYY-MM。
        String billingCycle = "2019-09";

        // 账单总览查询
        QueryBillOverviewRequest queryBillOverviewRequest = new QueryBillOverviewRequest();
        queryBillOverviewRequest.setBillingCycle(billingCycle);
        QueryBillOverviewResponse queryBillOverviewResponse = queryBillDemo.queryBillOverview(queryBillOverviewRequest, client);
        log_print("queryBillOverview",queryBillOverviewResponse);

         // 账单查询
        QueryBillRequest queryBillRequest = new QueryBillRequest();
        queryBillRequest.setBillingCycle(billingCycle);
        QueryBillResponse queryBillResponse = queryBillDemo.queryBill(queryBillRequest, client);
        log_print("queryBill",queryBillResponse);

         // 实例账单查询
        QueryInstanceBillRequest queryInstanceBillRequest = new QueryInstanceBillRequest();
        queryInstanceBillRequest.setBillingCycle(billingCycle);
        QueryInstanceBillResponse queryInstanceBillResponse = queryBillDemo.queryInstanceBill(queryInstanceBillRequest, client);
        log_print("queryInstanceBill",queryInstanceBillResponse);

        // 月分摊费用查询
        QueryInstanceGaapCostRequest queryInstanceGaapCostRequest = new QueryInstanceGaapCostRequest();
        queryInstanceGaapCostRequest.setBillingCycle(billingCycle);
        QueryInstanceGaapCostResponse queryInstanceGaapCostResponse = queryBillDemo.queryInstanceGaapCost(queryInstanceGaapCostRequest, client);
        log_print("queryInstanceGaapCost",queryInstanceGaapCostResponse);
    }
}

运行结果

正确运行结果类似如下:
-------------------------------queryBillOverview-------------------------------
{
    "requestId": "9B7F7A8C-021A-4102-8D1E-40151E2C2FD6",
    "success": true,
    "code": "Success",
    "message": "Successful!",
    "data": {
        "Items": {
            "Item": [
                {
                    "SubscriptionType": "PayAsYouGo",
                    "ProductCode": "nat_gw",
                    "ProductDetail": "NAT Gateway (Pay-As-You-Go)",
                    "DeductedByPrepaidCard": 0,
                    "PretaxAmount": 744,
                    "DeductedByCoupons": 0,
                    "Item": "PayAsYouGoBill",
                    "PaymentAmount": 0,
                    "OutstandingAmount": 744,
                    "DeductedByCashCoupons": 0,
                    "ProductName": "",
                    "PretaxGrossAmount": 744,
                    "Currency": "CNY",
                    "InvoiceDiscount": 0
                }
            ],
          "BillingCycle": "2019-09",
            "AccountID": "16****13",
            "AccountName": "zh****18"
        }
    }
}
-------------------------------queryBill-------------------------------
{
    "requestId": "1EDD8473-9B9A-4631-AF5C-BF24BAC29707",
    "success": true,
    "code": "Success",
    "message": "Successful!",
    "data": {
        "billingCycle": "2019-09",
        "accountID": "123157908552xxxx",
        "accountName": "xxxxx",
        "pageNum": 1,
        "pageSize": 20,
        "totalCount": 17125,
        "items": [
            {
                "recordID": "201909158115xxxxx",
                "item": "PayAsYouGoBill",
                "ownerID": "",
                "usageStartTime": "2019-09-30 23:00:00",
                "usageEndTime": "2019-10-01 00:00:00",
                "paymentTime": "",
                "productCode": "vod",
                "productType": "",
                "subscriptionType": "PayAsYouGo",
                "productName": "视频点播",
                "productDetail": "视频点播",
                "pretaxGrossAmount": 0.0,
                "deductedByCoupons": 0.0,
                "invoiceDiscount": 0.0,
                "pretaxAmount": 0.0,
                "currency": "CNY",
                "paymentAmount": 0.0,
                "deductedByCashCoupons": 0.0,
                "deductedByPrepaidCard": 0.0,
                "outstandingAmount": 0.0,
                "status": "NoSettle",
                "roundDownDiscount": "0.0"
            }

        ]
    }
}
-------------------------------queryInstanceBill-------------------------------
{
    "requestId": "E8F3799E-3D76-4530-B2C7-45FC6A4BF060",
    "success": true,
    "code": "Success",
    "message": "Successful!",
    "data": {
        "billingCycle": "2019-09",
        "accountID": "123157908552xxxx",
        "accountName": "xxxxx",
        "totalCount": 503,
        "pageNum": 1,
        "pageSize": 20,
        "items": [
            {
                "instanceID": "cas-cn-78v1biixxxxx",
                "billingType": "其它",
                "costUnit": "未分配",
                "productCode": "cas",
                "productType": "cas",
                "subscriptionType": "Subscription",
                "productName": "SSL证书",
                "productDetail": "云盾证书服务",
                "ownerID": "1231579085529xxxx",
                "billingItem": "",
                "listPrice": "",
                "listPriceUnit": "",
                "usage": "",
                "usageUnit": "",
                "deductedByResourcePackage": "",
                "pretaxGrossAmount": 0.0,
                "invoiceDiscount": 0.0,
                "deductedByCoupons": 0.0,
                "pretaxAmount": 0.0,
                "deductedByCashCoupons": 0.0,
                "deductedByPrepaidCard": 0.0,
                "paymentAmount": 0.0,
                "outstandingAmount": 0.0,
                "currency": "CNY",
                "nickName": "",
                "resourceGroup": "默认资源组",
                "tag": "",
                "instanceConfig": "",
                "instanceSpec": "",
                "internetIP": "",
                "intranetIP": "",
                "region": "",
                "zone": "",
                "item": "SubscriptionOrder",
                "servicePeriod": "31649718"
            }
        ]
    }
}
-------------------------------queryInstanceGaapCost-------------------------------
{
    "requestId": "93790222-E0DA-4665-9EE0-56C3EE7A56C4",
    "success": true,
    "code": "Success",
    "data": {
        "Module": [
            {
                "PayerAccount": "null",
                "GaapDeductedByCashCoupons": 0,
                "PricingDiscount": 0,
                "MonthGaapPretaxAmount": 0.74,
                "MonthGaapDeductedByCoupons": 0,
                "PretaxAmount": 0,
                "UnallocatedPaymentAmount": 0,
                "UnallocatedPretaxGrossAmount": 0,
                "UnallocatedPretaxAmount": 0,
                "UnallocatedPricingDiscount": 0,
                "PayTime": "2019-09-15 15:59:57",
                "PaymentCurrency": "CNY",
                "ProductType": "ossbag",
                "UnallocatedDeductedByCoupons": 0,
                "OrderType": "Renewal",
                "UnallocatedPretaxAmountLocal": 0,
                "PretaxGrossAmount": 0,
                "Currency": "CNY",
                "GaapPretaxAmountLocal": 0,
                "GaapPaymentAmount": 0,
                "SubscriptionType": "Subscription",
                "BillingCycle": "2019-09",
                "ProductCode": "flowpack",
                "InstanceID": "OSSBAG-cn-0xl0njj3xxxx",
                "PretaxAmountLocal": 0,
                "GaapDeductedByCoupons": 0,
                "GaapPretaxAmount": 0,
                "OrderId": "202147900290xxx",
                "GaapDeductedByPrepaidCard": 0,
                "DeductedByPrepaidCard": 0,
                "GaapPretaxGrossAmount": 0,
                "SubOrderId": "202147900290xxx",
                "MonthGaapPaymentAmount": 0,
                "DeductedByCoupons": 0,
                "UsageEndDate": "2019-09-20 00:00:00",
                "UnallocatedDeductedByPrepaidCard": 0,
                "MonthGaapDeductedByPrepaidCard": 0,
                "UsageStartDate": "2019-09-01 00:00:00",
                "MonthGaapPretaxGrossAmount": 0.99,
                "PaymentAmount": 0,
                "MonthGaapPricingDiscount": 0.25,
                "DeductedByCashCoupons": 0,
                "OwnerID": "102872886221xxxx",
                "MonthGaapDeductedByCashCoupons": 0.74,
                "UnallocatedDeductedByCashCoupons": 0,
                "MonthGaapPretaxAmountLocal": 0.74,
                "GaapPricingDiscount": 0,
                "BillType": "SubscriptionOrder"
            }
        ]
    }
}