mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-07 19:54:15 +08:00
🚧 Refactor the eligible-feature REST response
This commit is contained in:
parent
33bd9ecce8
commit
6591889079
8 changed files with 49 additions and 31 deletions
|
@ -184,21 +184,17 @@ class ApplepayModule implements ServiceModule, ExtendingModule, ExecutableModule
|
|||
|
||||
add_filter(
|
||||
'woocommerce_paypal_payments_rest_common_merchant_data',
|
||||
function( array $merchant_data ) use ( $c ): array {
|
||||
if ( ! isset( $merchant_data['features'] ) ) {
|
||||
$merchant_data['features'] = array();
|
||||
}
|
||||
|
||||
function( array $features ) use ( $c ): array {
|
||||
$product_status = $c->get( 'applepay.apple-product-status' );
|
||||
assert( $product_status instanceof AppleProductStatus );
|
||||
|
||||
$apple_pay_enabled = $product_status->is_active();
|
||||
|
||||
$merchant_data['features']['apple_pay'] = array(
|
||||
$features['apple_pay'] = array(
|
||||
'enabled' => $apple_pay_enabled,
|
||||
);
|
||||
|
||||
return $merchant_data;
|
||||
return $features;
|
||||
}
|
||||
);
|
||||
|
||||
|
|
|
@ -234,21 +234,17 @@ class GooglepayModule implements ServiceModule, ExtendingModule, ExecutableModul
|
|||
|
||||
add_filter(
|
||||
'woocommerce_paypal_payments_rest_common_merchant_data',
|
||||
function ( array $merchant_data ) use ( $c ): array {
|
||||
if ( ! isset( $merchant_data['features'] ) ) {
|
||||
$merchant_data['features'] = array();
|
||||
}
|
||||
|
||||
function ( array $features ) use ( $c ): array {
|
||||
$product_status = $c->get( 'googlepay.helpers.apm-product-status' );
|
||||
assert( $product_status instanceof ApmProductStatus );
|
||||
|
||||
$google_pay_enabled = $product_status->is_active();
|
||||
|
||||
$merchant_data['features']['google_pay'] = array(
|
||||
$features['google_pay'] = array(
|
||||
'enabled' => $google_pay_enabled,
|
||||
);
|
||||
|
||||
return $merchant_data;
|
||||
return $features;
|
||||
}
|
||||
);
|
||||
|
||||
|
|
|
@ -16,11 +16,11 @@ const TabOverview = () => {
|
|||
const [ todosData, setTodosData ] = useState( todosDataDefault );
|
||||
const [ isRefreshing, setIsRefreshing ] = useState( false );
|
||||
|
||||
const { merchant } = useMerchantInfo();
|
||||
const { merchantFeatures } = useMerchantInfo();
|
||||
const { refreshFeatureStatuses } = useDispatch( STORE_NAME );
|
||||
|
||||
const features = featuresDefault.map( ( feature ) => {
|
||||
const merchantFeature = merchant?.features?.[ feature.id ];
|
||||
const merchantFeature = merchantFeatures?.[ feature.id ];
|
||||
return {
|
||||
...feature,
|
||||
enabled: merchantFeature?.enabled ?? false,
|
||||
|
|
|
@ -47,10 +47,16 @@ const useHooks = () => {
|
|||
( select ) => select( STORE_NAME ).merchant(),
|
||||
[]
|
||||
);
|
||||
|
||||
// Read-only properties.
|
||||
const wooSettings = useSelect(
|
||||
( select ) => select( STORE_NAME ).wooSettings(),
|
||||
[]
|
||||
);
|
||||
const features = useSelect(
|
||||
( select ) => select( STORE_NAME ).features(),
|
||||
[]
|
||||
);
|
||||
|
||||
const savePersistent = async ( setter, value ) => {
|
||||
setter( value );
|
||||
|
@ -73,6 +79,7 @@ const useHooks = () => {
|
|||
authenticateWithOAuth,
|
||||
merchant,
|
||||
wooSettings,
|
||||
features,
|
||||
webhooks,
|
||||
startWebhookSimulation,
|
||||
checkWebhookSimulationState,
|
||||
|
@ -130,7 +137,7 @@ export const useWebhooks = () => {
|
|||
};
|
||||
};
|
||||
export const useMerchantInfo = () => {
|
||||
const { merchant } = useHooks();
|
||||
const { merchant, features } = useHooks();
|
||||
const { refreshMerchantData } = useDispatch( STORE_NAME );
|
||||
|
||||
const verifyLoginStatus = useCallback( async () => {
|
||||
|
@ -146,6 +153,7 @@ export const useMerchantInfo = () => {
|
|||
|
||||
return {
|
||||
merchant, // Merchant details
|
||||
features, // Eligible merchant features
|
||||
verifyLoginStatus, // Callback
|
||||
};
|
||||
};
|
||||
|
|
|
@ -30,6 +30,21 @@ const defaultTransient = Object.freeze( {
|
|||
storeCountry: '',
|
||||
storeCurrency: '',
|
||||
} ),
|
||||
|
||||
features: Object.freeze( {
|
||||
save_paypal_and_venmo: {
|
||||
enabled: false,
|
||||
},
|
||||
advanced_credit_and_debit_cards: {
|
||||
enabled: false,
|
||||
},
|
||||
apple_pay: {
|
||||
enabled: false,
|
||||
},
|
||||
google_pay: {
|
||||
enabled: false,
|
||||
},
|
||||
} ),
|
||||
} );
|
||||
|
||||
const defaultPersistent = Object.freeze( {
|
||||
|
@ -83,13 +98,14 @@ const commonReducer = createReducer( defaultTransient, defaultPersistent, {
|
|||
[ ACTION_TYPES.DO_REFRESH_MERCHANT ]: ( state ) => ( {
|
||||
...state,
|
||||
merchant: Object.freeze( { ...defaultTransient.merchant } ),
|
||||
features: Object.freeze( { ...defaultTransient.features } ),
|
||||
} ),
|
||||
|
||||
[ ACTION_TYPES.HYDRATE ]: ( state, payload ) => {
|
||||
const newState = setPersistent( state, payload.data );
|
||||
|
||||
// Populate read-only properties.
|
||||
[ 'wooSettings', 'merchant' ].forEach( ( key ) => {
|
||||
[ 'wooSettings', 'merchant', 'features' ].forEach( ( key ) => {
|
||||
if ( ! payload[ key ] ) {
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ export const persistentData = ( state ) => {
|
|||
};
|
||||
|
||||
export const transientData = ( state ) => {
|
||||
const { data, merchant, wooSettings, ...transientState } =
|
||||
const { data, merchant, features, wooSettings, ...transientState } =
|
||||
getState( state );
|
||||
return transientState || EMPTY_OBJ;
|
||||
};
|
||||
|
@ -30,6 +30,10 @@ export const merchant = ( state ) => {
|
|||
return getState( state ).merchant || EMPTY_OBJ;
|
||||
};
|
||||
|
||||
export const features = ( state ) => {
|
||||
return getState( state ).features || EMPTY_OBJ;
|
||||
};
|
||||
|
||||
export const wooSettings = ( state ) => {
|
||||
return getState( state ).wooSettings || EMPTY_OBJ;
|
||||
};
|
||||
|
|
|
@ -201,10 +201,12 @@ class CommonRestEndpoint extends RestEndpoint {
|
|||
$this->merchant_info_map
|
||||
);
|
||||
|
||||
$extra_data['merchant'] = apply_filters(
|
||||
if ( $this->settings->is_merchant_connected() ) {
|
||||
$extra_data['features'] = apply_filters(
|
||||
'woocommerce_paypal_payments_rest_common_merchant_data',
|
||||
$extra_data['merchant'],
|
||||
array(),
|
||||
);
|
||||
}
|
||||
|
||||
return $extra_data;
|
||||
}
|
||||
|
|
|
@ -550,16 +550,12 @@ class WCGatewayModule implements ServiceModule, ExtendingModule, ExecutableModul
|
|||
|
||||
add_filter(
|
||||
'woocommerce_paypal_payments_rest_common_merchant_data',
|
||||
function( array $merchant_data ) use ( $c ): array {
|
||||
if ( ! isset( $merchant_data['features'] ) ) {
|
||||
$merchant_data['features'] = array();
|
||||
}
|
||||
|
||||
function( array $features ) use ( $c ): array {
|
||||
$billing_agreements_endpoint = $c->get( 'api.endpoint.billing-agreements' );
|
||||
assert( $billing_agreements_endpoint instanceof BillingAgreementsEndpoint );
|
||||
|
||||
$reference_transactions_enabled = $billing_agreements_endpoint->reference_transaction_enabled();
|
||||
$merchant_data['features']['save_paypal_and_venmo'] = array(
|
||||
$features['save_paypal_and_venmo'] = array(
|
||||
'enabled' => $reference_transactions_enabled,
|
||||
);
|
||||
|
||||
|
@ -567,11 +563,11 @@ class WCGatewayModule implements ServiceModule, ExtendingModule, ExecutableModul
|
|||
assert( $dcc_product_status instanceof DCCProductStatus );
|
||||
|
||||
$dcc_enabled = $dcc_product_status->dcc_is_active();
|
||||
$merchant_data['features']['advanced_credit_and_debit_cards'] = array(
|
||||
$features['advanced_credit_and_debit_cards'] = array(
|
||||
'enabled' => $dcc_enabled,
|
||||
);
|
||||
|
||||
return $merchant_data;
|
||||
return $features;
|
||||
}
|
||||
);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue