From a17287f80fd566eabdcde466af79b4484b306fc6 Mon Sep 17 00:00:00 2001 From: Jens Konopka Date: Tue, 13 Jul 2021 08:50:27 +0200 Subject: [PATCH 1/2] add checks for switching payment method in myaccount --- meta/documents/changelog_de.md | 7 +- meta/documents/changelog_en.md | 7 +- meta/documents/user_guide_de.md | 4 + plugin.json | 2 +- src/Methods/PaymentAbstract.php | 42 +++++++--- src/Methods/PaymentOrderValidator.php | 110 ++++++++++++++++++++++++++ 6 files changed, 157 insertions(+), 15 deletions(-) create mode 100644 src/Methods/PaymentOrderValidator.php diff --git a/meta/documents/changelog_de.md b/meta/documents/changelog_de.md index 3e2ee413..03b89944 100644 --- a/meta/documents/changelog_de.md +++ b/meta/documents/changelog_de.md @@ -1,6 +1,11 @@ # Release Notes für PAYONE -## 2.2.1 +## 2.1.2 + +### Behoben +- Bei der Prüfung, ob eine Zahlungsart für die nachträgliche Bezahlung zur Verfügung steht, werden die entsprechenden Regeln angewendet, die auch für den Checkout gelten. + +## 2.1.1 ### Behoben - Durch eine fehlerhafte Konfiguration des Plugins konnte es Fehler im Webshop geben, dies wurde behoben. diff --git a/meta/documents/changelog_en.md b/meta/documents/changelog_en.md index 4bc6c3aa..159837e6 100644 --- a/meta/documents/changelog_en.md +++ b/meta/documents/changelog_en.md @@ -1,6 +1,11 @@ # Release Notes for PAYONE -## 2.2.1 +## 2.1.2 + +### Fixed +- When checking whether a payment method is available for subsequent payment, the same rules that are applied to the checkout als apply here. + +## 2.1.1 ### Fixed - Due to a faulty configuration of the plugin there could be errors in the webshop, this has been fixed. diff --git a/meta/documents/user_guide_de.md b/meta/documents/user_guide_de.md index f8d3e811..c77aa9cc 100644 --- a/meta/documents/user_guide_de.md +++ b/meta/documents/user_guide_de.md @@ -42,3 +42,7 @@ https://www.psg-projektmanagement.de/payone-plentymarkets/ Termin vereinbaren: hier klicken + +Die PSG Projektmanagement GmbH ist ein langjähriger Partner der PAYONE, sowie ein langjähriger Partner von plentymarkets und ist somit Dein zentraler Ansprechpartner der optimal mit allen Partnern vernetzt ist. Bitte wende dich nicht direkt an PAYONE, um den reibungslosen Ablauf nicht zu gefährden. + +Hier der offizielle Link zur Partnerschaft mit PAYONE: [https://payone.com/DE-de/plentymarkets-payone-plugin](https://payone.com/DE-de/plentymarkets-payone-plugin) diff --git a/plugin.json b/plugin.json index 84009a97..9d23b2a5 100644 --- a/plugin.json +++ b/plugin.json @@ -1,5 +1,5 @@ { - "version": "2.1.1", + "version": "2.1.2", "license": "MIT", "pluginIcon": "icon_plugin_xs.png", "price": 0.0, diff --git a/src/Methods/PaymentAbstract.php b/src/Methods/PaymentAbstract.php index 1d309816..2243c813 100644 --- a/src/Methods/PaymentAbstract.php +++ b/src/Methods/PaymentAbstract.php @@ -38,7 +38,8 @@ public function __construct( Application $application, PaymentValidator $paymentValidator, SettingsService $settingsService - ) { + ) + { $this->paymentValidator = $paymentValidator; $this->app = $app = $application; $this->settingsService = $settingsService; @@ -63,7 +64,7 @@ public function getName(string $lang = 'de'): string { /** @var Translator $translator */ $translator = pluginApp(Translator::class); - return $translator->trans('Payone::PaymentMethods.'.$this::PAYMENT_CODE, [], $lang); + return $translator->trans('Payone::PaymentMethods.' . $this::PAYMENT_CODE, [], $lang); } /** @@ -93,7 +94,7 @@ public function getDescription(string $lang = 'de'): string { /** @var Translator $translator */ $translator = pluginApp(Translator::class); - return $translator->trans('Payone::PaymentMethods.'.$this::PAYMENT_CODE.'_DESCRIPTION', [], $lang); + return $translator->trans('Payone::PaymentMethods.' . $this::PAYMENT_CODE . '_DESCRIPTION', [], $lang); } /** @@ -109,9 +110,9 @@ public function getCode(): string */ public function getMaxCartAmount(): float { - $amount = $this->settingsService->getPaymentSettingsValue('MaximumAmount',$this::PAYMENT_CODE); + $amount = $this->settingsService->getPaymentSettingsValue('MaximumAmount', $this::PAYMENT_CODE); - return $amount ? (float) $amount : 0.; + return $amount ? (float)$amount : 0.; } /** @@ -119,9 +120,9 @@ public function getMaxCartAmount(): float */ public function getMinCartAmount(): float { - $amount = $this->settingsService->getPaymentSettingsValue('MinimumAmount',$this::PAYMENT_CODE); + $amount = $this->settingsService->getPaymentSettingsValue('MinimumAmount', $this::PAYMENT_CODE); - return $amount ? (float) $amount : 0.; + return $amount ? (float)$amount : 0.; } /** @@ -155,7 +156,7 @@ public function isBackendActive(): bool /** * Get name for the backend * - * @param string $lang + * @param string $lang * @return string */ public function getBackendName(string $lang = 'de'): string @@ -181,13 +182,13 @@ public function canHandleSubscriptions(): bool public function getBackendIcon(): string { $app = pluginApp(Application::class); - $icon = $app->getUrlPath(PluginConstants::NAME).'/images/logos/'.strtolower($this::PAYMENT_CODE).'_backend_icon.svg'; + $icon = $app->getUrlPath(PluginConstants::NAME) . '/images/logos/' . strtolower($this::PAYMENT_CODE) . '_backend_icon.svg'; return $icon; } /** * Can the delivery address be different from the invoice address? - * + * * @return bool */ public function canHandleDifferingDeliveryAddress(): bool @@ -197,7 +198,7 @@ public function canHandleDifferingDeliveryAddress(): bool /** * Check if all settings for the payment method are set. - * + * * @param SettingsService $settingsService * @return bool */ @@ -208,7 +209,7 @@ public function validateSettings(SettingsService $settingsService): bool /** * Is the payment method active for the given currency? - * + * * @param $currency * @return bool */ @@ -216,4 +217,21 @@ public function isActiveForCurrency($currency): bool { return true; } + + /** + * @param int|null $orderId + * @return bool + */ + public function isSwitchableTo($orderId = null): bool + { + if($orderId > 0) { + /** @var PaymentOrderValidator $paymentOrderValidator */ + $paymentOrderValidator = pluginApp(PaymentOrderValidator::class); + + return (bool)$this->settingsService->getPaymentSettingsValue('active', $this::PAYMENT_CODE) + && $paymentOrderValidator->validate($this, $this->settingsService, $orderId); + } + + return false; + } } diff --git a/src/Methods/PaymentOrderValidator.php b/src/Methods/PaymentOrderValidator.php new file mode 100644 index 00000000..c13f495c --- /dev/null +++ b/src/Methods/PaymentOrderValidator.php @@ -0,0 +1,110 @@ +addressHelper = $addressHelper; + $this->logger = $logger; + } + + /** + * @param PaymentAbstract $payment + * + * @return bool + */ + public function validate(PaymentAbstract $payment, SettingsService $settingsService, int $orderId) + { + /** @var OrderRepositoryContract $orderRepositoryContract */ + $orderRepositoryContract = pluginApp(OrderRepositoryContract::class); + /** @var AuthHelper $authHelper */ + $authHelper = pluginApp(AuthHelper::class); + /** @var Order $order */ + $order = $authHelper->processUnguarded( + function () use ($orderRepositoryContract, $orderId) { + return $orderRepositoryContract->findOrderById($orderId); + } + ); + + $orderAmount = $order->amount->invoiceTotal; + if ($payment->getMinCartAmount() && $orderAmount < $payment->getMinCartAmount()) { + $this->log($payment->getName(), 'Payment.minCartAmount', $orderAmount); + return false; + } + + if ($payment->getMaxCartAmount() && $orderAmount > $payment->getMaxCartAmount()) { + $this->log($payment->getName(), 'Payment.maxCartAmount', $orderAmount); + return false; + } + + $billingAddress = $order->billingAddress; + $deliveryAddress = $order->deliveryAddress; + if (!$billingAddress) { + // TODO: shouldn't this be 'return false'? + return true; + } + + if (!in_array($billingAddress->countryId, $payment->getAllowedCountries())) { + $this->log($payment->getName(), 'Payment.countryNotAllowed', $billingAddress->countryId); + return false; + } + + if (!$payment->canHandleDifferingDeliveryAddress() && $deliveryAddress && $billingAddress->id != $deliveryAddress->id) { + return false; + } + + if (!$payment->validateSettings($settingsService)) { + return false; + } + + if (!$payment->isActiveForCurrency($order->amount->currency)) { + return false; + } + + return true; + } + + /** + * @param string $payment + * @param string $code + * @param string $value + */ + protected function log($payment, $code, $value) + { + $logger = $this->logger->setIdentifier(__METHOD__); + $logger->debug( + $code, + [ + 'basketID' => $this->basket->id, + 'customer' => $this->basket->customerId, + 'payment' => $payment, + 'value' => $value, + ] + ); + } +} From 255bc03c84f8a3cc2417697b526043e28256bbcb Mon Sep 17 00:00:00 2001 From: Jens Konopka Date: Wed, 14 Jul 2021 07:31:32 +0200 Subject: [PATCH 2/2] Update src/Methods/PaymentOrderValidator.php Co-authored-by: Emmanouil Stafilarakis --- src/Methods/PaymentOrderValidator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Methods/PaymentOrderValidator.php b/src/Methods/PaymentOrderValidator.php index c13f495c..87324d9b 100644 --- a/src/Methods/PaymentOrderValidator.php +++ b/src/Methods/PaymentOrderValidator.php @@ -47,7 +47,7 @@ public function validate(PaymentAbstract $payment, SettingsService $settingsServ /** @var Order $order */ $order = $authHelper->processUnguarded( function () use ($orderRepositoryContract, $orderId) { - return $orderRepositoryContract->findOrderById($orderId); + return $orderRepositoryContract->findById($orderId, ['amounts', 'addresses']); } );