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

AD-225 Implement Correct Handling of 302 Redirect Status in Axios Library #387

Merged
merged 1 commit into from
Apr 9, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import axios, {AxiosInstance} from "axios";
import {ErrorHandler} from "../errorhandler/ErrorHandler";


export const adyenAxios: AxiosInstance = axios.create({
maxRedirects: 0,
})
adyenAxios.interceptors.response.use(
response => {
if (!response.request.responseURL.endsWith(response.config.url)) {
window.location.replace(response.request.responseURL)
}
return response
},
error => {
ErrorHandler.handleError(error)
return Promise.reject(error);
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Payment from "../payment/Payment";
import RedirectOnIncompleteData from "../common/RedirectOnIncompleteData";
import {CheckoutSteps} from "../../types/checkoutStepsEnum";
import {CartDataService} from "../../service/cartDataService";
import {useParams} from "react-router-dom";

export function PaymentStep() {
const {errorCode} = useParams();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {AxiosError} from "axios";
import {ErrorResponse} from "../../types/errorResponse";
import {isEmpty} from "../../util/stringUtil";
import {store} from "../../store/store";
import {createDefaultResponseData, createError} from "../../util/notificationUtil";
import {ErrorResponse} from "../types/errorResponse";
import {isEmpty} from "../util/stringUtil";
import {store} from "../store/store";
import {createDefaultResponseData, createError} from "../util/notificationUtil";

export class ErrorHandler {

Expand All @@ -12,7 +12,6 @@ export class ErrorHandler {
errorResponseData = createDefaultResponseData()
} else {
errorResponseData = error.response.data;

if (!errorResponseData || isEmpty(errorResponseData.errorCode)) {
errorResponseData = createDefaultResponseData()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import axios, {AxiosError} from "axios";
import {AxiosError} from "axios";
import {CSRFToken, urlContextPath} from "../util/baseUrlUtil";
import {AddressModel} from "../reducers/types";
import {store} from "../store/store";
import {isNotEmpty} from "../util/stringUtil";
import {AddressConfigModel} from "../reducers/addressConfigReducer";
import {AddressData} from "../types/addressData";
import {ErrorResponse} from "../types/errorResponse";
import {ErrorHandler} from "../components/common/ErrorHandler";
import {adyenAxios} from "../axios/AdyenAxios";

export class AddressService {

static fetchAddressBook() {
axios.get(urlContextPath + '/api/account/delivery-address', {
adyenAxios.get(urlContextPath + '/api/account/delivery-address', {
headers: {
"X-Requested-With": "XMLHttpRequest"
}
Expand All @@ -21,22 +21,20 @@ export class AddressService {
store.dispatch({type: "addressBook/setAddressBook", payload: addressModels})
})
.catch((errorResponse: AxiosError<ErrorResponse>) => {
ErrorHandler.handleError(errorResponse)
console.error("Address book fetch error")
return false
})
}

static async selectDeliveryAddress(addressId: string) {
return axios.post(urlContextPath + '/api/checkout/delivery-address', addressId, {
return adyenAxios.post(urlContextPath + '/api/checkout/delivery-address', addressId, {
headers: {
'Content-Type': 'text/plain',
'CSRFToken': CSRFToken
}
})
.then(() => true)
.catch((errorResponse: AxiosError<ErrorResponse>) => {
ErrorHandler.handleError(errorResponse)
console.error('Error on address select')
return false
})
Expand All @@ -45,21 +43,20 @@ export class AddressService {
static async addDeliveryAddress(address: AddressModel, saveInAddressBook: boolean, isShippingAddress: boolean, isBillingAddress: boolean,
editAddress: boolean): Promise<boolean> {
const payload = this.mapAddressModelToAddressForm(address, saveInAddressBook, isShippingAddress, isBillingAddress, editAddress);
return axios.post(urlContextPath + '/api/account/delivery-address', payload, {
return adyenAxios.post(urlContextPath + '/api/account/delivery-address', payload, {
headers: {
'Content-Type': 'application/json; charset=utf-8',
'CSRFToken': CSRFToken
}
}).then(() => true)
.catch((errorResponse: AxiosError<ErrorResponse>) => {
ErrorHandler.handleError(errorResponse)
console.error('Error on address select')
return false
})
}

static fetchAddressConfig() {
axios.get(urlContextPath + '/api/configuration/shipping-address', {
adyenAxios.get(urlContextPath + '/api/configuration/shipping-address', {
headers: {
"X-Requested-With": "XMLHttpRequest"
}
Expand All @@ -69,7 +66,6 @@ export class AddressService {
store.dispatch({type: "addressConfig/setAddressConfig", payload: addressConfigModel})
})
.catch((errorResponse: AxiosError<ErrorResponse>) => {
ErrorHandler.handleError(errorResponse)
console.error("Address config fetch error")
return false
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import axios, {AxiosError, AxiosResponse} from "axios";
import {AxiosError, AxiosResponse} from "axios";
import {urlContextPath} from "../util/baseUrlUtil";
import {store} from "../store/store";
import {AdyenConfigData} from "../types/adyenConfigData";
import {ErrorResponse} from "../types/errorResponse";
import {ErrorHandler} from "../components/common/ErrorHandler";
import {adyenAxios} from "../axios/AdyenAxios";

export class AdyenConfigService {

public static fetchPaymentMethodsConfig() {
axios.get(urlContextPath + '/api/checkout/payment-methods-configuration', {
adyenAxios.get(urlContextPath + '/api/checkout/payment-methods-configuration', {
headers: {
"X-Requested-With": "XMLHttpRequest"
}
})
.then((response: AxiosResponse<AdyenConfigData>) => {
store.dispatch({type: "adyenConfig/setAdyenConfig", payload: response.data})
})
.catch((errorResponse:AxiosError<ErrorResponse>) => {
ErrorHandler.handleError(errorResponse)
.catch((errorResponse: AxiosError<ErrorResponse>) => {
console.error("Payment method config fetch error.")
return false
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
import {store} from "../store/store";
import axios, {AxiosError, AxiosResponse} from "axios";
import {AxiosError, AxiosResponse} from "axios";
import {urlContextPath} from "../util/baseUrlUtil";
import {CartData} from "../types/cartData";
import {ErrorResponse} from "../types/errorResponse";
import {ErrorHandler} from "../components/common/ErrorHandler";
import {adyenAxios} from "../axios/AdyenAxios";

export class CartDataService {
static fetchCartData() {
store.dispatch({type: "loading/cartData/start"})

axios.get(urlContextPath + '/api/checkout/cart-data', {
adyenAxios.get(urlContextPath + '/api/checkout/cart-data', {
headers: {
"X-Requested-With": "XMLHttpRequest"
}
})
.then((response: AxiosResponse<CartData>) => {
store.dispatch({type: "cartData/setCartData", payload: response.data})
store.dispatch({type: "loading/cartData/end"})


//set selected delivery mode
if (response.data.deliveryMode) {
store.dispatch({type: "shippingMethod/setShippingMethod", payload: response.data.deliveryMode.code})
}
})
.catch((errorResponse:AxiosError<ErrorResponse>) => {
ErrorHandler.handleError(errorResponse)
.catch((errorResponse: AxiosError<ErrorResponse>) => {
console.error('Cart data fetch error.')
return false
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import axios, {AxiosError, AxiosResponse} from "axios";
import {AxiosError, AxiosResponse} from "axios";
import {CSRFToken, urlContextPath} from "../util/baseUrlUtil";
import {AdyenAddressForm, AdyenPaymentForm} from "../types/paymentForm";
import {AddressModel} from "../reducers/types";
import {store} from "../store/store";
import {CardState} from "../types/paymentState";
import {PaymentAction} from "@adyen/adyen-web/dist/types/types";
import {ErrorResponse} from "../types/errorResponse";
import {ErrorHandler} from "../components/common/ErrorHandler";
import {adyenAxios} from "../axios/AdyenAxios";

export interface PaymentResponse {
success: boolean,
Expand All @@ -17,7 +17,7 @@ export interface PaymentResponse {

export class PaymentService {
static async placeOrder(paymentForm: AdyenPaymentForm) {
return axios.post<PaymentResponse>(urlContextPath + '/api/checkout/place-order', paymentForm, {
return adyenAxios.post<PaymentResponse>(urlContextPath + '/api/checkout/place-order', paymentForm, {
headers: {
'Content-Type': 'application/json',
'CSRFToken': CSRFToken
Expand All @@ -40,8 +40,6 @@ export class PaymentService {
success: false,
error: errorResponse.response.data.errorCode
}
} else {
ErrorHandler.handleError(errorResponse)
}
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import axios, {AxiosError} from "axios";
import {AxiosError} from "axios";
import {CSRFToken, urlContextPath} from "../util/baseUrlUtil";
import {ErrorResponse} from "../types/errorResponse";
import {ErrorHandler} from "../components/common/ErrorHandler";
import {adyenAxios} from "../axios/AdyenAxios";

export class PaymentStatusService {
static fetchPaymentStatus(orderCode: string) {
return axios.post(urlContextPath + '/api/checkout/get-payment-status', orderCode, {
return adyenAxios.post(urlContextPath + '/api/checkout/get-payment-status', orderCode, {
headers: {
'Content-Type': 'application/json',
'CSRFToken': CSRFToken
Expand All @@ -14,8 +14,7 @@ export class PaymentStatusService {
.then(response => {
return response.data
})
.catch((errorResponse:AxiosError<ErrorResponse>) => {
ErrorHandler.handleError(errorResponse)
.catch((errorResponse: AxiosError<ErrorResponse>) => {
console.error("Payment status fetch error.")
return false
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import axios, {AxiosError, AxiosResponse} from "axios";
import {AxiosError, AxiosResponse} from "axios";
import {CSRFToken, urlContextPath} from "../util/baseUrlUtil";
import {store} from "../store/store";
import {ShippingMethodModel} from "../reducers/types";
import {CartData} from "../types/cartData";
import {isEmpty} from "../util/stringUtil";
import {ErrorResponse} from "../types/errorResponse";
import {ErrorHandler} from "../components/common/ErrorHandler";
import {adyenAxios} from "../axios/AdyenAxios";

export class ShippingMethodService {
public static fetchShippingMethods() {
axios.get(urlContextPath + '/api/checkout/delivery-methods', {
adyenAxios.get(urlContextPath + '/api/checkout/delivery-methods', {
headers: {
"X-Requested-With": "XMLHttpRequest"
}
Expand All @@ -25,15 +25,14 @@ export class ShippingMethodService {

})
.catch((errorResponse: AxiosError<ErrorResponse>) => {
ErrorHandler.handleError(errorResponse)
console.error('Shipping method fetch error.')
return false
})
}


static selectShippingMethod(shippingMethodId: string) {
return axios.post(urlContextPath + '/api/checkout/select-delivery-method', shippingMethodId, {
return adyenAxios.post(urlContextPath + '/api/checkout/select-delivery-method', shippingMethodId, {
headers: {
'Content-Type': 'text/plain',
'CSRFToken': CSRFToken
Expand All @@ -49,7 +48,6 @@ export class ShippingMethodService {
return true;
})
.catch((errorResponse: AxiosError<ErrorResponse>) => {
ErrorHandler.handleError(errorResponse)
console.error('Error on shipping method select.')
return false
})
Expand Down
Loading