Skip to content

Commit

Permalink
NTR: added more logs in PPE
Browse files Browse the repository at this point in the history
  • Loading branch information
Vitalij Mik committed Oct 11, 2024
1 parent 4297f60 commit 1759533
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,18 @@ public function finishCheckout(SalesChannelContext $context): Response
return new RedirectResponse($returnUrl);
}

$payPalExpressSession = $this->paypalExpress->loadSession($payPalExpressSessionId, $context);
try {
$payPalExpressSession = $this->paypalExpress->loadSession($payPalExpressSessionId, $context);
} catch (\Throwable $e) {
$this->logger->critical('Failed to load session from mollie', [
'message' => $e->getMessage(),
'sessionId' => $payPalExpressSessionId
]);
return new RedirectResponse($returnUrl);
}



$methodDetails = $payPalExpressSession->methodDetails;


Expand All @@ -146,32 +157,66 @@ public function finishCheckout(SalesChannelContext $context): Response
return new RedirectResponse($returnUrl);
}

$billingAddress = $methodDetails->billingAddress;
$billingAddress = null;

$shippingAddress = $methodDetails->shippingAddress;
$shippingAddress->streetAdditional = $billingAddress->streetAdditional;
$shippingAddress->email = $billingAddress->email;
$shippingAddress->phone = $billingAddress->phone;
$shippingAddress->phone = '';
if ($methodDetails->billingAddress->streetAdditional !== null) {
$shippingAddress->streetAdditional = $methodDetails->billingAddress->streetAdditional;
}
if ($methodDetails->billingAddress->phone !== null) {
$shippingAddress->phone = $methodDetails->billingAddress->phone;
}
if ($methodDetails->billingAddress->email !== null) {
$shippingAddress->email = $methodDetails->billingAddress->email;
}
if ($methodDetails->billingAddress->streetAndNumber !== null) {
try {
$billingAddress = AddressStruct::createFromApiResponse($methodDetails->billingAddress);
} catch (\Throwable $e) {
$this->logger->error('Failed to create billing address', [
'message' => $e->getMessage(),
'shippingAddress' => $shippingAddress,
'billingAddress' => $methodDetails->billingAddress
]);
return new RedirectResponse($returnUrl);
}
}

try {
$shippingAddress = AddressStruct::createFromApiResponse($shippingAddress);
} catch (\Throwable $e) {
$this->logger->error('Failed to create shipping address', [
'message' => $e->getMessage(),
'shippingAddress' => $shippingAddress,
'billingAddress' => $methodDetails->billingAddress
]);
return new RedirectResponse($returnUrl);
}

$billingAddress = AddressStruct::createFromApiResponse($methodDetails->billingAddress);

$shippingAddress = AddressStruct::createFromApiResponse($methodDetails->shippingAddress);

try {
# we have to update the cart extension before a new user is created and logged in, otherwise the extension is not saved
$cartExtension = new ArrayStruct([
CustomFieldsInterface::PAYPAL_EXPRESS_AUTHENTICATE_ID => $payPalExpressSession->authenticationId
]);
$cart->addExtension(CustomFieldsInterface::MOLLIE_KEY, $cartExtension);

$this->cartService->updateCart($cart);

# we have to update the cart extension before a new user is created and logged in, otherwise the extension is not saved
$cartExtension = new ArrayStruct([
CustomFieldsInterface::PAYPAL_EXPRESS_AUTHENTICATE_ID => $payPalExpressSession->authenticationId
]);
$cart->addExtension(CustomFieldsInterface::MOLLIE_KEY, $cartExtension);
$this->cartService->updateCart($cart);
$this->cartService->persistCart($cart, $context);

$this->cartService->persistCart($cart, $context);

# create new account or find existing and login
$this->paypalExpress->prepareCustomer($shippingAddress, 1, $context, $billingAddress);
} catch (\Throwable $e) {
$this->logger->error('Failed to create customer or cart', [
'message' => $e->getMessage(),
]);
return new RedirectResponse($returnUrl);
}

# create new account or find existing and login
$this->paypalExpress->prepareCustomer($shippingAddress, 1, $context, $billingAddress);


$returnUrl = $this->getCheckoutConfirmPage($this->router);
Expand Down
2 changes: 1 addition & 1 deletion src/Service/PayPalExpressConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function isEnabled(): bool
*/
public function assign(array $structData): array
{
$structData['paypalExpressEnabled'] = $structData['paypalExpressEnabled'] ?? $this->isEnabled();
$structData['paypalExpressEnabled'] = $this->isEnabled();
$structData['paypalExpressButtonStyle'] = $structData['paypalExpressButtonStyle'] ?? $this->buttonStyle;
$structData['paypalExpressButtonShape'] = $structData['paypalExpressButtonShape'] ?? $this->buttonShape;
$structData['paypalExpressRestrictions'] = $structData['paypalExpressRestrictions'] ?? $this->restrictions;
Expand Down
18 changes: 12 additions & 6 deletions src/Service/SettingsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class SettingsService implements PluginSettingsServiceInterface

private const PHONE_NUMBER_FIELD = 'showPhoneNumberField';

private const REQUIRE_DATA_PROTECTION ='requireDataProtectionCheckbox';
private const REQUIRE_DATA_PROTECTION = 'requireDataProtectionCheckbox';

private const PAYMENT_FINALIZE_TRANSACTION_TIME = 'paymentFinalizeTransactionTime';
const LIVE_API_KEY = 'liveApiKey';
Expand Down Expand Up @@ -53,7 +53,11 @@ class SettingsService implements PluginSettingsServiceInterface
private $envCypressMode;
private PayPalExpressConfig $payPalExpressConfig;

private ?MollieSettingStruct $cachedStruct = null;
/**
* @var array<string,MollieSettingStruct>
*/
private array $cachedStructs = [];

/**
* @param SystemConfigService $systemConfigService
* @param SalesChannelRepositoryInterface $repoSalesChannels
Expand All @@ -80,8 +84,10 @@ public function __construct(SystemConfigService $systemConfigService, SalesChann
*/
public function getSettings(?string $salesChannelId = null): MollieSettingStruct
{
if ($this->cachedStruct instanceof MollieSettingStruct) {
return $this->cachedStruct;
$cacheKey = $salesChannelId ?? 'all';

if (isset($this->cachedStructs[$cacheKey])) {
return $this->cachedStructs[$cacheKey];
}
$structData = [];
/** @var array<mixed> $systemConfigData */
Expand Down Expand Up @@ -118,9 +124,9 @@ public function getSettings(?string $salesChannelId = null): MollieSettingStruct
}


$this->cachedStruct = (new MollieSettingStruct())->assign($structData);
$this->cachedStructs[$cacheKey] = (new MollieSettingStruct())->assign($structData);

return $this->cachedStruct;
return $this->cachedStructs[$cacheKey];
}

/**
Expand Down

0 comments on commit 1759533

Please sign in to comment.