ecard / cms
A package containing features used by Ecard CMS modules
Requires
- php: ^5.6.0 || ^7.0.0 || ^8.0.0
- ext-curl: *
- ext-fileinfo: *
- ext-json: *
- ext-mbstring: *
- ext-zip: *
- apimatic/unirest-php: ^3.0.1
- fig/http-message-util: ^1.1.5
- giggsey/libphonenumber-for-php: ^8.13.46
- lcobucci/jwt: ^3.0.0 || ^4.0.0 || ^5.0.0
- symfony/translation: ^3.4.0 || ^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0 || ^8.0.0
Requires (Dev)
- yoast/phpunit-polyfills: ^2.0.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
ecard/cms is the composer package containing features used by Ecard CMS modules.
This package is available at https://packagist.org/packages/ecard/cms.
Operators
The operators (names, colors, legal mentions) are defined in the operator section of etc/ecard/config.json.
$app->operator is the operator given by the operator parameter of App::get() (one of the operator.enabled
keys, case sensitive), or the default operator (operator.default) without it (or when it is null):
$app = App::get(['locale' => 'fr_FR', 'operator' => 'cacf', 'cms' => [/* ... */]]); $app->operator->getKey(); // 'cacf' $app->operator->getNameUpper(); // 'PLEDGBYSOFINCO' $app->operator->getNameLower(); // 'pledgbysofinco' $app->operator->getNameCamel(); // 'PledgBySofinco' $app->operator->getNameFront(); // 'Sofinco' $app->operator->getPrimaryColor(); // '007D8F' (without '#') $app->operator->getSecondaryColor(); // '003D37' $app->operator->getLegalMentions(); // 'SOFINCO est une marque de CA Consumer Finance, ...'
An operator can also be built alone: new Ecard\Cms\App\Model\Operator('cacf') (it loads the app config),
or new Operator('cacf', $app->config), and new Operator() builds the default operator.
An operator key which is not an enabled operator throws an OperatorException (App::get() too).
Payment schedules
frontPaymentSchedule() (no authentication) and backPaymentSchedule() (secured, see below) simulate
payment schedules. The uid prefix selects the simulated merchants:
mer_: the merchant (the returned body holds its schedule),cmp_: all the eligible merchants of the company,mgrp_: all the eligible merchants of the merchant group.
For a company or a merchant group, the returned body is paginated (items, page_number, total_items,
total_pages, has_prev, has_next): the optional $page and $perPage parameters are only allowed with
these uids.
$app->api->frontPaymentSchedule('prod', $merchantUid, 25000); $app->api->frontPaymentSchedule('prod', $companyUid, 25000, 2, 50); // second page of 50 merchants
Secured api calls
The secured api calls (backPaymentSchedule(), refund()) handle their access token by themselves,
with the api credentials of the mode given by $app->credentials_provider:
// the uid prefix selects the endpoint: merchant (mer_), company (cmp_) or merchant group (mgrp_) $app->api->backPaymentSchedule('prod', $merchantUid, 25000); $app->api->backPaymentSchedule('prod', $companyUid, 25000, 1, 50); // paginated $app->api->refund('prod', $purchaseUid, 500);
- the access token is requested with
token(), then reused from a cache until it expires (60 seconds before its expiration, seeaccess_token.expiry_margininetc/api/config.json), seeaccessToken(), - when a call is rejected with a http 401 (e.g. a token revoked before its expiration), the cached token is forgotten, and the call is retried once with a new one,
- they throw an
ApiException:MSG_MISSING_CREDENTIALS_API_KEY/MSG_MISSING_CREDENTIALS_API_SECRETwhen the credentials provider has no api key / api secret for the mode,MSG_ACCESS_TOKEN_NOT_DELIVERED(whose code is the http code) when the api does not deliver a token, e.g. with invalid credentials.
Refund via the dashboard credentials
refundViaDashboard() calls the same refund endpoint as refund(), but authenticated with the merchant dashboard
credentials of the mode given by $app->credentials_provider
(basic authentication),
without any access token:
$app->api->refundViaDashboard('prod', $purchaseUid, 500);
It throws an ApiException (MSG_MISSING_CREDENTIALS_DASHBOARD_USERNAME / MSG_MISSING_CREDENTIALS_DASHBOARD_PASSWORD)
when the credentials provider has no dashboard username / dashboard password for the mode. Invalid dashboard credentials
are not retried: the response of the api is returned.
Credentials
$app->credentials_provider gives the credentials of each mode: the api credentials (api key, api secret) and the
dashboard credentials (dashboard username, dashboard password). By default, they are read from environment variables
(EnvCredentialsProvider):
| mode | api key | api secret | dashboard username | dashboard password |
|---|---|---|---|---|
staging |
PLEDG_STAGING_API_KEY |
PLEDG_STAGING_API_SECRET |
PLEDG_STAGING_DASHBOARD_USERNAME |
PLEDG_STAGING_DASHBOARD_PASSWORD |
prod |
PLEDG_PRODUCTION_API_KEY |
PLEDG_PRODUCTION_API_SECRET |
PLEDG_PRODUCTION_DASHBOARD_USERNAME |
PLEDG_PRODUCTION_DASHBOARD_PASSWORD |
To read them from the cms database, implement Ecard\Cms\App\Component\Api\CredentialsProviderInterface
(each method returns null when the credential is not configured), and give it in the cms.components
parameter of App::get() (see Cms components):
use Ecard\Cms\App\Component\Api\CredentialsProviderInterface; final class MyCmsCredentialsProvider implements CredentialsProviderInterface { public function getApiKey($mode) { return my_cms_config_get('pledg_' . $mode . '_api_key'); // e.g. get_option() with WordPress } public function getApiSecret($mode) { return my_cms_config_get('pledg_' . $mode . '_api_secret'); } public function getDashboardUsername($mode) { return my_cms_config_get('pledg_' . $mode . '_dashboard_username'); } public function getDashboardPassword($mode) { return my_cms_config_get('pledg_' . $mode . '_dashboard_password'); } } $params['cms']['components']['credentials_provider'] = new MyCmsCredentialsProvider(); $app = App::get($params);
Access tokens cache
By default, the tokens are cached in memory, for the current php request only (MemoryTokenStorage).
They are protected against accidental disclosure: kept masked in memory (with a random per-process key),
shown as *** by var_dump() and print_r(), and never serialized. To share them between
requests, implement Ecard\Cms\App\Component\Api\TokenStorageInterface with the cache of the cms,
and give it in the cms.components parameter of App::get() (see Cms components):
use Ecard\Cms\App\Component\Api\TokenStorageInterface; final class MyCmsTokenStorage implements TokenStorageInterface { public function get($key) { $stored = my_cms_cache_get($key); // e.g. get_transient($key) with WordPress return is_array($stored) ? $stored : null; } public function set($key, $token, $expiresAt) { my_cms_cache_set($key, ['token' => $token, 'expires_at' => $expiresAt], $expiresAt - time()); } public function delete($key) { my_cms_cache_delete($key); } } $params['cms']['components']['token_storage'] = new MyCmsTokenStorage(); $app = App::get($params);
The storage keys are hashes: they never hold the api credentials. But a stored token is a secret, valid for several minutes: keep it on the server side only (never in a cookie, a page, or a log).
Cms components
The cms.components parameter of App::get() holds the objects provided by the cms module (each one is optional):
$app = App::get([ 'locale' => 'fr_FR', 'cms' => [ 'name' => 'prestashop', 'version' => '1.7.8.1', 'components' => [ 'credentials_provider' => new MyCmsCredentialsProvider(), // CredentialsProviderInterface 'token_storage' => new MyCmsTokenStorage(), // TokenStorageInterface ], ], ]);
- each given component replaces the current one (
$app->credentials_provider,$app->token_storage), - a
nullcomponent is replaced by a new default one (EnvCredentialsProvider,MemoryTokenStorage), - a missing component keeps the current one, given by a previous
App::get()call (the app is a singleton): a cms serving several shops (e.g. shopify) must give the credentials provider of the current shop to eachApp::get()call, - they are not kept in
$app->parameters, - an invalid value (an object which does not implement the expected interface, a string...) or an unknown
component name throws an
AppException, without any change of the current components.
Security
The api key, the api secret, the dashboard password, the access tokens and the jwt secret keys must never be logged:
- Never log the response of
$app->api->token(): itsbodyandraw_bodyhold the access token. - Use
accessToken()(see above, used by the secured api calls) rather thantoken(): it reuses an access token until it expires, instead of requesting a new one before each api call. - The exceptions thrown by the api calls (e.g. on a timeout) hold the arguments of the called functions in their
stack trace, which is often logged. Since php 8.2, the parameters holding these secrets are hidden from the traces
(
SensitiveParameterattribute). With php < 8.2, the first 15 characters of each string argument are visible: setzend.exception_ignore_args = Onin the php.ini of the server (default value ofphp.ini-productionsince php 7.4) to remove all arguments from the traces.