ipunkt / abobereich-api-client
abobereich API 客户端。在线和离线订阅的订阅和定期计费服务。
Requires
- guzzlehttp/guzzle: ^6.0
This package is auto-updated.
Last update: 2023-03-23 02:52:47 UTC
README
abobereich API 客户端。在线和离线订阅的订阅和定期计费服务。
配置
您需要一个指向 abobereich 服务器的基 URL 以及一个带秘密的 API 密钥。凭据可以在 abobereich 服务器上的租户对话框中进行配置。
$uri = 'https://www.abobereich.de';
$key = 'YOUR_API_KEY_HERE';
$secret = 'YOUR_API_SECRET_HERE';
$client = new \Abobereich\ApiClient\Client($uri, $key, $secret);
上下文
API 客户端有不同的上下文,因此您可以将 API 部分分割成单独的上下文。
租户
$client->tenants(); // the tenants context
获取我的租户
/** @var \Abobereich\ApiClient\Models\Tenant $tenant */
$tenant = $client->tenants()->me();
账户
$client->accounts(); // the accounts context
获取所有账户
/** @var array|\Abobereich\ApiClient\Models\Account[] $accounts */
$accounts = $client->accounts()->all();
通过 ID 获取账户
/** @var \Abobereich\ApiClient\Models\Account $account */
$account = $client->accounts()->find($id);
查找账户
您有多种方式可以查找账户。以下是有效示例
/** @var \Abobereich\ApiClient\Models\Account $account */
$account = $client->accounts()->findById(1234);
$account = $client->accounts()->findByIdentifier('YOUR_ACCOUNT_IDENTIFIER');
$account = $client->accounts()->findByEmail('name@mail.com');
$account = $client->accounts()->findByName('John Doe');
不建议使用 findByName
方法来查找单个账户。但对于测试数据来说也是有用的。
创建账户
首先,您必须创建一个模型实例并设置值。所有这些都是可选的,因此您可以创建一个空账户以保持您的账户数据私密。在这种情况下,您必须将 API 返回的 id 存储起来,以便稍后在订阅处理中重新识别此账户。
$account = new \Abobereich\ApiClient\Models\Account();
$account->setName('John Doe')
->setExternalIdentifier('123456-AbcD');// here you can set the account identification from your system
try {
/** @var \Abobereich\ApiClient\Models\Account $account */
$account = $client->accounts()->store($account);
echo 'Model created with ID: ' . $account->getId() . PHP_EOL;
} catch (\Abobereich\ApiClient\Exceptions\InvalidRequestDataException $e) {
echo $e->getMessage() . ': ' . implode(', ', $e->getErrors());
} catch (\Abobereich\ApiClient\Exceptions\ModelNotCreatedException $e) {
echo 'Model NOT CREATED, Error: ' . $e->getMessage() . PHP_EOL;
}
更新账户
通常,您已经通过 all()
加载了账户,或者创建账户实例后存储在您的数据库中。
// $account already loaded (IMPORTANT: getId() returns the correct id)
$account->setName('John Doe')
->setData('city', 'Los Angeles');
try {
$account = $client->accounts()->update($account);
echo 'Model updated with ID: ' . $account->getId() . PHP_EOL;
} catch (\Abobereich\ApiClient\Exceptions\InvalidRequestDataException $e) {
echo $e->getMessage() . ': ' . implode(', ', $e->getErrors());
} catch (\Abobereich\ApiClient\Exceptions\ModelNotUpdatedException $e) {
echo 'Model NOT UPDATED, Error: ' . $e->getMessage() . PHP_EOL;
}
产品
$client->products(); // the products context
获取所有产品
/** @var array|\Abobereich\ApiClient\Models\Product[] $products */
$products = $client->products()->all();
通过 ID 获取产品
/** @var \Abobereich\ApiClient\Models\Product $product */
$product = $client->products()->find($id);
查找产品
您有多种方式可以查找产品。以下是有效示例
/** @var \Abobereich\ApiClient\Models\Product $product */
$product = $client->products()->findById(1234);
$product = $client->products()->findBySlug('product-slug');
$product = $client->products()->findByName('John Doe');
不建议使用 findByName
方法来查找单个产品。但对于测试数据来说也是有用的。
计划
计划始终取决于产品。因此,您必须为计划上下文设置产品。
$client->plans($product); // the plans context
// or later on
$plansContext->setProduct($product); // set another product for the plans context
获取产品的所有计划
/** @var array|\Abobereich\ApiClient\Models\Plan[] $plans */
$plans = $client->plans($product)->all();
获取具有一个或多个关联标签的产品所有计划
/** @var array|\Abobereich\ApiClient\Models\Plan[] $plans */
$plans = $client->plans($product)->allByTag('TAG');
获取一个产品的计划
/** @var array|\Abobereich\ApiClient\Models\Plan $plan */
$plan = $client->plans($product)->find($id);
查找一个产品的计划
您有多种方式查找计划。以下是有效示例:
/** @var array|\Abobereich\ApiClient\Models\Plan $plan */
$plan = $client->plans($product)->findById($id);
$plan = $client->plans($product)->findByIdentifier('YOUR_ACCOUNT_IDENTIFIER');
$plan = $client->plans($product)->findBySlug('plan-slug');
$plan = $client->plans($product)->findByName('PLAN 123');
不建议使用findByName
方法来查找单个计划。但对于测试数据来说也很有用。
订阅
订阅是账户可以拥有的合同。拥有合同(订阅)和成为订阅福利的受益人之间存在区别。
获取所有订阅(未过滤)
/** @var array|\Abobereich\ApiClient\Models\Subscription[] $subscriptions */
$subscriptions = $client->subscriptions()->all();
获取产品的所有订阅
$product = 1;// or @var \Abobereich\ApiClient\Models\Product $product
/** @var array|\Abobereich\ApiClient\Models\Subscription[] $subscriptions */
$subscriptions = $client->subscriptions()->allForProduct($product);
获取账户(承包商)的所有订阅
$account = 1;// or @var \Abobereich\ApiClient\Models\Account $account
/** @var array|\Abobereich\ApiClient\Models\Subscription[] $subscriptions */
$subscriptions = $client->subscriptions()->allForAccount($account);
获取订阅受益人账户的所有订阅
$account = 1;// or @var \Abobereich\ApiClient\Models\Account $account
/** @var array|\Abobereich\ApiClient\Models\Subscription[] $subscriptions */
$subscriptions = $client->subscriptions()->allForBeingSubscriber($account);
获取订阅
/** @var array|\Abobereich\ApiClient\Models\Subscription $subscription */
$subscription = $client->subscriptions()->find($id);
通过其他属性查找订阅
您有多种方式查找订阅。以下是有效示例:
/** @var array|\Abobereich\ApiClient\Models\Subscription $subscription */
$subscription = $client->subscriptions()->findById($id);
$subscription = $client->subscriptions()->findByNumber('PQXNC-KWFXO-JVUYO-03642');
$subscription = $client->subscriptions()->findByIdentifier('EXTERNAL_Identifier-FOR-subscRiptIOn);
创建新的订阅
您必须通过API创建和存储订阅模型。
$account = $client->accounts()->findByEmail('john@doe.com');
$plan = $client->plans($product)->findBySlug($slug);
$subscription = new \Abobereich\ApiClient\Models\Subscription();
$subscription->setAccountId($account)
->setSubscriptionNumber('1234-ABO-YXCE')
->setPlanId($plan);
$subscription = $client->subscriptions()->save($subscription); // or $client->subscriptions()->store($subscription);
更新现有订阅
您必须通过API获取存储的订阅模型。
$subscription = $client->subscriptions()->findByNumber('1234-ABO-YXCE);
$subscription->setNextBillingDate(date('Y-m-d H:i:s'));
$subscription = $client->subscriptions()->save($subscription); // or $client->subscriptions()->update($subscription);
订阅者
每个订阅可以有一个或多个订阅者。您可以在计划的订阅者数量中定义具体数量。订阅者消费订阅的福利。它不必是订阅的承包商,但它可以是。例如,您收取家庭订阅合同,承包商是您,但订阅者是您的家庭成员。
订阅者始终依赖于订阅。因此,您必须为订阅者的上下文设置订阅。
$client->subscribers($subscription); // the subscribers context
// or later on
$subscriptionsContext->setSubscription($subscription); // set another subscription for the subscribers context
获取订阅的所有订阅者
/** @var array|\Abobereich\ApiClient\Models\Subscriber[] $subscribers */
$subscribers = $client->subscribers($subscription)->all();
获取订阅的订阅者
/** @var array|\Abobereich\ApiClient\Models\Subscriber $subscriber */
$subscriber = $client->subscribers($subscription)->find($id);
创建订阅者
首先,您需要创建一个模型实例并设置值。订阅者有一个账户和订阅的连接。订阅已通过订阅者上下文设置,但相关的账户缺失。
$account = $client->accounts()->find(1);
$subscriber = new \Abobereich\ApiClient\Models\Subscriber();
$subscriber->setAccountId($account);
try {
$subscriber = $client->subscribers($subscription)->store($subscriber);
echo 'Model created with ID: ' . $subscriber->getId() . PHP_EOL;
} catch (\Abobereich\ApiClient\Exceptions\InvalidRequestDataException $e) {
echo $e->getMessage() . ': ' . implode(', ', $e->getErrors());
} catch (\Abobereich\ApiClient\Exceptions\ModelNotCreatedException $e) {
echo 'Model NOT CREATED, Error: ' . $e->getMessage() . PHP_EOL;
}
块
非常实验性!!!
块API用于营销用例。块是文本或HTML块。它与产品相关联,其值取决于计划。因此,您可以使您的价格表动态化。您可以在abobereich创建无限数量的文本块。所有块都可以有语言。对于前端渲染,只需检索所有块并显示它们。您的销售和营销团队可以在abobereich中操作它们,因此您可以在不进行任何前端渲染的情况下进行A/B测试。在abobereich中维护块。
块上下文始终与产品相关联,因此请设置它。
$client->blocks($product); // the blocks context
// or later on
$blocksContext->setProduct($product); // set another product for the blocks context
获取产品的所有块
/** @var array $blocks */
$blocks = $client->blocks($product)->all();
获取一个或多个语言的产品的所有块
/** @var array $blocks */
$blocks = $client->blocks($product)->allWithLanguage('en');
/** @var array $blocks */
$blocks = $client->blocks($product)->allWithLanguage(['en', 'de', 'fr']);
结果块数组
结果块数组具有以下结构
{
"PLAN-SLUG": {
"BLOCK-IDENTIFIER": {
"LANGUAGE-CODE": "CONTENT-IN-LANGUAGE"
}
}
}
所有语言中立的块都将设置在所有返回的语言中。因此,您不需要检查数组设置。每个语言中的每个块都将包含在结果中。空值将为空。
示例
{
"super-plan": {
"teaser_image": {
"de": "teaser.png",
"en": "teaser.png",
},
"subscription_headline": {
"de": "Super Plan - Das musst du haben...",
"en": "All you need is SUPER PLAN!",
}
}
}