Skip to content

Commit

Permalink
move vaulting-check from Payment-Service to the module-settings
Browse files Browse the repository at this point in the history
  • Loading branch information
mariolorenz committed Dec 13, 2024
1 parent f697c86 commit 39b9bee
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 79 deletions.
6 changes: 2 additions & 4 deletions src/Controller/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions src/Controller/PayPalVaultingCardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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();
}

Expand Down
7 changes: 5 additions & 2 deletions src/Controller/PayPalVaultingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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();
}

Expand Down
5 changes: 2 additions & 3 deletions src/Controller/PaymentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
Expand Down
14 changes: 0 additions & 14 deletions src/Core/ViewConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
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
69 changes: 16 additions & 53 deletions src/Service/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -81,7 +77,7 @@ class Payment
/** @var SCAValidatorInterface */
private $scaValidator;

/** @var ModuleSettingsService */
/** @var ModuleSettings */
private $moduleSettingsService;

private $logger;
Expand All @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
Expand All @@ -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(
'',
Expand Down Expand Up @@ -332,7 +317,7 @@ public function doCapturePayPalOrder(
$this->getCustomIdParameter($order)
);

/** @var $result ApiOrderModel */
/** @var $result Order */
$result = $orderService->capturePaymentForOrder(
'',
$checkoutOrderId,
Expand All @@ -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(
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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()
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
Expand All @@ -834,27 +812,12 @@ 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);
}

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;
}
}

0 comments on commit 39b9bee

Please sign in to comment.