Skip to content

Commit

Permalink
Add error handling for country and shipping method resolving
Browse files Browse the repository at this point in the history
ISSUE: AD4CR22I-14
  • Loading branch information
filipkojic committed Jan 3, 2025
1 parent f77d80b commit cfc5377
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

namespace Adyen\Shopware\Controller\StoreApi\ExpressCheckout;

use Adyen\Shopware\Exception\ResolveCountryException;
use Adyen\Shopware\Exception\ResolveShippingMethodException;
use Adyen\Shopware\Service\ExpressCheckoutService;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Symfony\Component\HttpFoundation\JsonResponse;
Expand Down Expand Up @@ -81,13 +83,41 @@ public function getExpressCheckoutConfig(
$newShipping = [];
}

return new JsonResponse($this->expressCheckoutService->getExpressCheckoutConfigOnProductPage(
$productId,
$quantity,
$salesChannelContext,
$newAddress,
$newShipping
));
try {
$config = $this->expressCheckoutService->getExpressCheckoutConfigOnProductPage(
$productId,
$quantity,
$salesChannelContext,
$newAddress,
$newShipping
);

return new JsonResponse($config);
} catch (ResolveCountryException $e) {
return new JsonResponse([
'error' => [
'reason' => 'SHIPPING_ADDRESS_INVALID',
'message' => $e->getMessage(),
'intent' => 'SHIPPING_ADDRESS',
]
], 400);
} catch (ResolveShippingMethodException $e) {
return new JsonResponse([
'error' => [
'reason' => 'SHIPPING_OPTION_INVALID',
'message' => $e->getMessage(),
'intent' => 'SHIPPING_OPTION',
]
], 400);
} catch (\Exception $e) {
// Fallback for unexpected errors
return new JsonResponse([
'error' => [
'reason' => 'OTHER_ERROR',
'message' => $e->getMessage(),
]
], 500);
}
}

/**
Expand Down
30 changes: 30 additions & 0 deletions src/Exception/ResolveCountryException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types=1);
/**
* ######
* ######
* ############ ####( ###### #####. ###### ############ ############
* ############# #####( ###### #####. ###### ############# #############
* ###### #####( ###### #####. ###### ##### ###### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ######
* ############# ############# ############# ############# ##### ######
* ############ ############ ############# ############ ##### ######
* ######
* #############
* ############
*
* Adyen Payment Module
*
* Copyright (c) 2020 Adyen B.V.
* This file is open source and available under the MIT license.
* See the LICENSE file for more info.
*
* Author: Adyen <[email protected]>
*/

namespace Adyen\Shopware\Exception;

class ResolveCountryException extends \Exception
{

}
30 changes: 30 additions & 0 deletions src/Exception/ResolveShippingMethodException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php declare(strict_types=1);
/**
* ######
* ######
* ############ ####( ###### #####. ###### ############ ############
* ############# #####( ###### #####. ###### ############# #############
* ###### #####( ###### #####. ###### ##### ###### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ##### ######
* ###### ###### #####( ###### #####. ###### ##### ##### ######
* ############# ############# ############# ############# ##### ######
* ############ ############ ############# ############ ##### ######
* ######
* #############
* ############
*
* Adyen Payment Module
*
* Copyright (c) 2020 Adyen B.V.
* This file is open source and available under the MIT license.
* See the LICENSE file for more info.
*
* Author: Adyen <[email protected]>
*/

namespace Adyen\Shopware\Exception;

