mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 18:16:38 +08:00
✨ Add merchant details to “common” hydration path
This commit is contained in:
parent
0462905123
commit
c97464d7e2
4 changed files with 60 additions and 9 deletions
|
@ -8,7 +8,7 @@
|
|||
export const STORE_NAME = 'wc/paypal/common';
|
||||
|
||||
/**
|
||||
* REST path to hydrate data of this module by loading data from the WP DB..
|
||||
* REST path to hydrate data of this module by loading data from the WP DB.
|
||||
*
|
||||
* Used by resolvers.
|
||||
*
|
||||
|
|
|
@ -17,6 +17,13 @@ const defaultTransient = {
|
|||
activities: new Map(),
|
||||
|
||||
// Read only values, provided by the server via hydrate.
|
||||
merchant: {
|
||||
isConnected: false,
|
||||
isSandbox: false,
|
||||
id: '',
|
||||
email: '',
|
||||
},
|
||||
|
||||
wooSettings: {
|
||||
storeCountry: '',
|
||||
storeCurrency: '',
|
||||
|
@ -75,13 +82,18 @@ const commonReducer = createReducer( defaultTransient, defaultPersistent, {
|
|||
[ ACTION_TYPES.HYDRATE ]: ( state, payload ) => {
|
||||
const newState = setPersistent( state, payload.data );
|
||||
|
||||
if ( payload.wooSettings ) {
|
||||
newState.wooSettings = {
|
||||
...newState.wooSettings,
|
||||
...payload.wooSettings,
|
||||
};
|
||||
// Populate read-only properties.
|
||||
[ 'wooSettings', 'merchant' ].forEach( ( key ) => {
|
||||
if ( ! payload[ key ] ) {
|
||||
return;
|
||||
}
|
||||
|
||||
newState[ key ] = Object.freeze( {
|
||||
...newState[ key ],
|
||||
...payload[ key ],
|
||||
} );
|
||||
} );
|
||||
|
||||
return newState;
|
||||
},
|
||||
} );
|
||||
|
|
|
@ -16,7 +16,8 @@ export const persistentData = ( state ) => {
|
|||
};
|
||||
|
||||
export const transientData = ( state ) => {
|
||||
const { data, wooSettings, ...transientState } = getState( state );
|
||||
const { data, merchant, wooSettings, ...transientState } =
|
||||
getState( state );
|
||||
return transientState || EMPTY_OBJ;
|
||||
};
|
||||
|
||||
|
|
|
@ -61,7 +61,27 @@ class CommonRestEndpoint extends RestEndpoint {
|
|||
);
|
||||
|
||||
/**
|
||||
* Map the internal flags to JS names.
|
||||
* Map merchant details to JS names.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private array $merchant_info_map = array(
|
||||
'merchant_connected' => array(
|
||||
'js_name' => 'isConnected',
|
||||
),
|
||||
'sandbox_merchant' => array(
|
||||
'js_name' => 'isSandbox',
|
||||
),
|
||||
'merchant_id' => array(
|
||||
'js_name' => 'id',
|
||||
),
|
||||
'merchant_email' => array(
|
||||
'js_name' => 'email',
|
||||
),
|
||||
);
|
||||
|
||||
/**
|
||||
* Map woo-settings to JS names.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
|
@ -124,6 +144,7 @@ class CommonRestEndpoint extends RestEndpoint {
|
|||
);
|
||||
|
||||
$extra_data = $this->add_woo_settings( array() );
|
||||
$extra_data = $this->add_merchant_info( $extra_data );
|
||||
|
||||
return $this->return_success( $js_data, $extra_data );
|
||||
}
|
||||
|
@ -147,6 +168,23 @@ class CommonRestEndpoint extends RestEndpoint {
|
|||
return $this->get_details();
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends the "merchant" attribute to the extra_data collection, which
|
||||
* contains details about the merchant's PayPal account, like the merchant ID.
|
||||
*
|
||||
* @param array $extra_data Initial extra_data collection.
|
||||
*
|
||||
* @return array Updated extra_data collection.
|
||||
*/
|
||||
protected function add_merchant_info( array $extra_data ) : array {
|
||||
$extra_data['merchant'] = $this->sanitize_for_javascript(
|
||||
$this->settings->to_array(),
|
||||
$this->merchant_info_map
|
||||
);
|
||||
|
||||
return $extra_data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends the "wooSettings" attribute to the extra_data collection to
|
||||
* provide WooCommerce store details, like the store country and currency.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue