diff --git a/controllers/front/bancontactAjax.php b/controllers/front/bancontactAjax.php index 588259705..777aaed4c 100644 --- a/controllers/front/bancontactAjax.php +++ b/controllers/front/bancontactAjax.php @@ -66,7 +66,7 @@ private function createTransaction() $paymentMethod, $orderNumber ); - $newPayment = $this->module->api->payments->create($paymentData->jsonSerialize(), ['include' => 'details.qrCode']); + $newPayment = $this->module->getApiClient()->payments->create($paymentData->jsonSerialize(), ['include' => 'details.qrCode']); $this->ajaxDie(json_encode( [ diff --git a/controllers/front/payScreen.php b/controllers/front/payScreen.php index 3b9de0164..a627d00cb 100644 --- a/controllers/front/payScreen.php +++ b/controllers/front/payScreen.php @@ -58,7 +58,7 @@ public function setMedia() $profileIdProvider = $this->module->getMollieContainer(ProfileIdProviderInterface::class); Media::addJsDef([ - 'profileId' => $profileIdProvider->getProfileId($this->module->api), + 'profileId' => $profileIdProvider->getProfileId($this->module->getApiClient()), ]); $this->addJS("{$this->module->getPathUri()}views/js/front/mollie_iframe.js"); $this->addCSS("{$this->module->getPathUri()}views/css/mollie_iframe.css"); diff --git a/controllers/front/return.php b/controllers/front/return.php index e655bc5f5..3c56cb176 100644 --- a/controllers/front/return.php +++ b/controllers/front/return.php @@ -217,9 +217,9 @@ protected function processGetStatus() $isOrder = TransactionUtility::isOrderTransaction($transactionId); if ($isOrder) { - $transaction = $this->module->api->orders->get($transactionId, ['embed' => 'payments']); + $transaction = $this->module->getApiClient()->orders->get($transactionId, ['embed' => 'payments']); } else { - $transaction = $this->module->api->payments->get($transactionId); + $transaction = $this->module->getApiClient()->payments->get($transactionId); } $orderStatus = $transaction->status; diff --git a/controllers/front/webhook.php b/controllers/front/webhook.php index 1d0f9f03e..97e7caa74 100644 --- a/controllers/front/webhook.php +++ b/controllers/front/webhook.php @@ -76,16 +76,16 @@ protected function executeWebhook() $this->respond('failed', HttpStatusCode::HTTP_UNPROCESSABLE_ENTITY, 'Missing transaction id'); } - if (!$this->module->api) { + if (!$this->module->getApiClient()) { $this->respond('failed', HttpStatusCode::HTTP_UNAUTHORIZED, 'API key is missing or incorrect'); } try { if (TransactionUtility::isOrderTransaction($transactionId)) { - $transaction = $this->module->api->orders->get($transactionId, ['embed' => 'payments']); + $transaction = $this->module->getApiClient()->orders->get($transactionId, ['embed' => 'payments']); } else { - $transaction = $this->module->api->payments->get($transactionId); + $transaction = $this->module->getApiClient()->payments->get($transactionId); if ($transaction->orderId) { - $transaction = $this->module->api->orders->get($transaction->orderId, ['embed' => 'payments']); + $transaction = $this->module->getApiClient()->orders->get($transaction->orderId, ['embed' => 'payments']); } } $metaData = $transaction->metadata; diff --git a/mollie.php b/mollie.php index fd13aa287..d72b37322 100644 --- a/mollie.php +++ b/mollie.php @@ -24,6 +24,7 @@ use Mollie\Service\ExceptionService; use Mollie\Utility\PsVersionUtility; use Mollie\Verification\IsPaymentInformationAvailable; +use PrestaShop\PrestaShop\Core\Localization\Locale\Repository; require_once __DIR__ . '/vendor/autoload.php'; @@ -68,7 +69,7 @@ public function __construct() parent::__construct(); - $this->ps_versions_compliancy = ['min' => '1.7', 'max' => _PS_VERSION_]; + $this->ps_versions_compliancy = ['min' => '1.7', 'max' => '1.7.8.9']; $this->displayName = $this->l('Mollie'); $this->description = $this->l('Mollie Payments'); @@ -144,6 +145,15 @@ public function uninstall() return parent::uninstall(); } + public function getApiClient(int $shopId = null) + { + if (!$this->api) { + $this->setApiKey($shopId); + } + + return $this->api; + } + public function enable($force_all = false) { if (!$this->isPhpVersionCompliant()) { @@ -366,11 +376,17 @@ public function hookDisplayHeader(array $params) return; } + $apiClient = $this->getApiClient(); + + if (!$apiClient) { + return ''; + } + /** @var ProfileIdProviderInterface $profileIdProvider */ $profileIdProvider = $this->getMollieContainer(ProfileIdProviderInterface::class); Media::addJsDef([ - 'profileId' => $profileIdProvider->getProfileId($this->api), + 'profileId' => $profileIdProvider->getProfileId($apiClient), 'isoCode' => $this->context->language->locale, 'isTestMode' => \Mollie\Config\Config::isTestMode(), ]); @@ -699,7 +715,9 @@ public function hookActionOrderStatusUpdate(array $params = []) return; } - if (!$this->api) { + $apiClient = $this->getApiClient($this->context->shop->id); + + if (!$apiClient) { return; } @@ -720,7 +738,7 @@ public function hookActionOrderStatusUpdate(array $params = []) $logger = $this->getMollieContainer(PrestaLoggerInterface::class); try { - $shipmentSenderHandler->handleShipmentSender($this->api, $order, $orderStatus); + $shipmentSenderHandler->handleShipmentSender($apiClient, $order, $orderStatus); } catch (ShipmentCannotBeSentException $exception) { $logger->error($exceptionService->getErrorMessageForException( $exception, @@ -796,7 +814,7 @@ public function hookActionEmailSendBefore($params) 'id_order' => (int) $order->id, ]); - $feeTaxIncl = $molOrderPaymentFee->fee_tax_incl ?? 0.00; + $feeTaxIncl = !empty($molOrderPaymentFee->fee_tax_incl) ? (float) $molOrderPaymentFee->fee_tax_incl : 0.00; if (PsVersionUtility::isPsVersionLowerThan(_PS_VERSION_, '1.7.6.0')) { $orderFee = $tools->displayPrice( @@ -804,7 +822,18 @@ public function hookActionEmailSendBefore($params) $orderCurrency ); } else { - $orderFee = $this->context->getCurrentLocale()->formatPrice( + /** + * NOTE: Locale in context is set at init() method but in this case init() doesn't always get executed first. + */ + /** @var Repository $localeRepo */ + $localeRepo = $this->get('prestashop.core.localization.locale.repository'); + + /** + * NOTE: context language is set based on customer/employee context + */ + $locale = $localeRepo->getLocale($this->context->language->getLocale()); + + $orderFee = $locale->formatPrice( $feeTaxIncl, $orderCurrency->iso_code ); @@ -830,19 +859,20 @@ public function hookDisplayPDFInvoice($params): string return ''; } - $localeRepo = $this->get('prestashop.core.localization.locale.repository'); + /** @var InvoicePdfTemplateBuilder $invoiceTemplateBuilder */ + $invoiceTemplateBuilder = $this->getMollieContainer(InvoicePdfTemplateBuilder::class); - if (!$localeRepo) { - return ''; - } + $locale = null; - /** - * NOTE: context language is set based on customer/employee context - */ - $locale = $localeRepo->getLocale($this->context->language->getLocale()); + if (PsVersionUtility::isPsVersionHigherThen(_PS_VERSION_, '1.7.6.0')) { + /** @var Repository $localeRepo */ + $localeRepo = $this->get('prestashop.core.localization.locale.repository'); - /** @var InvoicePdfTemplateBuilder $invoiceTemplateBuilder */ - $invoiceTemplateBuilder = $this->getMollieContainer(InvoicePdfTemplateBuilder::class); + /** + * NOTE: context language is set based on customer/employee context + */ + $locale = $localeRepo->getLocale($this->context->language->getLocale()); + } $templateParams = $invoiceTemplateBuilder ->setOrder($params['object']->getOrder()) @@ -931,6 +961,12 @@ public function hookActionValidateOrder($params) return; } + $apiClient = $this->getApiClient($this->context->shop->id); + + if (!$apiClient) { + return; + } + //NOTE as mollie-email-send is only in manual order creation in backoffice this should work only when mollie payment is chosen. if (!empty(Tools::getValue('mollie-email-send')) && $params['order']->module === $this->name @@ -957,7 +993,7 @@ public function hookActionValidateOrder($params) $orderReference ); - $newPayment = $this->api->payments->create($paymentData->jsonSerialize()); + $newPayment = $apiClient->payments->create($paymentData->jsonSerialize()); /** @var \Mollie\Repository\PaymentMethodRepository $paymentMethodRepository */ $paymentMethodRepository = $this->getMollieContainer(\Mollie\Repository\PaymentMethodRepository::class); diff --git a/src/Application/CommandHandler/RequestApplePayPaymentSessionHandler.php b/src/Application/CommandHandler/RequestApplePayPaymentSessionHandler.php index 9731f9e25..919bfd0e6 100644 --- a/src/Application/CommandHandler/RequestApplePayPaymentSessionHandler.php +++ b/src/Application/CommandHandler/RequestApplePayPaymentSessionHandler.php @@ -39,7 +39,7 @@ public function __construct(Mollie $module, ApiServiceInterface $apiService) public function handle(RequestApplePayPaymentSession $command): array { try { - $response = $this->apiService->requestApplePayPaymentSession($this->module->api, $command->getValidationUrl()); + $response = $this->apiService->requestApplePayPaymentSession($this->module->getApiClient(), $command->getValidationUrl()); } catch (MollieApiException $e) { /* Message is only displayed in console */ return [ diff --git a/src/Builder/FormBuilder.php b/src/Builder/FormBuilder.php index 8995092cc..7774f690d 100644 --- a/src/Builder/FormBuilder.php +++ b/src/Builder/FormBuilder.php @@ -115,7 +115,7 @@ public function __construct( public function buildSettingsForm() { $isApiKeyProvided = (bool) EnvironmentUtility::getApiKey(); - $isApiKeyProvided = ($isApiKeyProvided && $this->module->api !== null); + $isApiKeyProvided = ($isApiKeyProvided && $this->module->getApiClient() !== null); $inputs = $this->getAccountSettingsSection($isApiKeyProvided); @@ -394,7 +394,7 @@ protected function getAccountSettingsSection($isApiKeyProvided) 'name' => '', 'title' => $this->module->l('Payment methods', self::FILE_NAME), ]; - $molliePaymentMethods = $this->apiService->getMethodsForConfig($this->module->api); + $molliePaymentMethods = $this->apiService->getMethodsForConfig($this->module->getApiClient()); if (empty($molliePaymentMethods)) { $input[] = [ diff --git a/src/Builder/InvoicePdfTemplateBuilder.php b/src/Builder/InvoicePdfTemplateBuilder.php index 004f425fb..964c9a9b7 100644 --- a/src/Builder/InvoicePdfTemplateBuilder.php +++ b/src/Builder/InvoicePdfTemplateBuilder.php @@ -27,7 +27,7 @@ final class InvoicePdfTemplateBuilder implements TemplateBuilderInterface private $order; /** @var MolOrderPaymentFeeRepositoryInterface */ private $molOrderPaymentFeeRepository; - /** @var Locale */ + /** @var Locale|null */ private $locale; /** @var CurrencyRepositoryInterface */ private $currencyRepository; @@ -51,7 +51,10 @@ public function setOrder(Order $order): InvoicePdfTemplateBuilder return $this; } - public function setLocale(Locale $locale): InvoicePdfTemplateBuilder + /** + * @param Locale|null $locale + */ + public function setLocale($locale): InvoicePdfTemplateBuilder { $this->locale = $locale; diff --git a/src/Service/CancelService.php b/src/Service/CancelService.php index 6cf09599f..a34af7abc 100644 --- a/src/Service/CancelService.php +++ b/src/Service/CancelService.php @@ -52,7 +52,7 @@ public function doCancelOrderLines($transactionId, $lines = []) { try { /** @var Order $payment */ - $order = $this->module->api->orders->get($transactionId, ['embed' => 'payments']); + $order = $this->module->getApiClient()->orders->get($transactionId, ['embed' => 'payments']); if ([] === $lines) { $order->cancel(); } else { diff --git a/src/Service/ConfigFieldService.php b/src/Service/ConfigFieldService.php index a9efc5585..0a150b18a 100644 --- a/src/Service/ConfigFieldService.php +++ b/src/Service/ConfigFieldService.php @@ -99,8 +99,8 @@ public function getConfigFieldsValues() Config::MOLLIE_KLARNA_INVOICE_ON => Configuration::get(Config::MOLLIE_KLARNA_INVOICE_ON), ]; - if (Mollie\Utility\EnvironmentUtility::getApiKey() && $this->module->api !== null) { - foreach ($this->apiService->getMethodsForConfig($this->module->api) as $method) { + if (Mollie\Utility\EnvironmentUtility::getApiKey() && $this->module->getApiClient() !== null) { + foreach ($this->apiService->getMethodsForConfig($this->module->getApiClient()) as $method) { $countryIds = $this->countryRepository->getMethodCountryIds($method['id']); if ($countryIds) { $configFields = array_merge($configFields, [Config::MOLLIE_COUNTRIES . $method['id'] . '[]' => $countryIds]); diff --git a/src/Service/CustomerService.php b/src/Service/CustomerService.php index d73e926c1..73bd4c4d9 100644 --- a/src/Service/CustomerService.php +++ b/src/Service/CustomerService.php @@ -96,7 +96,7 @@ public function getCustomer(int $customerId) public function createCustomer($name, $email) { try { - return $this->mollie->api->customers->create( + return $this->mollie->getApiClient()->customers->create( [ 'name' => $name, 'email' => $email, diff --git a/src/Service/MollieOrderCreationService.php b/src/Service/MollieOrderCreationService.php index 389e41b1c..4214cbdb4 100644 --- a/src/Service/MollieOrderCreationService.php +++ b/src/Service/MollieOrderCreationService.php @@ -140,10 +140,10 @@ private function createPayment($data, $selectedApi) try { if (Config::MOLLIE_ORDERS_API === $selectedApi) { /** @var MollieOrderAlias $payment */ - $payment = $this->module->api->orders->create($data, ['embed' => 'payments']); + $payment = $this->module->getApiClient()->orders->create($data, ['embed' => 'payments']); } else { /** @var MolliePaymentAlias $payment */ - $payment = $this->module->api->payments->create($data); + $payment = $this->module->getApiClient()->payments->create($data); } return $payment; diff --git a/src/Service/MollieOrderInfoService.php b/src/Service/MollieOrderInfoService.php index c3153029a..c2c3d7ebb 100644 --- a/src/Service/MollieOrderInfoService.php +++ b/src/Service/MollieOrderInfoService.php @@ -80,7 +80,7 @@ public function displayMollieOrderInfo($input) $transaction = $this->paymentMethodRepository->getPaymentBy('transaction_id', $transactionId); $order = new Order($transaction['order_id']); $this->module->updateApiKey($order->id_shop); - if (!$this->module->api) { + if (!$this->module->getApiClient()) { return ['success' => false]; } try { @@ -96,12 +96,12 @@ public function displayMollieOrderInfo($input) return [ 'success' => isset($status['status']) && 'success' === $status['status'], - 'payment' => $this->apiService->getFilteredApiPayment($this->module->api, $transactionId, false), + 'payment' => $this->apiService->getFilteredApiPayment($this->module->getApiClient(), $transactionId, false), ]; case 'retrieve': return [ 'success' => true, - 'payment' => $this->apiService->getFilteredApiPayment($this->module->api, $transactionId, false), + 'payment' => $this->apiService->getFilteredApiPayment($this->module->getApiClient(), $transactionId, false), ]; default: return ['success' => false]; @@ -117,21 +117,21 @@ public function displayMollieOrderInfo($input) return [ 'success' => true, - 'order' => $this->apiService->getFilteredApiOrder($this->module->api, $transactionId), + 'order' => $this->apiService->getFilteredApiOrder($this->module->getApiClient(), $transactionId), 'tracking' => $tracking, ]; case 'ship': $status = $this->shipService->doShipOrderLines($transactionId, isset($input['orderLines']) ? $input['orderLines'] : [], isset($input['tracking']) ? $input['tracking'] : null); - return array_merge($status, ['order' => $this->apiService->getFilteredApiOrder($this->module->api, $transactionId)]); + return array_merge($status, ['order' => $this->apiService->getFilteredApiOrder($this->module->getApiClient(), $transactionId)]); case 'refund': $status = $this->refundService->doRefundOrderLines($input['order'], isset($input['orderLines']) ? $input['orderLines'] : []); - return array_merge($status, ['order' => $this->apiService->getFilteredApiOrder($this->module->api, $input['order']['id'])]); + return array_merge($status, ['order' => $this->apiService->getFilteredApiOrder($this->module->getApiClient(), $input['order']['id'])]); case 'cancel': $status = $this->cancelService->doCancelOrderLines($transactionId, isset($input['orderLines']) ? $input['orderLines'] : []); - return array_merge($status, ['order' => $this->apiService->getFilteredApiOrder($this->module->api, $transactionId)]); + return array_merge($status, ['order' => $this->apiService->getFilteredApiOrder($this->module->getApiClient(), $transactionId)]); default: return ['success' => false]; } diff --git a/src/Service/MolliePaymentMailService.php b/src/Service/MolliePaymentMailService.php index 8cf0d86ce..5423912da 100644 --- a/src/Service/MolliePaymentMailService.php +++ b/src/Service/MolliePaymentMailService.php @@ -73,7 +73,7 @@ public function sendSecondChanceMail($orderId) $this->module->updateApiKey($order->id_shop); /** @var MollieApiClient $api */ - $api = $this->module->api; + $api = $this->module->getApiClient(); try { if (TransactionUtility::isOrderTransaction($transactionId)) { diff --git a/src/Service/PaymentMethodService.php b/src/Service/PaymentMethodService.php index 8321fa787..f99a75759 100644 --- a/src/Service/PaymentMethodService.php +++ b/src/Service/PaymentMethodService.php @@ -188,7 +188,7 @@ public function savePaymentMethod($method) public function getMethodsForCheckout() { $apiKey = EnvironmentUtility::getApiKey(); - if (!$apiKey || $this->module->api === null) { + if (!$apiKey || $this->module->getApiClient() === null) { return []; } /* @phpstan-ignore-next-line */ @@ -478,7 +478,7 @@ private function getSupportedMollieMethods() $cartAmount = $this->orderTotalProvider->getOrderTotal(); /** @var BaseCollection|MethodCollection $methods */ - $methods = $this->module->api->methods->allActive( + $methods = $this->module->getApiClient()->methods->allActive( [ 'resource' => 'orders', 'include' => 'issuers', diff --git a/src/Service/RefundService.php b/src/Service/RefundService.php index dfc6edea7..f21e1ece5 100644 --- a/src/Service/RefundService.php +++ b/src/Service/RefundService.php @@ -59,7 +59,7 @@ public function doPaymentRefund($transactionId, $amount = null) { try { /** @var Payment $payment */ - $payment = $this->module->api->payments->get($transactionId); + $payment = $this->module->getApiClient()->payments->get($transactionId); if ($amount) { $payment->refund([ 'amount' => [ @@ -112,7 +112,7 @@ public function doRefundOrderLines(array $orderData, $lines = []) $availableRefund = $orderData['availableRefundAmount']; try { /** @var MollieOrderAlias $payment */ - $order = $this->module->api->orders->get($transactionId, ['embed' => 'payments']); + $order = $this->module->getApiClient()->orders->get($transactionId, ['embed' => 'payments']); $isOrderLinesRefundPossible = RefundUtility::isOrderLinesRefundPossible($lines, $availableRefund); if ($isOrderLinesRefundPossible) { $refund = RefundUtility::getRefundLines($lines); diff --git a/src/Service/SettingsSaveService.php b/src/Service/SettingsSaveService.php index 860c7d9d6..5244aa095 100644 --- a/src/Service/SettingsSaveService.php +++ b/src/Service/SettingsSaveService.php @@ -168,9 +168,9 @@ public function saveSettings(&$errors = []) return []; } - if ($oldEnvironment === $environment && $apiKey && $this->module->api !== null) { + if ($oldEnvironment === $environment && $apiKey && $this->module->getApiClient() !== null) { $savedPaymentMethods = []; - foreach ($this->apiService->getMethodsForConfig($this->module->api) as $method) { + foreach ($this->apiService->getMethodsForConfig($this->module->getApiClient()) as $method) { $paymentMethodId = $method['obj']->id; try { $paymentMethod = $this->paymentMethodService->savePaymentMethod($method); diff --git a/src/Service/ShipService.php b/src/Service/ShipService.php index 37bbefda2..333139a12 100644 --- a/src/Service/ShipService.php +++ b/src/Service/ShipService.php @@ -43,7 +43,7 @@ public function doShipOrderLines($transactionId, $lines = [], $tracking = null) { try { /** @var MollieOrderAlias $payment */ - $order = $this->module->api->orders->get($transactionId, ['embed' => 'payments']); + $order = $this->module->getApiClient()->orders->get($transactionId, ['embed' => 'payments']); $shipment = [ 'lines' => array_map(function ($line) { return array_intersect_key( diff --git a/src/Service/TransactionService.php b/src/Service/TransactionService.php index 9708662bc..c4451156c 100644 --- a/src/Service/TransactionService.php +++ b/src/Service/TransactionService.php @@ -183,9 +183,10 @@ public function processTransaction($apiPayment) $this->updatePaymentDescription($apiPayment, $orderId); } elseif (strpos($apiPayment->description, OrderNumberUtility::ORDER_NUMBER_PREFIX) === 0) { $this->handlePaymentDescription($apiPayment); - } elseif ($apiPayment->amountRefunded->value > 0) { + } elseif ($orderId) { $this->orderStatusService->setOrderStatus($orderId, $apiPayment->status); } + $orderId = Order::getOrderByCartId((int) $apiPayment->metadata->cart_id); } break; @@ -214,7 +215,7 @@ public function processTransaction($apiPayment) try { $this->shipmentSenderHandler->handleShipmentSender( - $this->module->api, + $this->module->getApiClient(), $order, new \OrderState($order->current_state) ); @@ -294,7 +295,7 @@ public function updateOrderTransaction($transactionId, $orderReference) $transactionInfos = []; $isOrder = TransactionUtility::isOrderTransaction($transactionId); if ($isOrder) { - $transaction = $this->module->api->orders->get($transactionId, ['embed' => 'payments']); + $transaction = $this->module->getApiClient()->orders->get($transactionId, ['embed' => 'payments']); /** @var PaymentCollection|null $payments */ $payments = $transaction->payments(); @@ -307,7 +308,7 @@ public function updateOrderTransaction($transactionId, $orderReference) } } } else { - $transaction = $this->module->api->payments->get($transactionId); + $transaction = $this->module->getApiClient()->payments->get($transactionId); $transactionInfos = $this->getPaymentTransactionInfo($transaction, $transactionInfos); } @@ -455,7 +456,7 @@ private function updateOrderDescription(MollieOrderAlias $apiPayment, int $order $payment->description = 'Order ' . $orderNumber; $payment->update(); } - $this->module->api->orders->update( + $this->module->getApiClient()->orders->update( $apiPayment->id, [ 'orderNumber' => $orderNumber,