mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-04 08:47:23 +08:00
Run eslint autofix
This commit is contained in:
parent
36a13f6500
commit
11105d913b
141 changed files with 14160 additions and 11825 deletions
|
@ -1,55 +1,62 @@
|
|||
import MessagesBootstrap from "../../../../ppcp-button/resources/js/modules/ContextBootstrap/MessagesBootstap";
|
||||
import {debounce} from "../Helper/debounce";
|
||||
import MessagesBootstrap from '../../../../ppcp-button/resources/js/modules/ContextBootstrap/MessagesBootstap';
|
||||
import { debounce } from '../Helper/debounce';
|
||||
|
||||
class BlockCheckoutMessagesBootstrap {
|
||||
constructor(scriptData) {
|
||||
this.messagesBootstrap = new MessagesBootstrap(scriptData, null);
|
||||
this.lastCartTotal = null;
|
||||
}
|
||||
constructor( scriptData ) {
|
||||
this.messagesBootstrap = new MessagesBootstrap( scriptData, null );
|
||||
this.lastCartTotal = null;
|
||||
}
|
||||
|
||||
init() {
|
||||
this.messagesBootstrap.init();
|
||||
init() {
|
||||
this.messagesBootstrap.init();
|
||||
|
||||
this._updateCartTotal();
|
||||
this._updateCartTotal();
|
||||
|
||||
if (wp.data?.subscribe) {
|
||||
wp.data.subscribe(debounce(() => {
|
||||
this._updateCartTotal();
|
||||
}, 300));
|
||||
}
|
||||
}
|
||||
if ( wp.data?.subscribe ) {
|
||||
wp.data.subscribe(
|
||||
debounce( () => {
|
||||
this._updateCartTotal();
|
||||
}, 300 )
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
_getCartTotal() {
|
||||
if (!wp.data.select) {
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
_getCartTotal() {
|
||||
if ( ! wp.data.select ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const cart = wp.data.select('wc/store/cart')
|
||||
if (!cart) {
|
||||
return null;
|
||||
}
|
||||
const cart = wp.data.select( 'wc/store/cart' );
|
||||
if ( ! cart ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const totals = cart.getCartTotals();
|
||||
return parseInt(totals.total_price, 10) / 10 ** totals.currency_minor_unit;
|
||||
}
|
||||
const totals = cart.getCartTotals();
|
||||
return (
|
||||
parseInt( totals.total_price, 10 ) /
|
||||
10 ** totals.currency_minor_unit
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
_updateCartTotal() {
|
||||
const currentTotal = this._getCartTotal();
|
||||
if (currentTotal === null) {
|
||||
return;
|
||||
}
|
||||
/**
|
||||
* @private
|
||||
*/
|
||||
_updateCartTotal() {
|
||||
const currentTotal = this._getCartTotal();
|
||||
if ( currentTotal === null ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (currentTotal !== this.lastCartTotal) {
|
||||
this.lastCartTotal = currentTotal;
|
||||
jQuery(document.body).trigger('ppcp_block_cart_total_updated', [currentTotal]);
|
||||
}
|
||||
}
|
||||
if ( currentTotal !== this.lastCartTotal ) {
|
||||
this.lastCartTotal = currentTotal;
|
||||
jQuery( document.body ).trigger( 'ppcp_block_cart_total_updated', [
|
||||
currentTotal,
|
||||
] );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default BlockCheckoutMessagesBootstrap;
|
||||
|
|
|
@ -1,87 +1,95 @@
|
|||
import {useEffect, useState} from '@wordpress/element';
|
||||
import { useEffect, useState } from '@wordpress/element';
|
||||
|
||||
import {
|
||||
PayPalScriptProvider,
|
||||
PayPalCardFieldsProvider,
|
||||
PayPalCardFieldsForm,
|
||||
} from "@paypal/react-paypal-js";
|
||||
PayPalScriptProvider,
|
||||
PayPalCardFieldsProvider,
|
||||
PayPalCardFieldsForm,
|
||||
} from '@paypal/react-paypal-js';
|
||||
|
||||
import {CheckoutHandler} from "./checkout-handler";
|
||||
import {createOrder, onApprove} from "../card-fields-config";
|
||||
import {cartHasSubscriptionProducts} from "../Helper/Subscription";
|
||||
import { CheckoutHandler } from './checkout-handler';
|
||||
import { createOrder, onApprove } from '../card-fields-config';
|
||||
import { cartHasSubscriptionProducts } from '../Helper/Subscription';
|
||||
|
||||
export function CardFields({config, eventRegistration, emitResponse, components}) {
|
||||
const {onPaymentSetup} = eventRegistration;
|
||||
const {responseTypes} = emitResponse;
|
||||
const { PaymentMethodIcons } = components;
|
||||
export function CardFields( {
|
||||
config,
|
||||
eventRegistration,
|
||||
emitResponse,
|
||||
components,
|
||||
} ) {
|
||||
const { onPaymentSetup } = eventRegistration;
|
||||
const { responseTypes } = emitResponse;
|
||||
const { PaymentMethodIcons } = components;
|
||||
|
||||
const [cardFieldsForm, setCardFieldsForm] = useState();
|
||||
const getCardFieldsForm = (cardFieldsForm) => {
|
||||
setCardFieldsForm(cardFieldsForm)
|
||||
}
|
||||
const [ cardFieldsForm, setCardFieldsForm ] = useState();
|
||||
const getCardFieldsForm = ( cardFieldsForm ) => {
|
||||
setCardFieldsForm( cardFieldsForm );
|
||||
};
|
||||
|
||||
const getSavePayment = (savePayment) => {
|
||||
localStorage.setItem('ppcp-save-card-payment', savePayment);
|
||||
}
|
||||
const getSavePayment = ( savePayment ) => {
|
||||
localStorage.setItem( 'ppcp-save-card-payment', savePayment );
|
||||
};
|
||||
|
||||
const hasSubscriptionProducts = cartHasSubscriptionProducts(config.scriptData);
|
||||
useEffect(() => {
|
||||
localStorage.removeItem('ppcp-save-card-payment');
|
||||
const hasSubscriptionProducts = cartHasSubscriptionProducts(
|
||||
config.scriptData
|
||||
);
|
||||
useEffect( () => {
|
||||
localStorage.removeItem( 'ppcp-save-card-payment' );
|
||||
|
||||
if(hasSubscriptionProducts) {
|
||||
localStorage.setItem('ppcp-save-card-payment', 'true');
|
||||
}
|
||||
if ( hasSubscriptionProducts ) {
|
||||
localStorage.setItem( 'ppcp-save-card-payment', 'true' );
|
||||
}
|
||||
}, [ hasSubscriptionProducts ] );
|
||||
|
||||
}, [hasSubscriptionProducts])
|
||||
useEffect(
|
||||
() =>
|
||||
onPaymentSetup( () => {
|
||||
async function handlePaymentProcessing() {
|
||||
await cardFieldsForm.submit().catch( ( error ) => {
|
||||
return {
|
||||
type: responseTypes.ERROR,
|
||||
};
|
||||
} );
|
||||
|
||||
useEffect(
|
||||
() =>
|
||||
onPaymentSetup(() => {
|
||||
async function handlePaymentProcessing() {
|
||||
await cardFieldsForm.submit()
|
||||
.catch((error) => {
|
||||
return {
|
||||
type: responseTypes.ERROR,
|
||||
}
|
||||
});
|
||||
return {
|
||||
type: responseTypes.SUCCESS,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
type: responseTypes.SUCCESS,
|
||||
}
|
||||
}
|
||||
return handlePaymentProcessing();
|
||||
} ),
|
||||
[ onPaymentSetup, cardFieldsForm ]
|
||||
);
|
||||
|
||||
return handlePaymentProcessing();
|
||||
}),
|
||||
[onPaymentSetup, cardFieldsForm]
|
||||
);
|
||||
|
||||
return (
|
||||
<>
|
||||
<PayPalScriptProvider
|
||||
options={{
|
||||
clientId: config.scriptData.client_id,
|
||||
components: "card-fields",
|
||||
dataNamespace: 'ppcp-block-card-fields',
|
||||
}}
|
||||
>
|
||||
<PayPalCardFieldsProvider
|
||||
createOrder={createOrder}
|
||||
onApprove={onApprove}
|
||||
onError={(err) => {
|
||||
console.error(err);
|
||||
}}
|
||||
>
|
||||
<PayPalCardFieldsForm/>
|
||||
<PaymentMethodIcons icons={config.card_icons} align="left" />
|
||||
<CheckoutHandler
|
||||
getCardFieldsForm={getCardFieldsForm}
|
||||
getSavePayment={getSavePayment}
|
||||
hasSubscriptionProducts={hasSubscriptionProducts}
|
||||
saveCardText={config.save_card_text}
|
||||
is_vaulting_enabled={config.is_vaulting_enabled}
|
||||
/>
|
||||
</PayPalCardFieldsProvider>
|
||||
</PayPalScriptProvider>
|
||||
</>
|
||||
)
|
||||
return (
|
||||
<>
|
||||
<PayPalScriptProvider
|
||||
options={ {
|
||||
clientId: config.scriptData.client_id,
|
||||
components: 'card-fields',
|
||||
dataNamespace: 'ppcp-block-card-fields',
|
||||
} }
|
||||
>
|
||||
<PayPalCardFieldsProvider
|
||||
createOrder={ createOrder }
|
||||
onApprove={ onApprove }
|
||||
onError={ ( err ) => {
|
||||
console.error( err );
|
||||
} }
|
||||
>
|
||||
<PayPalCardFieldsForm />
|
||||
<PaymentMethodIcons
|
||||
icons={ config.card_icons }
|
||||
align="left"
|
||||
/>
|
||||
<CheckoutHandler
|
||||
getCardFieldsForm={ getCardFieldsForm }
|
||||
getSavePayment={ getSavePayment }
|
||||
hasSubscriptionProducts={ hasSubscriptionProducts }
|
||||
saveCardText={ config.save_card_text }
|
||||
is_vaulting_enabled={ config.is_vaulting_enabled }
|
||||
/>
|
||||
</PayPalCardFieldsProvider>
|
||||
</PayPalScriptProvider>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
|
|
@ -1,28 +1,34 @@
|
|||
import {useEffect} from '@wordpress/element';
|
||||
import {usePayPalCardFields} from "@paypal/react-paypal-js";
|
||||
import { useEffect } from '@wordpress/element';
|
||||
import { usePayPalCardFields } from '@paypal/react-paypal-js';
|
||||
|
||||
export const CheckoutHandler = ({getCardFieldsForm, getSavePayment, hasSubscriptionProducts, saveCardText, is_vaulting_enabled}) => {
|
||||
const {cardFieldsForm} = usePayPalCardFields();
|
||||
export const CheckoutHandler = ( {
|
||||
getCardFieldsForm,
|
||||
getSavePayment,
|
||||
hasSubscriptionProducts,
|
||||
saveCardText,
|
||||
is_vaulting_enabled,
|
||||
} ) => {
|
||||
const { cardFieldsForm } = usePayPalCardFields();
|
||||
|
||||
useEffect(() => {
|
||||
getCardFieldsForm(cardFieldsForm)
|
||||
}, []);
|
||||
useEffect( () => {
|
||||
getCardFieldsForm( cardFieldsForm );
|
||||
}, [] );
|
||||
|
||||
if (!is_vaulting_enabled) {
|
||||
return null;
|
||||
}
|
||||
if ( ! is_vaulting_enabled ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<input
|
||||
type="checkbox"
|
||||
id="save"
|
||||
name="save"
|
||||
onChange={(e) => getSavePayment(e.target.checked)}
|
||||
defaultChecked={hasSubscriptionProducts}
|
||||
disabled={hasSubscriptionProducts}
|
||||
/>
|
||||
<label htmlFor="save">{saveCardText}</label>
|
||||
</>
|
||||
)
|
||||
}
|
||||
return (
|
||||
<>
|
||||
<input
|
||||
type="checkbox"
|
||||
id="save"
|
||||
name="save"
|
||||
onChange={ ( e ) => getSavePayment( e.target.checked ) }
|
||||
defaultChecked={ hasSubscriptionProducts }
|
||||
disabled={ hasSubscriptionProducts }
|
||||
/>
|
||||
<label htmlFor="save">{ saveCardText }</label>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
|
|
@ -1,171 +1,179 @@
|
|||
/**
|
||||
* @param {String} fullName
|
||||
* @returns {Array}
|
||||
* @param {string} fullName
|
||||
* @return {Array}
|
||||
*/
|
||||
export const splitFullName = (fullName) => {
|
||||
fullName = fullName.trim()
|
||||
if (!fullName.includes(' ')) {
|
||||
return [fullName, ''];
|
||||
}
|
||||
const parts = fullName.split(' ');
|
||||
const firstName = parts[0];
|
||||
parts.shift();
|
||||
const lastName = parts.join(' ');
|
||||
return [firstName, lastName];
|
||||
}
|
||||
export const splitFullName = ( fullName ) => {
|
||||
fullName = fullName.trim();
|
||||
if ( ! fullName.includes( ' ' ) ) {
|
||||
return [ fullName, '' ];
|
||||
}
|
||||
const parts = fullName.split( ' ' );
|
||||
const firstName = parts[ 0 ];
|
||||
parts.shift();
|
||||
const lastName = parts.join( ' ' );
|
||||
return [ firstName, lastName ];
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {Object} address
|
||||
* @returns {Object}
|
||||
* @return {Object}
|
||||
*/
|
||||
export const paypalAddressToWc = (address) => {
|
||||
let map = {
|
||||
country_code: 'country',
|
||||
address_line_1: 'address_1',
|
||||
address_line_2: 'address_2',
|
||||
admin_area_1: 'state',
|
||||
admin_area_2: 'city',
|
||||
postal_code: 'postcode',
|
||||
};
|
||||
if (address.city) { // address not from API, such as onShippingChange
|
||||
map = {
|
||||
country_code: 'country',
|
||||
state: 'state',
|
||||
city: 'city',
|
||||
postal_code: 'postcode',
|
||||
};
|
||||
}
|
||||
const result = {};
|
||||
Object.entries(map).forEach(([paypalKey, wcKey]) => {
|
||||
if (address[paypalKey]) {
|
||||
result[wcKey] = address[paypalKey];
|
||||
}
|
||||
});
|
||||
export const paypalAddressToWc = ( address ) => {
|
||||
let map = {
|
||||
country_code: 'country',
|
||||
address_line_1: 'address_1',
|
||||
address_line_2: 'address_2',
|
||||
admin_area_1: 'state',
|
||||
admin_area_2: 'city',
|
||||
postal_code: 'postcode',
|
||||
};
|
||||
if ( address.city ) {
|
||||
// address not from API, such as onShippingChange
|
||||
map = {
|
||||
country_code: 'country',
|
||||
state: 'state',
|
||||
city: 'city',
|
||||
postal_code: 'postcode',
|
||||
};
|
||||
}
|
||||
const result = {};
|
||||
Object.entries( map ).forEach( ( [ paypalKey, wcKey ] ) => {
|
||||
if ( address[ paypalKey ] ) {
|
||||
result[ wcKey ] = address[ paypalKey ];
|
||||
}
|
||||
} );
|
||||
|
||||
const defaultAddress = {
|
||||
first_name: '',
|
||||
last_name: '',
|
||||
company: '',
|
||||
address_1: '',
|
||||
address_2: '',
|
||||
city: '',
|
||||
state: '',
|
||||
postcode: '',
|
||||
country: '',
|
||||
phone: '',
|
||||
};
|
||||
const defaultAddress = {
|
||||
first_name: '',
|
||||
last_name: '',
|
||||
company: '',
|
||||
address_1: '',
|
||||
address_2: '',
|
||||
city: '',
|
||||
state: '',
|
||||
postcode: '',
|
||||
country: '',
|
||||
phone: '',
|
||||
};
|
||||
|
||||
return {...defaultAddress, ...result};
|
||||
}
|
||||
return { ...defaultAddress, ...result };
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {Object} shipping
|
||||
* @returns {Object}
|
||||
* @return {Object}
|
||||
*/
|
||||
export const paypalShippingToWc = (shipping) => {
|
||||
const [firstName, lastName] = (shipping.name ? splitFullName(shipping.name.full_name) : ['','']);
|
||||
return {
|
||||
...paypalAddressToWc(shipping.address),
|
||||
first_name: firstName,
|
||||
last_name: lastName,
|
||||
}
|
||||
}
|
||||
export const paypalShippingToWc = ( shipping ) => {
|
||||
const [ firstName, lastName ] = shipping.name
|
||||
? splitFullName( shipping.name.full_name )
|
||||
: [ '', '' ];
|
||||
return {
|
||||
...paypalAddressToWc( shipping.address ),
|
||||
first_name: firstName,
|
||||
last_name: lastName,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {Object} payer
|
||||
* @returns {Object}
|
||||
* @return {Object}
|
||||
*/
|
||||
export const paypalPayerToWc = (payer) => {
|
||||
const firstName = payer?.name?.given_name ?? '';
|
||||
const lastName = payer?.name?.surname ?? '';
|
||||
const address = payer.address ? paypalAddressToWc(payer.address) : {};
|
||||
return {
|
||||
...address,
|
||||
first_name: firstName,
|
||||
last_name: lastName,
|
||||
email: payer.email_address,
|
||||
}
|
||||
}
|
||||
export const paypalPayerToWc = ( payer ) => {
|
||||
const firstName = payer?.name?.given_name ?? '';
|
||||
const lastName = payer?.name?.surname ?? '';
|
||||
const address = payer.address ? paypalAddressToWc( payer.address ) : {};
|
||||
return {
|
||||
...address,
|
||||
first_name: firstName,
|
||||
last_name: lastName,
|
||||
email: payer.email_address,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {Object} subscriber
|
||||
* @returns {Object}
|
||||
* @return {Object}
|
||||
*/
|
||||
export const paypalSubscriberToWc = (subscriber) => {
|
||||
const firstName = subscriber?.name?.given_name ?? '';
|
||||
const lastName = subscriber?.name?.surname ?? '';
|
||||
const address = subscriber.address ? paypalAddressToWc(subscriber.shipping_address.address) : {};
|
||||
return {
|
||||
...address,
|
||||
first_name: firstName,
|
||||
last_name: lastName,
|
||||
email: subscriber.email_address,
|
||||
}
|
||||
}
|
||||
export const paypalSubscriberToWc = ( subscriber ) => {
|
||||
const firstName = subscriber?.name?.given_name ?? '';
|
||||
const lastName = subscriber?.name?.surname ?? '';
|
||||
const address = subscriber.address
|
||||
? paypalAddressToWc( subscriber.shipping_address.address )
|
||||
: {};
|
||||
return {
|
||||
...address,
|
||||
first_name: firstName,
|
||||
last_name: lastName,
|
||||
email: subscriber.email_address,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {Object} order
|
||||
* @returns {Object}
|
||||
* @return {Object}
|
||||
*/
|
||||
export const paypalOrderToWcShippingAddress = (order) => {
|
||||
const shipping = order.purchase_units[0].shipping;
|
||||
if (!shipping) {
|
||||
return {};
|
||||
}
|
||||
export const paypalOrderToWcShippingAddress = ( order ) => {
|
||||
const shipping = order.purchase_units[ 0 ].shipping;
|
||||
if ( ! shipping ) {
|
||||
return {};
|
||||
}
|
||||
|
||||
const res = paypalShippingToWc(shipping);
|
||||
const res = paypalShippingToWc( shipping );
|
||||
|
||||
// use the name from billing if the same, to avoid possible mistakes when splitting full_name
|
||||
if (order.payer) {
|
||||
const billingAddress = paypalPayerToWc(order.payer);
|
||||
if (`${res.first_name} ${res.last_name}` === `${billingAddress.first_name} ${billingAddress.last_name}`) {
|
||||
res.first_name = billingAddress.first_name;
|
||||
res.last_name = billingAddress.last_name;
|
||||
}
|
||||
}
|
||||
// use the name from billing if the same, to avoid possible mistakes when splitting full_name
|
||||
if ( order.payer ) {
|
||||
const billingAddress = paypalPayerToWc( order.payer );
|
||||
if (
|
||||
`${ res.first_name } ${ res.last_name }` ===
|
||||
`${ billingAddress.first_name } ${ billingAddress.last_name }`
|
||||
) {
|
||||
res.first_name = billingAddress.first_name;
|
||||
res.last_name = billingAddress.last_name;
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
return res;
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param order
|
||||
* @returns {{shippingAddress: Object, billingAddress: Object}}
|
||||
* @param order
|
||||
* @return {{shippingAddress: Object, billingAddress: Object}}
|
||||
*/
|
||||
export const paypalOrderToWcAddresses = (order) => {
|
||||
const shippingAddress = paypalOrderToWcShippingAddress(order);
|
||||
let billingAddress = shippingAddress;
|
||||
if (order.payer) {
|
||||
billingAddress = paypalPayerToWc(order.payer);
|
||||
// no billing address, such as if billing address retrieval is not allowed in the merchant account
|
||||
if (!billingAddress.address_line_1) {
|
||||
// use only non empty values from payer address, otherwise it will override shipping address
|
||||
let payerAddress = Object.fromEntries(
|
||||
Object.entries(billingAddress).filter(
|
||||
([key, value]) => value !== '' && key !== 'country'
|
||||
)
|
||||
);
|
||||
export const paypalOrderToWcAddresses = ( order ) => {
|
||||
const shippingAddress = paypalOrderToWcShippingAddress( order );
|
||||
let billingAddress = shippingAddress;
|
||||
if ( order.payer ) {
|
||||
billingAddress = paypalPayerToWc( order.payer );
|
||||
// no billing address, such as if billing address retrieval is not allowed in the merchant account
|
||||
if ( ! billingAddress.address_line_1 ) {
|
||||
// use only non empty values from payer address, otherwise it will override shipping address
|
||||
const payerAddress = Object.fromEntries(
|
||||
Object.entries( billingAddress ).filter(
|
||||
( [ key, value ] ) => value !== '' && key !== 'country'
|
||||
)
|
||||
);
|
||||
|
||||
billingAddress = {
|
||||
...shippingAddress,
|
||||
...payerAddress
|
||||
};
|
||||
}
|
||||
}
|
||||
billingAddress = {
|
||||
...shippingAddress,
|
||||
...payerAddress,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return {billingAddress, shippingAddress};
|
||||
}
|
||||
return { billingAddress, shippingAddress };
|
||||
};
|
||||
|
||||
/**
|
||||
*
|
||||
* @param subscription
|
||||
* @returns {{shippingAddress: Object, billingAddress: Object}}
|
||||
* @param subscription
|
||||
* @return {{shippingAddress: Object, billingAddress: Object}}
|
||||
*/
|
||||
export const paypalSubscriptionToWcAddresses = (subscription) => {
|
||||
const shippingAddress = paypalSubscriberToWc(subscription.subscriber);
|
||||
let billingAddress = shippingAddress;
|
||||
return {billingAddress, shippingAddress};
|
||||
}
|
||||
export const paypalSubscriptionToWcAddresses = ( subscription ) => {
|
||||
const shippingAddress = paypalSubscriberToWc( subscription.subscriber );
|
||||
const billingAddress = shippingAddress;
|
||||
return { billingAddress, shippingAddress };
|
||||
};
|
||||
|
||||
/**
|
||||
* Merges two WC addresses.
|
||||
|
@ -173,22 +181,28 @@ export const paypalSubscriptionToWcAddresses = (subscription) => {
|
|||
*
|
||||
* @param {Object} address1
|
||||
* @param {Object} address2
|
||||
* @returns {any}
|
||||
* @return {any}
|
||||
*/
|
||||
export const mergeWcAddress = (address1, address2) => {
|
||||
if ('billingAddress' in address1) {
|
||||
return {
|
||||
billingAddress: mergeWcAddress(address1.billingAddress, address2.billingAddress),
|
||||
shippingAddress: mergeWcAddress(address1.shippingAddress, address2.shippingAddress),
|
||||
}
|
||||
}
|
||||
export const mergeWcAddress = ( address1, address2 ) => {
|
||||
if ( 'billingAddress' in address1 ) {
|
||||
return {
|
||||
billingAddress: mergeWcAddress(
|
||||
address1.billingAddress,
|
||||
address2.billingAddress
|
||||
),
|
||||
shippingAddress: mergeWcAddress(
|
||||
address1.shippingAddress,
|
||||
address2.shippingAddress
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
let address2WithoutEmpty = {...address2};
|
||||
Object.keys(address2).forEach(key => {
|
||||
if (address2[key] === '') {
|
||||
delete address2WithoutEmpty[key];
|
||||
}
|
||||
});
|
||||
const address2WithoutEmpty = { ...address2 };
|
||||
Object.keys( address2 ).forEach( ( key ) => {
|
||||
if ( address2[ key ] === '' ) {
|
||||
delete address2WithoutEmpty[ key ];
|
||||
}
|
||||
} );
|
||||
|
||||
return {...address1, ...address2WithoutEmpty};
|
||||
}
|
||||
return { ...address1, ...address2WithoutEmpty };
|
||||
};
|
||||
|
|
|
@ -1,22 +1,24 @@
|
|||
/**
|
||||
* @param str
|
||||
* @returns {string}
|
||||
* @param str
|
||||
* @return {string}
|
||||
*/
|
||||
export const toSnakeCase = (str) => {
|
||||
return str.replace(/[\w]([A-Z])/g, function(m) {
|
||||
return m[0] + "_" + m[1];
|
||||
}).toLowerCase();
|
||||
}
|
||||
export const toSnakeCase = ( str ) => {
|
||||
return str
|
||||
.replace( /[\w]([A-Z])/g, function ( m ) {
|
||||
return m[ 0 ] + '_' + m[ 1 ];
|
||||
} )
|
||||
.toLowerCase();
|
||||
};
|
||||
|
||||
/**
|
||||
* @param obj
|
||||
* @returns {{}}
|
||||
* @param obj
|
||||
* @return {{}}
|
||||
*/
|
||||
export const convertKeysToSnakeCase = (obj) => {
|
||||
const newObj = {};
|
||||
Object.keys(obj).forEach((key) => {
|
||||
const newKey = toSnakeCase(key);
|
||||
newObj[newKey] = obj[key];
|
||||
});
|
||||
return newObj;
|
||||
}
|
||||
export const convertKeysToSnakeCase = ( obj ) => {
|
||||
const newObj = {};
|
||||
Object.keys( obj ).forEach( ( key ) => {
|
||||
const newKey = toSnakeCase( key );
|
||||
newObj[ newKey ] = obj[ key ];
|
||||
} );
|
||||
return newObj;
|
||||
};
|
||||
|
|
|
@ -1,16 +1,18 @@
|
|||
/**
|
||||
* @param {Object} scriptData
|
||||
* @returns {Boolean}
|
||||
* @return {boolean}
|
||||
*/
|
||||
export const isPayPalSubscription = (scriptData) => {
|
||||
return scriptData.data_client_id.has_subscriptions
|
||||
&& scriptData.data_client_id.paypal_subscriptions_enabled;
|
||||
}
|
||||
export const isPayPalSubscription = ( scriptData ) => {
|
||||
return (
|
||||
scriptData.data_client_id.has_subscriptions &&
|
||||
scriptData.data_client_id.paypal_subscriptions_enabled
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {Object} scriptData
|
||||
* @returns {Boolean}
|
||||
* @return {boolean}
|
||||
*/
|
||||
export const cartHasSubscriptionProducts = (scriptData) => {
|
||||
return !! scriptData?.locations_with_subscription_product?.cart;
|
||||
}
|
||||
export const cartHasSubscriptionProducts = ( scriptData ) => {
|
||||
return !! scriptData?.locations_with_subscription_product?.cart;
|
||||
};
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
export const debounce = (callback, delayMs) => {
|
||||
let timeoutId = null;
|
||||
return (...args) => {
|
||||
window.clearTimeout(timeoutId);
|
||||
timeoutId = window.setTimeout(() => {
|
||||
callback.apply(null, args);
|
||||
}, delayMs);
|
||||
};
|
||||
export const debounce = ( callback, delayMs ) => {
|
||||
let timeoutId = null;
|
||||
return ( ...args ) => {
|
||||
window.clearTimeout( timeoutId );
|
||||
timeoutId = window.setTimeout( () => {
|
||||
callback.apply( null, args );
|
||||
}, delayMs );
|
||||
};
|
||||
};
|
||||
|
|
|
@ -1,17 +1,19 @@
|
|||
import { registerPaymentMethod } from '@woocommerce/blocks-registry';
|
||||
import {CardFields} from "./Components/card-fields";
|
||||
import { CardFields } from './Components/card-fields';
|
||||
|
||||
const config = wc.wcSettings.getSetting('ppcp-credit-card-gateway_data');
|
||||
const config = wc.wcSettings.getSetting( 'ppcp-credit-card-gateway_data' );
|
||||
|
||||
registerPaymentMethod({
|
||||
name: config.id,
|
||||
label: <div dangerouslySetInnerHTML={{__html: config.title}}/>,
|
||||
content: <CardFields config={config}/>,
|
||||
edit: <div></div>,
|
||||
ariaLabel: config.title,
|
||||
canMakePayment: () => {return true},
|
||||
supports: {
|
||||
showSavedCards: true,
|
||||
features: config.supports
|
||||
}
|
||||
})
|
||||
registerPaymentMethod( {
|
||||
name: config.id,
|
||||
label: <div dangerouslySetInnerHTML={ { __html: config.title } } />,
|
||||
content: <CardFields config={ config } />,
|
||||
edit: <div></div>,
|
||||
ariaLabel: config.title,
|
||||
canMakePayment: () => {
|
||||
return true;
|
||||
},
|
||||
supports: {
|
||||
showSavedCards: true,
|
||||
features: config.supports,
|
||||
},
|
||||
} );
|
||||
|
|
|
@ -1,45 +1,46 @@
|
|||
export async function createOrder() {
|
||||
const config = wc.wcSettings.getSetting('ppcp-credit-card-gateway_data');
|
||||
const config = wc.wcSettings.getSetting( 'ppcp-credit-card-gateway_data' );
|
||||
|
||||
return fetch(config.scriptData.ajax.create_order.endpoint, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
nonce: config.scriptData.ajax.create_order.nonce,
|
||||
context: config.scriptData.context,
|
||||
payment_method: 'ppcp-credit-card-gateway',
|
||||
save_payment_method: localStorage.getItem('ppcp-save-card-payment') === 'true',
|
||||
}),
|
||||
})
|
||||
.then((response) => response.json())
|
||||
.then((order) => {
|
||||
return order.data.id;
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
});
|
||||
return fetch( config.scriptData.ajax.create_order.endpoint, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify( {
|
||||
nonce: config.scriptData.ajax.create_order.nonce,
|
||||
context: config.scriptData.context,
|
||||
payment_method: 'ppcp-credit-card-gateway',
|
||||
save_payment_method:
|
||||
localStorage.getItem( 'ppcp-save-card-payment' ) === 'true',
|
||||
} ),
|
||||
} )
|
||||
.then( ( response ) => response.json() )
|
||||
.then( ( order ) => {
|
||||
return order.data.id;
|
||||
} )
|
||||
.catch( ( err ) => {
|
||||
console.error( err );
|
||||
} );
|
||||
}
|
||||
|
||||
export async function onApprove(data) {
|
||||
const config = wc.wcSettings.getSetting('ppcp-credit-card-gateway_data');
|
||||
export async function onApprove( data ) {
|
||||
const config = wc.wcSettings.getSetting( 'ppcp-credit-card-gateway_data' );
|
||||
|
||||
return fetch(config.scriptData.ajax.approve_order.endpoint, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
order_id: data.orderID,
|
||||
nonce: config.scriptData.ajax.approve_order.nonce,
|
||||
}),
|
||||
})
|
||||
.then((response) => response.json())
|
||||
.then((data) => {
|
||||
localStorage.removeItem('ppcp-save-card-payment');
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(err);
|
||||
});
|
||||
return fetch( config.scriptData.ajax.approve_order.endpoint, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify( {
|
||||
order_id: data.orderID,
|
||||
nonce: config.scriptData.ajax.approve_order.nonce,
|
||||
} ),
|
||||
} )
|
||||
.then( ( response ) => response.json() )
|
||||
.then( ( data ) => {
|
||||
localStorage.removeItem( 'ppcp-save-card-payment' );
|
||||
} )
|
||||
.catch( ( err ) => {
|
||||
console.error( err );
|
||||
} );
|
||||
}
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -1,34 +1,34 @@
|
|||
import {render, screen} from '@testing-library/react';
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
import '@testing-library/jest-dom';
|
||||
import {CheckoutHandler} from "../Components/checkout-handler";
|
||||
import { CheckoutHandler } from '../Components/checkout-handler';
|
||||
|
||||
test('checkbox label displays the given text', async () => {
|
||||
render(
|
||||
<CheckoutHandler
|
||||
getCardFieldsForm={() => {}}
|
||||
saveCardText="Foo"
|
||||
is_vaulting_enabled={true}
|
||||
/>
|
||||
);
|
||||
test( 'checkbox label displays the given text', async () => {
|
||||
render(
|
||||
<CheckoutHandler
|
||||
getCardFieldsForm={ () => {} }
|
||||
saveCardText="Foo"
|
||||
is_vaulting_enabled={ true }
|
||||
/>
|
||||
);
|
||||
|
||||
await expect(screen.getByLabelText('Foo')).toBeInTheDocument();
|
||||
});
|
||||
await expect( screen.getByLabelText( 'Foo' ) ).toBeInTheDocument();
|
||||
} );
|
||||
|
||||
test('click checkbox calls function passing checked value', async () => {
|
||||
const getSavePayment = jest.fn();
|
||||
test( 'click checkbox calls function passing checked value', async () => {
|
||||
const getSavePayment = jest.fn();
|
||||
|
||||
render(
|
||||
<CheckoutHandler
|
||||
getSavePayment={getSavePayment}
|
||||
getCardFieldsForm={() => {}}
|
||||
saveCardText="Foo"
|
||||
is_vaulting_enabled={true}
|
||||
/>
|
||||
);
|
||||
render(
|
||||
<CheckoutHandler
|
||||
getSavePayment={ getSavePayment }
|
||||
getCardFieldsForm={ () => {} }
|
||||
saveCardText="Foo"
|
||||
is_vaulting_enabled={ true }
|
||||
/>
|
||||
);
|
||||
|
||||
await userEvent.click(screen.getByLabelText('Foo'));
|
||||
await userEvent.click( screen.getByLabelText( 'Foo' ) );
|
||||
|
||||
await expect(getSavePayment.mock.calls).toHaveLength(1);
|
||||
await expect(getSavePayment.mock.calls[0][0]).toBe(true);
|
||||
});
|
||||
await expect( getSavePayment.mock.calls ).toHaveLength( 1 );
|
||||
await expect( getSavePayment.mock.calls[ 0 ][ 0 ] ).toBe( true );
|
||||
} );
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue