Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PSPAYPAL-817 - refactor vaulting: #359

Merged
merged 5 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/Controller/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,10 @@ public function render()
$user = $this->getUser();

if ($user) {
$isVaultingPossible = false;
$paymentId = $this->getPayment()->getId();
$moduleSettings = $this->getServiceFromContainer(ModuleSettings::class);
if ($moduleSettings->getIsVaultingActive() && $user->getFieldData('oxpassword')) {
$isVaultingPossible = true;
}
$isVaultingPossible = $moduleSettings->isVaultingAllowedForPayment($paymentId)
&& $user->getFieldData('oxpassword');

$this->addTplParam('oscpaypal_isVaultingPossible', $isVaultingPossible);

Expand Down
36 changes: 13 additions & 23 deletions src/Controller/PayPalVaultingCardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,28 @@
namespace OxidSolutionCatalysts\PayPal\Controller;

use OxidEsales\Eshop\Application\Controller\AccountController;
use OxidEsales\Eshop\Core\Registry;
use OxidSolutionCatalysts\PayPal\Core\ServiceFactory;
use OxidSolutionCatalysts\PayPal\Core\Config;
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
{
/**
* @var string Current class template name.
*/
// phpcs:ignore PSR2.Classes.PropertyDeclaration
protected $_sThisTemplate = 'modules/osc/paypal/account_vaulting_card.tpl';
use ServiceContainer;
use AccountControllerTrait;

public function render()
{
$this->_aViewData['vaultingUserId'] = $this->getViewConfig()->getUserIdForVaulting();
$this->_aViewData['vaultingUserId'] = oxNew(Config::class)->getUserIdForVaulting();
$moduleSettings = $this->getServiceFromContainer(ModuleSettings::class);

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
);
if ($moduleSettings->isVaultingAllowedForACDC()) {
$this->_sThisTemplate = 'modules/osc/paypal/account_vaulting_card.tpl';
}

return parent::render();
}
}
35 changes: 12 additions & 23 deletions src/Controller/PayPalVaultingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,29 @@

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\Core\Config;
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)
*/
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;
use AccountControllerTrait;

public function render()
{
$this->_aViewData['vaultingUserId'] = $this->getViewConfig()->getUserIdForVaulting();
$this->_aViewData['vaultingUserId'] = oxNew(Config::class)->getUserIdForVaulting();
$moduleSettings = $this->getServiceFromContainer(ModuleSettings::class);

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
);
if ($moduleSettings->isVaultingAllowedForPayPal()) {
$this->_sThisTemplate = 'modules/osc/paypal/account_vaulting_paypal.tpl';
}

return parent::render();
}
}
23 changes: 2 additions & 21 deletions src/Controller/PaymentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,11 @@ public function render()
$vaultedPaymentSources = [];
foreach ($vaultedPaymentTokens as $vaultedPaymentToken) {
foreach ($vaultedPaymentToken["payment_source"] as $paymentType => $paymentSource) {
if (!$this->paymentTypeExists($paymentType)) {
continue;
}
if ($paymentType === "card") {
if ($paymentType === "card" && $moduleSettings->isVaultingAllowedForACDC()) {
$string = $lang->translateString("OSC_PAYPAL_CARD_ENDING_IN");
$vaultedPaymentSources[$paymentType][] = $paymentSource["brand"] . " " .
$string . $paymentSource["last_digits"];
} elseif ($paymentType === "paypal") {
} elseif ($paymentType === "paypal" && $moduleSettings->isVaultingAllowedForPayPal()) {
$string = $lang->translateString("OSC_PAYPAL_CARD_PAYPAL_PAYMENT");
$vaultedPaymentSources[$paymentType][] =
$string . " " . $paymentSource["email_address"];
Expand All @@ -88,22 +85,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
{

Expand Down
15 changes: 15 additions & 0 deletions src/Core/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 0 additions & 2 deletions src/Core/PayPalDefinitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ final class PayPalDefinitions
'onlybrutto' => false,
'buttonpayment' => false,
'defaulton' => true,
'vaultingtype' => 'paypal'
],
//Express PayPal
self::EXPRESS_PAYPAL_PAYMENT_ID => [
Expand All @@ -156,7 +155,6 @@ final class PayPalDefinitions
'onlybrutto' => false,
'buttonpayment' => true,
'defaulton' => true,
'vaultingtype' => 'paypal'
],
self::PUI_PAYPAL_PAYMENT_ID => [
'descriptions' => [
Expand Down
22 changes: 10 additions & 12 deletions src/Core/ViewConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -265,20 +266,17 @@ public function showPayPalExpressInMiniBasket(): bool

public function getUserIdForVaulting(): string
{
if (!$this->getUser()) {
return "";
}

$payPalCustomerId = $this->getUser()->getFieldData("oscpaypalcustomerid");

if (!$payPalCustomerId) {
return "";
}
return oxNew(Config::class)->getUserIdForVaulting();
}

$vaultingService = Registry::get(ServiceFactory::class)->getVaultingService();
$response = $vaultingService->generateUserIdToken($payPalCustomerId);
public function isVaultingAllowedForPayPal(): bool
{
return $this->getServiceFromContainer(ModuleSettings::class)->isVaultingAllowedForPayPal();
}

return $response["id_token"];
public function isVaultingAllowedForACDC(): bool
{
return $this->getServiceFromContainer(ModuleSettings::class)->isVaultingAllowedForACDC();
}

/**
Expand Down
51 changes: 50 additions & 1 deletion src/Service/ModuleSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
*
Expand Down Expand Up @@ -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);
Expand All @@ -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
*/
Expand Down
Loading
Loading