pondersource/billing-api

适用于 Heroku, Github, AWS 的计费 API

2.0.2 2022-04-13 12:36 UTC

This package is auto-updated.

Last update: 2024-09-13 18:03:05 UTC


README

要求

  • PHP 7.3+ 或 8.x

为账单平台 API 工作的 PHP 客户端,适用于发票、账户、应用程序等。

用法

composer require pondersource/billing-api

  • 检查此存储库
  • 创建一个 .env 文件,您可以在 CLI 中运行 cp .env.example .env
  • 取消注释/注释您想要检索发票或使用信息的服务(Google、AWS、Github、Heroku)。
  • 运行 composer install
  • 转到 https://github.com/settings/tokens/new 并创建一个个人访问令牌。选择“admin:org”和“user”作为作用域。
  • 将其保存在您的 .env 文件中作为
GITHUB_ACCESS_TOKEN=ghp_0AwgbEb....
  • 您需要显示自己的 Github 用户名
$user_billing = $github->getUserSharedStorageBilling("michielbdejong");
  • 运行 echo -n; heroku auth:token 以获取 Heroku 的个人令牌,或者您可以在 CLI 中直接运行 heroku auth:token
  • 将其保存在您的 .env 文件中作为
HEROKU_API_KEY=30a84169-7c38-4b71-a19...
  • 与 Google 合作时,您需要首先在 CLI 中运行 cloud auth application-default login
  • 它将生成您可以将其中复制粘贴到 Google 的 json 文件(putenv('GOOGLE_APPLICATION_CREDENTIALS='.realpath("application_default_credentials.json")))。
  • 运行 php index.php
  • 当您运行 php index.php 时,每个服务都将生成 UBL 和 JSON 项的 XML 或 JSON 集合。

Heroku API 客户端

use PonderSource\HerokuApi\HerokuClient;

$her = new HerokuClient([
    'apiKey' =>  getenv('HEROKU_API_KEY'),
]);

$her->getHerokuInvoice();
$her->getHerokuTeamInvoices();

我们使用 Heroku Invoices 为简单用户,我们可以以相同的方式为团队使用,我将向您展示一个函数的响应。我们将直接与 Heroku 的平台 API 合作并获取信息。

  • 简单 Heroku 发票的第一个方法的响应 JSON
[
    {
        "charges_total": 0,
        "created_at": "2022-02-17T02:45:30Z",
        "credits_total": 0,
        "id": "22e1c650-291e-48b7-b462-4bbd9eba4a0e",
        "number": 62983158,
        "period_end": "2022-03-01",
        "period_start": "2022-02-01",
        "state": 1,
        "total": 0,
        "updated_at": "2022-03-01T12:59:11Z"
    },
    {
        "charges_total": 0,
        "created_at": "2022-03-01T02:44:04Z",
        "credits_total": 0,
        "id": "e4bbdb15-40db-4de8-b89f-d2f921766302",
        "number": 63410419,
        "period_end": "2022-03-17",
        "period_start": "2022-03-01",
        "state": 0,
        "total": 0,
        "updated_at": "2022-03-17T02:40:34Z"
    }
]
  • 简单 Heroku 发票的第一个方法的响应 UBL
