Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create endpoint and formatter for gather cms data #142

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 61 additions & 43 deletions src/Endpoints/Merchants.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,58 +27,76 @@

use Alma\API\Entities\FeePlan;
use Alma\API\Entities\Merchant;
use Alma\API\Entities\MerchantData\MerchantData;
use Alma\API\Exceptions\RequestException;
use Alma\API\RequestError;

class Merchants extends Base
{
const MERCHANTS_PATH = '/v1/merchants';
const ME_PATH = '/v1/me';
const MERCHANTS_PATH = '/v1/merchants';
joyet-simon marked this conversation as resolved.
Show resolved Hide resolved
const ME_PATH = '/v1/me';

/**
* @return Merchant
* @throws RequestError
*/
public function me()
{
$res = $this->request(self::ME_PATH . '/extended-data')->get();
/**
* @return Merchant
* @throws RequestError
*/
public function me()
{
$res = $this->request(self::ME_PATH . '/extended-data')->get();

if ($res->isError()) {
throw new RequestError($res->errorMessage, null, $res);
}
if ($res->isError()) {
throw new RequestError($res->errorMessage, null, $res);
}

return new Merchant($res->json);
}
return new Merchant($res->json);
}

/**
* @param $kind string Either FeePlan::KIND_GENERAL or FeePlan::KIND_POS. The former is applied to online payments,
* while the latter will be used when creating a Payment with origin=pos_* for
* retail/point-of-sale use cases. Defaults to FeePlan::KIND_GENERAL
* @param string|int[] $installmentsCounts Only include fee plans that match the given installments counts, or use
* the string "all" (default) to get all available fee plans
* @param bool $includeDeferred Include deferred fee plans (i.e. Pay Later plans) in the response
* @return FeePlan[] An array of available fee plans (some might be disabled, check FeePlan->allowed for each)
* @throws RequestError
*/
public function feePlans($kind = FeePlan::KIND_GENERAL, $installmentsCounts = "all", $includeDeferred = false)
{
if (is_array($installmentsCounts)) {
$only = implode(",", $installmentsCounts);
} else {
$only = $installmentsCounts;
}
/**
* @param $kind string Either FeePlan::KIND_GENERAL or FeePlan::KIND_POS. The former is applied to online payments,
* while the latter will be used when creating a Payment with origin=pos_* for
* retail/point-of-sale use cases. Defaults to FeePlan::KIND_GENERAL
* @param string|int[] $installmentsCounts Only include fee plans that match the given installments counts, or use
* the string "all" (default) to get all available fee plans
* @param bool $includeDeferred Include deferred fee plans (i.e. Pay Later plans) in the response
* @return FeePlan[] An array of available fee plans (some might be disabled, check FeePlan->allowed for each)
* @throws RequestError
*/
public function feePlans($kind = FeePlan::KIND_GENERAL, $installmentsCounts = "all", $includeDeferred = false)
{
if (is_array($installmentsCounts)) {
$only = implode(",", $installmentsCounts);
} else {
$only = $installmentsCounts;
}

$res = $this->request(self::ME_PATH . "/fee-plans")->setQueryParams(array(
"kind" => $kind,
"only" => $only,
"deferred" => $includeDeferred ? "true" : "false" // Avoid conversion to "0"/"1" our API doesn't recognize
))->get();
$res = $this->request(self::ME_PATH . "/fee-plans")->setQueryParams(array(
"kind" => $kind,
"only" => $only,
"deferred" => $includeDeferred ? "true" : "false" // Avoid conversion to "0"/"1" our API doesn't recognize
))->get();

if ($res->isError()) {
throw new RequestError($res->errorMessage, null, $res);
}
if ($res->isError()) {
throw new RequestError($res->errorMessage, null, $res);
}

return array_map(function ($val) {
return new FeePlan($val);
}, $res->json);
}
return array_map(function ($val) {
return new FeePlan($val);
}, $res->json);
}

/**
* @param string $url The URL to send to Alma for integrations configuration
* @throws RequestException
* @throws RequestError
*/
public function sendIntegrationsConfigurationsUrl($url)
{
$res = $this->request(self::ME_PATH . "/integrations/configurations")->setRequestBody(array(
"endpoint_url" => $url
))->post();

if ($res->isError()) {
throw new RequestException($res->errorMessage, null, $res);
}
}
}
120 changes: 120 additions & 0 deletions src/Entities/MerchantData/CmsFeatures.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

