mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
enable gateways in the account -> order -> pay-now
This commit is contained in:
parent
324ff49496
commit
96fd3d6a2a
10 changed files with 302 additions and 58 deletions
|
@ -2,6 +2,7 @@ import MiniCartBootstap from './modules/ContextBootstrap/MiniCartBootstap';
|
|||
import SingleProductBootstap from './modules/ContextBootstrap/SingleProductBootstap';
|
||||
import CartBootstrap from './modules/ContextBootstrap/CartBootstap';
|
||||
import CheckoutBootstap from './modules/ContextBootstrap/CheckoutBootstap';
|
||||
import PayNowBootstrap from "./modules/ContextBootstrap/PayNowBootstrap";
|
||||
import Renderer from './modules/Renderer/Renderer';
|
||||
import ErrorHandler from './modules/ErrorHandler';
|
||||
import CreditCardRenderer from "./modules/Renderer/CreditCardRenderer";
|
||||
|
@ -52,6 +53,15 @@ const bootstrap = () => {
|
|||
checkoutBootstap.init();
|
||||
}
|
||||
|
||||
if (context === 'pay-now' ) {
|
||||
const payNowBootstrap = new PayNowBootstrap(
|
||||
PayPalCommerceGateway,
|
||||
renderer,
|
||||
messageRenderer
|
||||
);
|
||||
payNowBootstrap.init();
|
||||
}
|
||||
|
||||
if (context !== 'checkout') {
|
||||
messageRenderer.render();
|
||||
}
|
||||
|
|
|
@ -16,7 +16,8 @@ class CheckoutActionHandler {
|
|||
|
||||
|
||||
const errorHandler = this.errorHandler;
|
||||
const formValues = jQuery('form.checkout').serialize();
|
||||
const formSelector = this.config.context === 'checkout' ? 'form.checkout' : 'form#order_review';
|
||||
const formValues = jQuery(formSelector).serialize();
|
||||
|
||||
return fetch(this.config.ajax.create_order.endpoint, {
|
||||
method: 'POST',
|
||||
|
@ -25,6 +26,7 @@ class CheckoutActionHandler {
|
|||
payer,
|
||||
bn_code:bnCode,
|
||||
context:this.config.context,
|
||||
order_id:this.config.order_id,
|
||||
form:formValues
|
||||
})
|
||||
}).then(function (res) {
|
||||
|
@ -38,7 +40,7 @@ class CheckoutActionHandler {
|
|||
input.setAttribute('type', 'hidden');
|
||||
input.setAttribute('name', 'ppcp-resume-order');
|
||||
input.setAttribute('value', data.data.purchase_units[0].custom_id);
|
||||
document.querySelector('form.checkout').append(input);
|
||||
document.querySelector(formSelector).append(input);
|
||||
return data.data.id;
|
||||
});
|
||||
}
|
||||
|
|
|
@ -0,0 +1,80 @@
|
|||
import ErrorHandler from '../ErrorHandler';
|
||||
import CheckoutActionHandler from '../ActionHandler/CheckoutActionHandler';
|
||||
|
||||
class PayNowBootstrap {
|
||||
constructor(gateway, renderer, messages) {
|
||||
this.gateway = gateway;
|
||||
this.renderer = renderer;
|
||||
this.messages = messages
|
||||
}
|
||||
|
||||
init() {
|
||||
|
||||
this.render();
|
||||
|
||||
jQuery(document.body).on('updated_checkout', () => {
|
||||
this.render();
|
||||
});
|
||||
|
||||
jQuery(document.body).
|
||||
on('updated_checkout payment_method_selected', () => {
|
||||
this.switchBetweenPayPalandOrderButton();
|
||||
});
|
||||
this.switchBetweenPayPalandOrderButton();
|
||||
}
|
||||
|
||||
shouldRender() {
|
||||
if (document.querySelector(this.gateway.button.cancel_wrapper)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return document.querySelector(this.gateway.button.wrapper) !== null || document.querySelector(this.gateway.hosted_fields.wrapper) !== null;
|
||||
}
|
||||
|
||||
render() {
|
||||
if (!this.shouldRender()) {
|
||||
return;
|
||||
}
|
||||
if (document.querySelector(this.gateway.hosted_fields.wrapper + '>div')) {
|
||||
document.querySelector(this.gateway.hosted_fields.wrapper + '>div').setAttribute('style', '');
|
||||
}
|
||||
const actionHandler = new CheckoutActionHandler(
|
||||
PayPalCommerceGateway,
|
||||
new ErrorHandler(this.gateway.labels.error.generic),
|
||||
);
|
||||
|
||||
this.renderer.render(
|
||||
this.gateway.button.wrapper,
|
||||
this.gateway.hosted_fields.wrapper,
|
||||
actionHandler.configuration(),
|
||||
);
|
||||
}
|
||||
|
||||
switchBetweenPayPalandOrderButton() {
|
||||
const currentPaymentMethod = jQuery(
|
||||
'input[name="payment_method"]:checked').val();
|
||||
|
||||
if (currentPaymentMethod !== 'ppcp-gateway' && currentPaymentMethod !== 'ppcp-credit-card-gateway') {
|
||||
this.renderer.hideButtons(this.gateway.button.wrapper);
|
||||
this.renderer.hideButtons(this.gateway.messages.wrapper);
|
||||
this.renderer.hideButtons(this.gateway.hosted_fields.wrapper);
|
||||
jQuery('#place_order').show();
|
||||
}
|
||||
else {
|
||||
jQuery('#place_order').hide();
|
||||
if (currentPaymentMethod === 'ppcp-gateway') {
|
||||
this.renderer.showButtons(this.gateway.button.wrapper);
|
||||
this.renderer.showButtons(this.gateway.messages.wrapper);
|
||||
this.messages.render();
|
||||
this.renderer.hideButtons(this.gateway.hosted_fields.wrapper);
|
||||
}
|
||||
if (currentPaymentMethod === 'ppcp-credit-card-gateway') {
|
||||
this.renderer.hideButtons(this.gateway.button.wrapper);
|
||||
this.renderer.hideButtons(this.gateway.messages.wrapper);
|
||||
this.renderer.showButtons(this.gateway.hosted_fields.wrapper);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default PayNowBootstrap;
|
|
@ -12,7 +12,7 @@ const onApprove = (context, errorHandler) => {
|
|||
if (!data.success) {
|
||||
errorHandler.genericError();
|
||||
console.error(data);
|
||||
if (typeof actions.restart !== 'undefined') {
|
||||
if (typeof actions !== 'undefined' && typeof actions.restart !== 'undefined') {
|
||||
return actions.restart();
|
||||
}
|
||||
throw new Error(data.data.message);
|
||||
|
|
|
@ -10,7 +10,10 @@ class CreditCardRenderer {
|
|||
render(wrapper, contextConfig) {
|
||||
|
||||
if (
|
||||
this.defaultConfig.context !== 'checkout'
|
||||
(
|
||||
this.defaultConfig.context !== 'checkout'
|
||||
&& this.defaultConfig.context !== 'pay-now'
|
||||
)
|
||||
|| wrapper === null
|
||||
|| document.querySelector(wrapper) === null
|
||||
) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue