Skip to content

Commit

Permalink
Vendic#5 - send post request with cart data to vs-api before redirect…
Browse files Browse the repository at this point in the history
… to magento checkout
  • Loading branch information
glevhen committed Jun 5, 2019
1 parent ab52a82 commit 1253ed9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@
"bugs": {
"url": "https://github.com/Vendic/vsf-external-checkout/issues"
},
"homepage": "https://github.com/Vendic/vsf-external-checkout#readme"
"homepage": "https://github.com/Vendic/vsf-external-checkout#readme",
"dependencies": {
"axios": "^0.19.0"
}
}
27 changes: 23 additions & 4 deletions router/beforeEach.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,47 @@ import {Route} from 'vue-router'
import rootStore from '@vue-storefront/core/store'
import config from 'config'
import {storeCodeFromRoute} from '@vue-storefront/core/lib/multistore'
import axios from 'axios'

export function beforeEach(to: Route, from: Route, next) {

const cartToken: string = rootStore.state.cart.cartServerToken;
const userToken: string = rootStore.state.user.token;
const externalCheckoutConfig = {...config.externalCheckout};
const cmsUrl: string = externalCheckoutConfig.cmsUrl;
const cmsCartSyncUrl: string = externalCheckoutConfig.cmsCartSyncUrl;
const magentoOrderEndpoint: string = externalCheckoutConfig.magentoOrderEndpoint;
const postRequestEnabled: boolean = externalCheckoutConfig.usePostRequest;
const stores = externalCheckoutConfig.stores;
const storeCode = storeCodeFromRoute(to)
const multistoreEnabled: boolean = config.storeViews.multistore
const storeCode = storeCodeFromRoute(to);
const multistoreEnabled: boolean = config.storeViews.multistore;

let cartData = rootStore.state.cart;

if (multistoreEnabled) {
if (storeCode in stores && to.name === storeCode + '-checkout') {
window.location.replace(stores[storeCode].cmsUrl + '/vue/cart/sync/token/' + userToken + '/cart/' + cartToken)
syncCart(stores[storeCode].cmsUrl + '/vue/cart/sync/token/' + userToken + '/cart/' + cartToken)
} else {
next()
}
} else {
if (to.name === 'checkout') {
window.location.replace(cmsUrl + '/vue/cart/sync/token/' + userToken + '/cart/' + cartToken)
syncCart(cmsUrl + '/vue/cart/sync/token/' + userToken + '/cart/' + cartToken)
} else {
next()
}
}

function syncCart(redirectUrl) {
if (postRequestEnabled) {
axios.post(magentoOrderEndpoint, {'cartData': cartData, 'cmsCartSyncUrl': cmsCartSyncUrl})
.then(function (response) {
window.location.replace(redirectUrl)
console.log(response)
})
.catch(function (error) {
console.log(error)
});
}
}
}

0 comments on commit 1253ed9

Please sign in to comment.