esempla / govtech-api
Esempla GovTech API 的绑定
1.1.5
2020-01-21 14:46 UTC
Requires
- php: >=5.4.0
- lcobucci/jwt: ^3.3
- yiisoft/yii2-httpclient: ^2.0
This package is auto-updated.
Last update: 2024-09-17 23:57:52 UTC
README
先决条件
本项目包含 Esempla GovTech API 的 PHP 绑定,一组与业务相关的工具。完整的 API 文档可以在这里找到
请注意,这些绑定的当前状态可能落后于 API 状态。在这种情况下,请随时向我们发送相应的 issue
要求
- PHP 版本 >=5.4.0 (>=7.0.0)
yii2-httpclient
版本 ^2.0lcobucci/jwt
版本 ^3.3
在未来的版本中,可以通过替换 http 客户端来移除对
yii2
生态系统的依赖
示例
首先,您需要设置一个带有 EsemplaGovTechConfig
实例的 params
项
<?php
use Esempla\GovTech\EsemplaGovTechConfig;
const CONFIG_CREDENTIALS = 'some_config_key';
$config = new EsemplaGovTechConfig();
$config->ClientID('your_client_id');
$config->ClientSecret('your_client_secret');
$config->Sandbox(true); // by default, `sandbox` is set to false, so all API calls lead to production API installation
return array(
CONFIG_CREDENTIALS => $config
);
授权
<?php
use Esempla\GovTech\EsemplaGovTechSSO\EsemplaGovTechSSO;
$sso = new EsemplaGovTechSSO(CONFIG_CREDENTIALS);
if (!$sso->Login()) {
// some failure login handling
}
else {
$token = $sso->EsemplaGovTechProviderToken();
// variable $token now contains the obtained authorization token
// it is automatically cached in any `IEsemplaGovTechProvider`, but you would like to store it to use in other requests
}
授权令牌重用
首先,让我们引入一些样板代码
<?php
use Esempla\GovTech\EsemplaGovTechSSO\EsemplaGovTechSSO;
use Esempla\GovTech\EsemplaGovTechInvoicing\EsemplaGovTechInvoicing;
use Esempla\GovTech\EsemplaGovTechInvoicing\EsemplaGovTechInvoicingDTO;
$authorization = '<stored-authorization-token-value>';
有几种重用授权令牌的方式。让我们通过以下示例来理解它们
<?php
/**
* @var EsemplaGovTechInvoicing $invoicing
*/
$invoicing = EsemplaGovTechSSO::Authorize(CONFIG_CREDENTIALS, $authorization, new EsemplaGovTechInvoicing());
if ($invoicing == null) {
// some failure token refreshing handling
exit(); // or `return` if in function/method
}
或者
<?php
$sso = new EsemplaGovTechSSO(CONFIG_CREDENTIALS);
$sso->EsemplaGovTechProviderToken($authorization);
if ($sso->AuthorizationNeed() && !$sso->Login()) {
// some failure token refreshing handling
exit(); // or `return` if in function/method
}
/**
* @var EsemplaGovTechInvoicing $invoicing
*/
$invoicing = $sso->ContinueWith(new EsemplaGovTechInvoicing());
或者
<?php
$invoicing = new EsemplaGovTechInvoicing(CONFIG_CREDENTIALS);
$invoicing->EsemplaGovTechProviderToken($authorization);
if ($invoicing->AuthorizationNeed()) {
$sso = new EsemplaGovTechSSO(CONFIG_CREDENTIALS);
if (!$sso->Login()) {
// some failure token refreshing handling
exit(); // or `return` if in function/method
}
$invoicing->EsemplaGovTechProviderToken($sso->EsemplaGovTechProviderToken());
}
现在,我们可以进行一些 API 调用,例如
<?php
$dto = new EsemplaGovTechInvoicingDTO();
$dto->Sender('<IDNP>');
$dto->Service('<serviceID>'); // this should be equal to identifier of invoicing service, that is provided for your project
$dto->Amount(9.99); // optional (see corresponding API documentation) parameter
$invoice = $invoicing->InvoiceCreate($dto);
if ($invoice == null) {
// some failure invoice creation handling
exit(); // or `return` if in function/method
}
$invoice_id = $invoice->ID(); // returned invoice identifier
var_dump($invoice_id);
测试
您可以通过在 php
命令中输入测试文件的名称来运行测试(位于 ./tests
),以测试现有功能。例如
$ php ./tests/SSO/TestCheckToken.php