mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-04 08:47:23 +08:00
Refactor GooglePay contect handlers to reuse action handlers.
This commit is contained in:
parent
318217acb9
commit
c6379ca980
8 changed files with 78 additions and 64 deletions
|
@ -1,14 +1,13 @@
|
|||
import ErrorHandler from "../../../../ppcp-button/resources/js/modules/ErrorHandler";
|
||||
import CartActionHandler
|
||||
from "../../../../ppcp-button/resources/js/modules/ActionHandler/CartActionHandler";
|
||||
import onApprove
|
||||
from "../../../../ppcp-button/resources/js/modules/OnApproveHandler/onApproveForContinue";
|
||||
|
||||
class BaseHandler {
|
||||
|
||||
constructor(buttonConfig, ppcpConfig) {
|
||||
constructor(buttonConfig, ppcpConfig, externalHandler) {
|
||||
this.buttonConfig = buttonConfig;
|
||||
this.ppcpConfig = ppcpConfig;
|
||||
this.externalHandler = externalHandler;
|
||||
}
|
||||
|
||||
transactionInfo() {
|
||||
|
@ -42,30 +41,25 @@ class BaseHandler {
|
|||
}
|
||||
|
||||
createOrder() {
|
||||
const errorHandler = new ErrorHandler(
|
||||
this.ppcpConfig.labels.error.generic,
|
||||
document.querySelector('.woocommerce-notices-wrapper')
|
||||
);
|
||||
|
||||
const actionHandler = new CartActionHandler(
|
||||
this.ppcpConfig,
|
||||
errorHandler,
|
||||
);
|
||||
|
||||
return actionHandler.configuration().createOrder(null, null);
|
||||
return this.actionHandler().configuration().createOrder(null, null);
|
||||
}
|
||||
|
||||
approveOrderForContinue(data, actions) {
|
||||
const errorHandler = new ErrorHandler(
|
||||
approveOrder(data, actions) {
|
||||
return this.actionHandler().configuration().onApprove(data, actions);
|
||||
}
|
||||
|
||||
actionHandler() {
|
||||
return new CartActionHandler(
|
||||
this.ppcpConfig,
|
||||
this.errorHandler(),
|
||||
);
|
||||
}
|
||||
|
||||
errorHandler() {
|
||||
return new ErrorHandler(
|
||||
this.ppcpConfig.labels.error.generic,
|
||||
document.querySelector('.woocommerce-notices-wrapper')
|
||||
);
|
||||
|
||||
let onApproveHandler = onApprove({
|
||||
config: this.ppcpConfig
|
||||
}, errorHandler);
|
||||
|
||||
return onApproveHandler(data, actions);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2,6 +2,14 @@ import BaseHandler from "./BaseHandler";
|
|||
|
||||
class CartBlockHandler extends BaseHandler {
|
||||
|
||||
createOrder() {
|
||||
return this.externalHandler.createOrder();
|
||||
}
|
||||
|
||||
approveOrder(data, actions) {
|
||||
return this.externalHandler.onApprove(data, actions);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default CartBlockHandler;
|
||||
|
|
|
@ -2,6 +2,14 @@ import BaseHandler from "./BaseHandler";
|
|||
|
||||
class CheckoutBlockHandler extends BaseHandler{
|
||||
|
||||
createOrder() {
|
||||
return this.externalHandler.createOrder();
|
||||
}
|
||||
|
||||
approveOrder(data, actions) {
|
||||
return this.externalHandler.onApprove(data, actions);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default CheckoutBlockHandler;
|
||||
|
|
|
@ -1,26 +1,16 @@
|
|||
import Spinner from "../../../../ppcp-button/resources/js/modules/Helper/Spinner";
|
||||
import BaseHandler from "./BaseHandler";
|
||||
import CheckoutActionHandler
|
||||
from "../../../../ppcp-button/resources/js/modules/ActionHandler/CheckoutActionHandler";
|
||||
import ErrorHandler from "../../../../ppcp-button/resources/js/modules/ErrorHandler";
|
||||
import BaseHandler from "./BaseHandler";
|
||||
|
||||
class CheckoutHandler extends BaseHandler {
|
||||
|
||||
createOrder() {
|
||||
const errorHandler = new ErrorHandler(
|
||||
this.ppcpConfig.labels.error.generic,
|
||||
document.querySelector('.woocommerce-notices-wrapper')
|
||||
);
|
||||
|
||||
const spinner = new Spinner();
|
||||
|
||||
const actionHandler = new CheckoutActionHandler(
|
||||
actionHandler() {
|
||||
return new CheckoutActionHandler(
|
||||
this.ppcpConfig,
|
||||
errorHandler,
|
||||
spinner
|
||||
this.errorHandler(),
|
||||
new Spinner()
|
||||
);
|
||||
|
||||
return actionHandler.configuration().createOrder(null, null);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -7,21 +7,21 @@ import MiniCartHandler from "./MiniCartHandler";
|
|||
|
||||
class ContextHandlerFactory {
|
||||
|
||||
static create(context, buttonConfig, ppcpConfig) {
|
||||
static create(context, buttonConfig, ppcpConfig, externalActionHandler) {
|
||||
switch (context) {
|
||||
case 'product':
|
||||
return new SingleProductHandler(buttonConfig, ppcpConfig);
|
||||
return new SingleProductHandler(buttonConfig, ppcpConfig, externalActionHandler);
|
||||
case 'cart':
|
||||
return new CartHandler(buttonConfig, ppcpConfig);
|
||||
return new CartHandler(buttonConfig, ppcpConfig, externalActionHandler);
|
||||
case 'checkout':
|
||||
case 'pay-now': // same as checkout
|
||||
return new CheckoutHandler(buttonConfig, ppcpConfig);
|
||||
return new CheckoutHandler(buttonConfig, ppcpConfig, externalActionHandler);
|
||||
case 'mini-cart':
|
||||
return new MiniCartHandler(buttonConfig, ppcpConfig);
|
||||
return new MiniCartHandler(buttonConfig, ppcpConfig, externalActionHandler);
|
||||
case 'cart-block':
|
||||
return new CartBlockHandler(buttonConfig, ppcpConfig);
|
||||
return new CartBlockHandler(buttonConfig, ppcpConfig, externalActionHandler);
|
||||
case 'checkout-block':
|
||||
return new CheckoutBlockHandler(buttonConfig, ppcpConfig);
|
||||
return new CheckoutBlockHandler(buttonConfig, ppcpConfig, externalActionHandler);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,6 +4,9 @@ import SimulateCart from "../../../../ppcp-button/resources/js/modules/Helper/Si
|
|||
import ErrorHandler from "../../../../ppcp-button/resources/js/modules/ErrorHandler";
|
||||
import UpdateCart from "../../../../ppcp-button/resources/js/modules/Helper/UpdateCart";
|
||||
import BaseHandler from "./BaseHandler";
|
||||
import CheckoutActionHandler
|
||||
from "../../../../ppcp-button/resources/js/modules/ActionHandler/CheckoutActionHandler";
|
||||
import Spinner from "../../../../ppcp-button/resources/js/modules/Helper/Spinner";
|
||||
|
||||
class SingleProductHandler extends BaseHandler {
|
||||
|
||||
|
@ -48,23 +51,16 @@ class SingleProductHandler extends BaseHandler {
|
|||
});
|
||||
}
|
||||
|
||||
createOrder() {
|
||||
const errorHandler = new ErrorHandler(
|
||||
this.ppcpConfig.labels.error.generic,
|
||||
document.querySelector('.woocommerce-notices-wrapper')
|
||||
);
|
||||
|
||||
const actionHandler = new SingleProductActionHandler(
|
||||
actionHandler() {
|
||||
return new SingleProductActionHandler(
|
||||
this.ppcpConfig,
|
||||
new UpdateCart(
|
||||
this.ppcpConfig.ajax.change_cart.endpoint,
|
||||
this.ppcpConfig.ajax.change_cart.nonce,
|
||||
),
|
||||
document.querySelector('form.cart'),
|
||||
errorHandler,
|
||||
this.errorHandler(),
|
||||
);
|
||||
|
||||
return actionHandler.configuration().createOrder();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -15,8 +15,11 @@ class GooglepayButton {
|
|||
this.contextHandler = ContextHandlerFactory.create(
|
||||
this.context,
|
||||
this.buttonConfig,
|
||||
this.ppcpConfig
|
||||
this.ppcpConfig,
|
||||
this.externalHandler
|
||||
);
|
||||
|
||||
console.log('[GooglePayButton] new Button', this);
|
||||
}
|
||||
|
||||
init(config) {
|
||||
|
@ -176,13 +179,18 @@ class GooglepayButton {
|
|||
if (confirmOrderResponse.status === "APPROVED") {
|
||||
|
||||
let approveFailed = false;
|
||||
await this.contextHandler.approveOrderForContinue({
|
||||
await this.contextHandler.approveOrder({
|
||||
orderID: id
|
||||
}, {
|
||||
}, { // actions mock object.
|
||||
restart: () => new Promise((resolve, reject) => {
|
||||
approveFailed = true;
|
||||
resolve();
|
||||
})
|
||||
}),
|
||||
order: {
|
||||
get: () => new Promise((resolve, reject) => {
|
||||
resolve(null);
|
||||
})
|
||||
}
|
||||
});
|
||||
|
||||
if (!approveFailed) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue