File: D:/HostingSpaces/blijegasten/blijegasten.be/resources/js/shop/components/checkoutService.js
import { ErrorResponse } from "../../global/models/errorResponse";
import { CheckoutInformationResponse } from "../../global/models/checkoutInformationResponse";
/**
* The checkout service knows how to do api calls and handle the responses from the api.
* All related to the checkout process. It does not do anything with the DOM.
*/
class CheckoutService {
constructor() {
this._baseRoute = '/';
}
/**
* Sets the shipping address so that the shipping costs for example
* can be calculated on advance
*
* @param {Address} address
* @return {Promise<boolean>} Resolves when the shipping address is set. Rejects with an ErrorResponse when it is not.
*/
setShippingAddress(address)
{
let self = this;
return new Promise(function(resolve, reject) {
Ajax.post(
self._baseRoute+'setShippingAddress',
address,
function(xhr) {
// let response = JSON.parse(xhr.response);
if(ErrorResponse.is(xhr.response, false) === false) {
resolve()
} else {
let response = ErrorResponse.fromJsonString(xhr.response);
reject(response);
}
}
);
})
}
/**
* Returns most recent information about the checkout process
*/
getCheckoutInformation()
{
let self = this;
return new Promise(function(resolve, reject) {
Ajax.get(
self._baseRoute+'getCheckoutInformation',
function(xhr) {
// let response = JSON.parse(xhr.response);
let isErrorResponse = ErrorResponse.is(xhr.response, false);
let isCheckoutInformationResponse = CheckoutInformationResponse.is(xhr.response, false);
if(!isErrorResponse) {
if(isCheckoutInformationResponse) {
let checkoutInformationResponse = CheckoutInformationResponse.fromJsonString(xhr.response);
resolve(checkoutInformationResponse);
}
else {
console.error('CheckoutService:getCheckoutInformation The response was not a valid CheckoutInformationResponse: '+xhr.response);
reject();
}
} else {
let response = ErrorResponse.fromJsonString(xhr.response);
reject(response);
}
}
);
})
}
/**
* @param {string} coupon
* @return {Promise}
*/
addCoupon(coupon) {
let self = this;
if(typeof coupon !== 'string') {
console.error('CheckoutService:addCoupon could not add coupon. It must be a string but was not.');
return Promise.reject('CheckoutService:addCoupon could not add coupon. It must be a string but was not.');
}
return new Promise(function(resolve, reject) {
Ajax.post(
self._baseRoute+'addCouponCode',
{'coupon': coupon},
function(xhr) {
// let response = JSON.parse(xhr.response);
let isErrorResponse = ErrorResponse.is(xhr.response, false);
let isCheckoutInformationResponse = CheckoutInformationResponse.is(xhr.response, false);
if(!isErrorResponse) {
if(isCheckoutInformationResponse) {
let checkoutInformationResponse = CheckoutInformationResponse.fromJsonString(xhr.response);
resolve(checkoutInformationResponse);
}
else {
console.error('CheckoutService:addCoupon The response was not a valid CheckoutInformationResponse: '+xhr.response);
reject();
}
} else {
let response = ErrorResponse.fromJsonString(xhr.response);
reject(response);
}
}
);
});
}
/**
* @param {string} coupon
* @return {Promise}
*/
removeCoupon(coupon) {
let self = this;
if(typeof coupon !== 'string') {
console.error('CheckoutService:removeCoupon could not remove coupon. It must be a string but was not.', coupon);
return Promise.reject('CheckoutService:removeCoupon could not remove coupon. It must be a string but was not.');
}
return new Promise(function(resolve, reject) {
Ajax.post(
self._baseRoute+'removeCouponCode',
{'coupon': coupon},
function(xhr) {
// let response = JSON.parse(xhr.response);
let isErrorResponse = ErrorResponse.is(xhr.response, false);
let isCheckoutInformationResponse = CheckoutInformationResponse.is(xhr.response, false);
if(!isErrorResponse) {
if(isCheckoutInformationResponse) {
let checkoutInformationResponse = CheckoutInformationResponse.fromJsonString(xhr.response);
resolve(checkoutInformationResponse);
}
else {
console.error('CheckoutService:removeCoupon The response was not a valid CheckoutInformationResponse: '+xhr.response);
reject();
}
} else {
let response = ErrorResponse.fromJsonString(xhr.response);
reject(response);
}
}
);
});
}
}
export { CheckoutService }