From 9657409cd8bee0d223720f3edf6a5352d97acfde Mon Sep 17 00:00:00 2001 From: TakeshiDaveau Date: Fri, 17 Jan 2025 09:49:11 +0100 Subject: [PATCH 01/15] 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 @@ - Date: Fri, 17 Jan 2025 11:07:13 +0100 Subject: [PATCH 02/15] fix: remove UrlBuilder from BillingPresenter --- src/Presenter/BillingPresenter.php | 29 ----------------------------- 1 file changed, 29 deletions(-) diff --git a/src/Presenter/BillingPresenter.php b/src/Presenter/BillingPresenter.php index 4c159ef..93bfa41 100644 --- a/src/Presenter/BillingPresenter.php +++ b/src/Presenter/BillingPresenter.php @@ -23,7 +23,6 @@ use Module; use PrestaShopCorp\Billing\Builder\EnvBuilder; -use PrestaShopCorp\Billing\Builder\UrlBuilder; use PrestaShopCorp\Billing\Exception\BillingContextException; use PrestaShopCorp\Billing\Wrappers\BillingContextWrapper; @@ -36,11 +35,6 @@ class BillingPresenter */ private $envBuilder; - /** - * @var UrlBuilder - */ - private $urlBuilder; - /** * @var BillingContextWrapper */ @@ -65,7 +59,6 @@ public function __construct( $this->setModule($module); $this->setEnvBuilder(new EnvBuilder()); - $this->setUrlBuilder(new UrlBuilder()); $this->setBillingContextWrapper($billingContextWrapper); } @@ -222,28 +215,6 @@ private function getEnvBuilder() return $this->envBuilder; } - /** - * setUrlBuilder - * - * @param UrlBuilder $urlBuilder - * - * @return void - */ - private function setUrlBuilder(UrlBuilder $urlBuilder) - { - $this->urlBuilder = $urlBuilder; - } - - /** - * getUrlBuilder - * - * @return UrlBuilder - */ - private function getUrlBuilder() - { - return $this->urlBuilder; - } - /** * setBillingContextWrapper * From 32da17a9f06374e7650201a6e62f636a310eb2ed Mon Sep 17 00:00:00 2001 From: TakeshiDaveau Date: Fri, 17 Jan 2025 11:12:39 +0100 Subject: [PATCH 03/15] fix: php fixer --- src/Services/BillingService.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Services/BillingService.php b/src/Services/BillingService.php index a344197..fd88d6f 100644 --- a/src/Services/BillingService.php +++ b/src/Services/BillingService.php @@ -27,7 +27,8 @@ use PrestaShopCorp\Billing\Clients\BillingServiceSubscriptionClient; use PrestaShopCorp\Billing\Wrappers\BillingContextWrapper; -class BillingService { +class BillingService +{ /** * Retrieve the Billing customer associated with the shop * on which your module is installed From e369ce920e281dba9c4e8befc96fdf244371d821 Mon Sep 17 00:00:00 2001 From: TakeshiDaveau Date: Fri, 17 Jan 2025 11:13:06 +0100 Subject: [PATCH 04/15] ci: add php version for unit test --- .github/workflows/billing-qc-php.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/billing-qc-php.yml b/.github/workflows/billing-qc-php.yml index 7cd4d4e..9aaa051 100644 --- a/.github/workflows/billing-qc-php.yml +++ b/.github/workflows/billing-qc-php.yml @@ -39,7 +39,7 @@ jobs: strategy: matrix: os: [ubuntu-latest] - version: [5.6, 7.1, 7.2, 8.0] + version: [5.6, 7.1, 7.2, 8.0, 8.1, 8.2] runs-on: ubuntu-latest steps: - name: Setup PHP with tools From 9e6c9fe9e1b9c95b29e5bbd7be118321e2b62ff8 Mon Sep 17 00:00:00 2001 From: TakeshiDaveau Date: Fri, 17 Jan 2025 11:13:42 +0100 Subject: [PATCH 05/15] docs: fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index aa0a58b..95062a9 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ services: class: 'PrestaShopCorp\Billing\Wrappers\BillingContextWrapper' arguments: - "@ps_accounts.facade" - - "@builtfor_Example.context" + - "@builtfor_example.context" - true # if true you are in sandbox mode, if false or empty not in sandbox ps_billings.facade: From 12decd14684e47af7a077fb6c981fe007435efa6 Mon Sep 17 00:00:00 2001 From: TakeshiDaveau Date: Fri, 17 Jan 2025 11:14:51 +0100 Subject: [PATCH 06/15] ci: remove php 8.2 --- .github/workflows/billing-qc-php.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/billing-qc-php.yml b/.github/workflows/billing-qc-php.yml index 9aaa051..d97310f 100644 --- a/.github/workflows/billing-qc-php.yml +++ b/.github/workflows/billing-qc-php.yml @@ -39,7 +39,7 @@ jobs: strategy: matrix: os: [ubuntu-latest] - version: [5.6, 7.1, 7.2, 8.0, 8.1, 8.2] + version: [5.6, 7.1, 7.2, 8.0, 8.1] runs-on: ubuntu-latest steps: - name: Setup PHP with tools From 8f9bdb8260a812eb01a782e8588a41979a718273 Mon Sep 17 00:00:00 2001 From: TakeshiDaveau Date: Fri, 17 Jan 2025 15:55:27 +0100 Subject: [PATCH 07/15] docs: fix typo in readme.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 95062a9..15cce05 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ services: class: 'PrestaShopCorp\Billing\Presenter\BillingPresenter' arguments: - "@ps_billings.context_wrapper" - - "@builtfor_Example.module" + - "@builtfor_example.module" ``` ## How to use it From 48c6d917d61ee4309c954b572e545d7888c48aee Mon Sep 17 00:00:00 2001 From: TakeshiDaveau Date: Fri, 17 Jan 2025 16:08:56 +0100 Subject: [PATCH 08/15] ci: fix php unit version --- .github/workflows/billing-qc-php.yml | 71 +++++++++++++++------------- 1 file changed, 39 insertions(+), 32 deletions(-) diff --git a/.github/workflows/billing-qc-php.yml b/.github/workflows/billing-qc-php.yml index d97310f..f825dbb 100644 --- a/.github/workflows/billing-qc-php.yml +++ b/.github/workflows/billing-qc-php.yml @@ -25,38 +25,45 @@ jobs: - name: PHP syntax checker 8.2 uses: prestashop/github-action-php-lint/8.2@master - php-cs-fixer: - name: PHP-CS-FIXER - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v3 - - name: Run PHP-CS-Fixer - uses: prestashopcorp/github-action-php-cs-fixer@master - - phpunit: - name: PHPUNIT - strategy: - matrix: - os: [ubuntu-latest] - version: [5.6, 7.1, 7.2, 8.0, 8.1] - runs-on: ubuntu-latest - steps: - - name: Setup PHP with tools - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.version }} - - name: Checkout - uses: actions/checkout@v3 +phpunit: + name: PHPUNIT + strategy: + matrix: + php-config: + - php-version: 5.6 + phpunit-version: 5 + - php-version: 7.1 + phpunit-version: 7 + - php-version: 7.2 + phpunit-version: 7 + - php-version: 7.3 + phpunit-version: 9 + - php-version: 7.4 + phpunit-version: 9 + - php-version: 8.0 + phpunit-version: 9 + - php-version: 8.1 + phpunit-version: 10 + - php-version: 8.2 + phpunit-version: 11 + runs-on: ubuntu-latest + steps: + - name: Setup PHP with tools + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-config.php-version }} + + - name: Checkout + uses: actions/checkout@v3 - - name: Update dependencies - run: composer update + - name: Update dependencies + run: composer update - - name: Install dependencies - run: composer install + - name: Install dependencies + run: composer install - - name: PHPUnit tests - run: | - wget -O phpunit https://phar.phpunit.de/phpunit-5.phar - chmod +x phpunit - ./phpunit tests + - name: Download and Run PHPUnit + run: | + wget -O phpunit https://phar.phpunit.de/phpunit-${{ matrix.php-config.phpunit-version }}.phar + chmod +x phpunit + ./phpunit tests From ff0b9aa158a9ebd07c1ebd3613dfc890e829c6b3 Mon Sep 17 00:00:00 2001 From: TakeshiDaveau Date: Fri, 17 Jan 2025 16:30:09 +0100 Subject: [PATCH 09/15] chore: test pre-commit hook --- .github/workflows/billing-qc-php.yml | 76 ++++++++++++++-------------- .php-cs-fixer.dist.php | 4 +- composer.json | 7 ++- src/Services/BillingService.php | 9 ++-- 4 files changed, 49 insertions(+), 47 deletions(-) diff --git a/.github/workflows/billing-qc-php.yml b/.github/workflows/billing-qc-php.yml index f825dbb..cac85cc 100644 --- a/.github/workflows/billing-qc-php.yml +++ b/.github/workflows/billing-qc-php.yml @@ -25,45 +25,45 @@ jobs: - name: PHP syntax checker 8.2 uses: prestashop/github-action-php-lint/8.2@master -phpunit: - name: PHPUNIT - strategy: - matrix: - php-config: - - php-version: 5.6 - phpunit-version: 5 - - php-version: 7.1 - phpunit-version: 7 - - php-version: 7.2 - phpunit-version: 7 - - php-version: 7.3 - phpunit-version: 9 - - php-version: 7.4 - phpunit-version: 9 - - php-version: 8.0 - phpunit-version: 9 - - php-version: 8.1 - phpunit-version: 10 - - php-version: 8.2 - phpunit-version: 11 - runs-on: ubuntu-latest - steps: - - name: Setup PHP with tools - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php-config.php-version }} + phpunit: + name: PHPUNIT + strategy: + matrix: + php-config: + - php-version: 5.6 + phpunit-version: 5 + - php-version: 7.1 + phpunit-version: 7 + - php-version: 7.2 + phpunit-version: 7 + - php-version: 7.3 + phpunit-version: 9 + - php-version: 7.4 + phpunit-version: 9 + - php-version: 8.0 + phpunit-version: 9 + - php-version: 8.1 + phpunit-version: 10 + - php-version: 8.2 + phpunit-version: 11 + runs-on: ubuntu-latest + steps: + - name: Setup PHP with tools + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-config.php-version }} - - name: Checkout - uses: actions/checkout@v3 + - name: Checkout + uses: actions/checkout@v3 - - name: Update dependencies - run: composer update + - name: Update dependencies + run: composer update - - name: Install dependencies - run: composer install + - name: Install dependencies + run: composer install - - name: Download and Run PHPUnit - run: | - wget -O phpunit https://phar.phpunit.de/phpunit-${{ matrix.php-config.phpunit-version }}.phar - chmod +x phpunit - ./phpunit tests + - name: Download and Run PHPUnit + run: | + wget -O phpunit https://phar.phpunit.de/phpunit-${{ matrix.php-config.phpunit-version }}.phar + chmod +x phpunit + ./phpunit tests diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php index e872c65..5b524f8 100644 --- a/.php-cs-fixer.dist.php +++ b/.php-cs-fixer.dist.php @@ -4,8 +4,6 @@ /** @var \Symfony\Component\Finder\Finder $finder */ $finder = $config->setUsingCache(true)->getFinder(); -$finder->in(__DIR__) - ->exclude('vendor') - ->notPath('src/Config/Config.php'); +$finder->in(__DIR__)->exclude('vendor'); return $config; diff --git a/composer.json b/composer.json index db88acc..dbf745a 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,12 @@ "php": ">=5.6" }, "require-dev": { - "prestashop/php-dev-tools": "^3" + "prestashop/php-dev-tools": "^5.0", + "friendsofphp/php-cs-fixer": "^2.19", + "phpstan/phpstan": "^2.1", + "prestashop/header-stamp": "^2.3", + "prestashop/autoindex": "^2.1", + "squizlabs/php_codesniffer": "^3.11" }, "config": { "allow-plugins": { diff --git a/src/Services/BillingService.php b/src/Services/BillingService.php index fd88d6f..b3e15b8 100644 --- a/src/Services/BillingService.php +++ b/src/Services/BillingService.php @@ -22,12 +22,8 @@ namespace PrestaShopCorp\Billing\Services; use Module; -use PrestaShopCorp\Billing\Builder\UrlBuilder; -use PrestaShopCorp\Billing\Clients\BillingApiGatewayClient; -use PrestaShopCorp\Billing\Clients\BillingServiceSubscriptionClient; -use PrestaShopCorp\Billing\Wrappers\BillingContextWrapper; -class BillingService +class BillingService { /** * Retrieve the Billing customer associated with the shop @@ -49,6 +45,7 @@ public function getCurrentCustomer() '%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; } @@ -72,6 +69,7 @@ public function getCurrentSubscription() '%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 null; } @@ -94,6 +92,7 @@ public function 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 components of your product.', __METHOD__ )); + return null; } } From f1ace2ecbca7f80d85a07ef3a6f6a35fb1e690b4 Mon Sep 17 00:00:00 2001 From: TakeshiDaveau Date: Fri, 17 Jan 2025 16:38:05 +0100 Subject: [PATCH 10/15] chore: add pre-commit hook PHP CS Fixer --- README.md | 55 ++++++++++++++++++++++++++++++++-------- composer.json | 10 +++++++- scripts/install-hooks.sh | 9 +++++++ 3 files changed, 63 insertions(+), 11 deletions(-) create mode 100644 scripts/install-hooks.sh diff --git a/README.md b/README.md index 15cce05..95916ef 100644 --- a/README.md +++ b/README.md @@ -40,15 +40,15 @@ services: ps_billings.context_wrapper: class: 'PrestaShopCorp\Billing\Wrappers\BillingContextWrapper' arguments: - - "@ps_accounts.facade" - - "@builtfor_example.context" + - '@ps_accounts.facade' + - '@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" - - "@builtfor_example.module" + - '@ps_billings.context_wrapper' + - '@builtfor_example.module' ``` ## How to use it @@ -75,19 +75,53 @@ For example in your main module's class `getContent` method. ## Contribute -### Code style +### Pre-commit Hook: Code Validation with PHP-CS-Fixer +This project uses a **pre-commit hook** to ensure the code follows the standards defined by `PHP-CS-Fixer`. Before each commit, the hook automatically performs a check and blocks the commit if any style issues are detected. + +#### Installation + +The hook is automatically installed when you run one of the following commands: + +```bash +# Install dependencies +$ composer install + +# Update dependencies +$ composer update ``` -php vendor/bin/php-cs-fixer fix + +#### How It Works + +During a commit (git commit), the hook runs: + +```bash +PHP_CS_FIXER_IGNORE_ENV=1 vendor/bin/php-cs-fixer fix ``` +#### Troubleshooting + +- Manually reinstall the hook: If the hook is not installed or was removed, you can reinstall it manually by running: + + ```bash + bash scripts/install-hooks.sh + ``` + +- Bypass the hook temporarily: If you need to bypass the hook for a specific commit, use the `--no-verify` option: + + ```bash + git commit --no-verify + ``` + + ⚠️ Note: Use this option cautiously, as it skips all pre-commit checks. + ### Automatic tests #### Install Please follow theses steps to launch unit tests -``` +```bash # Needs to have wget, for OS without wget pleae see the official website (or just visit this link) PHP_VERSION=$(php -r "echo PHP_MAJOR_VERSION.PHP_MINOR_VERSION;") if [[ ${PHP_VERSION} -gt "72" ]]; then @@ -104,7 +138,7 @@ chmod +x phpunit #### Run -``` +```bash ./phpunit tests ``` @@ -113,7 +147,8 @@ chmod +x phpunit PrestaShop module system is not able to handle multiple version of the same library. **Here is an example:** -- Module A requires the v1 of a libA + +- Module A requires the v1 of a libA - Module B requires the v2 of this same libA If someone install module A then module B, only the v1 of libA will be loaded for both Module A and Module B. @@ -122,4 +157,4 @@ If someone install module A then module B, only the v1 of libA will be loaded fo When introducing a breaking change to a class or method signature, you should instead create a new class rather to changing the existing one. -By creating a new class it will force the autoloader to use the last version of the lib. \ No newline at end of file +By creating a new class it will force the autoloader to use the last version of the lib. diff --git a/composer.json b/composer.json index dbf745a..5b5bdd7 100644 --- a/composer.json +++ b/composer.json @@ -26,5 +26,13 @@ "allow-plugins": { "php-http/discovery": true } - } + }, + "scripts": { + "post-install-cmd": [ + "bash scripts/install-hooks.sh" + ], + "post-update-cmd": [ + "bash scripts/install-hooks.sh" + ] +} } diff --git a/scripts/install-hooks.sh b/scripts/install-hooks.sh new file mode 100644 index 0000000..1ff6e13 --- /dev/null +++ b/scripts/install-hooks.sh @@ -0,0 +1,9 @@ +#!/bin/bash +HOOKS_DIR=".git/hooks" +HOOK_FILE="pre-commit" + +echo "Installing pre-commit hook..." +mkdir -p "$HOOKS_DIR" +cp ./scripts/$HOOK_FILE "$HOOKS_DIR/$HOOK_FILE" +chmod +x "$HOOKS_DIR/$HOOK_FILE" +echo "Pre-commit hook installed successfully." \ No newline at end of file From f6f8dc94ad8b7f3404428682b021cc3eb9134f53 Mon Sep 17 00:00:00 2001 From: TakeshiDaveau Date: Fri, 17 Jan 2025 16:39:44 +0100 Subject: [PATCH 11/15] build: remove useless dev dependencies --- composer.json | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/composer.json b/composer.json index 5b5bdd7..70888e3 100644 --- a/composer.json +++ b/composer.json @@ -16,11 +16,7 @@ }, "require-dev": { "prestashop/php-dev-tools": "^5.0", - "friendsofphp/php-cs-fixer": "^2.19", - "phpstan/phpstan": "^2.1", - "prestashop/header-stamp": "^2.3", - "prestashop/autoindex": "^2.1", - "squizlabs/php_codesniffer": "^3.11" + "friendsofphp/php-cs-fixer": "^2.19" }, "config": { "allow-plugins": { From 3596b705509a265757a16248fd65be159c457b6f Mon Sep 17 00:00:00 2001 From: TakeshiDaveau Date: Mon, 20 Jan 2025 14:45:15 +0100 Subject: [PATCH 12/15] ci: do not install dev tools in CI --- .github/workflows/billing-qc-php.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/billing-qc-php.yml b/.github/workflows/billing-qc-php.yml index cac85cc..bb6e512 100644 --- a/.github/workflows/billing-qc-php.yml +++ b/.github/workflows/billing-qc-php.yml @@ -60,7 +60,7 @@ jobs: run: composer update - name: Install dependencies - run: composer install + run: composer install -no-dev - name: Download and Run PHPUnit run: | From 049aca88a4541231afa2cff6eb3e1327dd82e32b Mon Sep 17 00:00:00 2001 From: TakeshiDaveau Date: Mon, 20 Jan 2025 14:46:21 +0100 Subject: [PATCH 13/15] ci: do not updte dev tools in CI --- .github/workflows/billing-qc-php.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/billing-qc-php.yml b/.github/workflows/billing-qc-php.yml index bb6e512..2da5a38 100644 --- a/.github/workflows/billing-qc-php.yml +++ b/.github/workflows/billing-qc-php.yml @@ -57,10 +57,10 @@ jobs: uses: actions/checkout@v3 - name: Update dependencies - run: composer update + run: composer update --no-dev - name: Install dependencies - run: composer install -no-dev + run: composer install --no-dev - name: Download and Run PHPUnit run: | From a3382665b18c3e0f426d57a7c6fcc449bde8bb9a Mon Sep 17 00:00:00 2001 From: TakeshiDaveau Date: Mon, 20 Jan 2025 14:49:49 +0100 Subject: [PATCH 14/15] chore: change version for php-cs-fixer --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 70888e3..17055b4 100644 --- a/composer.json +++ b/composer.json @@ -15,7 +15,7 @@ "php": ">=5.6" }, "require-dev": { - "prestashop/php-dev-tools": "^5.0", + "prestashop/php-dev-tools": ">=3.0", "friendsofphp/php-cs-fixer": "^2.19" }, "config": { From 073a7257f69a3117b174da5ed45c1225a8e87e28 Mon Sep 17 00:00:00 2001 From: TakeshiDaveau Date: Mon, 20 Jan 2025 14:53:10 +0100 Subject: [PATCH 15/15] ci: revert useless --no-dev --- .github/workflows/billing-qc-php.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/billing-qc-php.yml b/.github/workflows/billing-qc-php.yml index 2da5a38..cac85cc 100644 --- a/.github/workflows/billing-qc-php.yml +++ b/.github/workflows/billing-qc-php.yml @@ -57,10 +57,10 @@ jobs: uses: actions/checkout@v3 - name: Update dependencies - run: composer update --no-dev + run: composer update - name: Install dependencies - run: composer install --no-dev + run: composer install - name: Download and Run PHPUnit run: |