<?xml version="1.0"?>
<x1:invoice xmlns:x1="http://example.org/invoices">
 <x1:InvoiceTeamItem xmlns:x1="http://example.org/invoices">
  <x1:charges_total xmlns:x1="http://example.org/invoices">0</x1:charges_total>
  <x1:created_at xmlns:x1="http://example.org/invoices">2022-02-17T02:45:30Z</x1:created_at>
  <x1:credits_total xmlns:x1="http://example.org/invoices"/>
  <x1:id xmlns:x1="http://example.org/invoices">22e1c650-291e-48b7-b462-4bbd9eba4a0e</x1:id>
  <x1:number xmlns:x1="http://example.org/invoices">62983158</x1:number>
  <x1:period_start xmlns:x1="http://example.org/invoices">2022-02-01</x1:period_start>
  <x1:period_end xmlns:x1="http://example.org/invoices">2022-03-01</x1:period_end>
  <x1:state xmlns:x1="http://example.org/invoices">1</x1:state>
  <x1:total xmlns:x1="http://example.org/invoices">0</x1:total>
  <x1:updated_at xmlns:x1="http://example.org/invoices">2022-03-01T12:59:11Z</x1:updated_at>
 </x1:InvoiceTeamItem>
 <x1:InvoiceTeamItem xmlns:x1="http://example.org/invoices">
  <x1:charges_total xmlns:x1="http://example.org/invoices">0</x1:charges_total>
  <x1:created_at xmlns:x1="http://example.org/invoices">2022-03-01T02:44:04Z</x1:created_at>
  <x1:credits_total xmlns:x1="http://example.org/invoices"/>
  <x1:id xmlns:x1="http://example.org/invoices">e4bbdb15-40db-4de8-b89f-d2f921766302</x1:id>
  <x1:number xmlns:x1="http://example.org/invoices">63410419</x1:number>
  <x1:period_start xmlns:x1="http://example.org/invoices">2022-03-01</x1:period_start>
  <x1:period_end xmlns:x1="http://example.org/invoices">2022-03-17</x1:period_end>
  <x1:state xmlns:x1="http://example.org/invoices">0</x1:state>
  <x1:total xmlns:x1="http://example.org/invoices">0</x1:total>
  <x1:updated_at xmlns:x1="http://example.org/invoices">2022-03-17T02:40:34Z</x1:updated_at>
 </x1:InvoiceTeamItem>
</x1:invoice>

Google API 客户端

use PonderSource\GoogleApi\Google;

$google = new Google([
    'apiKey' => putenv('GOOGLE_APPLICATION_CREDENTIALS='.realpath("application_default_credentials.json"))
]);
$google->getCloudbillingSkus();

在您的 json 文件中,您可以在上方看到说明。我们使用 Google Cloud 库获取服务和其他信息。

  • JSON 中的一个示例响应
[
    {
        "sku_name": "services\/0069-3716-5463\/skus\/00C0-AE90-D5AE",
        "sku_id": "00C0-AE90-D5AE",
        "sku_description": "Custom Commit Plan QUser",
        "sku_provider_name": "Qubole",
        "sku_service_name": "Qubole Data Service",
        "sku_resource": "ApplicationServices",
        "sku_group": "Qubole",
        "sku_usage_type": "OnDemand",
        "sku_effective_time": "18:42:26",
        "sku_usage_unit": "mo",
        "sku_usage_unit_description": "month",
        "sku_base_unit": "s",
        "sku_base_unit_description": "second",
        "sku_base_unit_conversion_factor": 2674800,
        "sku_display_quantity": 1,
        "sku_start_usage_amount": 0,
        "sku_unit_price": "USD"
    },
]
  • 显示 UBL 中的一个示例
