mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 18:16:38 +08:00
Add data store boilerplace and default data
This commit is contained in:
parent
d1a67406ef
commit
47ae8ff670
16 changed files with 723 additions and 6 deletions
|
@ -1,4 +1,8 @@
|
|||
import { OnboardingStoreName, CommonStoreName } from './index';
|
||||
import {
|
||||
OnboardingStoreName,
|
||||
CommonStoreName,
|
||||
PaymentStoreName,
|
||||
} from './index';
|
||||
|
||||
export const addDebugTools = ( context, modules ) => {
|
||||
if ( ! context || ! context?.debug ) {
|
||||
|
@ -33,7 +37,11 @@ export const addDebugTools = ( context, modules ) => {
|
|||
};
|
||||
|
||||
context.resetStore = () => {
|
||||
const stores = [ OnboardingStoreName, CommonStoreName ];
|
||||
const stores = [
|
||||
OnboardingStoreName,
|
||||
CommonStoreName,
|
||||
PaymentStoreName,
|
||||
];
|
||||
|
||||
stores.forEach( ( storeName ) => {
|
||||
const store = wp.data.dispatch( storeName );
|
||||
|
|
|
@ -1,16 +1,20 @@
|
|||
import { addDebugTools } from './debug';
|
||||
import * as Onboarding from './onboarding';
|
||||
import * as Common from './common';
|
||||
import * as Payment from './payment';
|
||||
|
||||
Onboarding.initStore();
|
||||
Common.initStore();
|
||||
Payment.initStore();
|
||||
|
||||
export const OnboardingHooks = Onboarding.hooks;
|
||||
export const CommonHooks = Common.hooks;
|
||||
export const PaymentHooks = Payment.hooks;
|
||||
|
||||
export const OnboardingStoreName = Onboarding.STORE_NAME;
|
||||
export const CommonStoreName = Common.STORE_NAME;
|
||||
export const PaymentStoreName = Payment.STORE_NAME;
|
||||
|
||||
export * from './constants';
|
||||
|
||||
addDebugTools( window.ppcpSettings, [ Onboarding, Common ] );
|
||||
addDebugTools( window.ppcpSettings, [ Onboarding, Common, Payment ] );
|
||||
|
|
45
modules/ppcp-settings/resources/js/data/payment/README.md
Normal file
45
modules/ppcp-settings/resources/js/data/payment/README.md
Normal file
|
@ -0,0 +1,45 @@
|
|||
# Store template
|
||||
|
||||
This template contains all files for a Redux store.
|
||||
|
||||
## New Store: Redux integration
|
||||
|
||||
1. Copy this folder, give it a correct name.
|
||||
2. Check each file for `<UNKNOWN>` placeholders and `TODO` remarks.
|
||||
3. Edit the main store-index file and add the relevant store integration there.
|
||||
4. Check the debug-module, and add relevant debug code.
|
||||
- Register the store in the `reset()` method.
|
||||
|
||||
---
|
||||
|
||||
Main store-index:
|
||||
`modules/ppcp-settings/resources/js/data/index.js`
|
||||
|
||||
Sample store integration:
|
||||
```js
|
||||
import * as YourStore from './yourStore';
|
||||
// ...
|
||||
YourStore.initStore();
|
||||
// ...
|
||||
export const YourStoreHooks = YourStore.hooks;
|
||||
// ...
|
||||
export const YourStoreName = YourStore.STORE_NAME;
|
||||
// ...
|
||||
addDebugTools( window.ppcpSettings, [ ..., YourStoreName ] );
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
### New Store: PHP integration
|
||||
|
||||
1. Create the **REST endpoint** for hydrating and persisting data.
|
||||
- `modules/ppcp-settings/src/Endpoint/YourStoreRestEndpoint.php`
|
||||
- Extend from base class `RestEndpoint`
|
||||
2. Create the **data model** class to manage the DB interaction.
|
||||
- `modules/ppcp-settings/src/Data/YourStoreSettings.php`
|
||||
- Extend from base class `AbstractDataModel`
|
||||
3. Create relevant **DI services** for both files.
|
||||
- `modules/ppcp-settings/services.php`
|
||||
4. Register the REST endpoint in the **service module**.
|
||||
- `modules/ppcp-settings/src/SettingsModule.php`
|
||||
- Find the action `rest_api_init`
|
|
@ -0,0 +1,18 @@
|
|||
/**
|
||||
* Action Types: Define unique identifiers for actions across all store modules.
|
||||
*
|
||||
* @file
|
||||
*/
|
||||
|
||||
export default {
|
||||
// Transient data.
|
||||
SET_TRANSIENT: 'PAYMENT:SET_TRANSIENT',
|
||||
|
||||
// Persistent data.
|
||||
SET_PERSISTENT: 'PAYMENT:SET_PERSISTENT',
|
||||
RESET: 'PAYMENT:RESET',
|
||||
HYDRATE: 'PAYMENT:HYDRATE',
|
||||
|
||||
// Controls - always start with "DO_".
|
||||
DO_PERSIST_DATA: 'PAYMENT:DO_PERSIST_DATA',
|
||||
};
|
71
modules/ppcp-settings/resources/js/data/payment/actions.js
Normal file
71
modules/ppcp-settings/resources/js/data/payment/actions.js
Normal file
|
@ -0,0 +1,71 @@
|
|||
/**
|
||||
* Action Creators: Define functions to create action objects.
|
||||
*
|
||||
* These functions update state or trigger side effects (e.g., async operations).
|
||||
* Actions are categorized as Transient, Persistent, or Side effect.
|
||||
*
|
||||
* @file
|
||||
*/
|
||||
|
||||
import { select } from '@wordpress/data';
|
||||
|
||||
import ACTION_TYPES from './action-types';
|
||||
import { STORE_NAME } from './constants';
|
||||
|
||||
/**
|
||||
* @typedef {Object} Action An action object that is handled by a reducer or control.
|
||||
* @property {string} type - The action type.
|
||||
* @property {Object?} payload - Optional payload for the action.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Special. Resets all values in the store to initial defaults.
|
||||
*
|
||||
* @return {Action} The action.
|
||||
*/
|
||||
export const reset = () => ( { type: ACTION_TYPES.RESET } );
|
||||
|
||||
/**
|
||||
* Persistent. Set the full store details during app initialization.
|
||||
*
|
||||
* @param {{data: {}, flags?: {}}} payload
|
||||
* @return {Action} The action.
|
||||
*/
|
||||
export const hydrate = ( payload ) => ( {
|
||||
type: ACTION_TYPES.HYDRATE,
|
||||
payload,
|
||||
} );
|
||||
|
||||
/**
|
||||
* Transient. Marks the store as "ready", i.e., fully initialized.
|
||||
*
|
||||
* @param {boolean} isReady
|
||||
* @return {Action} The action.
|
||||
*/
|
||||
export const setIsReady = ( isReady ) => ( {
|
||||
type: ACTION_TYPES.SET_TRANSIENT,
|
||||
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.
|
||||
*
|
||||
* @return {Action} The action.
|
||||
*/
|
||||
export const persist = function* () {
|
||||
const data = yield select( STORE_NAME ).persistentData();
|
||||
|
||||
yield { type: ACTION_TYPES.DO_PERSIST_DATA, data };
|
||||
};
|
28
modules/ppcp-settings/resources/js/data/payment/constants.js
Normal file
28
modules/ppcp-settings/resources/js/data/payment/constants.js
Normal file
|
@ -0,0 +1,28 @@
|
|||
/**
|
||||
* Name of the Redux store module.
|
||||
*
|
||||
* Used by: Reducer, Selector, Index
|
||||
*
|
||||
* @type {string}
|
||||
*/
|
||||
export const STORE_NAME = 'wc/paypal/payment';
|
||||
|
||||
/**
|
||||
* REST path to hydrate data of this module by loading data from the WP DB.
|
||||
*
|
||||
* Used by: Resolvers
|
||||
* See: payment.php
|
||||
*
|
||||
* @type {string}
|
||||
*/
|
||||
export const REST_HYDRATE_PATH = '/wc/v3/wc_paypal/payment';
|
||||
|
||||
/**
|
||||
* REST path to persist data of this module to the WP DB.
|
||||
*
|
||||
* Used by: Controls
|
||||
* See: payment.php
|
||||
*
|
||||
* @type {string}
|
||||
*/
|
||||
export const REST_PERSIST_PATH = '/wc/v3/wc_paypal/payment';
|
23
modules/ppcp-settings/resources/js/data/payment/controls.js
vendored
Normal file
23
modules/ppcp-settings/resources/js/data/payment/controls.js
vendored
Normal file
|
@ -0,0 +1,23 @@
|
|||
/**
|
||||
* Controls: Implement side effects, typically asynchronous operations.
|
||||
*
|
||||
* Controls use ACTION_TYPES keys as identifiers.
|
||||
* They are triggered by corresponding actions and handle external interactions.
|
||||
*
|
||||
* @file
|
||||
*/
|
||||
|
||||
import apiFetch from '@wordpress/api-fetch';
|
||||
|
||||
import { REST_PERSIST_PATH } from './constants';
|
||||
import ACTION_TYPES from './action-types';
|
||||
|
||||
export const controls = {
|
||||
async [ ACTION_TYPES.DO_PERSIST_DATA ]( { data } ) {
|
||||
return await apiFetch( {
|
||||
path: REST_PERSIST_PATH,
|
||||
method: 'POST',
|
||||
data,
|
||||
} );
|
||||
},
|
||||
};
|
65
modules/ppcp-settings/resources/js/data/payment/hooks.js
Normal file
65
modules/ppcp-settings/resources/js/data/payment/hooks.js
Normal file
|
@ -0,0 +1,65 @@
|
|||
/**
|
||||
* Hooks: Provide the main API for components to interact with the store.
|
||||
*
|
||||
* These encapsulate store interactions, offering a consistent interface.
|
||||
* Hooks simplify data access and manipulation for components.
|
||||
*
|
||||
* @file
|
||||
*/
|
||||
|
||||
import { useSelect, useDispatch } from '@wordpress/data';
|
||||
|
||||
import { STORE_NAME } from './constants';
|
||||
|
||||
const useTransient = ( key ) =>
|
||||
useSelect(
|
||||
( select ) => select( STORE_NAME ).transientData()?.[ key ],
|
||||
[ key ]
|
||||
);
|
||||
|
||||
const usePersistent = ( key ) =>
|
||||
useSelect(
|
||||
( select ) => select( STORE_NAME ).persistentData()?.[ key ],
|
||||
[ key ]
|
||||
);
|
||||
|
||||
const useHooks = () => {
|
||||
const {
|
||||
persist,
|
||||
|
||||
// TODO: Replace with real property.
|
||||
setSampleValue,
|
||||
} = useDispatch( STORE_NAME );
|
||||
|
||||
// Read-only flags and derived state.
|
||||
// Nothing here yet.
|
||||
|
||||
// Transient accessors.
|
||||
const isReady = useTransient( 'isReady' );
|
||||
|
||||
// Persistent accessors.
|
||||
// TODO: Replace with real property.
|
||||
const sampleValue = usePersistent( 'sampleValue' );
|
||||
|
||||
return {
|
||||
persist,
|
||||
isReady,
|
||||
sampleValue,
|
||||
setSampleValue,
|
||||
};
|
||||
};
|
||||
|
||||
export const useState = () => {
|
||||
const { persist, isReady } = useHooks();
|
||||
return { persist, isReady };
|
||||
};
|
||||
|
||||
// TODO: Replace with real hook.
|
||||
export const useSampleValue = () => {
|
||||
const { sampleValue, setSampleValue } = useHooks();
|
||||
|
||||
return {
|
||||
sampleValue,
|
||||
setSampleValue,
|
||||
};
|
||||
};
|
24
modules/ppcp-settings/resources/js/data/payment/index.js
Normal file
24
modules/ppcp-settings/resources/js/data/payment/index.js
Normal file
|
@ -0,0 +1,24 @@
|
|||
import { createReduxStore, register } from '@wordpress/data';
|
||||
import { controls as wpControls } from '@wordpress/data-controls';
|
||||
|
||||
import { STORE_NAME } from './constants';
|
||||
import reducer from './reducer';
|
||||
import * as selectors from './selectors';
|
||||
import * as actions from './actions';
|
||||
import * as hooks from './hooks';
|
||||
import { resolvers } from './resolvers';
|
||||
import { controls } from './controls';
|
||||
|
||||
export const initStore = () => {
|
||||
const store = createReduxStore( STORE_NAME, {
|
||||
reducer,
|
||||
controls: { ...wpControls, ...controls },
|
||||
actions,
|
||||
selectors,
|
||||
resolvers,
|
||||
} );
|
||||
|
||||
register( store );
|
||||
};
|
||||
|
||||
export { hooks, selectors, STORE_NAME };
|
56
modules/ppcp-settings/resources/js/data/payment/reducer.js
Normal file
56
modules/ppcp-settings/resources/js/data/payment/reducer.js
Normal file
|
@ -0,0 +1,56 @@
|
|||
/**
|
||||
* Reducer: Defines store structure and state updates for this module.
|
||||
*
|
||||
* Manages both transient (temporary) and persistent (saved) state.
|
||||
* The initial state must define all properties, as dynamic additions are not supported.
|
||||
*
|
||||
* @file
|
||||
*/
|
||||
|
||||
import { createReducer, createSetters } from '../utils';
|
||||
import ACTION_TYPES from './action-types';
|
||||
|
||||
// Store structure.
|
||||
|
||||
// Transient: Values that are _not_ saved to the DB (like app lifecycle-flags).
|
||||
const defaultTransient = Object.freeze( {
|
||||
isReady: false,
|
||||
} );
|
||||
|
||||
// Persistent: Values that are loaded from the DB.
|
||||
const defaultPersistent = Object.freeze( {
|
||||
// TODO: Add real DB properties here.
|
||||
sampleValue: 'foo',
|
||||
} );
|
||||
|
||||
// Reducer logic.
|
||||
|
||||
const [ setTransient, setPersistent ] = createSetters(
|
||||
defaultTransient,
|
||||
defaultPersistent
|
||||
);
|
||||
|
||||
const reducer = createReducer( defaultTransient, defaultPersistent, {
|
||||
[ ACTION_TYPES.SET_TRANSIENT ]: ( state, payload ) =>
|
||||
setTransient( state, payload ),
|
||||
|
||||
[ ACTION_TYPES.SET_PERSISTENT ]: ( state, payload ) =>
|
||||
setPersistent( state, payload ),
|
||||
|
||||
[ ACTION_TYPES.RESET ]: ( state ) => {
|
||||
const cleanState = setTransient(
|
||||
setPersistent( state, defaultPersistent ),
|
||||
defaultTransient
|
||||
);
|
||||
|
||||
// Keep "read-only" details and initialization flags.
|
||||
cleanState.isReady = true;
|
||||
|
||||
return cleanState;
|
||||
},
|
||||
|
||||
[ ACTION_TYPES.HYDRATE ]: ( state, payload ) =>
|
||||
setPersistent( state, payload.data ),
|
||||
} );
|
||||
|
||||
export default reducer;
|
37
modules/ppcp-settings/resources/js/data/payment/resolvers.js
Normal file
37
modules/ppcp-settings/resources/js/data/payment/resolvers.js
Normal file
|
@ -0,0 +1,37 @@
|
|||
/**
|
||||
* Resolvers: Handle asynchronous data fetching for the store.
|
||||
*
|
||||
* These functions update store state with data from external sources.
|
||||
* Each resolver corresponds to a specific selector (selector with same name must exist).
|
||||
* Resolvers are called automatically when selectors request unavailable data.
|
||||
*
|
||||
* @file
|
||||
*/
|
||||
|
||||
import { dispatch } from '@wordpress/data';
|
||||
import { __ } from '@wordpress/i18n';
|
||||
import { apiFetch } from '@wordpress/data-controls';
|
||||
|
||||
import { STORE_NAME, REST_HYDRATE_PATH } from './constants';
|
||||
|
||||
export const resolvers = {
|
||||
/**
|
||||
* Retrieve settings from the site's REST API.
|
||||
*/
|
||||
*persistentData() {
|
||||
try {
|
||||
const result = yield apiFetch( { path: REST_HYDRATE_PATH } );
|
||||
|
||||
yield dispatch( STORE_NAME ).hydrate( result );
|
||||
yield dispatch( STORE_NAME ).setIsReady( true );
|
||||
} catch ( e ) {
|
||||
yield dispatch( 'core/notices' ).createErrorNotice(
|
||||
// TODO: Add the module name to the error message.
|
||||
__(
|
||||
'Error retrieving payment details.',
|
||||
'woocommerce-paypal-payments'
|
||||
)
|
||||
);
|
||||
}
|
||||
},
|
||||
};
|
21
modules/ppcp-settings/resources/js/data/payment/selectors.js
Normal file
21
modules/ppcp-settings/resources/js/data/payment/selectors.js
Normal file
|
@ -0,0 +1,21 @@
|
|||
/**
|
||||
* Selectors: Extract specific pieces of state from the store.
|
||||
*
|
||||
* These functions provide a consistent interface for accessing store data.
|
||||
* They allow components to retrieve data without knowing the store structure.
|
||||
*
|
||||
* @file
|
||||
*/
|
||||
|
||||
const EMPTY_OBJ = Object.freeze( {} );
|
||||
|
||||
const getState = ( state ) => state || EMPTY_OBJ;
|
||||
|
||||
export const persistentData = ( state ) => {
|
||||
return getState( state ).data || EMPTY_OBJ;
|
||||
};
|
||||
|
||||
export const transientData = ( state ) => {
|
||||
const { data, ...transientState } = getState( state );
|
||||
return transientState || EMPTY_OBJ;
|
||||
};
|
|
@ -13,10 +13,12 @@ use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
|
|||
use WooCommerce\PayPalCommerce\Settings\Ajax\SwitchSettingsUiEndpoint;
|
||||
use WooCommerce\PayPalCommerce\Settings\Data\GeneralSettings;
|
||||
use WooCommerce\PayPalCommerce\Settings\Data\OnboardingProfile;
|
||||
use WooCommerce\PayPalCommerce\Settings\Data\PaymentSettings;
|
||||
use WooCommerce\PayPalCommerce\Settings\Endpoint\AuthenticationRestEndpoint;
|
||||
use WooCommerce\PayPalCommerce\Settings\Endpoint\CommonRestEndpoint;
|
||||
use WooCommerce\PayPalCommerce\Settings\Endpoint\LoginLinkRestEndpoint;
|
||||
use WooCommerce\PayPalCommerce\Settings\Endpoint\OnboardingRestEndpoint;
|
||||
use WooCommerce\PayPalCommerce\Settings\Endpoint\PaymentRestEndpoint;
|
||||
use WooCommerce\PayPalCommerce\Settings\Endpoint\RefreshFeatureStatusEndpoint;
|
||||
use WooCommerce\PayPalCommerce\Settings\Endpoint\WebhookSettingsEndpoint;
|
||||
use WooCommerce\PayPalCommerce\Settings\Handler\ConnectionListener;
|
||||
|
@ -64,12 +66,18 @@ 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' ) );
|
||||
},
|
||||
'settings.rest.common' => static function ( ContainerInterface $container ) : CommonRestEndpoint {
|
||||
return new CommonRestEndpoint( $container->get( 'settings.data.general' ) );
|
||||
},
|
||||
'settings.rest.payment' => static function ( ContainerInterface $container ) : PaymentRestEndpoint {
|
||||
return new PaymentRestEndpoint( $container->get( 'settings.data.payment' ) );
|
||||
},
|
||||
'settings.rest.refresh_feature_status' => static function ( ContainerInterface $container ) : RefreshFeatureStatusEndpoint {
|
||||
return new RefreshFeatureStatusEndpoint(
|
||||
$container->get( 'wcgateway.settings' ),
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
/**
|
||||
* PaymentSettings class
|
||||
* Payment Methods class
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\Settings\Data
|
||||
*/
|
||||
|
@ -21,7 +21,7 @@ class PaymentSettings extends AbstractDataModel {
|
|||
*
|
||||
* @var string
|
||||
*/
|
||||
protected const OPTION_KEY = 'woocommerce-ppcp-data-payment-settings';
|
||||
protected const OPTION_KEY = 'woocommerce-ppcp-data-payment';
|
||||
|
||||
/**
|
||||
* Get default values for the model.
|
||||
|
@ -29,6 +29,182 @@ class PaymentSettings extends AbstractDataModel {
|
|||
* @return array
|
||||
*/
|
||||
protected function get_defaults() : array {
|
||||
return 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) 800–925–0304',
|
||||
'woocommerce-paypal-payments'
|
||||
),
|
||||
'icon' => 'payment-method-oxxo',
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
132
modules/ppcp-settings/src/Endpoint/PaymentRestEndpoint.php
Normal file
132
modules/ppcp-settings/src/Endpoint/PaymentRestEndpoint.php
Normal file
|
@ -0,0 +1,132 @@
|
|||
<?php
|
||||
/**
|
||||
* REST endpoint to manage the payment methods page.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\Settings\Endpoint
|
||||
*/
|
||||
|
||||
declare( strict_types = 1 );
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\Settings\Endpoint;
|
||||
|
||||
use WP_REST_Server;
|
||||
use WP_REST_Response;
|
||||
use WP_REST_Request;
|
||||
use WooCommerce\PayPalCommerce\Settings\Data\PaymentSettings;
|
||||
|
||||
/**
|
||||
* REST controller for the "Payment Methods" settings tab.
|
||||
*
|
||||
* This API acts as the intermediary between the "external world" and our
|
||||
* internal data model.
|
||||
*/
|
||||
class PaymentRestEndpoint extends RestEndpoint {
|
||||
/**
|
||||
* The base path for this REST controller.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $rest_base = 'payment';
|
||||
|
||||
/**
|
||||
* The settings instance.
|
||||
*
|
||||
* @var PaymentSettings
|
||||
*/
|
||||
protected PaymentSettings $settings;
|
||||
|
||||
/**
|
||||
* Field mapping for request to profile transformation.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private array $field_map = array(
|
||||
'paymentMethodsPayPalCheckout' => array(
|
||||
'js_name' => 'paymentMethodsPayPalCheckout',
|
||||
),
|
||||
'paymentMethodsOnlineCardPayments' => array(
|
||||
'js_name' => 'paymentMethodsOnlineCardPayments',
|
||||
),
|
||||
'paymentMethodsAlternative' => array(
|
||||
'js_name' => 'paymentMethodsAlternative',
|
||||
),
|
||||
);
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param PaymentSettings $settings The settings instance.
|
||||
*/
|
||||
public function __construct( PaymentSettings $settings ) {
|
||||
$this->settings = $settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure REST API routes.
|
||||
*/
|
||||
public function register_routes() : void {
|
||||
/**
|
||||
* GET wc/v3/wc_paypal/payment
|
||||
*/
|
||||
register_rest_route(
|
||||
$this->namespace,
|
||||
'/' . $this->rest_base,
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_details' ),
|
||||
'permission_callback' => array( $this, 'check_permission' ),
|
||||
)
|
||||
);
|
||||
|
||||
/**
|
||||
* POST wc/v3/wc_paypal/payment
|
||||
* {
|
||||
* // Fields mentioned in $field_map[]['js_name']
|
||||
* }
|
||||
*/
|
||||
register_rest_route(
|
||||
$this->namespace,
|
||||
'/' . $this->rest_base,
|
||||
array(
|
||||
'methods' => WP_REST_Server::EDITABLE,
|
||||
'callback' => array( $this, 'update_details' ),
|
||||
'permission_callback' => array( $this, 'check_permission' ),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all payment methods details.
|
||||
*
|
||||
* @return WP_REST_Response The current payment methods details.
|
||||
*/
|
||||
public function get_details() : WP_REST_Response {
|
||||
$js_data = $this->sanitize_for_javascript(
|
||||
$this->settings->to_array(),
|
||||
$this->field_map
|
||||
);
|
||||
|
||||
return $this->return_success(
|
||||
$js_data
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates payment methods details based on the request.
|
||||
*
|
||||
* @param WP_REST_Request $request Full data about the request.
|
||||
*
|
||||
* @return WP_REST_Response The updated payment methods details.
|
||||
*/
|
||||
public function update_details( WP_REST_Request $request ) : WP_REST_Response {
|
||||
$wp_data = $this->sanitize_for_wordpress(
|
||||
$request->get_params(),
|
||||
$this->field_map
|
||||
);
|
||||
|
||||
$this->settings->from_array( $wp_data );
|
||||
$this->settings->save();
|
||||
|
||||
return $this->get_details();
|
||||
}
|
||||
}
|
|
@ -207,6 +207,7 @@ class SettingsModule implements ServiceModule, ExecutableModule {
|
|||
$endpoints = array(
|
||||
$container->get( 'settings.rest.onboarding' ),
|
||||
$container->get( 'settings.rest.common' ),
|
||||
$container->get( 'settings.rest.payment' ),
|
||||
$container->get( 'settings.rest.connect_manual' ),
|
||||
$container->get( 'settings.rest.login_link' ),
|
||||
$container->get( 'settings.rest.webhooks' ),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue