mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-08 21:52:55 +08:00
Refactor GooglePay context handlers
This commit is contained in:
parent
b955fc648e
commit
00f6b467b6
10 changed files with 125 additions and 358 deletions
73
modules/ppcp-googlepay/resources/js/Context/BaseHandler.js
Normal file
73
modules/ppcp-googlepay/resources/js/Context/BaseHandler.js
Normal file
|
@ -0,0 +1,73 @@
|
|||
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) {
|
||||
this.buttonConfig = buttonConfig;
|
||||
this.ppcpConfig = ppcpConfig;
|
||||
}
|
||||
|
||||
transactionInfo() {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
fetch(
|
||||
this.ppcpConfig.ajax.cart_script_params.endpoint,
|
||||
{
|
||||
method: 'GET',
|
||||
credentials: 'same-origin',
|
||||
}
|
||||
)
|
||||
.then(result => result.json())
|
||||
.then(result => {
|
||||
if (! result.success) {
|
||||
return;
|
||||
}
|
||||
|
||||
// handle script reload
|
||||
const data = result.data;
|
||||
|
||||
resolve({
|
||||
countryCode: 'US', // data.country_code,
|
||||
currencyCode: 'USD', // data.currency_code,
|
||||
totalPriceStatus: 'FINAL',
|
||||
totalPrice: data.amount // Your amount
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
approveOrderForContinue(data, actions) {
|
||||
const errorHandler = new ErrorHandler(
|
||||
this.ppcpConfig.labels.error.generic,
|
||||
document.querySelector('.woocommerce-notices-wrapper')
|
||||
);
|
||||
|
||||
let onApproveHandler = onApprove({
|
||||
config: this.ppcpConfig
|
||||
}, errorHandler);
|
||||
|
||||
return onApproveHandler(data, actions);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default BaseHandler;
|
|
@ -1,74 +1,6 @@
|
|||
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";
|
||||
import BaseHandler from "./BaseHandler";
|
||||
|
||||
class CartBlockHandler {
|
||||
|
||||
constructor(buttonConfig, ppcpConfig) {
|
||||
console.log('NEW CartHandler');
|
||||
|
||||
this.buttonConfig = buttonConfig;
|
||||
this.ppcpConfig = ppcpConfig;
|
||||
}
|
||||
|
||||
transactionInfo() {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
fetch(
|
||||
this.ppcpConfig.ajax.cart_script_params.endpoint,
|
||||
{
|
||||
method: 'GET',
|
||||
credentials: 'same-origin',
|
||||
}
|
||||
)
|
||||
.then(result => result.json())
|
||||
.then(result => {
|
||||
if (! result.success) {
|
||||
return;
|
||||
}
|
||||
|
||||
// handle script reload
|
||||
const data = result.data;
|
||||
|
||||
resolve({
|
||||
countryCode: 'US', // data.country_code,
|
||||
currencyCode: 'USD', // data.currency_code,
|
||||
totalPriceStatus: 'FINAL',
|
||||
totalPrice: data.amount // Your amount
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
approveOrderForContinue(data, actions) {
|
||||
const errorHandler = new ErrorHandler(
|
||||
this.ppcpConfig.labels.error.generic,
|
||||
document.querySelector('.woocommerce-notices-wrapper')
|
||||
);
|
||||
|
||||
let onApproveHandler = onApprove({
|
||||
config: this.ppcpConfig
|
||||
}, errorHandler);
|
||||
|
||||
return onApproveHandler(data, actions);
|
||||
}
|
||||
class CartBlockHandler extends BaseHandler {
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -1,74 +1,6 @@
|
|||
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";
|
||||
import BaseHandler from "./BaseHandler";
|
||||
|
||||
class CartHandler {
|
||||
|
||||
constructor(buttonConfig, ppcpConfig) {
|
||||
console.log('NEW CartHandler');
|
||||
|
||||
this.buttonConfig = buttonConfig;
|
||||
this.ppcpConfig = ppcpConfig;
|
||||
}
|
||||
|
||||
transactionInfo() {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
fetch(
|
||||
this.ppcpConfig.ajax.cart_script_params.endpoint,
|
||||
{
|
||||
method: 'GET',
|
||||
credentials: 'same-origin',
|
||||
}
|
||||
)
|
||||
.then(result => result.json())
|
||||
.then(result => {
|
||||
if (! result.success) {
|
||||
return;
|
||||
}
|
||||
|
||||
// handle script reload
|
||||
const data = result.data;
|
||||
|
||||
resolve({
|
||||
countryCode: 'US', // data.country_code,
|
||||
currencyCode: 'USD', // data.currency_code,
|
||||
totalPriceStatus: 'FINAL',
|
||||
totalPrice: data.amount // Your amount
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
approveOrderForContinue(data, actions) {
|
||||
const errorHandler = new ErrorHandler(
|
||||
this.ppcpConfig.labels.error.generic,
|
||||
document.querySelector('.woocommerce-notices-wrapper')
|
||||
);
|
||||
|
||||
let onApproveHandler = onApprove({
|
||||
config: this.ppcpConfig
|
||||
}, errorHandler);
|
||||
|
||||
return onApproveHandler(data, actions);
|
||||
}
|
||||
class CartHandler extends BaseHandler {
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -1,74 +1,6 @@
|
|||
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";
|
||||
import BaseHandler from "./BaseHandler";
|
||||
|
||||
class CheckoutBlockHandler {
|
||||
|
||||
constructor(buttonConfig, ppcpConfig) {
|
||||
console.log('NEW CartHandler');
|
||||
|
||||
this.buttonConfig = buttonConfig;
|
||||
this.ppcpConfig = ppcpConfig;
|
||||
}
|
||||
|
||||
transactionInfo() {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
fetch(
|
||||
this.ppcpConfig.ajax.cart_script_params.endpoint,
|
||||
{
|
||||
method: 'GET',
|
||||
credentials: 'same-origin',
|
||||
}
|
||||
)
|
||||
.then(result => result.json())
|
||||
.then(result => {
|
||||
if (! result.success) {
|
||||
return;
|
||||
}
|
||||
|
||||
// handle script reload
|
||||
const data = result.data;
|
||||
|
||||
resolve({
|
||||
countryCode: 'US', // data.country_code,
|
||||
currencyCode: 'USD', // data.currency_code,
|
||||
totalPriceStatus: 'FINAL',
|
||||
totalPrice: data.amount // Your amount
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
approveOrderForContinue(data, actions) {
|
||||
const errorHandler = new ErrorHandler(
|
||||
this.ppcpConfig.labels.error.generic,
|
||||
document.querySelector('.woocommerce-notices-wrapper')
|
||||
);
|
||||
|
||||
let onApproveHandler = onApprove({
|
||||
config: this.ppcpConfig
|
||||
}, errorHandler);
|
||||
|
||||
return onApproveHandler(data, actions);
|
||||
}
|
||||
class CheckoutBlockHandler extends BaseHandler{
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -2,47 +2,9 @@ import Spinner from "../../../../ppcp-button/resources/js/modules/Helper/Spinner
|
|||
import CheckoutActionHandler
|
||||
from "../../../../ppcp-button/resources/js/modules/ActionHandler/CheckoutActionHandler";
|
||||
import ErrorHandler from "../../../../ppcp-button/resources/js/modules/ErrorHandler";
|
||||
import onApprove
|
||||
from "../../../../ppcp-button/resources/js/modules/OnApproveHandler/onApproveForContinue";
|
||||
import BaseHandler from "./BaseHandler";
|
||||
|
||||
class CheckoutHandler {
|
||||
|
||||
constructor(buttonConfig, ppcpConfig) {
|
||||
console.log('NEW CheckoutHandler');
|
||||
|
||||
this.buttonConfig = buttonConfig;
|
||||
this.ppcpConfig = ppcpConfig;
|
||||
}
|
||||
|
||||
transactionInfo() {
|
||||
return new Promise((resolve, reject) => {
|
||||
|
||||
fetch(
|
||||
this.ppcpConfig.ajax.cart_script_params.endpoint,
|
||||
{
|
||||
method: 'GET',
|
||||
credentials: 'same-origin',
|
||||
}
|
||||
)
|
||||
.then(result => result.json())
|
||||
.then(result => {
|
||||
if (! result.success) {
|
||||
return;
|
||||
}
|
||||
|
||||
// handle script reload
|
||||
const data = result.data;
|
||||
|
||||
resolve({
|
||||
countryCode: 'US', // data.country_code,
|
||||
currencyCode: 'USD', // data.currency_code,
|
||||
totalPriceStatus: 'FINAL',
|
||||
totalPrice: data.amount // Your amount
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
}
|
||||
class CheckoutHandler extends BaseHandler {
|
||||
|
||||
createOrder() {
|
||||
const errorHandler = new ErrorHandler(
|
||||
|
@ -61,19 +23,6 @@ class CheckoutHandler {
|
|||
return actionHandler.configuration().createOrder(null, null);
|
||||
}
|
||||
|
||||
approveOrderForContinue(data, actions) {
|
||||
const errorHandler = new ErrorHandler(
|
||||
this.ppcpConfig.labels.error.generic,
|
||||
document.querySelector('.woocommerce-notices-wrapper')
|
||||
);
|
||||
|
||||
let onApproveHandler = onApprove({
|
||||
config: this.ppcpConfig
|
||||
}, errorHandler);
|
||||
|
||||
return onApproveHandler(data, actions);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default CheckoutHandler;
|
||||
|
|
|
@ -14,7 +14,7 @@ class ContextHandlerFactory {
|
|||
case 'cart':
|
||||
return new CartHandler(buttonConfig, ppcpConfig);
|
||||
case 'checkout':
|
||||
case 'pay-now':
|
||||
case 'pay-now': // same as checkout
|
||||
return new CheckoutHandler(buttonConfig, ppcpConfig);
|
||||
case 'mini-cart':
|
||||
return new MiniCartHandler(buttonConfig, ppcpConfig);
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import CartHandler from "./CartHandler";
|
||||
import BaseHandler from "./BaseHandler";
|
||||
|
||||
class MiniCartHandler extends CartHandler {
|
||||
class MiniCartHandler extends BaseHandler {
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -3,17 +3,9 @@ import SingleProductActionHandler
|
|||
import SimulateCart from "../../../../ppcp-button/resources/js/modules/Helper/SimulateCart";
|
||||
import ErrorHandler from "../../../../ppcp-button/resources/js/modules/ErrorHandler";
|
||||
import UpdateCart from "../../../../ppcp-button/resources/js/modules/Helper/UpdateCart";
|
||||
import onApprove
|
||||
from "../../../../ppcp-button/resources/js/modules/OnApproveHandler/onApproveForContinue";
|
||||
import BaseHandler from "./BaseHandler";
|
||||
|
||||
class SingleProductHandler {
|
||||
|
||||
constructor(buttonConfig, ppcpConfig) {
|
||||
console.log('NEW SingleProductHandler');
|
||||
|
||||
this.buttonConfig = buttonConfig;
|
||||
this.ppcpConfig = ppcpConfig;
|
||||
}
|
||||
class SingleProductHandler extends BaseHandler {
|
||||
|
||||
transactionInfo() {
|
||||
const errorHandler = new ErrorHandler(
|
||||
|
@ -75,19 +67,6 @@ class SingleProductHandler {
|
|||
return actionHandler.configuration().createOrder();
|
||||
}
|
||||
|
||||
approveOrderForContinue(data, actions) {
|
||||
const errorHandler = new ErrorHandler(
|
||||
this.ppcpConfig.labels.error.generic,
|
||||
document.querySelector('.woocommerce-notices-wrapper')
|
||||
);
|
||||
|
||||
let onApproveHandler = onApprove({
|
||||
config: this.ppcpConfig
|
||||
}, errorHandler);
|
||||
|
||||
return onApproveHandler(data, actions);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default SingleProductHandler;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue