mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 14:57:26 +08:00
✨ New action to refresh merchant data from server
This commit is contained in:
parent
c97464d7e2
commit
b4d1596fd1
6 changed files with 70 additions and 0 deletions
|
@ -22,4 +22,5 @@ export default {
|
|||
DO_MANUAL_CONNECTION: 'COMMON:DO_MANUAL_CONNECTION',
|
||||
DO_SANDBOX_LOGIN: 'COMMON:DO_SANDBOX_LOGIN',
|
||||
DO_PRODUCTION_LOGIN: 'COMMON:DO_PRODUCTION_LOGIN',
|
||||
DO_REFRESH_MERCHANT: 'COMMON:DO_REFRESH_MERCHANT',
|
||||
};
|
||||
|
|
|
@ -180,3 +180,12 @@ export const connectViaIdAndSecret = function* () {
|
|||
useSandbox,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Side effect. Clears and refreshes the merchant data via a REST request.
|
||||
*
|
||||
* @return {Action} The action.
|
||||
*/
|
||||
export const refreshMerchantData = function* () {
|
||||
return yield { type: ACTION_TYPES.DO_REFRESH_MERCHANT };
|
||||
};
|
||||
|
|
|
@ -16,6 +16,15 @@ export const STORE_NAME = 'wc/paypal/common';
|
|||
*/
|
||||
export const REST_HYDRATE_PATH = '/wc/v3/wc_paypal/common';
|
||||
|
||||
/**
|
||||
* REST path to fetch merchant details from the WordPress DB.
|
||||
*
|
||||
* Used by controls.
|
||||
*
|
||||
* @type {string}
|
||||
*/
|
||||
export const REST_HYDRATE_MERCHANT_PATH = '/wc/v3/wc_paypal/common/merchant';
|
||||
|
||||
/**
|
||||
* REST path to persist data of this module to the WP DB.
|
||||
*
|
||||
|
|
|
@ -7,12 +7,15 @@
|
|||
* @file
|
||||
*/
|
||||
|
||||
import { dispatch } from '@wordpress/data';
|
||||
import apiFetch from '@wordpress/api-fetch';
|
||||
|
||||
import {
|
||||
STORE_NAME,
|
||||
REST_PERSIST_PATH,
|
||||
REST_MANUAL_CONNECTION_PATH,
|
||||
REST_CONNECTION_URL_PATH,
|
||||
REST_HYDRATE_MERCHANT_PATH,
|
||||
} from './constants';
|
||||
import ACTION_TYPES from './action-types';
|
||||
|
||||
|
@ -99,4 +102,23 @@ export const controls = {
|
|||
|
||||
return result;
|
||||
},
|
||||
|
||||
async [ ACTION_TYPES.DO_REFRESH_MERCHANT ]() {
|
||||
let result = null;
|
||||
|
||||
try {
|
||||
result = await apiFetch( { path: REST_HYDRATE_MERCHANT_PATH } );
|
||||
|
||||
if ( result.success && result.merchant ) {
|
||||
await dispatch( STORE_NAME ).hydrate( result );
|
||||
}
|
||||
} catch ( e ) {
|
||||
result = {
|
||||
success: false,
|
||||
error: e,
|
||||
};
|
||||
}
|
||||
|
||||
return result;
|
||||
},
|
||||
};
|
||||
|
|
|
@ -79,6 +79,11 @@ const commonReducer = createReducer( defaultTransient, defaultPersistent, {
|
|||
return setTransient( state, { activities: newActivities } );
|
||||
},
|
||||
|
||||
[ ACTION_TYPES.DO_REFRESH_MERCHANT ]: ( state ) => ( {
|
||||
...state,
|
||||
merchant: Object.freeze( { ...defaultTransient.merchant } ),
|
||||
} ),
|
||||
|
||||
[ ACTION_TYPES.HYDRATE ]: ( state, payload ) => {
|
||||
const newState = setPersistent( state, payload.data );
|
||||
|
||||
|
|
|
@ -130,6 +130,18 @@ class CommonRestEndpoint extends RestEndpoint {
|
|||
),
|
||||
)
|
||||
);
|
||||
|
||||
register_rest_route(
|
||||
$this->namespace,
|
||||
"/$this->rest_base/merchant",
|
||||
array(
|
||||
array(
|
||||
'methods' => WP_REST_Server::READABLE,
|
||||
'callback' => array( $this, 'get_merchant_details' ),
|
||||
'permission_callback' => array( $this, 'check_permission' ),
|
||||
),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -168,6 +180,18 @@ class CommonRestEndpoint extends RestEndpoint {
|
|||
return $this->get_details();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns only the (read-only) merchant details from the DB.
|
||||
*
|
||||
* @return WP_REST_Response Merchant details.
|
||||
*/
|
||||
public function get_merchant_details() : WP_REST_Response {
|
||||
$js_data = array(); // No persistent data.
|
||||
$extra_data = $this->add_merchant_info( array() );
|
||||
|
||||
return $this->return_success( $js_data, $extra_data );
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends the "merchant" attribute to the extra_data collection, which
|
||||
* contains details about the merchant's PayPal account, like the merchant ID.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue