Connect enable payment method to data store

This commit is contained in:
Emili Castells Guasch 2025-01-20 16:04:35 +01:00
parent 58777d6f91
commit 2bea225d9b
6 changed files with 30 additions and 256 deletions

View file

@ -1,25 +1,25 @@
import { useState, useCallback } from '@wordpress/element';
import SettingsBlock from './SettingsBlock';
import PaymentMethodItemBlock from './PaymentMethodItemBlock';
import { usePaymentMethods } from '../../../data/payment/hooks';
const PaymentMethodsBlock = ( {
paymentMethods,
className = '',
onTriggerModal,
} ) => {
const [ selectedMethods, setSelectedMethods ] = useState( {} );
const handleSelect = useCallback( ( methodId, isSelected ) => {
setSelectedMethods( ( prev ) => ( {
...prev,
[ methodId ]: isSelected,
} ) );
}, [] );
const { setPersistent } = usePaymentMethods();
if ( ! paymentMethods?.length ) {
return null;
}
const handleSelect = ( paymentMethod, isSelected ) => {
setPersistent( paymentMethod.id, {
...paymentMethod,
enabled: isSelected,
} );
};
return (
<SettingsBlock
className={ `ppcp-r-settings-block__payment-methods ${ className }` }
@ -28,11 +28,9 @@ const PaymentMethodsBlock = ( {
<PaymentMethodItemBlock
key={ paymentMethod.id }
{ ...paymentMethod }
isSelected={ Boolean(
selectedMethods[ paymentMethod.id ]
) }
isSelected={ paymentMethod.enabled }
onSelect={ ( checked ) =>
handleSelect( paymentMethod.id, checked )
handleSelect( paymentMethod, checked )
}
onTriggerModal={ () =>
onTriggerModal?.( paymentMethod.id )

View file

@ -47,18 +47,6 @@ export const setIsReady = ( isReady ) => ( {
payload: { isReady },
} );
/**
* Persistent. Sets a sample value.
* TODO: Replace with a real action/property.
*
* @param {string} value
* @return {Action} The action.
*/
export const setSampleValue = ( value ) => ( {
type: ACTION_TYPES.SET_PERSISTENT,
payload: { sampleValue: value },
} );
/**
* Side effect. Triggers the persistence of store data to the server.
*
@ -69,3 +57,15 @@ export const persist = function* () {
yield { type: ACTION_TYPES.DO_PERSIST_DATA, data };
};
/**
* Generic persistent-data updater.
*
* @param {string} prop Name of the property to update.
* @param {any} value The new value of the property.
* @return {Action} The action.
*/
export const setPersistent = ( prop, value ) => ( {
type: ACTION_TYPES.SET_PERSISTENT,
payload: { [ prop ]: value },
} );

View file

@ -24,12 +24,7 @@ const usePersistent = ( key ) =>
);
const useHooks = () => {
const {
persist,
// TODO: Replace with real property.
setSampleValue,
} = useDispatch( STORE_NAME );
const { persist, setPersistent } = useDispatch( STORE_NAME );
// Read-only flags and derived state.
// Nothing here yet.
@ -37,9 +32,6 @@ const useHooks = () => {
// Transient accessors.
const isReady = useTransient( 'isReady' );
// Persistent accessors.
const sampleValue = usePersistent( 'sampleValue' );
// PayPal checkout.
const paypal = usePersistent( 'ppcp-gateway' );
const venmo = usePersistent( 'venmo' );
@ -67,8 +59,7 @@ const useHooks = () => {
return {
persist,
isReady,
sampleValue,
setSampleValue,
setPersistent,
paypal,
venmo,
payLater,
@ -95,13 +86,11 @@ export const useState = () => {
return { persist, isReady };
};
// TODO: Replace with real hook.
export const useSampleValue = () => {
const { sampleValue, setSampleValue } = useHooks();
export const usePaymentMethods = () => {
const { setPersistent } = useHooks();
return {
sampleValue,
setSampleValue,
setPersistent,
};
};

View file

@ -66,9 +66,6 @@ return array(
$container->get( 'wcgateway.is-send-only-country' )
);
},
'settings.data.payment' => static function ( ContainerInterface $container ) : PaymentSettings {
return new PaymentSettings();
},
'settings.rest.onboarding' => static function ( ContainerInterface $container ) : OnboardingRestEndpoint {
return new OnboardingRestEndpoint( $container->get( 'settings.data.onboarding' ) );
},
@ -76,7 +73,7 @@ return array(
return new CommonRestEndpoint( $container->get( 'settings.data.general' ) );
},
'settings.rest.payment' => static function ( ContainerInterface $container ) : PaymentRestEndpoint {
return new PaymentRestEndpoint( $container->get( 'settings.data.payment' ) );
return new PaymentRestEndpoint();
},
'settings.rest.refresh_feature_status' => static function ( ContainerInterface $container ) : RefreshFeatureStatusEndpoint {
return new RefreshFeatureStatusEndpoint(

View file

@ -1,210 +0,0 @@
<?php
/**
* Payment Methods class
*
* @package WooCommerce\PayPalCommerce\Settings\Data
*/
declare( strict_types = 1 );
namespace WooCommerce\PayPalCommerce\Settings\Data;
use RuntimeException;
/**
* This class serves as a container for managing the payment settings.
*/
class PaymentSettings extends AbstractDataModel {
/**
* Option key where profile details are stored.
*
* @var string
*/
protected const OPTION_KEY = 'woocommerce-ppcp-data-payment';
/**
* Get default values for the model.
*
* @return array
*/
protected function get_defaults() : array {
return array(
'paymentMethodsPayPalCheckout' => array(
array(
'id' => 'paypal',
'title' => __( 'PayPal', 'woocommerce-paypal-payments' ),
'description' => __(
'Our all-in-one checkout solution lets you offer PayPal, Venmo, Pay Later options, and more to help maximize conversion.',
'woocommerce-paypal-payments'
),
'icon' => 'payment-method-paypal',
),
array(
'id' => 'venmo',
'title' => __( 'Venmo', 'woocommerce-paypal-payments' ),
'description' => __(
'Offer Venmo at checkout to millions of active users.',
'woocommerce-paypal-payments'
),
'icon' => 'payment-method-venmo',
),
array(
'id' => 'paypal_credit',
'title' => __( 'Pay Later', 'woocommerce-paypal-payments' ),
'description' => __(
'Get paid in full at checkout while giving your customers the flexibility to pay in installments over time with no late fees.',
'woocommerce-paypal-payments'
),
'icon' => 'payment-method-paypal',
),
array(
'id' => 'credit_and_debit_card_payments',
'title' => __(
'Credit and debit card payments',
'woocommerce-paypal-payments'
),
'description' => __(
"Accept all major credit and debit cards - even if your customer doesn't have a PayPal account.",
'woocommerce-paypal-payments'
),
'icon' => 'payment-method-cards',
),
),
'paymentMethodsOnlineCardPayments' => array(
array(
'id' => 'advanced_credit_and_debit_card_payments',
'title' => __(
'Advanced Credit and Debit Card Payments',
'woocommerce-paypal-payments'
),
'description' => __(
"Present custom credit and debit card fields to your payers so they can pay with credit and debit cards using your site's branding.",
'woocommerce-paypal-payments'
),
'icon' => 'payment-method-advanced-cards',
),
array(
'id' => 'fastlane',
'title' => __( 'Fastlane by PayPal', 'woocommerce-paypal-payments' ),
'description' => __(
"Tap into the scale and trust of PayPal's customer network to recognize shoppers and make guest checkout more seamless than ever.",
'woocommerce-paypal-payments'
),
'icon' => 'payment-method-fastlane',
),
array(
'id' => 'apple_pay',
'title' => __( 'Apple Pay', 'woocommerce-paypal-payments' ),
'description' => __(
'Allow customers to pay via their Apple Pay digital wallet.',
'woocommerce-paypal-payments'
),
'icon' => 'payment-method-apple-pay',
),
array(
'id' => 'google_pay',
'title' => __( 'Google Pay', 'woocommerce-paypal-payments' ),
'description' => __(
'Allow customers to pay via their Google Pay digital wallet.',
'woocommerce-paypal-payments'
),
'icon' => 'payment-method-google-pay',
),
),
'paymentMethodsAlternative' => array(
array(
'id' => 'bancontact',
'title' => __( 'Bancontact', 'woocommerce-paypal-payments' ),
'description' => __(
'Bancontact is the most widely used, accepted and trusted electronic payment method in Belgium. Bancontact makes it possible to pay directly through the online payment systems of all major Belgian banks.',
'woocommerce-paypal-payments'
),
'icon' => 'payment-method-bancontact',
),
array(
'id' => 'ideal',
'title' => __( 'iDEAL', 'woocommerce-paypal-payments' ),
'description' => __(
'iDEAL is a payment method in the Netherlands that allows buyers to select their issuing bank from a list of options.',
'woocommerce-paypal-payments'
),
'icon' => 'payment-method-ideal',
),
array(
'id' => 'eps',
'title' => __( 'eps', 'woocommerce-paypal-payments' ),
'description' => __(
'An online payment method in Austria, enabling Austrian buyers to make secure payments directly through their bank accounts. Transactions are processed in EUR.',
'woocommerce-paypal-payments'
),
'icon' => 'payment-method-eps',
),
array(
'id' => 'blik',
'title' => __( 'BLIK', 'woocommerce-paypal-payments' ),
'description' => __(
'A widely used mobile payment method in Poland, allowing Polish customers to pay directly via their banking apps. Transactions are processed in PLN.',
'woocommerce-paypal-payments'
),
'icon' => 'payment-method-blik',
),
array(
'id' => 'mybank',
'title' => __( 'MyBank', 'woocommerce-paypal-payments' ),
'description' => __(
'A European online banking payment solution primarily used in Italy, enabling customers to make secure bank transfers during checkout. Transactions are processed in EUR.',
'woocommerce-paypal-payments'
),
'icon' => 'payment-method-mybank',
),
array(
'id' => 'przelewy24',
'title' => __( 'Przelewy24', 'woocommerce-paypal-payments' ),
'description' => __(
'A popular online payment gateway in Poland, offering various payment options for Polish customers. Transactions can be processed in PLN or EUR.',
'woocommerce-paypal-payments'
),
'icon' => 'payment-method-przelewy24',
),
array(
'id' => 'trustly',
'title' => __( 'Trustly', 'woocommerce-paypal-payments' ),
'description' => __(
'A European payment method that allows buyers to make payments directly from their bank accounts, suitable for customers across multiple European countries. Supported currencies include EUR, DKK, SEK, GBP, and NOK.',
'woocommerce-paypal-payments'
),
'icon' => 'payment-method-trustly',
),
array(
'id' => 'multibanco',
'title' => __( 'Multibanco', 'woocommerce-paypal-payments' ),
'description' => __(
'An online payment method in Portugal, enabling Portuguese buyers to make secure payments directly through their bank accounts. Transactions are processed in EUR.',
'woocommerce-paypal-payments'
),
'icon' => 'payment-method-multibanco',
),
array(
'id' => 'pui',
'title' => __( 'Pay upon Invoice', 'woocommerce-paypal-payments' ),
'description' => __(
'Pay upon Invoice is an invoice payment method in Germany. It is a local buy now, pay later payment method that allows the buyer to place an order, receive the goods, try them, verify they are in good order, and then pay the invoice within 30 days.',
'woocommerce-paypal-payments'
),
'icon' => 'payment-method-ratepay',
),
array(
'id' => 'oxxo',
'title' => __( 'OXXO', 'woocommerce-paypal-payments' ),
'description' => __(
'OXXO is a Mexican chain of convenience stores. *Get PayPal account permission to use OXXO payment functionality by contacting us at (+52) 8009250304',
'woocommerce-paypal-payments'
),
'icon' => 'payment-method-oxxo',
),
),
);
}
}

View file

@ -52,7 +52,7 @@ class PaymentRestEndpoint extends RestEndpoint {
return array(
// PayPal checkout.
PayPalGateway::ID => array(
'id' => 'paypal',
'id' => 'ppcp-gateway',
'title' => __( 'PayPal', 'woocommerce-paypal-payments' ),
'description' => __(
'Our all-in-one checkout solution lets you offer PayPal, Venmo, Pay Later options, and more to help maximize conversion.',