apruvd / php_sdk_v3
Apruvd API V3版本的轻量级PHP SDK。
Requires
- nategood/httpful: ^0.2.20
This package is auto-updated.
Last update: 2024-09-19 12:00:45 UTC
README
轻量级PHP SDK,用于集成Apruvd API V3(即将成为遗留版本)。
API文档在此处找到。 https://app.apruvd.com/documentation/
服务
APIService
主要服务,包含对可用API端点的1:1方法映射。
$service = new APIService('secret_id', 'secret_key', 'optional_refresh_token', 'optional_access_token');
$service->{endpoint_method}();
APIAsyncResponseService
可选服务,用于从$_POST JSON数据中提取/转换到适当的响应模型。 $s
$callback = function($post_response){ ... };
\Apruvd\V3\APIAsyncResponseService::handle($callback);
// or if you don't require a callback function, you can get the response directly in your route/controller
$response = \Apruvd\V3\APIAsyncResponseService::handle();
// or if your framework supports JSON request parsing, you can create a response model from that object/array.
$response = \Apruvd\V3\APIAsyncResponseService::transactionResponse($json_object)
身份验证
v3 API有两种可行的身份验证模式:密钥ID/密钥和刷新/访问令牌。
密钥ID/密钥
此密钥对在应用程序设置页面生成,用作基本身份验证方案。所有调用都可以使用此密钥集进行。刷新/访问令牌身份验证方法是完全可选的
刷新/访问令牌
使用您的密钥ID/密钥,您可以使用以下方法请求刷新令牌
$token = $service->authenticateWithOAuth();
将返回一个令牌集,并自动绑定到服务类。当生成新令牌时,可以传递一个可选的匿名回调函数作为事件处理程序。
$service->onAccessTokenUpdate(function($token){ ... });
如果通过服务构造函数绑定了刷新令牌,后续的API调用将自动请求新的访问令牌,如果缺失或失败。此过程还可以通过直接调用处理。
$token = $service->authenticateWithOAuthRefresh();
建议使用onAccessTokenUpdate方法与您的首选存储例程一起使用,并且刷新和访问令牌可以通过服务构造函数回忆和传递。
模型
交易和购物车内容
有关模型详细信息,请参阅代码库和API文档
$transaction = new Transaction([
'mode' => 'Live',
'total' => 123.45,
'order_num' => '12345',
]);
$transaction->cart_contents[] = new CartContents(['item' => 'Foo', 'quantity' => 25, 'is_high_risk' => false]);
$response = $service->submitTransaction($transaction);
响应
所有响应都格式良好且有文档记录。以下APIModel类的属性对所有响应都可用,每个响应绑定自己的附加属性。
$response->code- 整数 | HTTP响应代码$response->detail- 字符串 | 可能的400/500错误响应消息$response->success- 布尔值 | HTTP是否在200范围内$response->validation_errors- 对象 | 可能的400验证错误响应消息。嵌套对象。$response->response- Httpful\Response | Httpful服务的完整响应。对调试很有用。
API方法和端点
submitTransaction(Transaction $transaction) : SubmitTransactionResponse
提交到 api/transactions/submit/ 作为POST
checkTransaction(String $transaction_id) : CheckTransactionResponse
提交到 api/transactions/status/?transaction_id={$transaction_id} 作为GET
updateTransaction(String $transaction_id, Transaction $transaction) : UpdateTransactionResponse
提交到 api/transactions/{$transaction_id}/update/ 作为POST
cancelTransaction(String $transaction_id) : CancelTransactionResponse
提交到 api/transactions/{$transaction_id}/cancel/ 作为POST
elevateTransaction($transaction_id, $is_order_number = false) : ElevateTransactionResponse
提交到 api/transactions/elevate/ 作为POST。交易ID或订单号可以使用,具体取决于布尔标志。
authenticateWithOAuth() : OAuthResponse
提交到 o/token/?grant_type=password 作为GET。
authenticateWithOAuthRefresh() : OAuthResponse
提交到 o/token/?grant_type=refresh 作为GET。
辅助方法
onAccessTokenUpdate(Closure $callback)
注册单个事件处理程序以处理Token更新事件。这对应于一个单个属性,每个事件周期只有一个回调。