class ResolveShippingMethodException extends \Exception
{

}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -33,61 +33,68 @@ export default class ExpressCheckoutPlugin extends Plugin {
let onPaymentDataChanged = (intermediatePaymentData) => {
console.log("onPaymentDataChanged triggered", intermediatePaymentData);
return new Promise(async resolve => {
const { callbackTrigger, shippingAddress, shippingOptionData } = intermediatePaymentData;
const paymentDataRequestUpdate = {};
try {
const {callbackTrigger, shippingAddress, shippingOptionData} = intermediatePaymentData;
const paymentDataRequestUpdate = {};

if (callbackTrigger === 'INITIALIZE' || callbackTrigger === 'SHIPPING_ADDRESS') {
console.log("ADDRESS trigger");
if (callbackTrigger === 'INITIALIZE' || callbackTrigger === 'SHIPPING_ADDRESS') {
console.log("ADDRESS trigger");

const extraData = {};
const extraData = {};

if (shippingAddress) {
extraData.newAddress = shippingAddress;
}
if (shippingAddress) {
extraData.newAddress = shippingAddress;
}

const response = await this.fetchExpressCheckoutConfig(adyenExpressCheckoutOptions.expressCheckoutConfigUrl, extraData);
const response = await this.fetchExpressCheckoutConfig(adyenExpressCheckoutOptions.expressCheckoutConfigUrl, extraData);

let shippingMethodsArray = response.shippingMethodsResponse;
let shippingMethodsArray = response.shippingMethodsResponse;

paymentDataRequestUpdate.newShippingOptionParameters = {
defaultSelectedOptionId: shippingMethodsArray[0].id,
shippingOptions: shippingMethodsArray
};
paymentDataRequestUpdate.newShippingOptionParameters = {
defaultSelectedOptionId: shippingMethodsArray[0].id,
shippingOptions: shippingMethodsArray
};

paymentDataRequestUpdate.newTransactionInfo = {
currencyCode: response.currency,
totalPriceStatus: "FINAL",
totalPrice: (parseInt(response.amount) / 100).toString(),
totalPriceLabel: "Total",
countryCode: response.countryCode,
};
}
paymentDataRequestUpdate.newTransactionInfo = {
currencyCode: response.currency,
totalPriceStatus: "FINAL",
totalPrice: (parseInt(response.amount) / 100).toString(),
totalPriceLabel: "Total",
countryCode: response.countryCode,
};
}

if (callbackTrigger === 'SHIPPING_OPTION') {
console.log("SHIPPING trigger")
if (callbackTrigger === 'SHIPPING_OPTION') {
console.log("SHIPPING trigger")

const extraData = {};
const extraData = {};

if (shippingAddress) {
extraData.newAddress = shippingAddress;
}
if (shippingAddress) {
extraData.newAddress = shippingAddress;
}

if (shippingOptionData) {
extraData.newShippingMethod = shippingOptionData;
}
if (shippingOptionData) {
extraData.newShippingMethod = shippingOptionData;
}

const response = await this.fetchExpressCheckoutConfig(adyenExpressCheckoutOptions.expressCheckoutConfigUrl, extraData);
const response = await this.fetchExpressCheckoutConfig(adyenExpressCheckoutOptions.expressCheckoutConfigUrl, extraData);

paymentDataRequestUpdate.newTransactionInfo = {
currencyCode: response.currency,
totalPriceStatus: "FINAL",
totalPrice: (parseInt(response.amount) / 100).toString(),
totalPriceLabel: "Total",
countryCode: response.countryCode,
};
}
paymentDataRequestUpdate.newTransactionInfo = {
currencyCode: response.currency,
totalPriceStatus: "FINAL",
totalPrice: (parseInt(response.amount) / 100).toString(),
totalPriceLabel: "Total",
countryCode: response.countryCode,
};
}

resolve(paymentDataRequestUpdate);
resolve(paymentDataRequestUpdate);
} catch (error) {
console.error("Error in onPaymentDataChanged:", error);
resolve({
error: error.error
});
}
});
};

Expand Down Expand Up @@ -130,11 +137,18 @@ export default class ExpressCheckoutPlugin extends Plugin {
"paypal": {},
"applepay": {}
};

this.quantityInput = document.querySelector('.product-detail-quantity-select') ||
document.querySelector('.product-detail-quantity-input');
console.log("kolicina" + this.quantityInput.value)

if(this.quantityInput) {
console.log("kolicina" + this.quantityInput.value)
}

this.listenOnQuantityChange();

console.log(adyenExpressCheckoutOptions);

this.mountExpressCheckoutComponents({
countryCode: adyenExpressCheckoutOptions.countryCode,
amount: adyenExpressCheckoutOptions.amount,
Expand All @@ -160,11 +174,26 @@ export default class ExpressCheckoutPlugin extends Plugin {
(response) => {
try {
const parsedResponse = JSON.parse(response);
console.log("Controller Response:", parsedResponse);
console.log("odgovor ")
console.log(parsedResponse);

console.log("status ")
console.log(this._client._request.status)

if (this._client._request.status >= 400) {
// if valid resonse, but contains error data
reject({
error: parsedResponse.error
});
return;
}

resolve(parsedResponse);
} catch (error) {
console.error("Failed to parse response:", error);
reject(error);
reject({
status: 500,
message: "Failed to parse server response.",
});
}
}
);
Expand Down
6 changes: 4 additions & 2 deletions src/Service/ExpressCheckoutService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Adyen\Shopware\Service;

use Adyen\Model\Checkout\PaymentMethodsResponse;
use Adyen\Shopware\Exception\ResolveCountryException;
use Adyen\Shopware\Exception\ResolveShippingMethodException;
use Adyen\Shopware\Util\Currency;
use Shopware\Core\Checkout\Cart\Cart;
use Shopware\Core\Checkout\Cart\Delivery\Struct\ShippingLocation;
Expand Down Expand Up @@ -292,7 +294,7 @@ private function resolveCountry(SalesChannelContext $salesChannelContext, array
$country = $this->countryRepository->search($criteria, $salesChannelContext->getContext())->first();

if (!$country) {
throw new \Exception('Invalid country information.');
throw new ResolveCountryException("No shipping country found!");
}

return $country;
Expand Down Expand Up @@ -324,7 +326,7 @@ private function resolveShippingMethod(

// If no shipping method is resolved, throw an exception
if (!$shippingMethod) {
throw new \Exception('No valid shipping method is available.');
throw new ResolveShippingMethodException("No shipping method found!");
}

return $shippingMethod;
Expand Down

0 comments on commit cfc5377

Please sign in to comment.