namespace Alma\API\Entities\MerchantData;

class CmsFeatures
{
/**
* @var bool | null
*/
private $almaEnabled;

/**
* @var bool | null
*/
private $widgetCartActivated;

/**
* @var bool | null
*/
private $widgetProductActivated;

/**
* @var mixed
joyet-simon marked this conversation as resolved.
Show resolved Hide resolved
*/
private $usedFeePlans;

/**
* @var int | null
*/
private $paymentMethodPosition;

/**
* @var bool | null
*/
private $inPageActivated;

/**
* @var bool | null
*/
private $logActivated;

/**
* @var string[]
*/
private $excludedCategories;

/**
* @var bool | null
*/
private $excludedCategoriesActivated;

/**
* @var array<array{name: string}>
*/
private $specificFeatures;

/**
* @var string[]
*/
private $countryRestriction;

/**
* @var bool | null
*/
private $isMultisite;

/**
* @var bool | null
*/
private $customWidgetCss;

/**
* CmsFeatures constructor.
* @param array $cmsFeaturesDataArray
*/
public function __construct($cmsFeaturesDataArray)
{
// Ensure values are properly initialized
$this->almaEnabled = isset($cmsFeaturesDataArray['alma_enabled']) ? $cmsFeaturesDataArray['alma_enabled'] : null;
$this->widgetCartActivated = isset($cmsFeaturesDataArray['widget_cart_activated']) ? $cmsFeaturesDataArray['widget_cart_activated'] : null;
$this->widgetProductActivated = isset($cmsFeaturesDataArray['widget_product_activated']) ? $cmsFeaturesDataArray['widget_product_activated'] : null;
$this->usedFeePlans = $cmsFeaturesDataArray['used_fee_plans'] ?: '';
joyet-simon marked this conversation as resolved.
Show resolved Hide resolved
$this->inPageActivated = isset($cmsFeaturesDataArray['in_page_activated']) ? $cmsFeaturesDataArray['in_page_activated'] : null;
$this->logActivated = isset($cmsFeaturesDataArray['log_activated']) ? $cmsFeaturesDataArray['log_activated'] : null;
$this->excludedCategories = $cmsFeaturesDataArray['excluded_categories'] ?: [];
$this->excludedCategoriesActivated = isset($cmsFeaturesDataArray['excluded_categories_activated']) ?
$cmsFeaturesDataArray['excluded_categories_activated'] : null;
$this->paymentMethodPosition = $cmsFeaturesDataArray['payment_method_position'] ?: null;
$this->specificFeatures = $cmsFeaturesDataArray['specific_features'] ?: [];
$this->countryRestriction = $cmsFeaturesDataArray['country_restriction'] ?: [];
$this->isMultisite = isset($cmsFeaturesDataArray['is_multisite']) ? $cmsFeaturesDataArray['is_multisite'] : null;
$this->customWidgetCss = isset($cmsFeaturesDataArray['custom_widget_css']) ? $cmsFeaturesDataArray['custom_widget_css'] : null;
}

/**
* @return array
*/
public function getProperties()
{
// Use array_filter with ARRAY_FILTER_USE_BOTH to remove null or empty values
return array_filter([
'alma_enabled' => $this->almaEnabled,
'widget_cart_activated' => $this->widgetCartActivated,
'widget_product_activated' => $this->widgetProductActivated,
'used_fee_plans' => $this->usedFeePlans,
'in_page_activated' => $this->inPageActivated,
'log_activated' => $this->logActivated,
'excluded_categories' => $this->excludedCategories,
'excluded_categories_activated' => $this->excludedCategoriesActivated,
'payment_method_position' => $this->paymentMethodPosition,
'specific_features' => $this->specificFeatures,
'country_restriction' => $this->countryRestriction,
'is_multisite' => $this->isMultisite,
'custom_widget_css' => $this->customWidgetCss,
], function($value) {
// Keep only values that are not null and not empty
return !is_null($value) && $value !== '';
});
}
}
91 changes: 91 additions & 0 deletions src/Entities/MerchantData/CmsInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Alma\API\Entities\MerchantData;

