Skip to content

Commit

Permalink
Release bug fixes (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
mandan2 committed Aug 29, 2023
1 parent 35f7c08 commit 422baf7
Show file tree
Hide file tree
Showing 19 changed files with 96 additions and 56 deletions.
2 changes: 1 addition & 1 deletion controllers/front/bancontactAjax.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
[
Expand Down
2 changes: 1 addition & 1 deletion controllers/front/payScreen.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions controllers/front/return.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions controllers/front/webhook.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
70 changes: 53 additions & 17 deletions mollie.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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');

Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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(),
]);
Expand Down Expand Up @@ -699,7 +715,9 @@ public function hookActionOrderStatusUpdate(array $params = [])
return;
}

if (!$this->api) {
$apiClient = $this->getApiClient($this->context->shop->id);

if (!$apiClient) {
return;
}

Expand All @@ -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,
Expand Down Expand Up @@ -796,15 +814,26 @@ 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(
$feeTaxIncl,
$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
);
Expand All @@ -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())
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down
4 changes: 2 additions & 2 deletions src/Builder/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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[] = [
Expand Down
7 changes: 5 additions & 2 deletions src/Builder/InvoicePdfTemplateBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/Service/CancelService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/Service/ConfigFieldService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
2 changes: 1 addition & 1 deletion src/Service/CustomerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/Service/MollieOrderCreationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 7 additions & 7 deletions src/Service/MollieOrderInfoService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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];
Expand All @@ -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];
}
Expand Down
2 changes: 1 addition & 1 deletion src/Service/MolliePaymentMailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Service/PaymentMethodService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions src/Service/RefundService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 422baf7

Please sign in to comment.