From fcd7bd2804636561320cdfa37c94c3d6412102c5 Mon Sep 17 00:00:00 2001 From: Michiel Gerritsen Date: Mon, 2 Dec 2024 14:50:07 +0100 Subject: [PATCH 1/9] Improvement: Add test scenario for when the API key is not set #829 --- .github/workflows/end-2-end-test.yml | 13 ++- .../workflows/templates/docker-compose.yml | 1 + .../templates/magento/configure-mollie.sh | 86 +++++++++---------- Test/End-2-end/cypress.config.js | 12 +++ .../cypress/e2e/magento/no-api-key.cy.js | 13 +++ 5 files changed, 77 insertions(+), 48 deletions(-) create mode 100644 Test/End-2-end/cypress/e2e/magento/no-api-key.cy.js diff --git a/.github/workflows/end-2-end-test.yml b/.github/workflows/end-2-end-test.yml index d9a1c9f8c30..c3ba5fcb846 100644 --- a/.github/workflows/end-2-end-test.yml +++ b/.github/workflows/end-2-end-test.yml @@ -51,14 +51,20 @@ jobs: include: - PHP_VERSION: php74-fpm MAGENTO_VERSION: 2.3.7-p4 + NO_API_KEY_TEST: false - PHP_VERSION: php82-fpm MAGENTO_VERSION: 2.4.6-p5 + NO_API_KEY_TEST: false + - PHP_VERSION: php74-fpm + MAGENTO_VERSION: 2.3.7-p4 + NO_API_KEY_TEST: true runs-on: ubuntu-latest env: PHP_VERSION: ${{ matrix.PHP_VERSION }} MAGENTO_VERSION: ${{ matrix.MAGENTO_VERSION }} MOLLIE_API_KEY_TEST: ${{ secrets.MOLLIE_API_KEY_TEST }} NGROK_AUTHTOKEN: ${{ secrets.NGROK_AUTHTOKEN }} + NO_API_KEY_TEST: ${{ matrix.NO_API_KEY_TEST }} CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }} CYPRESS_TESTRAIL_DOMAIN: ${{ secrets.TESTRAIL_DOMAIN }} CYPRESS_TESTRAIL_USERNAME: ${{ secrets.TESTRAIL_USERNAME }} @@ -93,10 +99,15 @@ jobs: docker exec magento-project-community-edition php /data/merge-config.php docker exec magento-project-community-edition ./retry "php bin/magento module:enable Mollie_Payment" docker exec magento-project-community-edition ./retry "php bin/magento setup:upgrade --no-interaction" - docker exec magento-project-community-edition /bin/bash /data/configure-mollie.sh docker exec magento-project-community-edition ./retry "bin/magento config:set payment/mollie_general/use_webhooks custom_url" docker exec magento-project-community-edition ./retry "bin/magento config:set payment/mollie_general/custom_webhook_url ${{ env.magento_url }}/mollie/checkout/webhook" + - name: Configure Mollie + run: | + if [ ${{ matrix.NO_API_KEY_TEST }} == 'false' ]; then + docker exec magento-project-community-edition /bin/bash /data/configure-mollie.sh + fi + - name: Prepare Magento run: | docker exec magento-project-community-edition /bin/bash ./change-base-url https://magento.test/ diff --git a/.github/workflows/templates/docker-compose.yml b/.github/workflows/templates/docker-compose.yml index bb5e8e3355d..54f3f7ddc1b 100644 --- a/.github/workflows/templates/docker-compose.yml +++ b/.github/workflows/templates/docker-compose.yml @@ -34,6 +34,7 @@ services: links: - "nginx-proxy:magento.test" environment: + - NO_API_KEY_TEST - CYPRESS_TESTRAIL_DOMAIN - CYPRESS_TESTRAIL_USERNAME - CYPRESS_TESTRAIL_PASSWORD diff --git a/.github/workflows/templates/magento/configure-mollie.sh b/.github/workflows/templates/magento/configure-mollie.sh index ccabb679fd5..aa40ddf8156 100644 --- a/.github/workflows/templates/magento/configure-mollie.sh +++ b/.github/workflows/templates/magento/configure-mollie.sh @@ -9,67 +9,59 @@ if [ -z "$MOLLIE_API_KEY_TEST" ]; then fi # General configuration -bin/magento config:set payment/mollie_general/profileid pfl_8yCABHRz37 & -magerun2 config:store:set payment/mollie_general/apikey_test $MOLLIE_API_KEY_TEST --encrypt & -bin/magento config:set payment/mollie_general/enabled 1 & -bin/magento config:set payment/mollie_general/type test & +bin/magento config:set payment/mollie_general/profileid pfl_8yCABHRz37 +magerun2 config:store:set payment/mollie_general/apikey_test $MOLLIE_API_KEY_TEST --encrypt +bin/magento config:set payment/mollie_general/enabled 1 +bin/magento config:set payment/mollie_general/type test # Enable all payment methods -bin/magento config:set payment/mollie_methods_applepay/active 1 & -bin/magento config:set payment/mollie_methods_banktransfer/active 1 & -bin/magento config:set payment/mollie_methods_billie/active 1 & -bin/magento config:set payment/mollie_methods_blik/active 1 & -bin/magento config:set payment/mollie_methods_creditcard/active 1 & -bin/magento config:set payment/mollie_methods_giftcard/active 1 & -bin/magento config:set payment/mollie_methods_ideal/active 1 & -bin/magento config:set payment/mollie_methods_kbc/active 1 & -bin/magento config:set payment/mollie_methods_klarnasliceit/active 1 & -bin/magento config:set payment/mollie_methods_paypal/active 1 & -bin/magento config:set payment/mollie_methods_przelewy24/active 1 & -bin/magento config:set payment/mollie_methods_payconiq/active 1 & -bin/magento config:set payment/mollie_methods_alma/active 1 & -bin/magento config:set payment/mollie_methods_bancontact/active 1 & -bin/magento config:set payment/mollie_methods_bancomatpay/active 1 & -bin/magento config:set payment/mollie_methods_belfius/active 1 & -bin/magento config:set payment/mollie_methods_eps/active 1 & -bin/magento config:set payment/mollie_methods_klarnapaylater/active 1 & -bin/magento config:set payment/mollie_methods_paymentlink/active 1 & -bin/magento config:set payment/mollie_methods_paysafecard/active 1 & -bin/magento config:set payment/mollie_methods_pointofsale/active 1 & -bin/magento config:set payment/mollie_methods_riverty/active 1 & -bin/magento config:set payment/mollie_methods_satispay/active 1 & -bin/magento config:set payment/mollie_methods_sofort/active 1 & -bin/magento config:set payment/mollie_methods_trustly/active 1 & -bin/magento config:set payment/mollie_methods_twint/active 1 & +bin/magento config:set payment/mollie_methods_applepay/active 1 +bin/magento config:set payment/mollie_methods_banktransfer/active 1 +bin/magento config:set payment/mollie_methods_billie/active 1 +bin/magento config:set payment/mollie_methods_blik/active 1 +bin/magento config:set payment/mollie_methods_creditcard/active 1 +bin/magento config:set payment/mollie_methods_giftcard/active 1 +bin/magento config:set payment/mollie_methods_ideal/active 1 +bin/magento config:set payment/mollie_methods_kbc/active 1 +bin/magento config:set payment/mollie_methods_klarnasliceit/active 1 +bin/magento config:set payment/mollie_methods_paypal/active 1 +bin/magento config:set payment/mollie_methods_przelewy24/active 1 +bin/magento config:set payment/mollie_methods_payconiq/active 1 +bin/magento config:set payment/mollie_methods_alma/active 1 +bin/magento config:set payment/mollie_methods_bancontact/active 1 +bin/magento config:set payment/mollie_methods_bancomatpay/active 1 +bin/magento config:set payment/mollie_methods_belfius/active 1 +bin/magento config:set payment/mollie_methods_eps/active 1 +bin/magento config:set payment/mollie_methods_klarnapaylater/active 1 +bin/magento config:set payment/mollie_methods_paymentlink/active 1 +bin/magento config:set payment/mollie_methods_paysafecard/active 1 +bin/magento config:set payment/mollie_methods_pointofsale/active 1 +bin/magento config:set payment/mollie_methods_riverty/active 1 +bin/magento config:set payment/mollie_methods_satispay/active 1 +bin/magento config:set payment/mollie_methods_sofort/active 1 +bin/magento config:set payment/mollie_methods_trustly/active 1 +bin/magento config:set payment/mollie_methods_twint/active 1 # Enable Components -bin/magento config:set payment/mollie_methods_creditcard/use_components 1 & - -# Enable QR -bin/magento config:set payment/mollie_methods_ideal/add_qr 1 & - -# Disable webhooks -bin/magento config:set payment/mollie_general/use_webhooks disabled & +bin/magento config:set payment/mollie_methods_creditcard/use_components 1 # Configure currency for the swiss store view -bin/magento config:set currency/options/allow EUR,CHF,PLN & +bin/magento config:set currency/options/allow EUR,CHF,PLN # Swiss scope -bin/magento config:set currency/options/default CHF --scope=ch & -bin/magento config:set payment/mollie_general/currency 0 --scope=ch & +bin/magento config:set currency/options/default CHF --scope=ch --scope-code=ch +bin/magento config:set payment/mollie_general/currency 0 --scope=ch --scope-code=ch # Polish scope -bin/magento config:set currency/options/default PLN --scope=pl & -bin/magento config:set payment/mollie_general/currency 0 --scope=pl & +bin/magento config:set currency/options/default PLN --scope=pl --scope-code=pl +bin/magento config:set payment/mollie_general/currency 0 --scope=pl --scope-code=pl # Disable the use of the base currency -bin/magento config:set payment/mollie_general/currency 0 & +bin/magento config:set payment/mollie_general/currency 0 # Insert rates, otherwise the currency switcher won't show -magerun2 db:query 'INSERT INTO `directory_currency_rate` (`currency_from`, `currency_to`, `rate`) VALUES ("EUR", "PLN", 1.0);' & -magerun2 db:query 'INSERT INTO `directory_currency_rate` (`currency_from`, `currency_to`, `rate`) VALUES ("EUR", "CHF", 1.0);' & - -wait +magerun2 db:query 'INSERT INTO `directory_currency_rate` (`currency_from`, `currency_to`, `rate`) VALUES ("EUR", "PLN", 1.0);' +magerun2 db:query 'INSERT INTO `directory_currency_rate` (`currency_from`, `currency_to`, `rate`) VALUES ("EUR", "CHF", 1.0);' if grep -q Magento_TwoFactorAuth "app/etc/config.php"; then ./retry "php bin/magento module:disable Magento_TwoFactorAuth -f" diff --git a/Test/End-2-end/cypress.config.js b/Test/End-2-end/cypress.config.js index c66c8db37ba..ba45e3e1e73 100644 --- a/Test/End-2-end/cypress.config.js +++ b/Test/End-2-end/cypress.config.js @@ -22,6 +22,18 @@ module.exports = defineConfig({ config.env.CI = true } + config.excludeSpecPattern = ['cypress/e2e/magento/no-api-key.cy.js']; + if (typeof process.env.NO_API_KEY_TEST != 'undefined' && process.env.NO_API_KEY_TEST == 'true') { + console.info('Running tests without API key'); + + config.specPattern = ['cypress/e2e/magento/no-api-key.cy.js']; + config.excludeSpecPattern = []; + + return config; + } + + process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = 0; + // Retrieve available method await new Promise((resolve, reject) => { var https = require('follow-redirects').https; diff --git a/Test/End-2-end/cypress/e2e/magento/no-api-key.cy.js b/Test/End-2-end/cypress/e2e/magento/no-api-key.cy.js new file mode 100644 index 00000000000..d6fc2143695 --- /dev/null +++ b/Test/End-2-end/cypress/e2e/magento/no-api-key.cy.js @@ -0,0 +1,13 @@ +import CheckoutPaymentPage from "Pages/frontend/CheckoutPaymentPage"; +import VisitCheckoutPaymentCompositeAction from "CompositeActions/VisitCheckoutPaymentCompositeAction"; + +const checkoutPaymentPage = new CheckoutPaymentPage(); +const visitCheckoutPayment = new VisitCheckoutPaymentCompositeAction(); + +describe('Test without API key', () => { + it('C4225556: Can still use the webshop when no API key is set and the module is disabled', () => { + visitCheckoutPayment.visit(); + + cy.contains('Ship To:').should('be.visible'); + }); +}); From 95afd7fde5e50b6f89dff714e82f0297e207a6ce Mon Sep 17 00:00:00 2001 From: Michiel Gerritsen Date: Mon, 2 Dec 2024 13:41:48 +0100 Subject: [PATCH 2/9] Bugfix: Fix first setup:upgrade issues #822 --- .github/workflows/end-2-end-test.yml | 3 +- .github/workflows/integration-test.yml | 2 +- .github/workflows/phpstan.yml | 2 +- .github/workflows/unit-test.yml | 2 +- .../Data/AddMollieShipmentIdAttribute.php | 51 ------------------- .../Data/AddMollieTransactionIdAttribute.php | 51 ------------------- Setup/Patch/Data/EncryptFallbackKeys.php | 39 +++++++------- 7 files changed, 22 insertions(+), 128 deletions(-) delete mode 100644 Setup/Patch/Data/AddMollieShipmentIdAttribute.php delete mode 100644 Setup/Patch/Data/AddMollieTransactionIdAttribute.php diff --git a/.github/workflows/end-2-end-test.yml b/.github/workflows/end-2-end-test.yml index c3ba5fcb846..321e15ed93f 100644 --- a/.github/workflows/end-2-end-test.yml +++ b/.github/workflows/end-2-end-test.yml @@ -98,7 +98,8 @@ jobs: run: | docker exec magento-project-community-edition php /data/merge-config.php docker exec magento-project-community-edition ./retry "php bin/magento module:enable Mollie_Payment" - docker exec magento-project-community-edition ./retry "php bin/magento setup:upgrade --no-interaction" + docker exec magento-project-community-edition ./retry "php bin/magento setup:upgrade --keep-generated --no-interaction" + docker exec magento-project-community-edition /bin/bash /data/configure-mollie.sh docker exec magento-project-community-edition ./retry "bin/magento config:set payment/mollie_general/use_webhooks custom_url" docker exec magento-project-community-edition ./retry "bin/magento config:set payment/mollie_general/custom_webhook_url ${{ env.magento_url }}/mollie/checkout/webhook" diff --git a/.github/workflows/integration-test.yml b/.github/workflows/integration-test.yml index eb2e6226f6c..0943c659387 100644 --- a/.github/workflows/integration-test.yml +++ b/.github/workflows/integration-test.yml @@ -30,7 +30,7 @@ jobs: run: docker cp $(pwd) magento-project-community-edition:/data/extensions/ && docker exec magento-project-community-edition ./install-composer-package mollie/magento2:@dev - name: Activate the extension - run: docker exec magento-project-community-edition ./retry "php bin/magento module:enable Mollie_Payment && php bin/magento setup:upgrade" + run: docker exec magento-project-community-edition ./retry "php bin/magento module:enable Mollie_Payment && php bin/magento setup:upgrade --keep-generated" - name: Run tests run: docker exec magento-project-community-edition bash -c "cd /data/dev/tests/integration/ && /data/vendor/bin/phpunit -c /data/dev/tests/integration/phpunit.xml" diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml index 8dc8ba01434..b637cc025bf 100644 --- a/.github/workflows/phpstan.yml +++ b/.github/workflows/phpstan.yml @@ -37,7 +37,7 @@ jobs: run: docker exec magento-project-community-edition ./install-composer-package mollie/magento2:@dev - name: Activate the extension - run: docker exec magento-project-community-edition ./retry "php bin/magento module:enable Mollie_Payment && php bin/magento setup:upgrade && php bin/magento setup:di:compile" + run: docker exec magento-project-community-edition ./retry "php bin/magento module:enable Mollie_Payment && php bin/magento setup:upgrade --keep-generated && php bin/magento setup:di:compile" - name: Run PHPStan continue-on-error: ${{ matrix.PHPSTAN_LEVEL == 2 }} diff --git a/.github/workflows/unit-test.yml b/.github/workflows/unit-test.yml index 73e1f052807..966fe3774f4 100644 --- a/.github/workflows/unit-test.yml +++ b/.github/workflows/unit-test.yml @@ -30,7 +30,7 @@ jobs: run: docker cp $(pwd) magento-project-community-edition:/data/extensions/ && docker exec magento-project-community-edition ./install-composer-package mollie/magento2:@dev - name: Activate the extension - run: docker exec magento-project-community-edition ./retry "php bin/magento module:enable Mollie_Payment && php bin/magento setup:upgrade && php bin/magento setup:di:compile" + run: docker exec magento-project-community-edition ./retry "php bin/magento module:enable Mollie_Payment && php bin/magento setup:upgrade --keep-generated && php bin/magento setup:di:compile" - name: Run tests run: docker exec magento-project-community-edition bash -c "vendor/bin/phpunit extensions/*/Test/Unit" diff --git a/Setup/Patch/Data/AddMollieShipmentIdAttribute.php b/Setup/Patch/Data/AddMollieShipmentIdAttribute.php deleted file mode 100644 index f1469d39d4f..00000000000 --- a/Setup/Patch/Data/AddMollieShipmentIdAttribute.php +++ /dev/null @@ -1,51 +0,0 @@ -salesSetupFactory = $salesSetupFactory; - } - - public function apply() - { - /** @var \Magento\Sales\Setup\SalesSetup $salesSetup */ - $salesSetup = $this->salesSetupFactory->create(); - - /** - * Add 'mollie_transaction_id' attributes for order - */ - $salesSetup->addAttribute('shipment', 'mollie_shipment_id', [ - 'type' => 'varchar', - 'visible' => false, - 'required' => false - ]); - - return $this; - } - - public function getAliases() - { - return []; - } - - public static function getDependencies() - { - return []; - } -} diff --git a/Setup/Patch/Data/AddMollieTransactionIdAttribute.php b/Setup/Patch/Data/AddMollieTransactionIdAttribute.php deleted file mode 100644 index 61679d1207a..00000000000 --- a/Setup/Patch/Data/AddMollieTransactionIdAttribute.php +++ /dev/null @@ -1,51 +0,0 @@ -salesSetupFactory = $salesSetupFactory; - } - - public function apply() - { - /** @var \Magento\Sales\Setup\SalesSetup $salesSetup */ - $salesSetup = $this->salesSetupFactory->create(); - - /** - * Add 'mollie_transaction_id' attributes for order - */ - $salesSetup->addAttribute('order', 'mollie_transaction_id', [ - 'type' => 'varchar', - 'visible' => false, - 'required' => false - ]); - - return $this; - } - - public function getAliases() - { - return []; - } - - public static function getDependencies() - { - return []; - } -} diff --git a/Setup/Patch/Data/EncryptFallbackKeys.php b/Setup/Patch/Data/EncryptFallbackKeys.php index 27512d1384a..0fc2385750f 100644 --- a/Setup/Patch/Data/EncryptFallbackKeys.php +++ b/Setup/Patch/Data/EncryptFallbackKeys.php @@ -8,53 +8,48 @@ namespace Mollie\Payment\Setup\Patch\Data; -use Magento\Framework\Api\Search\SearchCriteriaBuilder; +use Magento\Framework\App\ResourceConnection; use Magento\Framework\Encryption\EncryptorInterface; use Magento\Framework\Setup\Patch\DataPatchInterface; -use Mollie\Payment\Api\ApiKeyFallbackRepositoryInterface; class EncryptFallbackKeys implements DataPatchInterface { - /** - * @var SearchCriteriaBuilder - */ - private $searchCriteriaBuilder; - /** - * @var ApiKeyFallbackRepositoryInterface - */ - private $apiKeyFallbackRepository; /** * @var EncryptorInterface */ private $encryptor; + /** + * @var ResourceConnection + */ + private $resourceConnection; public function __construct( - SearchCriteriaBuilder $searchCriteriaBuilder, - ApiKeyFallbackRepositoryInterface $apiKeyFallbackRepository, - EncryptorInterface $encryptor + EncryptorInterface $encryptor, + ResourceConnection $resourceConnection ) { - $this->searchCriteriaBuilder = $searchCriteriaBuilder; - $this->apiKeyFallbackRepository = $apiKeyFallbackRepository; $this->encryptor = $encryptor; + $this->resourceConnection = $resourceConnection; } public function apply() { - $criteria = $this->searchCriteriaBuilder->create(); - $list = $this->apiKeyFallbackRepository->getList($criteria); + $connection = $this->resourceConnection->getConnection(); + $tableName = $connection->getTableName('mollie_payment_apikey_fallback'); - if ($list->getTotalCount() === 0) { + if (!$connection->isTableExists($tableName)) { return $this; } - foreach ($list->getItems() as $item) { - $start = substr($item->getApiKey(), 0, 4); + $query = 'select * from ' . $tableName; + $result = $connection->fetchAll($query); + foreach ($result as $row) { + $start = substr($row['api_key'], 0, 4); if (!in_array($start, ['live', 'test'])) { continue; } - $item->setApiKey($this->encryptor->encrypt($item->getApiKey())); - $this->apiKeyFallbackRepository->save($item); + $encrypted = $this->encryptor->encrypt($row['api_key']); + $connection->update($tableName, ['api_key' => $encrypted], ['id = ?' => $row['id']]); } return $this; From c27e648d5abe0b2e4d0331f303acd3131ab0dc97 Mon Sep 17 00:00:00 2001 From: Michiel Gerritsen Date: Thu, 21 Nov 2024 11:27:12 +0100 Subject: [PATCH 3/9] Bugfix: Allow to select the correct status for payment link #825 --- Model/Adminhtml/Source/PendingPaymentStatus.php | 17 +++++++++++++++++ etc/adminhtml/methods/paymentlink.xml | 4 ++-- 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 Model/Adminhtml/Source/PendingPaymentStatus.php diff --git a/Model/Adminhtml/Source/PendingPaymentStatus.php b/Model/Adminhtml/Source/PendingPaymentStatus.php new file mode 100644 index 00000000000..1db4377df39 --- /dev/null +++ b/Model/Adminhtml/Source/PendingPaymentStatus.php @@ -0,0 +1,17 @@ + - - Mollie\Payment\Model\Adminhtml\Source\NewStatus + + Mollie\Payment\Model\Adminhtml\Source\PendingPaymentStatus payment/mollie_methods_paymentlink/order_status_new 1 From 4f6be2572e8a27705a1a6b4dbf2cc69a28d0d280 Mon Sep 17 00:00:00 2001 From: Michiel Gerritsen Date: Thu, 14 Nov 2024 13:40:16 +0100 Subject: [PATCH 4/9] Bugfix: Set the region for Apple Pay orders --- Controller/ApplePay/PlaceOrder.php | 39 +++++++++++++-- .../Quote/SetRegionFromApplePayAddress.php | 49 +++++++++++++++++++ .../frontend/web/js/view/applepay/minicart.js | 25 ++++++++++ 3 files changed, 108 insertions(+), 5 deletions(-) create mode 100644 Service/Quote/SetRegionFromApplePayAddress.php diff --git a/Controller/ApplePay/PlaceOrder.php b/Controller/ApplePay/PlaceOrder.php index 835705e62a9..cc37ab51ae0 100644 --- a/Controller/ApplePay/PlaceOrder.php +++ b/Controller/ApplePay/PlaceOrder.php @@ -18,7 +18,9 @@ use Magento\Sales\Api\Data\OrderInterface; use Magento\Sales\Api\OrderRepositoryInterface; use Mollie\Payment\Api\Webapi\PaymentTokenRequestInterface; +use Mollie\Payment\Config; use Mollie\Payment\Service\PaymentToken\Generate; +use Mollie\Payment\Service\Quote\SetRegionFromApplePayAddress; class PlaceOrder extends Action { @@ -56,6 +58,14 @@ class PlaceOrder extends Action * @var OrderRepositoryInterface */ private $orderRepository; + /** + * @var SetRegionFromApplePayAddress + */ + private $setRegionFromApplePayAddress; + /** + * @var Config + */ + private $config; public function __construct( Context $context, @@ -64,7 +74,9 @@ public function __construct( QuoteManagement $quoteManagement, Session $checkoutSession, Generate $paymentToken, - OrderRepositoryInterface $orderRepository + SetRegionFromApplePayAddress $setRegionFromApplePayAddress, + OrderRepositoryInterface $orderRepository, + Config $config ) { parent::__construct($context); @@ -74,6 +86,8 @@ public function __construct( $this->checkoutSession = $checkoutSession; $this->paymentToken = $paymentToken; $this->orderRepository = $orderRepository; + $this->setRegionFromApplePayAddress = $setRegionFromApplePayAddress; + $this->config = $config; } public function execute() @@ -104,8 +118,22 @@ public function execute() $this->cartRepository->save($cart); $cart->getPayment()->addData(['method' => 'mollie_methods_applepay']); - /** @var OrderInterface $order */ - $order = $this->quoteManagement->submit($cart); + $response = $this->resultFactory->create(ResultFactory::TYPE_JSON); + + try { + /** @var OrderInterface $order */ + $order = $this->quoteManagement->submit($cart); + } catch (\Exception $exception) { + $this->config->addToLog('error', [ + 'message' => 'Error while try place Apple Pay order', + 'quote_id' => $cart->getId(), + 'exception' => $exception->getMessage(), + 'trace' => $exception->getTraceAsString(), + ]); + + return $response->setData(['error' => true, 'error_message' => $exception->getMessage()]); + } + $order->getPayment()->setAdditionalInformation( 'applepay_payment_token', $this->getRequest()->getParam('applePayPaymentToken') @@ -126,8 +154,7 @@ public function execute() ->setLastRealOrderId($order->getIncrementId()) ->setLastOrderId($order->getId()); - $response = $this->resultFactory->create(ResultFactory::TYPE_JSON); - return $response->setData(['url' => $url]); + return $response->setData(['url' => $url, 'error' => false, 'error_message' => '']); } private function updateAddress(AddressInterface $address, array $input) @@ -141,6 +168,8 @@ private function updateAddress(AddressInterface $address, array $input) AddressInterface::KEY_POSTCODE => $input['postalCode'], ]); + $this->setRegionFromApplePayAddress->execute($address, $input); + if (isset($input['phoneNumber'])) { $address->setTelephone($input['phoneNumber']); } diff --git a/Service/Quote/SetRegionFromApplePayAddress.php b/Service/Quote/SetRegionFromApplePayAddress.php new file mode 100644 index 00000000000..4171a3af2c0 --- /dev/null +++ b/Service/Quote/SetRegionFromApplePayAddress.php @@ -0,0 +1,49 @@ +countryInformationAcquirer = $countryInformationAcquirer; + } + + public function execute(AddressInterface $address, array $input): void + { + if (!array_key_exists('administrativeArea', $input)) { + return; + } + + try { + $information = $this->countryInformationAcquirer->getCountryInfo($input['countryCode']); + } catch (NoSuchEntityException $exception) { + return; + } + + $regions = $information->getAvailableRegions(); + if ($regions === null) { + $address->setRegion($input['administrativeArea']); + return; + } + + foreach ($regions as $region) { + if ($region->getCode() === $input['administrativeArea']) { + $address->setRegionId($region->getId()); + return; + } + } + } +} diff --git a/view/frontend/web/js/view/applepay/minicart.js b/view/frontend/web/js/view/applepay/minicart.js index a42d16c211d..17d245186f9 100644 --- a/view/frontend/web/js/view/applepay/minicart.js +++ b/view/frontend/web/js/view/applepay/minicart.js @@ -144,6 +144,18 @@ define([ return; } + if (result.error) { + this.sendMessage(result.error_message); + this.session.abort() + return; + } + + if (!result.url) { + this.sendMessage('Something went wrong, please try again later.'); + this.session.abort() + return; + } + this.session.completePayment(ApplePaySession.STATUS_SUCCESS); customerData.invalidate(['cart']); @@ -174,6 +186,19 @@ define([ }.bind(this); this.session.begin(); + }, + + sendMessage: function (message) { + var customerMessages = customerData.get('messages')() || {}, + messages = customerMessages.messages || []; + + messages.push({ + text: message, + type: 'error' + }); + + customerMessages.messages = messages; + customerData.set('messages', customerMessages); } }); }); From 2074cf554750d0e3450013d81b0ce9bc581cd61d Mon Sep 17 00:00:00 2001 From: Michiel Gerritsen Date: Thu, 7 Nov 2024 10:21:16 +0100 Subject: [PATCH 5/9] Improvement: Remove redundant code --- .../Order/Lines/Generator/MirasvitRewards.php | 93 ------------------- etc/di.xml | 1 - 2 files changed, 94 deletions(-) delete mode 100644 Service/Order/Lines/Generator/MirasvitRewards.php diff --git a/Service/Order/Lines/Generator/MirasvitRewards.php b/Service/Order/Lines/Generator/MirasvitRewards.php deleted file mode 100644 index ed9dd421447..00000000000 --- a/Service/Order/Lines/Generator/MirasvitRewards.php +++ /dev/null @@ -1,93 +0,0 @@ -mollieHelper = $mollieHelper; - $this->moduleManager = $moduleManager; - $this->objectManager = $objectManager; - } - - public function process(OrderInterface $order, array $orderLines): array - { - if (!$this->moduleManager->isEnabled('Mirasvit_Rewards')) { - return $orderLines; - } - - // The objectmanager is being used as most of the times this class won't be available. So only load it when we - // are sure that this module is enabled. - /** @var Purchase $purchaseHelper */ - // @phpstan-ignore-next-line - $purchaseHelper = $this->objectManager->create(Purchase::class); - - try { - $purchase = $purchaseHelper->getByOrder($order); - } catch (NoSuchEntityException $exception) { - // No rewards available. Skip it. - return $orderLines; - } - - $this->forceBaseCurrency = (bool)$this->mollieHelper->useBaseCurrency($order->getStoreId()); - $this->currency = $this->forceBaseCurrency ? $order->getBaseCurrencyCode() : $order->getOrderCurrencyCode(); - - $amount = $this->forceBaseCurrency ? $purchase->getBaseSpendAmount() : $purchase->getSpendAmount(); - - if (abs($amount) < 0.01) { - return $orderLines; - } - - $orderLines[] = [ - 'type' => 'surcharge', - 'name' => 'Mirasvit Rewards', - 'quantity' => 1, - 'unitPrice' => $this->mollieHelper->getAmountArray($this->currency, -$amount), - 'totalAmount' => $this->mollieHelper->getAmountArray($this->currency, -$amount), - 'vatRate' => 0, - 'vatAmount' => $this->mollieHelper->getAmountArray($this->currency, 0.0), - ]; - - return $orderLines; - } -} diff --git a/etc/di.xml b/etc/di.xml index 1349db4a581..cbe0d088657 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -162,7 +162,6 @@ Mollie\Payment\Service\Order\Lines\Generator\WeeeFeeGenerator Mollie\Payment\Service\Order\Lines\Generator\MageWorxRewardPoints Mollie\Payment\Service\Order\Lines\Generator\AmastyExtraFee - Mollie\Payment\Service\Order\Lines\Generator\MirasvitRewards Mollie\Payment\Service\Order\Lines\Generator\AheadworksAddFreeGift Mollie\Payment\Service\Order\Lines\Generator\MagentoGiftCard Mollie\Payment\Service\Order\Lines\Generator\MagentoGiftWrapping From 469d30c8107d5abe0562894af6bf928ce270b1ed Mon Sep 17 00:00:00 2001 From: Michiel Gerritsen Date: Mon, 4 Nov 2024 16:03:40 +0100 Subject: [PATCH 6/9] Bugfix: Catch getIssuer api key errors --- Model/MollieConfigProvider.php | 10 ++++++++-- .../Mollie/Wrapper/MollieApiClientFallbackWrapper.php | 7 +++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Model/MollieConfigProvider.php b/Model/MollieConfigProvider.php index 1fe2abd595c..8d6bffd1152 100644 --- a/Model/MollieConfigProvider.php +++ b/Model/MollieConfigProvider.php @@ -223,10 +223,16 @@ public function getActiveMethods(CartInterface $cart = null): array private function getIssuers(string $code, array $config): array { - $mollieApi = $this->mollieApiClient->loadByStore(); $issuerListType = $this->config->getIssuerListType($code, $this->storeManager->getStore()->getId()); $config['payment']['issuersListType'][$code] = $issuerListType; - $config['payment']['issuers'][$code] = $this->getIssuers->execute($mollieApi, $code, $issuerListType); + + try { + $mollieApi = $this->mollieApiClient->loadByStore(); + $config['payment']['issuers'][$code] = $this->getIssuers->execute($mollieApi, $code, $issuerListType); + } catch (\Exception $exception) { + $this->config->addTolog('error', 'Unable to load issuers: ' . $exception->getMessage()); + $config['payment']['issuers'][$code] = []; + } return $config; } diff --git a/Service/Mollie/Wrapper/MollieApiClientFallbackWrapper.php b/Service/Mollie/Wrapper/MollieApiClientFallbackWrapper.php index 14fcc22994a..2249b5857df 100644 --- a/Service/Mollie/Wrapper/MollieApiClientFallbackWrapper.php +++ b/Service/Mollie/Wrapper/MollieApiClientFallbackWrapper.php @@ -2,10 +2,17 @@ namespace Mollie\Payment\Service\Mollie\Wrapper; +use Mollie\Api\Exceptions\ApiException; +use Mollie\Api\Exceptions\IncompatiblePlatform; use Mollie\Api\MollieApiClient; class MollieApiClientFallbackWrapper extends MollieApiClient { + /** + * @throws ApiException If there's an API error during initialization. + * @throws IncompatiblePlatform If the platform is not compatible. + * @return void + */ public function initializeEndpoints() { parent::initializeEndpoints(); From eb88e891bc6b4c27e605f42e5a259f26629d7762 Mon Sep 17 00:00:00 2001 From: Piotr Matras Date: Tue, 26 Nov 2024 15:58:25 +0100 Subject: [PATCH 7/9] Fix fatal error in checkout if no api keys --- Model/MollieConfigProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Model/MollieConfigProvider.php b/Model/MollieConfigProvider.php index 8d6bffd1152..ee650a4cdc2 100644 --- a/Model/MollieConfigProvider.php +++ b/Model/MollieConfigProvider.php @@ -141,7 +141,7 @@ public function getMethodInstance($code) public function getConfig(): array { // Do not load the config when on the cart page. - if ($this->request->getControllerName() === 'cart') { + if (!$this->config->isModuleEnabled() || $this->request->getControllerName() === 'cart') { return []; } From 0e3b6d709e616379274511dd9b230c33fa07c66a Mon Sep 17 00:00:00 2001 From: Michiel Gerritsen Date: Tue, 3 Dec 2024 15:30:55 +0100 Subject: [PATCH 8/9] Fix test --- Test/Integration/Model/MollieConfigProviderTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Test/Integration/Model/MollieConfigProviderTest.php b/Test/Integration/Model/MollieConfigProviderTest.php index 3352feab5c4..ddd5f77a364 100644 --- a/Test/Integration/Model/MollieConfigProviderTest.php +++ b/Test/Integration/Model/MollieConfigProviderTest.php @@ -82,8 +82,16 @@ public function testConfigContainsTheUseComponentsValue() { $configMock = $this->createMock(Config::class); $useComponents = (bool)rand(0, 1); + $configMock->method('isModuleEnabled')->willReturn(true); $configMock->method('creditcardUseComponents')->willReturn($useComponents); + $api = new \Mollie\Api\MollieApiClient; + + /** @var FakeMollieApiClient $fakeMollieApiClient */ + $fakeMollieApiClient = $this->objectManager->get(FakeMollieApiClient::class); + $fakeMollieApiClient->setInstance($api); + $this->objectManager->addSharedInstance($fakeMollieApiClient, \Mollie\Payment\Service\Mollie\MollieApiClient::class); + /** @var MollieConfigProvider $instance */ $instance = $this->objectManager->create(MollieConfigProvider::class, ['config' => $configMock]); $result = $instance->getConfig(); @@ -109,6 +117,7 @@ public function configContainsGeneralSettingsProvider() public function testConfigContainsGeneralSettings($key, $method, $expected) { $configMock = $this->createMock(Config::class); + $configMock->method('isModuleEnabled')->willReturn(true); $configMock->method($method)->willReturn($expected); /** @var MollieConfigProvider $instance */ @@ -122,6 +131,7 @@ public function testConfigContainsGeneralSettings($key, $method, $expected) } /** + * @magentoConfigFixture default_store payment/mollie_general/enabled 1 * @magentoConfigFixture default_store payment/mollie_general/locale nl_NL */ public function testContainsTheLocale() From dfc40b6f1a38b99e0a816a7475f730b27c8e5fd8 Mon Sep 17 00:00:00 2001 From: Marvin Besselsen Date: Wed, 4 Dec 2024 14:08:32 +0100 Subject: [PATCH 9/9] Version bump --- composer.json | 2 +- etc/config.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 4045706ce50..a450e3db88c 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "mollie/magento2", "description": "Mollie Payment Module for Magento 2", - "version": "2.42.0", + "version": "2.42.1", "keywords": [ "mollie", "payment", diff --git a/etc/config.xml b/etc/config.xml index c4036be2ecf..42283167a98 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -8,7 +8,7 @@ - v2.42.0 + v2.42.1 0 0 test