<?xml version="1.0"?>
<x1:billing xmlns:x1="http://example.org/billings">
 <x1:BillingItem xmlns:x1="http://example.org/billings">
  <x1:sku_name xmlns:x1="http://example.org/billings">services/0069-3716-5463/skus/00C0-AE90-D5AE</x1:sku_name>
  <x1:sku_id xmlns:x1="http://example.org/billings">00C0-AE90-D5AE</x1:sku_id>
  <x1:sku_description xmlns:x1="http://example.org/billings">Custom Commit Plan QUser</x1:sku_description>
  <x1:sku_provider_name xmlns:x1="http://example.org/billings">Qubole</x1:sku_provider_name>
  <x1:sku_service_name xmlns:x1="http://example.org/billings">Qubole Data Service</x1:sku_service_name>
  <x1:sku_resource xmlns:x1="http://example.org/billings">ApplicationServices</x1:sku_resource>
  <x1:sku_group xmlns:x1="http://example.org/billings">Qubole</x1:sku_group>
  <x1:sku_usage_type xmlns:x1="http://example.org/billings">OnDemand</x1:sku_usage_type>
  <x1:sku_effective_time xmlns:x1="http://example.org/billings">18:42:26</x1:sku_effective_time>
  <x1:sku_usage_unit xmlns:x1="http://example.org/billings">mo</x1:sku_usage_unit>
  <x1:sku_usage_unit_description xmlns:x1="http://example.org/billings">month</x1:sku_usage_unit_description>
  <x1:sku_base_unit xmlns:x1="http://example.org/billings">s</x1:sku_base_unit>
  <x1:sku_base_unit_description xmlns:x1="http://example.org/billings">second</x1:sku_base_unit_description>
  <x1:sku_base_unit_conversion_factor xmlns:x1="http://example.org/billings">2674800</x1:sku_base_unit_conversion_factor>
  <x1:sku_display_quantity xmlns:x1="http://example.org/billings">1</x1:sku_display_quantity>
  <x1:sku_start_usage_amount xmlns:x1="http://example.org/billings">0</x1:sku_start_usage_amount>
  <x1:sku_unit_price xmlns:x1="http://example.org/billings"/>
 </x1:BillingItem>
</x1:billing>

GitHub API PHP 客户端

命名空间 PonderSource\GitHubApi

授权

我们需要提供用户的 TOKEN(我们建议使用带有到期日期的 TOKEN)

头部

调用 GitHub API 端点的必需头部

    "User-Agent: Example REST API Client",
    "Accept: application/vnd.github.v3+json",
    "Authorization: token TOKEN"

方法

我们可以请求用户或组织的计费信息

组织

用户

示例

获取组织的共享存储计费

billing-api/index.php 中,我们首先需要创建 GitHub 客户端

  1. 我们首先需要初始化 GitHub 客户端
$github = new GitHubClient();
  1. 现在我们可以选择 6 个可用函数之一,并检索组织或用户的计费信息(JSON)。
$github->getOrgSharedStorageBilling("org");
  1. 响应
{
 "days_left_in_billing_cycle": 20,
 "estimated_paid_storage_for_month": 15,
 "estimated_storage_for_month": 40
}

AWS API PHP 客户端

命名空间 PonderSource\AWSApi

  • 用于与 AWS 服务通信的 PHP 库:[AWS SDK](https://aws.amazon.com/sdk-for-php/).

  • Cost Explorer API 端点:https://ce.us-east-1.amazonaws.com.

凭证

~/.aws/credentials.ini 中检索凭证

  • AWS_ACCESS_KEY_ID

  • AWS_SECRET_ACCESS_KEY

使用 AWS CLI 使用临时安全凭证

aws sts get-session-token --serial-number arn-of-the-mfa-device --token-code code-from-token

根访问密钥 VS IAM 访问密钥

  • 根访问

    • 允许访问账户中所有资源的完全访问权限
  • IAM 访问密钥

    • 访问您 AWS 账户中用户的 AWS 服务和资源

示例

  1. billing-api/index.php 首先我们需要创建 AWS 客户端
$aws = new AWSClient([
    'region'  => 'us-east-1',
    'version' => 'latest',
    'credentials' => [
      'key' => $key,
      'secret' => $secret
    ],
    'endpoint' => 'https://ce.us-east-1.amazonaws.com'
]);

  1. 现在我们可以获取成本和用量报告。请注意,用户必须首先启用成本分析器(数据摄取可能需要一些时间)

$aws->getCostAndUsage([
'Granularity' => 'DAILY', // REQUIRED
'Metrics' => ['BlendedCost'], // REQUIRED
'TimePeriod' => [ // REQUIRED
		'Start' => '2022-01-03', // REQUIRED
    'End' => '2022-02-03', // REQUIRED
	],
],'aws_cost_and_usage');