checkout immediately after going through paypal on the checkout page

This commit is contained in:
David Remer 2020-04-09 09:33:18 +03:00
parent a6eb731590
commit 74129c5914
8 changed files with 75 additions and 10 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -3,11 +3,12 @@ import SingleProductConfig from './modules/SingleProductConfig';
import UpdateCart from './modules/UpdateCart';
import ErrorHandler from './modules/ErrorHandler';
import CartConfig from './modules/CartConfig';
import CheckoutConfig from './modules/CheckoutConfig';
const bootstrap = ()=> {
const context = PayPalCommerceGateway.context;
const errorHandler = new ErrorHandler();
const defaultConfigurator = new CartConfig(
const cartConfigurator = new CartConfig(
PayPalCommerceGateway,
errorHandler
);
@ -17,7 +18,7 @@ const bootstrap = ()=> {
const renderer = new Renderer(
PayPalCommerceGateway.button.mini_cart_wrapper
);
renderer.render(defaultConfigurator.configuration())
renderer.render(cartConfigurator.configuration())
}
jQuery( document.body ).on( 'wc_fragments_loaded wc_fragments_refreshed', () => {
if (! document.querySelector(PayPalCommerceGateway.button.mini_cart_wrapper)) {
@ -26,7 +27,7 @@ const bootstrap = ()=> {
const renderer = new Renderer(
PayPalCommerceGateway.button.mini_cart_wrapper
);
renderer.render(defaultConfigurator.configuration())
renderer.render(cartConfigurator.configuration())
} );
// Configure context buttons
@ -55,14 +56,17 @@ const bootstrap = ()=> {
);
}
if (context === 'cart') {
configurator = defaultConfigurator;
configurator = cartConfigurator;
jQuery( document.body ).on( 'updated_cart_totals updated_checkout', () => {
renderer.render(configurator.configuration())
});
}
if (context === 'checkout' ) {
configurator = defaultConfigurator;
configurator = new CheckoutConfig(
PayPalCommerceGateway,
errorHandler
);
if (!document.querySelector(PayPalCommerceGateway.button.cancel_wrapper)) {
@ -78,7 +82,7 @@ const bootstrap = ()=> {
}
}
jQuery(document.body).on('updated_checkout', () => {
renderer.render(defaultConfigurator.configuration());
renderer.render(configurator.configuration());
jQuery(document.body).trigger('payment_method_selected')
});
jQuery(document.body).on('payment_method_selected', toggleButtons );

View file

@ -1,4 +1,4 @@
import onApprove from "./onApprove";
import onApprove from "./onApproveForContinue.js";
class CartConfig {

View file

@ -0,0 +1,39 @@
import onApprove from "./onApproveForPayNow.js";
class CheckoutConfig {
constructor(config, errorHandler) {
this.config = config;
this.errorHandler = errorHandler;
}
configuration() {
const createOrder = (data, actions) => {
return fetch(this.config.ajax.create_order.endpoint, {
method: 'POST',
body: JSON.stringify({
nonce: this.config.ajax.create_order.nonce,
purchase_units:[]
})
}).then(function (res) {
return res.json();
}).then(function (data) {
if (!data.success) {
//Todo: Error handling
return;
}
return data.data.id;
});
}
return {
createOrder,
onApprove:onApprove(this),
onError: (error) => {
this.errorHandler.message(error);
}
}
}
}
export default CheckoutConfig;

View file

@ -1,6 +1,6 @@
import ButtonsToggleListener from "./ButtonsToggleListener";
import Product from "./Product";
import onApprove from "./onApprove";
import onApprove from "./onApproveForContinue";
class SingleProductConfig {
constructor(

View file

@ -0,0 +1,22 @@
const onApprove = (context) => {
return (data, actions) => {
return fetch(context.config.ajax.approve_order.endpoint, {
method: 'POST',
body: JSON.stringify({
nonce: context.config.ajax.approve_order.nonce,
order_id:data.orderID
})
}).then((res)=>{
return res.json();
}).then((data)=>{
if (!data.success) {
//Todo: Error handling
return;
}
document.querySelector('#place_order').click()
});
}
}
export default onApprove;