Refactor GooglePay context handlers

This commit is contained in:
Pedro Silva 2023-08-29 14:57:10 +01:00
parent b955fc648e
commit 00f6b467b6
No known key found for this signature in database
GPG key ID: E2EE20C0669D24B3
10 changed files with 125 additions and 358 deletions

View 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;

View file

@ -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 {
}

View file

@ -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 {
}

View file

@ -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{
}

View file

@ -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;

View file

@ -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);

View file

@ -1,6 +1,6 @@
import CartHandler from "./CartHandler";
import BaseHandler from "./BaseHandler";
class MiniCartHandler extends CartHandler {
class MiniCartHandler extends BaseHandler {
}

View file

@ -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;

View file

@ -26,18 +26,14 @@ class GooglepayButton {
this.isInitialized = true;
this.googlePayConfig = config;
console.log('googlePayConfig', this.googlePayConfig);
this.allowedPaymentMethods = config.allowedPaymentMethods;
this.isReadyToPayRequest = this.buildReadyToPayRequest(this.allowedPaymentMethods, config);
console.log('googleIsReadyToPayRequest', this.isReadyToPayRequest);
this.baseCardPaymentMethod = this.allowedPaymentMethods[0];
this.initClient();
this.paymentsClient.isReadyToPay(this.isReadyToPayRequest)
this.paymentsClient.isReadyToPay(
this.buildReadyToPayRequest(this.allowedPaymentMethods, config)
)
.then((response) => {
if (response.result) {
this.addButton(this.baseCardPaymentMethod);
@ -49,8 +45,6 @@ class GooglepayButton {
}
buildReadyToPayRequest(allowedPaymentMethods, baseRequest) {
console.log('allowedPaymentMethods', allowedPaymentMethods);
return Object.assign({}, baseRequest, {
allowedPaymentMethods: allowedPaymentMethods,
});
@ -71,7 +65,7 @@ class GooglepayButton {
* Add a Google Pay purchase button
*/
addButton(baseCardPaymentMethod) {
console.log('addGooglePayButton');
console.log('[GooglePayButton] addButton', this.context);
const wrapper =
(this.context === 'mini-cart')
@ -96,9 +90,11 @@ class GooglepayButton {
* Show Google Pay payment sheet when Google Pay payment button is clicked
*/
async onButtonClick() {
console.log('onGooglePaymentButtonClicked');
console.log('[GooglePayButton] onButtonClick', this.context);
const paymentDataRequest = await this.paymentDataRequest();
console.log('[GooglePayButton] onButtonClick: paymentDataRequest', paymentDataRequest, this.context);
this.paymentsClient.loadPaymentData(paymentDataRequest);
}
@ -111,61 +107,40 @@ class GooglepayButton {
const googlePayConfig = this.googlePayConfig;
const paymentDataRequest = Object.assign({}, baseRequest);
paymentDataRequest.allowedPaymentMethods = googlePayConfig.allowedPaymentMethods;
paymentDataRequest.transactionInfo = await this.transactionInfo();
paymentDataRequest.transactionInfo = await this.contextHandler.transactionInfo();
paymentDataRequest.merchantInfo = googlePayConfig.merchantInfo;
paymentDataRequest.callbackIntents = ["PAYMENT_AUTHORIZATION"];
paymentDataRequest.callbackIntents = ['PAYMENT_AUTHORIZATION'];
return paymentDataRequest;
}
async transactionInfo() {
return this.contextHandler.transactionInfo();
}
//------------------------
// Payment process
//------------------------
onPaymentAuthorized(paymentData) {
console.log('onPaymentAuthorized', paymentData);
return new Promise((resolve, reject) => {
this.processPayment(paymentData)
.then(function (data) {
console.log('resolve: ', data);
resolve(data);
})
.catch(function (errDetails) {
console.log('resolve: ERROR', errDetails);
resolve({ transactionState: "ERROR" });
});
});
console.log('[GooglePayButton] onPaymentAuthorized', this.context);
return this.processPayment(paymentData);
}
async processPayment(paymentData) {
console.log('processPayment');
console.log('[GooglePayButton] processPayment', this.context);
return new Promise(async (resolve, reject) => {
try {
console.log('ppcpConfig:', this.ppcpConfig);
let id = await this.contextHandler.createOrder();
console.log('PayPal Order ID:', id);
console.log('paypal.Googlepay().confirmOrder : paymentData', {
orderId: id,
paymentMethodData: paymentData.paymentMethodData
});
console.log('[GooglePayButton] processPayment: createOrder', id, this.context);
const confirmOrderResponse = await paypal.Googlepay().confirmOrder({
orderId: id,
paymentMethodData: paymentData.paymentMethodData
});
console.log('paypal.Googlepay().confirmOrder : confirmOrderResponse', confirmOrderResponse);
/** Capture the Order on your Server */
console.log('[GooglePayButton] processPayment: confirmOrder', confirmOrderResponse, this.context);
/** Capture the Order on the Server */
if (confirmOrderResponse.status === "APPROVED") {
console.log('onApprove', this.ppcpConfig);
let approveFailed = false;
await this.contextHandler.approveOrderForContinue({
@ -177,41 +152,38 @@ class GooglepayButton {
})
});
console.log('approveFailed', approveFailed);
if (approveFailed) {
resolve({
transactionState: 'ERROR',
error: {
intent: 'PAYMENT_AUTHORIZATION',
message: 'FAILED TO APPROVE',
}
})
if (!approveFailed) {
resolve(this.processPaymentResponse('SUCCESS'));
} else {
resolve(this.processPaymentResponse('ERROR', 'PAYMENT_AUTHORIZATION', 'FAILED TO APPROVE'));
}
resolve({transactionState: 'SUCCESS'});
} else {
resolve({
transactionState: 'ERROR',
error: {
intent: 'PAYMENT_AUTHORIZATION',
message: 'TRANSACTION FAILED',
}
})
resolve(this.processPaymentResponse('ERROR', 'PAYMENT_AUTHORIZATION', 'TRANSACTION FAILED'));
}
} catch(err) {
resolve({
transactionState: 'ERROR',
error: {
intent: 'PAYMENT_AUTHORIZATION',
message: err.message,
}
})
resolve(this.processPaymentResponse('ERROR', 'PAYMENT_AUTHORIZATION', err.message));
}
});
}
processPaymentResponse(state, intent = null, message = null) {
let response = {
transactionState: state,
}
if (intent || message) {
response.error = {
intent: intent,
message: message,
}
}
console.log('[GooglePayButton] processPaymentResponse', response, this.context);
return response;
}
}
export default GooglepayButton;

View file

@ -29,8 +29,6 @@ const GooglePayComponent = () => {
const bootstrap = function () {
const manager = new GooglepayManager(buttonConfig, ppcpConfig);
manager.init();
};
useEffect(() => {