From 9657409cd8bee0d223720f3edf6a5352d97acfde Mon Sep 17 00:00:00 2001 From: TakeshiDaveau Date: Fri, 17 Jan 2025 09:49:11 +0100 Subject: [PATCH] revert: remove billing service implementation --- README.md | 16 +- composer.json | 6 +- src/Builder/UrlBuilder.php | 139 ------- src/Clients/BillingAPIGatewayClient.php | 61 --- .../BillingServiceSubscriptionClient.php | 107 ----- src/Clients/GenericClient.php | 375 ------------------ src/Clients/Handler/HttpResponseHandler.php | 58 --- src/Clients/Handler/index.php | 11 - src/Clients/index.php | 11 - src/Services/BillingService.php | 251 ++---------- tests/Unit/Builder/UrlBuilderTest.php | 112 ------ .../Clients/BillingApiGatewayClientTest.php | 123 ------ .../BillingServiceSubscriptionClientTest.php | 274 ------------- tests/Unit/Clients/index.php | 11 - 14 files changed, 38 insertions(+), 1517 deletions(-) delete mode 100644 src/Builder/UrlBuilder.php delete mode 100644 src/Clients/BillingAPIGatewayClient.php delete mode 100644 src/Clients/BillingServiceSubscriptionClient.php delete mode 100644 src/Clients/GenericClient.php delete mode 100644 src/Clients/Handler/HttpResponseHandler.php delete mode 100644 src/Clients/Handler/index.php delete mode 100644 src/Clients/index.php delete mode 100644 tests/Unit/Builder/UrlBuilderTest.php delete mode 100644 tests/Unit/Clients/BillingApiGatewayClientTest.php delete mode 100644 tests/Unit/Clients/BillingServiceSubscriptionClientTest.php delete mode 100644 tests/Unit/Clients/index.php diff --git a/README.md b/README.md index f69b43d..aa0a58b 100644 --- a/README.md +++ b/README.md @@ -20,10 +20,12 @@ composer require prestashopcorp/module-lib-billing | 1.x | Security fixes | `module-lib-billing` | `PrestaShopCorp\Billing` | [v1][lib-1-repo] | N/A | >=5.6 | | 2.x | Security fixes | `module-lib-billing` | `PrestaShopCorp\Billing` | [v2][lib-2-repo] | N/A | >=7.2.5 | | 3.x | Latest | `module-lib-billing` | `PrestaShopCorp\Billing` | [v3][lib-3-repo] | N/A | >=5.6 | +| 4.x | Latest | `module-lib-billing` | `PrestaShopCorp\Billing` | [v4][lib-4-repo] | N/A | >=5.6 | [lib-1-repo]: https://github.com/PrestaShopCorp/module-lib-billing/tree/1.x [lib-2-repo]: https://github.com/PrestaShopCorp/module-lib-billing/tree/2.x -[lib-3-repo]: https://github.com/PrestaShopCorp/module-lib-billing +[lib-3-repo]: https://github.com/PrestaShopCorp/module-lib-billing/tree/2.3 +[lib-4-repo]: https://github.com/PrestaShopCorp/module-lib-billing ## Register as a service in your PSx container @@ -39,22 +41,14 @@ services: class: 'PrestaShopCorp\Billing\Wrappers\BillingContextWrapper' arguments: - "@ps_accounts.facade" - - "@rbm_example.context" + - "@builtfor_Example.context" - true # if true you are in sandbox mode, if false or empty not in sandbox ps_billings.facade: class: 'PrestaShopCorp\Billing\Presenter\BillingPresenter' arguments: - "@ps_billings.context_wrapper" - - "@rbm_example.module" - - # Remove this if you don't need BillingService - ps_billings.service: - class: PrestaShopCorp\Billing\Services\BillingService - public: true - arguments: - - "@ps_billings.context_wrapper" - - "@rbm_example.module" + - "@builtfor_Example.module" ``` ## How to use it diff --git a/composer.json b/composer.json index 5e71162..db88acc 100644 --- a/composer.json +++ b/composer.json @@ -12,12 +12,10 @@ ] }, "require": { - "php": ">=5.6", - "prestashop/module-lib-guzzle-adapter": "0.6" + "php": ">=5.6" }, "require-dev": { - "prestashop/php-dev-tools": "^3", - "guzzlehttp/guzzle": "^5.3" + "prestashop/php-dev-tools": "^3" }, "config": { "allow-plugins": { diff --git a/src/Builder/UrlBuilder.php b/src/Builder/UrlBuilder.php deleted file mode 100644 index 4e84517..0000000 --- a/src/Builder/UrlBuilder.php +++ /dev/null @@ -1,139 +0,0 @@ - - * @copyright Since 2007 PrestaShop SA and Contributors - * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 - */ - -namespace PrestaShopCorp\Billing\Builder; - -class UrlBuilder -{ - private $envName = null; - private $apiUrl = null; - - public function __construct($envName = null, $apiUrl = null) - { - $this->envName = $envName; - $this->apiUrl = $apiUrl; - } - - /** - * buildUIUrl - * - * @return string - */ - public function buildUIUrl() - { - switch ($this->getEnvName()) { - case 'development': - // Handle by .env in Billing UI - return null; - case 'integration': - return 'https://billing.distribution-' . $this->getEnvName() . '.prestashop.net'; - break; - case 'prestabulle1': - case 'prestabulle2': - case 'prestabulle3': - case 'prestabulle4': - case 'prestabulle5': - case 'prestabulle6': - return 'https://billing-' . $this->getEnvName() . '.distribution-integration.prestashop.net'; - break; - case 'preprod': - return 'https://billing.distribution-' . $this->getEnvName() . '.prestashop.net'; - break; - default: - return 'https://billing.distribution.prestashop.net'; - } - } - - /** - * buildAPIUrl - * - * @return string - */ - public function buildAPIUrl() - { - switch ($this->getEnvName()) { - case 'development': - return $this->getApiUrl() ? filter_var($this->getApiUrl(), FILTER_SANITIZE_URL) : null; - case 'integration': - return 'https://billing-api.distribution-' . $this->getEnvName() . '.prestashop.net'; - break; - case 'prestabulle1': - case 'prestabulle2': - case 'prestabulle3': - case 'prestabulle4': - case 'prestabulle5': - case 'prestabulle6': - return 'https://billing-api-' . str_replace('prestabulle', 'psbulle', $this->getEnvName()) . '.distribution-integration.prestashop.net'; - break; - case 'preprod': - return 'https://billing-api.distribution-' . $this->getEnvName() . '.prestashop.net'; - break; - default: - return 'https://billing-api.distribution.prestashop.net'; - } - } - - /** - * buildAPIGatewayUrl - * - * @return string - */ - public function buildAPIGatewayUrl() - { - switch ($this->getEnvName()) { - case 'development': - return $this->getApiUrl() ? filter_var($this->getApiUrl(), FILTER_SANITIZE_URL) : null; - case 'prestabulle1': - case 'prestabulle2': - case 'prestabulle3': - case 'prestabulle4': - case 'prestabulle5': - case 'prestabulle6': - return 'https://api-' . $this->getEnvName() . '.billing.integration.prestashop.com'; - break; - case 'preprod': - return 'https://api.billing.preproduction.prestashop.com'; - break; - default: - return 'https://api.billing.prestashop.com'; - } - } - - /** - * getEnvName - * - * @return string - */ - private function getEnvName() - { - return $this->envName; - } - - /** - * getApiUrl - * - * @return string - */ - private function getApiUrl() - { - return $this->apiUrl; - } -} diff --git a/src/Clients/BillingAPIGatewayClient.php b/src/Clients/BillingAPIGatewayClient.php deleted file mode 100644 index ce2a194..0000000 --- a/src/Clients/BillingAPIGatewayClient.php +++ /dev/null @@ -1,61 +0,0 @@ - - * @copyright Since 2007 PrestaShop SA and Contributors - * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 - */ - -namespace PrestaShopCorp\Billing\Clients; - -/** - * BillingApiGatewayClient low level client to access to billing API Gateway routes - */ -class BillingApiGatewayClient extends GenericClient -{ - const DEFAULT_API_VERSION = 'v1'; - - // https://prestashop-billing.stoplight.io/docs/api-gateway/14a1a9da838ee-retrieve-the-components-of-a-product - protected $possibleQueryParameters = [ - 'lang_iso_code', - 'filter_status', - 'filter_component_type', - ]; - - /** - * Constructor with parameters - * - * @param array{moduleName: string, client: null|BillingServiceSubscriptionClient, apiUrl: string, apiVersion: string, isSandbox: bool, token: string} $optionsParameters - * - * @return void - */ - public function __construct($optionsParameters = []) - { - parent::__construct($optionsParameters); - } - - public function retrieveProductComponents() - { - $params = [ - 'filter_status' => 'active', - ]; - - $this->setQueryParams($params) - ->setRoute('/products/' . $this->getproductId() . '/components'); - - return $this->get(); - } -} diff --git a/src/Clients/BillingServiceSubscriptionClient.php b/src/Clients/BillingServiceSubscriptionClient.php deleted file mode 100644 index 8c5b01d..0000000 --- a/src/Clients/BillingServiceSubscriptionClient.php +++ /dev/null @@ -1,107 +0,0 @@ - - * @copyright Since 2007 PrestaShop SA and Contributors - * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 - */ - -namespace PrestaShopCorp\Billing\Clients; - -/** - * BillingServiceSubscriptionClient low level client to access to billing API routes - */ -class BillingServiceSubscriptionClient extends GenericClient -{ - const DEFAULT_API_VERSION = 'v1'; - - protected $possibleQueryParameters = [ - 'lang_iso_code', - 'status', - 'limit', - 'offset', - ]; - - /** - * Constructor with parameters - * - * @param array{moduleName: string, client: null|BillingServiceSubscriptionClient, apiUrl: string, apiVersion: string, isSandbox: bool, token: string} $optionsParameters - * - * @return void - */ - public function __construct($optionsParameters = []) - { - parent::__construct($optionsParameters); - } - - /** - * retrieveCustomerById - * - * @param string $customerId the shop id - * - * @return array with success (bool), httpStatus (int), body (array) extract from the response - */ - public function retrieveCustomerById($customerId) - { - $this->setRoute('/customers/' . $customerId); - - return $this->get(); - } - - /** - * Retrieve the subscription of the customer for your module - * - * @param string $customerId the shop id - * - * @return array with success (bool), httpStatus (int), body (array) extract from the response - */ - public function retrieveSubscriptionByCustomerId($customerId) - { - $this->setRoute('/customers/' . $customerId . '/subscriptions/' . $this->getproductId()); - - return $this->get(); - } - - /** - * @deprecated since 3.0 and will be removed in next major version. - * @see getBillingApiGatewayClient()->retrieveProductComponents(); - * - * Retrieve plans associated with the module - * - * @param string $lang the lang of the user - * @param string $status whether you want to get only "active" plan, or the "archived", or both when set to null (default: "active") - * @param int $limit number of plan to return (default: "10") - * @param string $offset pagination start (default: null) - * - * @return array with success (bool), httpStatus (int), body (array) extracted from the response - */ - public function retrievePlans($lang, $status = 'active', $limit = 10, $offset = null) - { - $params = [ - 'lang_iso_code' => $lang, - 'status' => $status, - 'limit' => $limit, - ]; - - if ($offset) { - $params['offset'] = $offset; - } - $this->setQueryParams($params) - ->setRoute('/products/' . $this->getproductId() . '/plans'); - - return $this->get(); - } -} diff --git a/src/Clients/GenericClient.php b/src/Clients/GenericClient.php deleted file mode 100644 index 36bf099..0000000 --- a/src/Clients/GenericClient.php +++ /dev/null @@ -1,375 +0,0 @@ - - * @copyright Since 2007 PrestaShop SA and Contributors - * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 - */ - -namespace PrestaShopCorp\Billing\Clients; - -use GuzzleHttp\Psr7\Request; -use Prestashop\ModuleLibGuzzleAdapter\ClientFactory; -use PrestaShopCorp\Billing\Clients\Handler\HttpResponseHandler; -use PrestaShopCorp\Billing\Exception\MissingMandatoryParametersException; -use PrestaShopCorp\Billing\Exception\QueryParamsException; - -/** - * Construct the client used to make call to maasland. - */ -abstract class GenericClient -{ - /** - * If set to false, you will not be able to catch the error - * guzzle will show a different error message. - * - * @var bool - */ - protected $catchExceptions = false; - - /** - * Guzzle Client. - * - * @var Client - */ - protected $client; - - /** - * Api route. - * - * @var string - */ - protected $route; - - /** - * Api url. - * - * @var string - */ - protected $apiUrl; - - /** - * Set how long guzzle will wait a response before end it up. - * - * @var int - */ - protected $timeout = 10; - - /** - * Version of the API - * - * @var string - */ - protected $apiVersion; - - /** - * Technical name of the module - * - * @var string - */ - protected $productId; - - /** - * @var array - */ - protected $queryParameters = []; - - /** - * @var array - */ - protected $possibleQueryParameters = []; - - protected $mandatoryOptionsParameters = [ - 'productId', - 'client', - 'apiUrl', - 'token', - 'isSandbox', - 'apiVersion', - ]; - - protected $possibleOptionsParameters = [ - 'productId', - 'client', - 'apiUrl', - 'token', - 'isSandbox', - 'apiVersion', - 'timeout', - 'catchExceptions', - ]; - - /** - * GenericClient constructor. - */ - public function __construct($optionsParameters = []) - { - $checkededOptionsParams = array_diff_ukey(array_flip($this->mandatoryOptionsParameters), $optionsParameters, 'strcasecmp'); - if (!empty($checkededOptionsParams)) { - throw new MissingMandatoryParametersException($checkededOptionsParams); - } - - $filteredOptionsParams = array_intersect_key($optionsParameters, array_flip($this->possibleOptionsParameters)); - - extract($filteredOptionsParams, EXTR_PREFIX_SAME, 'generic'); - - // Client can be provided for tests or some specific use case - if (!isset($client) || null === $client) { - $this->setClientUrl($apiUrl); - $this->setTimeout(isset($timeout) ? $timeout : $this->getTimeout()); - $this->setCatchExceptions(isset($catchExceptions) ? $catchExceptions : $this->getCatchExceptions()); - - $clientParams = [ - 'base_url' => $this->getClientUrl(), - 'defaults' => [ - 'timeout' => $this->getTimeout(), - 'exceptions' => $this->getCatchExceptions(), - 'headers' => [ - 'Accept' => 'application/json', - 'Authorization' => 'Bearer ' . (string) $token, - 'User-Agent' => 'module-lib-billing v3 (' . $productId . ')', - ], - ], - ]; - - if (true === $isSandbox) { - $clientParams['defaults']['headers']['Sandbox'] = 'true'; - } - $client = (new ClientFactory())->getClient($clientParams); - } - - $this->setClient($client) - ->setproductId($productId) - ->setApiVersion($apiVersion); - } - - /** - * Wrapper of method post from guzzle client. - * - * @param array $options payload - * - * @return array return response or false if no response - */ - protected function get($options = []) - { - $response = $this->getClient()->sendRequest(new Request('GET', $this->getRoute(), $options)); - $responseHandler = new HttpResponseHandler(); - - return $responseHandler->handleResponse($response); - } - - /** - * Setter for client. - * - * @return void - */ - protected function setClient($client) - { - $this->client = $client; - - return $this; - } - - /** - * Setter for exceptions mode. - * - * @param bool $bool - * - * @return void - */ - protected function setCatchExceptions($bool) - { - $this->catchExceptions = (bool) $bool; - - return $this; - } - - /** - * Setter for route. - * - * @param string $route - * - * @return void - */ - protected function setRoute($route) - { - $this->route = $route; - if ($this->getQueryParameters()) { - $this->route .= $this->getQueryParameters(); - } - - return $this; - } - - /** - * Setter for timeout. - * - * @param int $timeout - * - * @return void - */ - protected function setTimeout(int $timeout) - { - $this->timeout = (int) $timeout; - - return $this; - } - - /** - * Setter for apiVersion. - * - * @param string $apiVersion - * - * @return void - */ - protected function setApiVersion($apiVersion) - { - $this->apiVersion = $apiVersion; - - return $this; - } - - /** - * @param array $params - * - * @return $this - */ - protected function setQueryParams(array $params) - { - $notAllowedParameters = array_diff_key($params, array_flip($this->possibleQueryParameters)); - if (!empty($notAllowedParameters)) { - throw new QueryParamsException($notAllowedParameters, $this->possibleQueryParameters); - } - - $filteredParams = array_intersect_key($params, array_flip($this->possibleQueryParameters)); - $this->queryParameters = '?' . http_build_query(array_merge($this->queryParameters, $filteredParams)); - - return $this; - } - - /** - * Setter for productId - * - * @param string $productId - * - * @return void - */ - protected function setproductId($productId) - { - $this->productId = $productId; - - return $this; - } - - protected function setClientUrl($apiUrl) - { - $this->apiUrl = $apiUrl; - - return $this; - } - - /** - * Getter for exceptions mode. - * - * @return bool - */ - protected function getCatchExceptions() - { - return $this->catchExceptions; - } - - /** - * Getter for client. - * - * @return ClientInterface - */ - protected function getClient() - { - return $this->client; - } - - /** - * Getter for route. - * - * @return string - */ - protected function getRoute() - { - if ($this->getApiVersion()) { - return $this->getApiVersion() . $this->route; - } - - return $this->route; - } - - /** - * Getter for client url. - * - * @return string - */ - protected function getClientUrl() - { - return $this->apiUrl; - } - - /** - * Getter for full url (client url & route). - * - * @return string - */ - protected function getUrl() - { - return $this->getClientUrl() . $this->getRoute(); - } - - /** - * Getter for timeout. - * - * @return int - */ - protected function getTimeout() - { - return $this->timeout; - } - - /** - * Getter for apiVersion. - * - * @return string - */ - protected function getApiVersion() - { - return $this->apiVersion; - } - - /** - * @return array - */ - protected function getQueryParameters() - { - return $this->queryParameters; - } - - /** - * Getter for productId - * - * @return string - */ - protected function getproductId() - { - return $this->productId; - } -} diff --git a/src/Clients/Handler/HttpResponseHandler.php b/src/Clients/Handler/HttpResponseHandler.php deleted file mode 100644 index eed883f..0000000 --- a/src/Clients/Handler/HttpResponseHandler.php +++ /dev/null @@ -1,58 +0,0 @@ - - * @copyright Since 2007 PrestaShop SA and Contributors - * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 - */ - -namespace PrestaShopCorp\Billing\Clients\Handler; - -/** - * HttpResponseHandler handle http call response - */ -class HttpResponseHandler -{ - /** - * Format api response. - * - * @param $response - * - * @return array - */ - public function handleResponse($response) - { - $responseContents = json_decode($response->getBody()->getContents(), true); - - return [ - 'success' => $this->responseIsSuccessful($response->getStatusCode()), - 'httpStatus' => $response->getStatusCode(), - 'body' => $responseContents, - ]; - } - - /** - * Check if the response is successful or not (response code 200 to 299). - * - * @param int $httpStatusCode - * - * @return bool - */ - private function responseIsSuccessful($httpStatusCode) - { - return '2' === substr((string) $httpStatusCode, 0, 1); - } -} diff --git a/src/Clients/Handler/index.php b/src/Clients/Handler/index.php deleted file mode 100644 index 88355f6..0000000 --- a/src/Clients/Handler/index.php +++ /dev/null @@ -1,11 +0,0 @@ -setBillingContextWrapper($billingContextWrapper) - ->setUrlBuilder(new UrlBuilder($this->getBillingContextWrapper()->getBillingEnv(), $apiUrl)) - ->setModule($module); - - $this->setBillingServiceSubscriptionClient(new BillingServiceSubscriptionClient([ - 'client' => null, - 'productId' => $this->getModule()->name, - 'apiUrl' => $this->getUrlBuilder()->buildAPIUrl(), - 'apiVersion' => $apiVersion ? $apiVersion : BillingServiceSubscriptionClient::DEFAULT_API_VERSION, - 'token' => $this->getBillingContextWrapper()->getAccessToken(), - 'isSandbox' => $this->getBillingContextWrapper()->isSandbox(), - ])); - - $this->setBillingApiGatewayClient(new BillingApiGatewayClient([ - 'client' => null, - 'productId' => $this->getModule()->name, - 'apiUrl' => $this->getUrlBuilder()->buildAPIGatewayUrl(), - 'apiVersion' => $apiVersion ? $apiVersion : BillingApiGatewayClient::DEFAULT_API_VERSION, - 'token' => $this->getBillingContextWrapper()->getAccessToken(), - 'isSandbox' => $this->getBillingContextWrapper()->isSandbox(), - ])); - } - +class BillingService { /** * Retrieve the Billing customer associated with the shop * on which your module is installed @@ -117,7 +36,19 @@ public function __construct( */ public function getCurrentCustomer() { - return $this->getBillingServiceSubscriptionClient()->retrieveCustomerById($this->getBillingContextWrapper()->getShopUuid()); + @trigger_error( + sprintf( + '%s is deprecated since version 4.0. See documentation billing documentation https://docs.cloud.prestashop.com/5-prestashop-billing/1-overview/ to know how to retrieve the current customer.', + __METHOD__ + ), + E_USER_DEPRECATED + ); + + \Tools::displayError(sprintf( + '%s is deprecated since version 4.0. See documentation billing documentation https://docs.cloud.prestashop.com/5-prestashop-billing/1-overview/ to know how to retrieve the current customer.', + __METHOD__ + )); + return null; } /** @@ -127,36 +58,20 @@ public function getCurrentCustomer() * @return array */ public function getCurrentSubscription() - { - return $this->getBillingServiceSubscriptionClient()->retrieveSubscriptionByCustomerId($this->getBillingContextWrapper()->getShopUuid()); - } - - /** - * @deprecated since 3.0 and will be removed in next major version. - * @see getProductComponents() - * - * Retrieve the plans associated to this module - * - * @return array - */ - public function getModulePlans() { @trigger_error( sprintf( - '%s is deprecated since version 3.0. Use %s instead.', - __METHOD__, - BillingService::class . '->getProductComponents()' + '%s is deprecated since version 4.0. See documentation billing documentation https://docs.cloud.prestashop.com/5-prestashop-billing/1-overview/ to know how to retrieve the current subscription.', + __METHOD__ ), E_USER_DEPRECATED ); \Tools::displayError(sprintf( - '%s is deprecated since version 3.0. Use %s instead.', - __METHOD__, - BillingService::class . '->getProductComponents()' + '%s is deprecated since version 4.0. See documentation billing documentation https://docs.cloud.prestashop.com/5-prestashop-billing/1-overview/ to know how to retrieve the current subscription.', + __METHOD__ )); - - return $this->getBillingServiceSubscriptionClient()->retrievePlans($this->getBillingContextWrapper()->getLanguageIsoCode()); + return null; } /** @@ -166,122 +81,18 @@ public function getModulePlans() */ public function getProductComponents() { - return $this->getBillingApiGatewayClient()->retrieveProductComponents(); - } - - /** - * setModule - * - * @param string $module - * - * @return void - */ - private function setModule($module) - { - $this->module = $module; - - return $this; - } - - /** - * getModule - * - * @return Module - */ - private function getModule() - { - return $this->module; - } - - /** - * setUrlBuilder - * - * @param string $urlBuilder - * - * @return void - */ - private function setUrlBuilder($urlBuilder) - { - $this->urlBuilder = $urlBuilder; - - return $this; - } - - /** - * getUrlBuilder - * - * @return UrlBuilder - */ - private function getUrlBuilder() - { - return $this->urlBuilder; - } - - /** - * setBillingServiceSubscriptionClient - * - * @param BillingServiceSubscriptionClient $billingClient - * - * @return void - */ - private function setBillingServiceSubscriptionClient($billingClient) - { - $this->billingServiceSubscriptionClient = $billingClient; - } - - /** - * getBillingServiceSubscriptionClient - * - * @return BillingServiceSubscriptionClient - */ - private function getBillingServiceSubscriptionClient() - { - return $this->billingServiceSubscriptionClient; - } - - /** - * setBillingApiGatewayClient - * - * @param BillingApiGatewayClient $billingClient - * - * @return void - */ - private function setBillingApiGatewayClient($billingClient) - { - $this->billingApiGatewayClient = $billingClient; - } - - /** - * getBillingApiGatewayClient - * - * @return BillingApiGatewayClient - */ - private function getBillingApiGatewayClient() - { - return $this->billingApiGatewayClient; - } - - /** - * setBillingContextWrapper - * - * @param BillingContextWrapper $billingContextWrapper - * - * @return void - */ - private function setBillingContextWrapper(BillingContextWrapper $billingContextWrapper) - { - $this->billingContextWrapper = $billingContextWrapper; - - return $this; - } + @trigger_error( + sprintf( + '%s is deprecated since version 4.0. See documentation billing documentation https://docs.cloud.prestashop.com/5-prestashop-billing/1-overview/ to know how to retrieve the components of your product.', + __METHOD__ + ), + E_USER_DEPRECATED + ); - /** - * getBillingContextWrapper - * - * @return BillingContextWrapper - */ - private function getBillingContextWrapper() - { - return $this->billingContextWrapper; + \Tools::displayError(sprintf( + '%s is deprecated since version 4.0. See documentation billing documentation https://docs.cloud.prestashop.com/5-prestashop-billing/1-overview/ to know how to retrieve the components of your product.', + __METHOD__ + )); + return null; } } diff --git a/tests/Unit/Builder/UrlBuilderTest.php b/tests/Unit/Builder/UrlBuilderTest.php deleted file mode 100644 index 4d0d8c8..0000000 --- a/tests/Unit/Builder/UrlBuilderTest.php +++ /dev/null @@ -1,112 +0,0 @@ - - * @copyright Since 2007 PrestaShop SA and Contributors - * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 - */ - -namespace PrestaShopCorp\Billing\Tests\Unit\Builder; - -use PHPUnit\Framework\TestCase; -use PrestaShopCorp\Billing\Builder\UrlBuilder; - -class UrlBuilderTest extends TestCase -{ - public function testBuildUIUrl() - { - $builder = new UrlBuilder('development'); - $this->assertEquals($builder->buildUIUrl(), null); - - $builder = new UrlBuilder('integration'); - $this->assertEquals($builder->buildUIUrl(), 'https://billing.distribution-integration.prestashop.net'); - $builder = new UrlBuilder('prestabulle1'); - $this->assertEquals($builder->buildUIUrl(), 'https://billing-prestabulle1.distribution-integration.prestashop.net'); - $builder = new UrlBuilder('prestabulle2'); - $this->assertEquals($builder->buildUIUrl(), 'https://billing-prestabulle2.distribution-integration.prestashop.net'); - $builder = new UrlBuilder('prestabulle3'); - $this->assertEquals($builder->buildUIUrl(), 'https://billing-prestabulle3.distribution-integration.prestashop.net'); - $builder = new UrlBuilder('prestabulle4'); - $this->assertEquals($builder->buildUIUrl(), 'https://billing-prestabulle4.distribution-integration.prestashop.net'); - $builder = new UrlBuilder('prestabulle5'); - $this->assertEquals($builder->buildUIUrl(), 'https://billing-prestabulle5.distribution-integration.prestashop.net'); - $builder = new UrlBuilder('prestabulle6'); - $this->assertEquals($builder->buildUIUrl(), 'https://billing-prestabulle6.distribution-integration.prestashop.net'); - - $builder = new UrlBuilder('preprod'); - $this->assertEquals($builder->buildUIUrl(), 'https://billing.distribution-preprod.prestashop.net'); - $builder = new UrlBuilder(); - $this->assertEquals($builder->buildUIUrl(), 'https://billing.distribution.prestashop.net'); - } - - public function testBuildAPIUrl() - { - $builder = new UrlBuilder('development'); - $this->assertEquals($builder->buildAPIUrl('development'), null); - $builder = new UrlBuilder('development', 'https://www.w3schoo��ls.co�m'); - $this->assertEquals($builder->buildAPIUrl(), 'https://www.w3schools.com'); - $builder = new UrlBuilder('development', 'https://www.w3schools.com'); - $this->assertEquals($builder->buildAPIUrl(), 'https://www.w3schools.com'); - - $builder = new UrlBuilder('integration'); - $this->assertEquals($builder->buildAPIUrl(), 'https://billing-api.distribution-integration.prestashop.net'); - $builder = new UrlBuilder('prestabulle1'); - $this->assertEquals($builder->buildAPIUrl(), 'https://billing-api-psbulle1.distribution-integration.prestashop.net'); - $builder = new UrlBuilder('prestabulle2'); - $this->assertEquals($builder->buildAPIUrl(), 'https://billing-api-psbulle2.distribution-integration.prestashop.net'); - $builder = new UrlBuilder('prestabulle3'); - $this->assertEquals($builder->buildAPIUrl(), 'https://billing-api-psbulle3.distribution-integration.prestashop.net'); - $builder = new UrlBuilder('prestabulle4'); - $this->assertEquals($builder->buildAPIUrl(), 'https://billing-api-psbulle4.distribution-integration.prestashop.net'); - $builder = new UrlBuilder('prestabulle5'); - $this->assertEquals($builder->buildAPIUrl(), 'https://billing-api-psbulle5.distribution-integration.prestashop.net'); - $builder = new UrlBuilder('prestabulle6'); - $this->assertEquals($builder->buildAPIUrl(), 'https://billing-api-psbulle6.distribution-integration.prestashop.net'); - - $builder = new UrlBuilder('preprod'); - $this->assertEquals($builder->buildAPIUrl(), 'https://billing-api.distribution-preprod.prestashop.net'); - $builder = new UrlBuilder(); - $this->assertEquals($builder->buildAPIUrl(), 'https://billing-api.distribution.prestashop.net'); - } - - public function testBuildAPIGatewayUrl() - { - $builder = new UrlBuilder('development'); - $this->assertEquals($builder->buildAPIGatewayUrl('development'), null); - $builder = new UrlBuilder('development', 'https://www.w3schoo��ls.co�m'); - $this->assertEquals($builder->buildAPIGatewayUrl(), 'https://www.w3schools.com'); - $builder = new UrlBuilder('development', 'https://www.w3schools.com'); - $this->assertEquals($builder->buildAPIGatewayUrl(), 'https://www.w3schools.com'); - - $builder = new UrlBuilder('prestabulle1'); - $this->assertEquals($builder->buildAPIGatewayUrl(), 'https://api-prestabulle1.billing.integration.prestashop.com'); - $builder = new UrlBuilder('prestabulle2'); - $this->assertEquals($builder->buildAPIGatewayUrl(), 'https://api-prestabulle2.billing.integration.prestashop.com'); - $builder = new UrlBuilder('prestabulle3'); - $this->assertEquals($builder->buildAPIGatewayUrl(), 'https://api-prestabulle3.billing.integration.prestashop.com'); - $builder = new UrlBuilder('prestabulle4'); - $this->assertEquals($builder->buildAPIGatewayUrl(), 'https://api-prestabulle4.billing.integration.prestashop.com'); - $builder = new UrlBuilder('prestabulle5'); - $this->assertEquals($builder->buildAPIGatewayUrl(), 'https://api-prestabulle5.billing.integration.prestashop.com'); - $builder = new UrlBuilder('prestabulle6'); - $this->assertEquals($builder->buildAPIGatewayUrl(), 'https://api-prestabulle6.billing.integration.prestashop.com'); - - $builder = new UrlBuilder('preprod'); - $this->assertEquals($builder->buildAPIGatewayUrl(), 'https://api.billing.preproduction.prestashop.com'); - $builder = new UrlBuilder(); - $this->assertEquals($builder->buildAPIGatewayUrl(), 'https://api.billing.prestashop.com'); - } -} diff --git a/tests/Unit/Clients/BillingApiGatewayClientTest.php b/tests/Unit/Clients/BillingApiGatewayClientTest.php deleted file mode 100644 index 578c118..0000000 --- a/tests/Unit/Clients/BillingApiGatewayClientTest.php +++ /dev/null @@ -1,123 +0,0 @@ -componentPlan = [ - 'items' => [[ - 'id' => 'rbm-advanced-EUR-Monthly', - 'productId' => 'rbm_example', - 'componentType' => 'plan', - 'status' => 'active', - 'isUsageBased' => false, - 'pricingModel' => 'flat_fee', - 'price' => 1000, - 'tiers' => [], - 'billingPeriodUnit' => 'month', - 'trialPeriodValue' => 10, - 'trialPeriodUnit' => 'day', - 'freeQuantity' => 0, - 'mandatoryComponentIds' => [], - ], - [ - 'id' => 'rbm-free-GBP-Monthly', - 'productId' => 'rbm_example', - 'componentType' => 'plan', - 'status' => 'active', - 'isUsageBased' => false, - 'pricingModel' => 'flat_fee', - 'price' => 300, - 'tiers' => [], - 'billingPeriodUnit' => 'month', - 'freeQuantity' => 0, - 'mandatoryComponentIds' => [], - ], ], - 'total' => 2, - ]; - - $this->componentAddon = []; - } - - public function testRetrieveProductComponentsPlan() - { - $billingClient = $this->getBillingApiGatewayClient($this->componentPlan); - $resultPlan = $billingClient->retrieveProductComponents(); - - // Test the format and the content - $this->assertEquals($resultPlan['success'], true); - $this->assertEquals($resultPlan['httpStatus'], 200); - $this->assertEquals($resultPlan['body'], $this->componentPlan); - } - - /** - * getBillingApiGatewayClient - * - * @param $mockData - * - * @return BillingApiGatewayClient - */ - private function getBillingApiGatewayClient($mockData) - { - $response = new Response(200, [], Stream::factory(json_encode($mockData))); - $mock = new Mock([ - $response, - ]); - - $client = new testApiGatewayClient([ - 'base_url' => 'http://localhost/', - 'defaults' => [ - 'timeout' => 20, - 'exceptions' => true, - 'headers' => [ - 'Accept' => 'application/json', - 'Authorization' => 'Bearer token', - ], - ], - ], $mockData); - - $client->getEmitter()->attach($mock); - - return new BillingApiGatewayClient([ - 'client' => $client, - 'productId' => 'rbm_example', - 'apiVersion' => BillingApiGatewayClient::DEFAULT_API_VERSION, - 'apiUrl' => 'http://localhost/', - 'token' => 'token', - 'isSandbox' => false, - ]); - } -} diff --git a/tests/Unit/Clients/BillingServiceSubscriptionClientTest.php b/tests/Unit/Clients/BillingServiceSubscriptionClientTest.php deleted file mode 100644 index 6f3ea74..0000000 --- a/tests/Unit/Clients/BillingServiceSubscriptionClientTest.php +++ /dev/null @@ -1,274 +0,0 @@ -customer = [ - 'id' => 'b2581e4b-0030-4fc8-9bf2-7f01c550a946', - 'email' => 'takeshi.daveau@prestashop.com', - 'auto_collection' => 'on', - 'created_at' => 1646842866, - 'billing_address' => [ - 'first_name' => 'Takeshi', - 'last_name' => 'Daveau', - 'company' => 'TDA', - 'line1' => 'Rue des rue', - 'city' => 'Lilas', - 'country' => 'FR', - 'zip' => '93333', - ], - 'card_status' => 'valid', - 'primary_payment_source_id' => 'pm_AzqMGNSzhOTDa1BEP', - 'payment_method' => [ - 'type' => 'card', - 'gateway' => 'stripe', - 'gateway_account_id' => 'gw_Azqe1TSLVjdNhdI', - 'status' => 'valid', - 'reference_id' => 'cus_LIQGgPFSj2r39T/card_1KbpQHGp5Dc2lo8uEdDJv8ac', - ], - 'cf_shop_id' => 'b2581e4b-0030-4fc8-9bf2-7f01c550a946', - 'cf_consent' => 'False', - ]; - - $this->subscription = [ - 'id' => '169lnASzhOWay1EQN', - 'plan_id' => 'rbm-advanced', - 'customer_id' => 'b2581e4b-0030-4fc8-9bf2-7f01c550a946', - 'status' => 'in_trial', - 'currency_code' => 'EUR', - 'has_scheduled_changes' => false, - 'billing_period' => 1, - 'billing_period_unit' => 'month', - 'due_invoices_count' => 0, - 'meta_data' => [ - 'module' => 'rbm_example', - ], - 'plan_amount' => 2000, - 'plan_quantity' => 1, - 'plan_unit_price' => 2000, - 'subscription_items' => [ - [ - 'item_price_id' => 'rbm-advanced', - 'amount' => 2000, - 'item_type' => 'plan', - 'quantity' => 1, - 'unit_price' => 2000, - ], - ], - 'created_at' => 1646931926, - 'cancelled_at' => 1648335600, - 'started_at' => 1646866800, - 'updated_at' => 1646934561, - 'trial_end' => 1648335599, - 'coupon' => [ - 'coupon_id' => 'TDATEST20PERCENT', - 'applied_count' => 1, - 'coupon_code' => 'tda6359-20', - 'apply_till' => 1654811999, - ], - 'is_free_trial_used' => true, - ]; - - $this->plans = [ - 'limit' => 100, - 'offset' => null, - 'results' => [ - [ - 'id' => 'rbm-free', - 'name' => 'rbm free', - 'details_plan' => [ - 'title' => 'rbm free', - 'features' => [ - 'Fonctionnalité 1 du rbm free', - 'Fonctionnalité 2 du rbm free', - 'Fonctionnalité 3 du rbm free', - 'Fonctionnalité 4 du rbm free', - ], - ], - 'price' => 100, - 'period' => 1, - 'currency_code' => 'EUR', - 'period_unit' => 'month', - 'trial_period' => 7, - 'trial_period_unit' => 'day', - 'pricing_model' => 'flat_fee', - 'meta_data' => [ - 'module' => 'rbm_example', - ], - ], - [ - 'id' => 'rbm-advanced', - 'name' => 'rbm advanced', - 'details_plan' => [ - 'title' => 'rbm advanced', - 'features' => [ - 'Fonctionnalité 1 du rbm advanced', - 'Fonctionnalité 2 du rbm advanced', - 'Fonctionnalité 3 du rbm advanced', - 'Fonctionnalité 4 du rbm advanced', - ], - ], - 'price' => 2000, - 'period' => 1, - 'currency_code' => 'EUR', - 'period_unit' => 'month', - 'trial_period' => 7, - 'trial_period_unit' => 'day', - 'pricing_model' => 'flat_fee', - 'meta_data' => [ - 'module' => 'rbm_example', - ], - ], - [ - 'id' => 'rbm-ultimate', - 'name' => 'rbm ultimate', - 'details_plan' => [ - 'title' => 'rbm ultimate', - 'features' => [ - 'Fonctionnalité 1 du rbm ultimate', - 'Fonctionnalité 2 du rbm ultimate', - 'Fonctionnalité 3 du rbm ultimate', - 'Fonctionnalité 4 du rbm ultimate', - ], - ], - 'price' => 10000, - 'period' => 1, - 'currency_code' => 'EUR', - 'period_unit' => 'month', - 'trial_period' => 7, - 'trial_period_unit' => 'day', - 'pricing_model' => 'flat_fee', - 'meta_data' => [ - 'module' => 'rbm_example', - ], - ], - [ - 'id' => 'rbm-exempl-test', - 'name' => 'rbm exempl test', - 'details_plan' => null, - 'price' => 999900, - 'period' => 1, - 'currency_code' => 'EUR', - 'period_unit' => 'month', - 'pricing_model' => 'flat_fee', - 'meta_data' => [ - 'module' => 'rbm_example', - ], - ], - ], - ]; - } - - public function testRetrieveCustomerById() - { - $billingClient = $this->getBillingServiceSubscriptionClient($this->customer); - $result = $billingClient->retrieveCustomerById('b2581e4b-0030-4fc8-9bf2-7f01c550a946'); - - // Test the format and the content - $this->assertEquals($result['success'], true); - $this->assertEquals($result['httpStatus'], 200); - $this->assertEquals($result['body'], $this->customer); - } - - public function testRetrieveSubscriptionByCustomerId() - { - $billingClient = $this->getBillingServiceSubscriptionClient($this->subscription); - $result = $billingClient->retrieveSubscriptionByCustomerId('b2581e4b-0030-4fc8-9bf2-7f01c550a946'); - - // Test the format and the content - $this->assertEquals($result['success'], true); - $this->assertEquals($result['httpStatus'], 200); - $this->assertEquals($result['body'], $this->subscription); - } - - public function testRetrievePlansShouldCallTheProperRoute() - { - $billingClient = $this->getBillingServiceSubscriptionClient($this->plans); - $result = $billingClient->retrievePlans('fr'); - - // Test the format and the content - $this->assertEquals($result['success'], true); - $this->assertEquals($result['httpStatus'], 200); - $this->assertEquals($result['body'], $this->plans); - } - - public function testThrowMandatoryParams() - { - $this->expectException(MissingMandatoryParametersException::class); - $client = new BillingServiceSubscriptionClient(); - } - - /** - * getBillingServiceSubscriptionClient - * - * @param $mockData - * - * @return BillingServiceSubscriptionClient - */ - private function getBillingServiceSubscriptionClient($mockData) - { - $response = new Response(200, [], Stream::factory(json_encode($mockData))); - $mock = new Mock([ - $response, - ]); - - $client = new testServiceSubscriptionClient([ - 'base_url' => 'http://localhost/', - 'defaults' => [ - 'timeout' => 20, - 'exceptions' => true, - 'headers' => [ - 'Accept' => 'application/json', - 'Authorization' => 'Bearer token', - ], - ], - ], $mockData); - - $client->getEmitter()->attach($mock); - - return new BillingServiceSubscriptionClient([ - 'client' => $client, - 'productId' => 'rbm_example', - 'apiVersion' => BillingServiceSubscriptionClient::DEFAULT_API_VERSION, - 'apiUrl' => 'http://localhost/', - 'token' => 'token', - 'isSandbox' => false, - ]); - } -} diff --git a/tests/Unit/Clients/index.php b/tests/Unit/Clients/index.php deleted file mode 100644 index 88355f6..0000000 --- a/tests/Unit/Clients/index.php +++ /dev/null @@ -1,11 +0,0 @@ -