From 0a292c03c0d02abe773b195a14206a2628b10dee Mon Sep 17 00:00:00 2001 From: "nils.baczynski@fatchip.de" Date: Thu, 24 Oct 2024 15:01:32 +0200 Subject: [PATCH 1/4] PSPAYPAL-817 - refactor vaulting: * The rules for when to display which vaulting type are now tied to the payments. * Now the vaulting is only possible and visible if the user is capable of purchasing with the payment that is allocated to the vaulting type. --- src/Controller/OrderController.php | 11 ++++---- .../PayPalVaultingCardController.php | 11 ++++---- src/Controller/PayPalVaultingController.php | 13 +++++----- src/Controller/PaymentController.php | 24 +++-------------- src/Core/PayPalDefinitions.php | 2 -- src/Core/ViewConfig.php | 17 +++++++++++- src/Service/Payment.php | 26 ++++++++++++++++++- .../blocks/page/account/inc/account_menu.tpl | 10 ++++--- 8 files changed, 68 insertions(+), 46 deletions(-) diff --git a/src/Controller/OrderController.php b/src/Controller/OrderController.php index 4fade89d1..24bc6db9d 100644 --- a/src/Controller/OrderController.php +++ b/src/Controller/OrderController.php @@ -92,11 +92,12 @@ public function render() $user = $this->getUser(); if ($user) { - $isVaultingPossible = false; - $moduleSettings = $this->getServiceFromContainer(ModuleSettings::class); - if ($moduleSettings->getIsVaultingActive() && $user->getFieldData('oxpassword')) { - $isVaultingPossible = true; - } + $paymentId = $this->getPayment()->getId(); + $paymentService = $this->getServiceFromContainer(PaymentService::class); + $paymentType = PayPalDefinitions::getPayPalDefinitions()[$paymentId]["vaultingtype"]; + + $isVaultingPossible = $paymentService->isVaultingAllowed($paymentId, $paymentType) + && $user->getFieldData('oxpassword'); $this->addTplParam('oscpaypal_isVaultingPossible', $isVaultingPossible); diff --git a/src/Controller/PayPalVaultingCardController.php b/src/Controller/PayPalVaultingCardController.php index 7e44030b8..84df78655 100644 --- a/src/Controller/PayPalVaultingCardController.php +++ b/src/Controller/PayPalVaultingCardController.php @@ -5,22 +5,23 @@ use OxidEsales\Eshop\Application\Controller\AccountController; use OxidEsales\Eshop\Core\Registry; use OxidSolutionCatalysts\PayPal\Core\ServiceFactory; +use OxidSolutionCatalysts\PayPal\Traits\ServiceContainer; /** * user account menu for saving paypal for purchase later (vaulting without purchase) */ class PayPalVaultingCardController extends AccountController { - /** - * @var string Current class template name. - */ - // phpcs:ignore PSR2.Classes.PropertyDeclaration - protected $_sThisTemplate = 'modules/osc/paypal/account_vaulting_card.tpl'; + use ServiceContainer; public function render() { $this->_aViewData['vaultingUserId'] = $this->getViewConfig()->getUserIdForVaulting(); + if ($this->getViewConfig()->isVaultingAllowedForACDC()) { + $this->_sThisTemplate = 'modules/osc/paypal/account_vaulting_card.tpl'; + } + return parent::render(); } diff --git a/src/Controller/PayPalVaultingController.php b/src/Controller/PayPalVaultingController.php index 7c5c3d1c5..64bc31317 100644 --- a/src/Controller/PayPalVaultingController.php +++ b/src/Controller/PayPalVaultingController.php @@ -2,26 +2,25 @@ namespace OxidSolutionCatalysts\PayPal\Controller; -use mysql_xdevapi\Exception; use OxidEsales\Eshop\Application\Controller\AccountController; -use OxidEsales\Eshop\Core\Registry; use OxidSolutionCatalysts\PayPal\Core\ServiceFactory; +use OxidSolutionCatalysts\PayPal\Traits\ServiceContainer; /** * user account menu for saving paypal for purchase later (vaulting without purchase) */ class PayPalVaultingController extends AccountController { - /** - * @var string Current class template name. - */ - // phpcs:ignore PSR2.Classes.PropertyDeclaration - protected $_sThisTemplate = 'modules/osc/paypal/account_vaulting_paypal.tpl'; + use ServiceContainer; public function render() { $this->_aViewData['vaultingUserId'] = $this->getViewConfig()->getUserIdForVaulting(); + if ($this->getViewConfig()->isVaultingAllowedForPayPal()) { + $this->_sThisTemplate = 'modules/osc/paypal/account_vaulting_paypal.tpl'; + } + return parent::render(); } diff --git a/src/Controller/PaymentController.php b/src/Controller/PaymentController.php index 6bf5116b1..0de89bfac 100644 --- a/src/Controller/PaymentController.php +++ b/src/Controller/PaymentController.php @@ -62,14 +62,12 @@ public function render() $vaultedPaymentSources = []; foreach ($vaultedPaymentTokens as $vaultedPaymentToken) { foreach ($vaultedPaymentToken["payment_source"] as $paymentType => $paymentSource) { - if (!$this->paymentTypeExists($paymentType)) { - continue; - } - if ($paymentType === "card") { + $viewConfig = oxNew(\OxidEsales\Eshop\Core\ViewConfig::class); + if ($paymentType === "card" && $viewConfig->isVaultingAllowedForACDC()) { $string = $lang->translateString("OSC_PAYPAL_CARD_ENDING_IN"); $vaultedPaymentSources[$paymentType][] = $paymentSource["brand"] . " " . $string . $paymentSource["last_digits"]; - } elseif ($paymentType === "paypal") { + } elseif ($paymentType === "paypal"&& $viewConfig->isVaultingAllowedForPayPal()) { $string = $lang->translateString("OSC_PAYPAL_CARD_PAYPAL_PAYMENT"); $vaultedPaymentSources[$paymentType][] = $string . " " . $paymentSource["email_address"]; @@ -88,22 +86,6 @@ public function render() return parent::render(); } - /** - * @param $paymentList - * @param $paymentType - * @return bool - */ - protected function paymentTypeExists($paymentType): bool - { - $paymentList = $this->getPaymentList(); - foreach ($paymentList as $payment) { - if (PayPalDefinitions::isPayPalVaultingPossible($payment->getId(), $paymentType)) { - return true; - } - } - return false; - } - public function getPayPalPuiFraudnetCmId(): string { diff --git a/src/Core/PayPalDefinitions.php b/src/Core/PayPalDefinitions.php index 3e031ef53..cadffc370 100644 --- a/src/Core/PayPalDefinitions.php +++ b/src/Core/PayPalDefinitions.php @@ -134,7 +134,6 @@ final class PayPalDefinitions 'onlybrutto' => false, 'buttonpayment' => false, 'defaulton' => true, - 'vaultingtype' => 'paypal' ], //Express PayPal self::EXPRESS_PAYPAL_PAYMENT_ID => [ @@ -156,7 +155,6 @@ final class PayPalDefinitions 'onlybrutto' => false, 'buttonpayment' => true, 'defaulton' => true, - 'vaultingtype' => 'paypal' ], self::PUI_PAYPAL_PAYMENT_ID => [ 'descriptions' => [ diff --git a/src/Core/ViewConfig.php b/src/Core/ViewConfig.php index c39780793..700cd638d 100644 --- a/src/Core/ViewConfig.php +++ b/src/Core/ViewConfig.php @@ -11,6 +11,7 @@ use OxidEsales\Eshop\Core\Registry; use OxidSolutionCatalysts\PayPal\Service\LanguageLocaleMapper; use OxidSolutionCatalysts\PayPal\Service\Logger; +use OxidSolutionCatalysts\PayPal\Service\Payment as PaymentService; use OxidSolutionCatalysts\PayPal\Traits\ServiceContainer; use OxidSolutionCatalysts\PayPal\Service\ModuleSettings; @@ -278,7 +279,7 @@ public function getUserIdForVaulting(): string $vaultingService = Registry::get(ServiceFactory::class)->getVaultingService(); $response = $vaultingService->generateUserIdToken($payPalCustomerId); - return $response["id_token"]; + return $response["id_token"] ?? ""; } /** @@ -601,4 +602,18 @@ public function isAcdcEligibility(): bool { return $this->getServiceFromContainer(ModuleSettings::class)->isAcdcEligibility(); } + + public function isVaultingAllowedForPayPal() + { + /** @var PaymentService $paymentService */ + $paymentService = $this->getServiceFromContainer(PaymentService::class); + return $paymentService->isVaultingAllowed(PayPalDefinitions::STANDARD_PAYPAL_PAYMENT_ID, "paypal"); + } + + public function isVaultingAllowedForACDC() + { + /** @var PaymentService $paymentService */ + $paymentService = $this->getServiceFromContainer(PaymentService::class); + return $paymentService->isVaultingAllowed(PayPalDefinitions::ACDC_PAYPAL_PAYMENT_ID, "card"); + } } diff --git a/src/Service/Payment.php b/src/Service/Payment.php index 5b3d10de7..0296a014b 100644 --- a/src/Service/Payment.php +++ b/src/Service/Payment.php @@ -14,6 +14,7 @@ use OxidEsales\Eshop\Core\Field; use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Session as EshopSession; +use OxidSolutionCatalysts\PayPal\Controller\PaymentController; use OxidSolutionCatalysts\PayPal\Core\ConfirmOrderRequestFactory; use OxidSolutionCatalysts\PayPal\Core\Constants; use OxidSolutionCatalysts\PayPal\Core\OrderRequestFactory; @@ -32,7 +33,6 @@ use OxidSolutionCatalysts\PayPalApi\Model\Orders\Order; use OxidSolutionCatalysts\PayPalApi\Model\Orders\Order as ApiModelOrder; use OxidSolutionCatalysts\PayPalApi\Model\Orders\Order as ApiOrderModel; -use OxidSolutionCatalysts\PayPalApi\Model\Orders\Order as OrderResponse; use OxidSolutionCatalysts\PayPalApi\Model\Orders\OrderAuthorizeRequest; use OxidSolutionCatalysts\PayPalApi\Model\Orders\OrderCaptureRequest; use OxidSolutionCatalysts\PayPalApi\Model\Payments\CaptureRequest; @@ -157,6 +157,15 @@ public function doCreatePayPalOrder( return $response; } + + public function isVaultingAllowed($paymentId, $paypalPaymentType) + { + $moduleSettings = $this->getServiceFromContainer(ModuleSettings::class); + + return $this->getIsPaymentActive($paymentId) + && $moduleSettings->getIsVaultingActive() + && PayPalDefinitions::isPayPalVaultingPossible($paymentId, $paypalPaymentType); + } public function doCreatePatchedOrder( EshopModelBasket $basket @@ -808,4 +817,19 @@ private function displayErrorIfInstrumentDeclined(?string $issue): void ); } } + + private function getIsPaymentActive($paymentId): bool + { + $paymentController = oxNew(PaymentController::class); + $paymentList = $paymentController->getPaymentList(); + + /** @var \OxidEsales\Eshop\Application\Model\Payment $payment */ + foreach ($paymentList as $payment) { + if ($payment->getId() == $paymentId) { + return true; + } + } + + return false; + } } diff --git a/views/blocks/page/account/inc/account_menu.tpl b/views/blocks/page/account/inc/account_menu.tpl index 0356d6d55..254c26555 100644 --- a/views/blocks/page/account/inc/account_menu.tpl +++ b/views/blocks/page/account/inc/account_menu.tpl @@ -1,8 +1,10 @@ [{if $oViewConf->getIsVaultingActive()}] - - [{if $oViewConf->isAcdcEligibility() }] + [{if $oViewConf->isVaultingAllowedForPayPal()}] + + [{/if}] + [{if $oViewConf->isAcdcEligibility() && $oViewConf->isVaultingAllowedForACDC()}] From 39b9beea61fefb0230ee609e95f469dda2813b77 Mon Sep 17 00:00:00 2001 From: Mario Lorenz Date: Fri, 13 Dec 2024 17:03:45 +0100 Subject: [PATCH 2/4] move vaulting-check from Payment-Service to the module-settings --- src/Controller/OrderController.php | 6 +- .../PayPalVaultingCardController.php | 6 +- src/Controller/PayPalVaultingController.php | 7 +- src/Controller/PaymentController.php | 5 +- src/Core/ViewConfig.php | 14 ---- src/Service/ModuleSettings.php | 51 +++++++++++++- src/Service/Payment.php | 69 +++++-------------- 7 files changed, 79 insertions(+), 79 deletions(-) diff --git a/src/Controller/OrderController.php b/src/Controller/OrderController.php index 24bc6db9d..b761bd518 100644 --- a/src/Controller/OrderController.php +++ b/src/Controller/OrderController.php @@ -93,10 +93,8 @@ public function render() if ($user) { $paymentId = $this->getPayment()->getId(); - $paymentService = $this->getServiceFromContainer(PaymentService::class); - $paymentType = PayPalDefinitions::getPayPalDefinitions()[$paymentId]["vaultingtype"]; - - $isVaultingPossible = $paymentService->isVaultingAllowed($paymentId, $paymentType) + $moduleSettings = $this->getServiceFromContainer(ModuleSettings::class); + $isVaultingPossible = $moduleSettings->isVaultingAllowedForPayment($paymentId) && $user->getFieldData('oxpassword'); $this->addTplParam('oscpaypal_isVaultingPossible', $isVaultingPossible); diff --git a/src/Controller/PayPalVaultingCardController.php b/src/Controller/PayPalVaultingCardController.php index 84df78655..e42a19881 100644 --- a/src/Controller/PayPalVaultingCardController.php +++ b/src/Controller/PayPalVaultingCardController.php @@ -5,6 +5,7 @@ use OxidEsales\Eshop\Application\Controller\AccountController; use OxidEsales\Eshop\Core\Registry; use OxidSolutionCatalysts\PayPal\Core\ServiceFactory; +use OxidSolutionCatalysts\PayPal\Service\ModuleSettings; use OxidSolutionCatalysts\PayPal\Traits\ServiceContainer; /** @@ -17,11 +18,12 @@ class PayPalVaultingCardController extends AccountController public function render() { $this->_aViewData['vaultingUserId'] = $this->getViewConfig()->getUserIdForVaulting(); + $moduleSettings = $this->getServiceFromContainer(ModuleSettings::class); - if ($this->getViewConfig()->isVaultingAllowedForACDC()) { + if ($moduleSettings->isVaultingAllowedForACDC()) { $this->_sThisTemplate = 'modules/osc/paypal/account_vaulting_card.tpl'; } - + return parent::render(); } diff --git a/src/Controller/PayPalVaultingController.php b/src/Controller/PayPalVaultingController.php index 64bc31317..6dc7504e6 100644 --- a/src/Controller/PayPalVaultingController.php +++ b/src/Controller/PayPalVaultingController.php @@ -4,7 +4,9 @@ use OxidEsales\Eshop\Application\Controller\AccountController; use OxidSolutionCatalysts\PayPal\Core\ServiceFactory; +use OxidSolutionCatalysts\PayPal\Service\ModuleSettings; use OxidSolutionCatalysts\PayPal\Traits\ServiceContainer; +use OxidEsales\Eshop\Core\Registry; /** * user account menu for saving paypal for purchase later (vaulting without purchase) @@ -16,11 +18,12 @@ class PayPalVaultingController extends AccountController public function render() { $this->_aViewData['vaultingUserId'] = $this->getViewConfig()->getUserIdForVaulting(); + $moduleSettings = $this->getServiceFromContainer(ModuleSettings::class); - if ($this->getViewConfig()->isVaultingAllowedForPayPal()) { + if ($moduleSettings->isVaultingAllowedForPayPal()) { $this->_sThisTemplate = 'modules/osc/paypal/account_vaulting_paypal.tpl'; } - + return parent::render(); } diff --git a/src/Controller/PaymentController.php b/src/Controller/PaymentController.php index 0de89bfac..cb923ea88 100644 --- a/src/Controller/PaymentController.php +++ b/src/Controller/PaymentController.php @@ -62,12 +62,11 @@ public function render() $vaultedPaymentSources = []; foreach ($vaultedPaymentTokens as $vaultedPaymentToken) { foreach ($vaultedPaymentToken["payment_source"] as $paymentType => $paymentSource) { - $viewConfig = oxNew(\OxidEsales\Eshop\Core\ViewConfig::class); - if ($paymentType === "card" && $viewConfig->isVaultingAllowedForACDC()) { + if ($paymentType === "card" && $moduleSettings->isVaultingAllowedForACDC()) { $string = $lang->translateString("OSC_PAYPAL_CARD_ENDING_IN"); $vaultedPaymentSources[$paymentType][] = $paymentSource["brand"] . " " . $string . $paymentSource["last_digits"]; - } elseif ($paymentType === "paypal"&& $viewConfig->isVaultingAllowedForPayPal()) { + } elseif ($paymentType === "paypal" && $moduleSettings->isVaultingAllowedForPayPal()) { $string = $lang->translateString("OSC_PAYPAL_CARD_PAYPAL_PAYMENT"); $vaultedPaymentSources[$paymentType][] = $string . " " . $paymentSource["email_address"]; diff --git a/src/Core/ViewConfig.php b/src/Core/ViewConfig.php index 700cd638d..51a0d219d 100644 --- a/src/Core/ViewConfig.php +++ b/src/Core/ViewConfig.php @@ -602,18 +602,4 @@ public function isAcdcEligibility(): bool { return $this->getServiceFromContainer(ModuleSettings::class)->isAcdcEligibility(); } - - public function isVaultingAllowedForPayPal() - { - /** @var PaymentService $paymentService */ - $paymentService = $this->getServiceFromContainer(PaymentService::class); - return $paymentService->isVaultingAllowed(PayPalDefinitions::STANDARD_PAYPAL_PAYMENT_ID, "paypal"); - } - - public function isVaultingAllowedForACDC() - { - /** @var PaymentService $paymentService */ - $paymentService = $this->getServiceFromContainer(PaymentService::class); - return $paymentService->isVaultingAllowed(PayPalDefinitions::ACDC_PAYPAL_PAYMENT_ID, "card"); - } } diff --git a/src/Service/ModuleSettings.php b/src/Service/ModuleSettings.php index b2028b2f1..a3295111e 100755 --- a/src/Service/ModuleSettings.php +++ b/src/Service/ModuleSettings.php @@ -22,6 +22,7 @@ use OxidSolutionCatalysts\PayPal\Core\Constants; use OxidSolutionCatalysts\PayPal\Core\PayPalDefinitions; use OxidSolutionCatalysts\PayPal\Module; +use OxidSolutionCatalysts\PayPal\Service\Payment as PaymentService; class ModuleSettings { @@ -43,6 +44,20 @@ class ModuleSettings */ protected $payPalCheckoutExpressPaymentEnabled = null; + /** + * is Vaulting allowed for PayPal + * + * @var bool + */ + protected $isVaultingAllowedForPayPal = null; + + /** + * is Vaulting allowed for ACDC + * + * @var bool + */ + protected $isVaultingAllowedForACDC = null; + /** * Country Restriction for PayPal as comma seperated string * @@ -564,7 +579,7 @@ public function getLegacySettingsTransferStatus(): bool */ public function isPayPalCheckoutExpressPaymentEnabled(): bool { - if ($this->payPalCheckoutExpressPaymentEnabled === null) { + if (is_null($this->payPalCheckoutExpressPaymentEnabled)) { $expressEnabled = false; $payment = oxNew(Payment::class); $payment->load(PayPalDefinitions::EXPRESS_PAYPAL_PAYMENT_ID); @@ -582,6 +597,40 @@ public function isPayPalCheckoutExpressPaymentEnabled(): bool return $this->payPalCheckoutExpressPaymentEnabled; } + /** check if Vaulting is allowed for PayPal */ + public function isVaultingAllowedForPayPal(): bool + { + if (is_null($this->isVaultingAllowedForPayPal)) { + $this->isVaultingAllowedForPayPal = $this->isVaultingAllowedForPayment( + PayPalDefinitions::STANDARD_PAYPAL_PAYMENT_ID + ); + } + return $this->isVaultingAllowedForPayPal; + } + + /** check if Vaulting is allowed for ACDC */ + public function isVaultingAllowedForACDC(): bool + { + if (is_null($this->isVaultingAllowedForACDC)) { + $this->isVaultingAllowedForACDC = $this->isVaultingAllowedForPayment( + PayPalDefinitions::ACDC_PAYPAL_PAYMENT_ID + ); + } + return $this->isVaultingAllowedForACDC; + } + + /** check if Vaulting is allowed for Payment-Method */ + public function isVaultingAllowedForPayment(string $paymentId): bool + { + $payment = oxNew(Payment::class); + $payment->load($paymentId); + $paymentEnabled = (bool)$payment->oxpayments__oxactive->value; + $paymentType = PayPalDefinitions::getPayPalDefinitions()[$paymentId]["vaultingtype"]; + return $paymentEnabled && + $this->getIsVaultingActive() && + PayPalDefinitions::isPayPalVaultingPossible($paymentId, $paymentType); + } + /** * Returns comma seperated String with the Country Restriction for PayPal Express */ diff --git a/src/Service/Payment.php b/src/Service/Payment.php index 1761e8db3..36aab5013 100644 --- a/src/Service/Payment.php +++ b/src/Service/Payment.php @@ -14,6 +14,7 @@ use OxidEsales\Eshop\Core\Field; use OxidEsales\Eshop\Core\Registry; use OxidEsales\Eshop\Core\Session as EshopSession; +use OxidEsales\Eshop\Core\ShopVersion; use OxidSolutionCatalysts\PayPal\Controller\PaymentController; use OxidSolutionCatalysts\PayPal\Core\ConfirmOrderRequestFactory; use OxidSolutionCatalysts\PayPal\Core\Constants; @@ -23,18 +24,13 @@ use OxidSolutionCatalysts\PayPal\Core\PayPalSession; use OxidSolutionCatalysts\PayPal\Core\ServiceFactory; use OxidSolutionCatalysts\PayPal\Exception\PayPalException; -use OxidSolutionCatalysts\PayPal\Exception\UserPhone as UserPhoneException; use OxidSolutionCatalysts\PayPal\Model\PayPalOrder as PayPalOrderModel; use OxidSolutionCatalysts\PayPal\Module; -use OxidSolutionCatalysts\PayPal\Service\ModuleSettings as ModuleSettingsService; use OxidSolutionCatalysts\PayPal\Traits\ServiceContainer; use OxidSolutionCatalysts\PayPalApi\Exception\ApiException; use OxidSolutionCatalysts\PayPalApi\Model\Orders\AuthorizationWithAdditionalData; use OxidSolutionCatalysts\PayPalApi\Model\Orders\ConfirmOrderRequest; use OxidSolutionCatalysts\PayPalApi\Model\Orders\Order; -use OxidSolutionCatalysts\PayPalApi\Model\Orders\Order as ApiModelOrder; -use OxidSolutionCatalysts\PayPalApi\Model\Orders\Order as ApiOrderModel; -use OxidSolutionCatalysts\PayPalApi\Model\Orders\Order as OrderResponse; use OxidSolutionCatalysts\PayPalApi\Model\Orders\OrderAuthorizeRequest; use OxidSolutionCatalysts\PayPalApi\Model\Orders\OrderCaptureRequest; use OxidSolutionCatalysts\PayPalApi\Model\Payments\CaptureRequest; @@ -81,7 +77,7 @@ class Payment /** @var SCAValidatorInterface */ private $scaValidator; - /** @var ModuleSettingsService */ + /** @var ModuleSettings */ private $moduleSettingsService; private $logger; @@ -90,7 +86,7 @@ public function __construct( EshopSession $eshopSession, OrderRepository $orderRepository, SCAValidatorInterface $scaValidator, - ModuleSettingsService $moduleSettingsService, + ModuleSettings $moduleSettingsService, Logger $logger, ServiceFactory $serviceFactory = null, PatchRequestFactory $patchRequestFactory = null, @@ -116,9 +112,7 @@ public function doCreatePayPalOrder( string $payPalPartnerAttributionId = '', string $returnUrl = null, string $cancelUrl = null, - bool $setProvidedAddress = true, - ?EshopModelOrder $order = null - #): ?ApiModelOrder + bool $setProvidedAddress = true ) { //TODO return value $this->setPaymentExecutionError(self::PAYMENT_ERROR_NONE); @@ -160,15 +154,6 @@ public function doCreatePayPalOrder( return $response; } - public function isVaultingAllowed($paymentId, $paypalPaymentType) - { - $moduleSettings = $this->getServiceFromContainer(ModuleSettings::class); - - return $this->getIsPaymentActive($paymentId) - && $moduleSettings->getIsVaultingActive() - && PayPalDefinitions::isPayPalVaultingPossible($paymentId, $paypalPaymentType); - } - public function doCreatePatchedOrder( EshopModelBasket $basket ): array { @@ -235,10 +220,10 @@ public function doCapturePayPalOrder( EshopModelOrder $order, string $checkoutOrderId, string $paymentId, - ApiOrderModel $payPalOrder = null - ): ApiOrderModel { + Order $payPalOrder = null + ): Order { - /** @var ApiOrderModel $payPalOrder */ + /** @var Order $payPalOrder */ if (is_null($payPalOrder) || !isset($payPalOrder->payment_source)) { $payPalOrder = $this->fetchOrderFields($checkoutOrderId); } @@ -258,7 +243,7 @@ public function doCapturePayPalOrder( //TODO: split into multiple methods if ($payPalOrder->intent === Constants::PAYPAL_ORDER_INTENT_AUTHORIZE) { // if order approved then authorize - if ($payPalOrder->status === ApiOrderModel::STATUS_APPROVED) { + if ($payPalOrder->status === Order::STATUS_APPROVED) { $request = new OrderAuthorizeRequest(); $payPalOrder = $orderService->authorizePaymentForOrder( '', @@ -332,7 +317,7 @@ public function doCapturePayPalOrder( $this->getCustomIdParameter($order) ); - /** @var $result ApiOrderModel */ + /** @var $result Order */ $result = $orderService->capturePaymentForOrder( '', $checkoutOrderId, @@ -357,7 +342,7 @@ public function doCapturePayPalOrder( $result->purchase_units[0]->payments->captures[0]->id : ''; $status = $result && $result->purchase_units[0]->payments->captures[0]->status ? - $result->purchase_units[0]->payments->captures[0]->status : ApiOrderModel::STATUS_SAVED; + $result->purchase_units[0]->payments->captures[0]->status : Order::STATUS_SAVED; /** @var PayPalOrderModel $paypalOrder */ $this->trackPayPalOrder( @@ -437,7 +422,7 @@ public function doConfirmUAPM( /** @var ApiOrderService $orderService */ $orderService = $this->serviceFactory->getOrderService(); - /** @var ApiModelOrder $response */ + /** @var Order $response */ $response = $orderService->confirmTheOrder( $payPalClientMetadataId, $checkoutOrderId, @@ -667,16 +652,9 @@ public function doExecutePuiPayment( Constants::PAYPAL_PUI_PROCESSING_INSTRUCTIONS, PayPalDefinitions::PUI_REQUEST_PAYMENT_SOURCE_NAME, $payPalClientMetadataId, - Constants::PAYPAL_PARTNER_ATTRIBUTION_ID_PPCP, - null, - null, - true, - $order + Constants::PAYPAL_PARTNER_ATTRIBUTION_ID_PPCP ); $payPalOrderId = $result->id; - } catch (UserPhoneException $e) { - //mistyped phone in last order step - $this->setPaymentExecutionError(self::PAYMENT_ERROR_PUI_PHONE); } catch (Exception $exception) { $this->setPaymentExecutionError(self::PAYMENT_ERROR_PUI_GENERIC); $this->logger->log('error', 'Error on pui order creation call.', [$exception]); @@ -745,7 +723,7 @@ public function getPayPalCheckoutOrder( ); } - public function fetchOrderFields(string $paypalOrderId, string $fields = ''): ApiOrderModel + public function fetchOrderFields(string $paypalOrderId, string $fields = ''): Order { return $this->serviceFactory ->getOrderService() @@ -759,7 +737,7 @@ public function fetchOrderFields(string $paypalOrderId, string $fields = ''): Ap /** * @throws StandardException */ - public function verify3D(string $paymentId, ApiOrderModel $payPalOrder): bool + public function verify3D(string $paymentId, Order $payPalOrder): bool { //no ACDC payment if ($paymentId != PayPalDefinitions::ACDC_PAYPAL_PAYMENT_ID) { @@ -824,7 +802,7 @@ private function displayErrorIfInstrumentDeclined(?string $issue): void */ public function getCustomIdParameter(?EshopModelOrder $order): string { - /** @var ModuleSettingsService $moduleSettings */ + /** @var ModuleSettings $moduleSettings */ $moduleSettings = $this->getServiceFromContainer(ModuleSettings::class); $module = oxNew(\OxidEsales\Eshop\Core\Module\Module::class); $module->load(Module::MODULE_ID); @@ -834,7 +812,7 @@ public function getCustomIdParameter(?EshopModelOrder $order): string $customID = [ 'oxordernr' => $orderNumber, 'moduleVersion' => $module->getInfo('version'), - 'oxidVersion' => \OxidEsales\Eshop\Core\ShopVersion::getVersion() + 'oxidVersion' => ShopVersion::getVersion() ]; return json_encode($customID); @@ -842,19 +820,4 @@ public function getCustomIdParameter(?EshopModelOrder $order): string return $orderNumber; } - - private function getIsPaymentActive($paymentId): bool - { - $paymentController = oxNew(PaymentController::class); - $paymentList = $paymentController->getPaymentList(); - - /** @var \OxidEsales\Eshop\Application\Model\Payment $payment */ - foreach ($paymentList as $payment) { - if ($payment->getId() == $paymentId) { - return true; - } - } - - return false; - } } From b20f652f42f3b45f57d096e00679c48bfa4c9fe9 Mon Sep 17 00:00:00 2001 From: Mario Lorenz Date: Fri, 13 Dec 2024 20:40:00 +0100 Subject: [PATCH 3/4] move getUserIdForVaulting from ViewConfig to Controller-Traits --- .../PayPalVaultingCardController.php | 22 ++------- src/Controller/PayPalVaultingController.php | 20 ++------ src/Core/ViewConfig.php | 18 ------- src/Traits/AccountControllerTrait.php | 48 +++++++++++++++++++ 4 files changed, 55 insertions(+), 53 deletions(-) create mode 100644 src/Traits/AccountControllerTrait.php diff --git a/src/Controller/PayPalVaultingCardController.php b/src/Controller/PayPalVaultingCardController.php index e42a19881..0febcff42 100644 --- a/src/Controller/PayPalVaultingCardController.php +++ b/src/Controller/PayPalVaultingCardController.php @@ -3,21 +3,21 @@ namespace OxidSolutionCatalysts\PayPal\Controller; use OxidEsales\Eshop\Application\Controller\AccountController; -use OxidEsales\Eshop\Core\Registry; -use OxidSolutionCatalysts\PayPal\Core\ServiceFactory; use OxidSolutionCatalysts\PayPal\Service\ModuleSettings; +use OxidSolutionCatalysts\PayPal\Traits\AccountControllerTrait; use OxidSolutionCatalysts\PayPal\Traits\ServiceContainer; /** - * user account menu for saving paypal for purchase later (vaulting without purchase) + * user account menu for saving paypal (acdc) for purchase later (vaulting without purchase) */ class PayPalVaultingCardController extends AccountController { use ServiceContainer; + use AccountControllerTrait; public function render() { - $this->_aViewData['vaultingUserId'] = $this->getViewConfig()->getUserIdForVaulting(); + $this->_aViewData['vaultingUserId'] = $this->getUserIdForVaulting(); $moduleSettings = $this->getServiceFromContainer(ModuleSettings::class); if ($moduleSettings->isVaultingAllowedForACDC()) { @@ -26,18 +26,4 @@ public function render() return parent::render(); } - - public function deleteVaultedPayment() - { - $paymentTokenId = Registry::getRequest()->getRequestEscapedParameter("paymentTokenId"); - $vaultingService = Registry::get(ServiceFactory::class)->getVaultingService(); - - if (!$vaultingService->deleteVaultedPayment($paymentTokenId)) { - Registry::getUtilsView()->addErrorToDisplay( - Registry::getLang()->translateString('OSC_PAYPAL_DELETE_FAILED'), - false, - true - ); - } - } } diff --git a/src/Controller/PayPalVaultingController.php b/src/Controller/PayPalVaultingController.php index 6dc7504e6..936da247d 100644 --- a/src/Controller/PayPalVaultingController.php +++ b/src/Controller/PayPalVaultingController.php @@ -3,10 +3,9 @@ namespace OxidSolutionCatalysts\PayPal\Controller; use OxidEsales\Eshop\Application\Controller\AccountController; -use OxidSolutionCatalysts\PayPal\Core\ServiceFactory; use OxidSolutionCatalysts\PayPal\Service\ModuleSettings; +use OxidSolutionCatalysts\PayPal\Traits\AccountControllerTrait; use OxidSolutionCatalysts\PayPal\Traits\ServiceContainer; -use OxidEsales\Eshop\Core\Registry; /** * user account menu for saving paypal for purchase later (vaulting without purchase) @@ -14,10 +13,11 @@ class PayPalVaultingController extends AccountController { use ServiceContainer; + use AccountControllerTrait; public function render() { - $this->_aViewData['vaultingUserId'] = $this->getViewConfig()->getUserIdForVaulting(); + $this->_aViewData['vaultingUserId'] = $this->getUserIdForVaulting(); $moduleSettings = $this->getServiceFromContainer(ModuleSettings::class); if ($moduleSettings->isVaultingAllowedForPayPal()) { @@ -26,18 +26,4 @@ public function render() return parent::render(); } - - public function deleteVaultedPayment() - { - $paymentTokenId = Registry::getRequest()->getRequestEscapedParameter("paymentTokenId"); - $vaultingService = Registry::get(ServiceFactory::class)->getVaultingService(); - - if (!$vaultingService->deleteVaultedPayment($paymentTokenId)) { - Registry::getUtilsView()->addErrorToDisplay( - Registry::getLang()->translateString('OSC_PAYPAL_DELETE_FAILED'), - false, - true - ); - } - } } diff --git a/src/Core/ViewConfig.php b/src/Core/ViewConfig.php index 51a0d219d..a5c97dc25 100644 --- a/src/Core/ViewConfig.php +++ b/src/Core/ViewConfig.php @@ -264,24 +264,6 @@ public function showPayPalExpressInMiniBasket(): bool return $showButton; } - public function getUserIdForVaulting(): string - { - if (!$this->getUser()) { - return ""; - } - - $payPalCustomerId = $this->getUser()->getFieldData("oscpaypalcustomerid"); - - if (!$payPalCustomerId) { - return ""; - } - - $vaultingService = Registry::get(ServiceFactory::class)->getVaultingService(); - $response = $vaultingService->generateUserIdToken($payPalCustomerId); - - return $response["id_token"] ?? ""; - } - /** * get Session Vault Success * diff --git a/src/Traits/AccountControllerTrait.php b/src/Traits/AccountControllerTrait.php new file mode 100644 index 000000000..e36c10011 --- /dev/null +++ b/src/Traits/AccountControllerTrait.php @@ -0,0 +1,48 @@ +getRequestEscapedParameter("paymentTokenId"); + $vaultingService = Registry::get(ServiceFactory::class)->getVaultingService(); + + if (!$vaultingService->deleteVaultedPayment($paymentTokenId)) { + Registry::getUtilsView()->addErrorToDisplay( + Registry::getLang()->translateString('OSC_PAYPAL_DELETE_FAILED'), + false, + true + ); + } + } + + public function getUserIdForVaulting(): string + { + if (!$this->getUser()) { + return ""; + } + + $payPalCustomerId = $this->getUser()->getFieldData("oscpaypalcustomerid"); + + if (!$payPalCustomerId) { + return ""; + } + + $vaultingService = Registry::get(ServiceFactory::class)->getVaultingService(); + $response = $vaultingService->generateUserIdToken($payPalCustomerId); + + return $response["id_token"] ?? ""; + } +} From 4139da3024199be38bb6a99b6f2127e04589bf45 Mon Sep 17 00:00:00 2001 From: Mario Lorenz Date: Fri, 13 Dec 2024 21:02:54 +0100 Subject: [PATCH 4/4] move getUserIdForVaulting to Config --- .../PayPalVaultingCardController.php | 3 ++- src/Controller/PayPalVaultingController.php | 3 ++- src/Core/Config.php | 15 +++++++++++++++ src/Core/ViewConfig.php | 15 +++++++++++++++ src/Traits/AccountControllerTrait.php | 18 ------------------ 5 files changed, 34 insertions(+), 20 deletions(-) diff --git a/src/Controller/PayPalVaultingCardController.php b/src/Controller/PayPalVaultingCardController.php index 0febcff42..04d5ea514 100644 --- a/src/Controller/PayPalVaultingCardController.php +++ b/src/Controller/PayPalVaultingCardController.php @@ -3,6 +3,7 @@ namespace OxidSolutionCatalysts\PayPal\Controller; use OxidEsales\Eshop\Application\Controller\AccountController; +use OxidSolutionCatalysts\PayPal\Core\Config; use OxidSolutionCatalysts\PayPal\Service\ModuleSettings; use OxidSolutionCatalysts\PayPal\Traits\AccountControllerTrait; use OxidSolutionCatalysts\PayPal\Traits\ServiceContainer; @@ -17,7 +18,7 @@ class PayPalVaultingCardController extends AccountController public function render() { - $this->_aViewData['vaultingUserId'] = $this->getUserIdForVaulting(); + $this->_aViewData['vaultingUserId'] = oxNew(Config::class)->getUserIdForVaulting(); $moduleSettings = $this->getServiceFromContainer(ModuleSettings::class); if ($moduleSettings->isVaultingAllowedForACDC()) { diff --git a/src/Controller/PayPalVaultingController.php b/src/Controller/PayPalVaultingController.php index 936da247d..3a0be289f 100644 --- a/src/Controller/PayPalVaultingController.php +++ b/src/Controller/PayPalVaultingController.php @@ -3,6 +3,7 @@ namespace OxidSolutionCatalysts\PayPal\Controller; use OxidEsales\Eshop\Application\Controller\AccountController; +use OxidSolutionCatalysts\PayPal\Core\Config; use OxidSolutionCatalysts\PayPal\Service\ModuleSettings; use OxidSolutionCatalysts\PayPal\Traits\AccountControllerTrait; use OxidSolutionCatalysts\PayPal\Traits\ServiceContainer; @@ -17,7 +18,7 @@ class PayPalVaultingController extends AccountController public function render() { - $this->_aViewData['vaultingUserId'] = $this->getUserIdForVaulting(); + $this->_aViewData['vaultingUserId'] = oxNew(Config::class)->getUserIdForVaulting(); $moduleSettings = $this->getServiceFromContainer(ModuleSettings::class); if ($moduleSettings->isVaultingAllowedForPayPal()) { diff --git a/src/Core/Config.php b/src/Core/Config.php index e6ddd228b..50ac9bd85 100755 --- a/src/Core/Config.php +++ b/src/Core/Config.php @@ -462,6 +462,21 @@ public function getIsVaultingActive(): bool return $this->getServiceFromContainer(ModuleSettings::class)->getIsVaultingActive(); } + public function getUserIdForVaulting(): string + { + $user = Registry::getConfig()->getUser(); + $payPalCustomerId = $user ? $user->getFieldData("oscpaypalcustomerid") : ''; + + if (!$payPalCustomerId) { + return ""; + } + + $vaultingService = Registry::get(ServiceFactory::class)->getVaultingService(); + $response = $vaultingService->generateUserIdToken($payPalCustomerId); + + return $response["id_token"] ?? ""; + } + public function getIsGooglePayDeliveryAdressActive(): bool { return $this->getServiceFromContainer(ModuleSettings::class)->getIsGooglePayDeliveryAddressActive(); diff --git a/src/Core/ViewConfig.php b/src/Core/ViewConfig.php index a5c97dc25..d47723349 100644 --- a/src/Core/ViewConfig.php +++ b/src/Core/ViewConfig.php @@ -264,6 +264,21 @@ public function showPayPalExpressInMiniBasket(): bool return $showButton; } + public function getUserIdForVaulting(): string + { + return oxNew(Config::class)->getUserIdForVaulting(); + } + + public function isVaultingAllowedForPayPal(): bool + { + return $this->getServiceFromContainer(ModuleSettings::class)->isVaultingAllowedForPayPal(); + } + + public function isVaultingAllowedForACDC(): bool + { + return $this->getServiceFromContainer(ModuleSettings::class)->isVaultingAllowedForACDC(); + } + /** * get Session Vault Success * diff --git a/src/Traits/AccountControllerTrait.php b/src/Traits/AccountControllerTrait.php index e36c10011..5d21bc8c0 100644 --- a/src/Traits/AccountControllerTrait.php +++ b/src/Traits/AccountControllerTrait.php @@ -27,22 +27,4 @@ public function deleteVaultedPayment() ); } } - - public function getUserIdForVaulting(): string - { - if (!$this->getUser()) { - return ""; - } - - $payPalCustomerId = $this->getUser()->getFieldData("oscpaypalcustomerid"); - - if (!$payPalCustomerId) { - return ""; - } - - $vaultingService = Registry::get(ServiceFactory::class)->getVaultingService(); - $response = $vaultingService->generateUserIdToken($payPalCustomerId); - - return $response["id_token"] ?? ""; - } }