class CmsInfo
{
/**
* @var string|null
*/
private $cmsName;

/**
* @var string|null
*/
private $cmsVersion;

/**
* @var array<array{name: string, version: string}>
*/
private $thirdPartiesPlugins;

/**
* @var array<array{name: string, version: string}>
*/
private $themes;

/**
* @var string|null
*/
private $languageName;

/**
* @var string|null
*/
private $languageVersion;

/**
* @var string|null
*/
private $almaPluginVersion;

/**
* @var string|null
*/
private $almaSdkVersion;

/**
* @var string|null
*/
private $almaSdkName;

/**
* CmsInfo constructor.
* @param array $cmsInfoDataArray
*/
public function __construct($cmsInfoDataArray)
{
// Initialize values or set them to null if not available
$this->cmsName = $cmsInfoDataArray['cms_name'] ?: null;
joyet-simon marked this conversation as resolved.
Show resolved Hide resolved
$this->cmsVersion = $cmsInfoDataArray['cms_version'] ?: null;
$this->thirdPartiesPlugins = $cmsInfoDataArray['third_parties_plugins'] ?: [];
$this->themes = $cmsInfoDataArray['themes'] ?: [];
$this->languageName = $cmsInfoDataArray['language_name'] ?: null;
$this->languageVersion = $cmsInfoDataArray['language_version'] ?: null;
$this->almaPluginVersion = $cmsInfoDataArray['alma_plugin_version'] ?: null;
$this->almaSdkVersion = $cmsInfoDataArray['alma_sdk_version'] ?: null;
$this->almaSdkName = $cmsInfoDataArray['alma_sdk_name'] ?: null;
}

/**
* @return array
*/
public function getProperties()
{
// Use array_filter with ARRAY_FILTER_USE_BOTH to remove null or empty values
return array_filter([
'cms_name' => $this->cmsName,
'cms_version' => $this->cmsVersion,
'third_parties_plugins' => $this->thirdPartiesPlugins,
'themes' => $this->themes,
'language_name' => $this->languageName,
'language_version' => $this->languageVersion,
'alma_plugin_version' => $this->almaPluginVersion,
'alma_sdk_version' => $this->almaSdkVersion,
'alma_sdk_name' => $this->almaSdkName,
], function($value) {
// Keep only values that are not null and not empty
return !is_null($value) && $value !== '';
});
}
}
25 changes: 25 additions & 0 deletions src/Lib/PayloadFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Alma\API\Lib;

use Alma\API\Entities\MerchantData\CmsFeatures;
use Alma\API\Entities\MerchantData\CmsInfo;

class PayloadFormatter
{
/**
* @param CmsInfo $cmsInfo
* @param CmsFeatures $cmsFeatures
* @return string
*/
public static function formatIntegrationConfigurationPayload(CmsInfo $cmsInfo, CmsFeatures $cmsFeatures)
joyet-simon marked this conversation as resolved.
Show resolved Hide resolved
joyet-simon marked this conversation as resolved.
Show resolved Hide resolved
{
$payload = [
"cms_info" => $cmsInfo->getProperties(),
"cms_features" => $cmsFeatures->getProperties(),
];

return json_encode($payload);
}

}
Loading