apruvd/php_sdk_v4

轻量级 PHP SDK,用于集成 Apruvd API v4。

v0.1.3 2019-08-07 15:55 UTC

This package is auto-updated.

Last update: 2024-09-08 03:46:21 UTC


README

轻量级 PHP SDK,用于集成 Apruvd API v4

服务

APIService

主要服务,包含对可用 API 端点的 1:1 方法映射。

$service = new APIService('$merchant_id', 'secret_id', 'secret_key', 'optional_refresh_token', 'optional_access_token');
$service->{endpoint_method}();

APIAsyncResponseService

可选服务,用于获取/转换 $_POST JSON 数据为相应的响应模型。The $s

$callback = function($post_response){ ... };
\Apruvd\V4\APIAsyncResponseService::handle($callback);
// or if you don't require a callback function, you can get the response directly in your route/controller
$response = \Apruvd\V4\APIAsyncResponseService::handle();
// or if your framework supports JSON request parsing, you can create a response model from that object/array.
$response = \Apruvd\V4\APIAsyncResponseService::transactionResponse($json_object)

身份验证

v4 API 有两种可行的身份验证模式:API 密钥和 OAuth 2.0

API 密钥

由应用设置页面生成的密钥/密钥对组成。用作基本身份验证方案。所有调用都可以使用此密钥集进行 - 使用 OAuth 2.0 流程是完全可选的

OAuth 2.0

此方法使用两个令牌,一个访问令牌和一个刷新令牌。使用您的 API 密钥,您可以获取一个刷新令牌。然后,您可以使用该令牌创建一个访问令牌,该令牌应用于请求中的身份验证。请注意,访问令牌比刷新令牌更快过期,但两者最终都会过期。

使用您的 API 密钥,您可以使用以下方法程序化地请求一个刷新令牌

$token = $service->createMerchantRefreshToken();

将返回一个令牌集,并将其自动绑定到服务类。当生成新令牌时,可以将可选的匿名回调函数传递给 API 服务作为事件处理器。

$service->onAccessTokenUpdate(function($token){ ... });

如果通过服务构造函数绑定了刷新令牌,后续的 API 调用将自动在缺少或失败尝试时请求新的访问令牌。此过程还可以通过直接调用处理。

$token = $service->createMerchantAccessToken();

建议使用 onAccessTokenUpdate 方法与您的首选存储程序配合使用,并且刷新和访问令牌可以调用并传递给服务构造函数。

模型

交易和购物车内容

有关模型详细信息,请参阅代码库和 API 文档。所有模型类型都可以接受对象/数组属性集用于构造函数注入。

$transaction = new Transaction([
    "order_id" => "Trans".time(),
    "discount_codes" => [
        new DiscountCode([
            'description' => 'description'
        ]);
    ],
    "cart_contents" => [
        new CartContent([
            'product' => new Product([
               'unique_id' => 'Product'.time(),
               'product_type' => 'Physical',
               'category' => 'Foo',
            ]),
            'quantity' => 10,
            'unit_price' => 5.21,
            'is_gift' => false,
            'gift_message' => 'gift '.time()
        ])
    ]
]);
$response = $service->createTransaction($transaction);

响应

所有响应都格式良好且经过文档记录。以下 APIModel 类属性对所有响应都可用,每个响应绑定它自己的附加属性。

  • $response->code - 整数 | HTTP 响应代码
  • $response->detail - 字符串 | 可能的 400/500 错误响应消息
  • $response->success - 布尔值 | HTTP 是否在 200 范围内
  • $response->validation_errors - 对象 | 可能的 400 验证错误响应消息。嵌套对象。
  • $response->response - Httpful\Response | Httpful 服务的完整响应。有助于调试。

API 方法和端点

readMerchants(String $id) : ReadMerchantsResponse

提交到 accounts/merchants/{$id}/ 作为 GET

createMerchantAccessToken() : CreateMerchantAccessTokenResponse

提交到 accounts/merchants/access_tokens/ 作为 POST

createMerchantRefreshToken() : CreateMerchantRefeshTokenResponse

提交到 accounts/merchants/refresh_tokens/ 作为 POST

listWebhookAPIKeys(int $page, int $page_size) : ListWebhookAPIKeysResponse

提交到 accounts/webhooks/api_keys/?page={$page}&page_size={$page_size} 作为 GET

createWebhookAPIKey(WebhookAPIKey $webhook_api_key) : UpsertWebhookAPIKeyResponse

提交到 accounts/webhooks/api_keys/ 作为 POST

readWebhookAPIKey(String $id) : ReadWebhookAPIKeyResponse

提交到 accounts/webhooks/api_keys/{$id}/ 作为 GET

updateWebhookAPIKey(String $id, WebhookAPIKey $webhook_api_key) : UpsertWebhookAPIKeyResponse

提交到 accounts/webhooks/api_keys/{$id}/ 作为 PUT

partialUpdateWebhookAPIKey(String $id, WebhookAPIKey $webhook_api_key) : UpsertWebhookAPIKeyResponse

提交到 accounts/webhooks/api_keys/{$id}/ 作为 PATCH

deleteWebhookAPIKey(String $id) : APIResponse

以DELETE方式提交至accounts/webhooks/api_keys/{$id}/

listWebhooks(int $page, int $page_size) : ListWebhooksResponse

以GET方式提交至accounts/webhooks/?page={$page}&page_size={$page_size}

createWebhook(Webhook $webhook) : UpsertWebhookResponse

以POST方式提交至accounts/webhooks/

readWebhook(String $id) : ReadWebhookResponse

以GET方式提交至accounts/webhooks/{$id}/

updateWebhook(String $id, Webhook $webhook) : UpsertWebhookResponse

以PUT方式提交至accounts/webhooks/{$id}/

partialUpdateWebhook(String $id, Webhook $webhook) : UpsertWebhookResponse

以PATCH方式提交至accounts/webhooks/{$id}/

deleteWebhook(String $id) : APIResponse

以DELETE方式提交至accounts/webhooks/{$id}/

createTransaction(Transaction $transaction) : UpsertTransactionResponse

以POST方式提交至transactions/

readOrderByID(String $id) : ReadTransactionResponse

以GET方式提交至transactions/by_order_id/{$id}/

updateOrderByID(String $id, Transaction $transaction) : UpsertTransactionResponse

以PUT方式提交至transactions/by_order_id/{$id}/

partialUpdateOrderByID(String $id, Transaction $transaction) : UpsertTransactionResponse

以PATCH方式提交至transactions/by_order_id/{$id}/

createSession(Session $session) : CreateSessionResponse

以POST方式提交至transactions/sessions/

updateSession(String $id, Session $session) : UpsertSessionResponse

以PUT方式提交至transactions/sessions/

partialUpdateSession(String $id, Session $session) : UpsertSessionResponse

以PATCH方式提交至transactions/sessions/

辅助方法

onAccessTokenUpdate(Closure $callback)

注册对Token更新事件的单一事件处理器。这映射到一个单一属性,每个事件周期只处理一个回调。

setToken(String $token)
getToken() : String
setRefreshToken(String $token)
getRefreshToken() : String