Skip to content

Commit c7122f1

Browse files
committed
update payment gateways api response
1 parent cf4ea9b commit c7122f1

File tree

6 files changed

+75
-45
lines changed

6 files changed

+75
-45
lines changed

includes/API/Payment_Gateways.php

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,28 @@
22

33
namespace WCPOS\WooCommercePOS\API;
44

5+
use WC_Payment_Gateway;
56
use WP_REST_Request;
7+
use WP_REST_Response;
68

79
class Payment_Gateways {
10+
/* @var WP_REST_Request $request */
811
private $request;
912

13+
/* @var $settings */
14+
private $settings;
15+
1016
/**
1117
* Payment Gateways constructor.
1218
*
1319
* @param $request WP_REST_Request
1420
*/
1521
public function __construct( WP_REST_Request $request ) {
1622
$this->request = $request;
23+
$this->settings = woocommerce_pos_get_settings( 'payment_gateways' );
1724

1825
add_filter( 'woocommerce_rest_check_permissions', array( $this, 'check_permissions' ), 10, 4 );
26+
add_filter( 'woocommerce_rest_prepare_payment_gateway', array( $this, 'prepare_payment_gateway' ), 10, 3 );
1927
}
2028

2129
/**
@@ -35,25 +43,25 @@ public function check_permissions( $permission, $context, $object_id, $object )
3543
}
3644

3745
/**
38-
* Returns array of all gateway ids, titles.
39-
*
40-
* @param array $fields
46+
* Filter payment gateway objects returned from the REST API.
4147
*
42-
* @return array|void
48+
* @param WP_REST_Response $response The response object.
49+
* @param WC_Payment_Gateway $gateway Payment gateway object.
50+
* @param WP_REST_Request $request Request object.
4351
*/
44-
public function get_all_posts( array $fields = array() ) {
45-
$pos_gateways = Settings::get_setting( 'checkout', 'gateways' );
46-
$enabled_pos_gateways = array();
52+
public function prepare_payment_gateway( WP_REST_Response $response, WC_Payment_Gateway $gateway, WP_REST_Request $request ): WP_REST_Response {
53+
$pos_setting = $this->settings['gateways'][ $gateway->id ] ?? null;
54+
$data = $response->get_data();
4755

48-
for ( $i = 0; $i < \count( $pos_gateways ); $i ++ ) {
49-
if ( $pos_gateways[ $i ]['enabled'] ) {
50-
$gateway = $pos_gateways[ $i ];
51-
52-
$gateway['order'] = $i;
53-
$enabled_pos_gateways[] = $gateway;
54-
}
56+
if ( $pos_setting ) {
57+
$data['enabled'] = $pos_setting['enabled'];
58+
$data['order'] = $pos_setting['order'];
59+
} else {
60+
$data['enabled'] = false;
5561
}
5662

57-
return $enabled_pos_gateways;
63+
$response->set_data( $data );
64+
65+
return $response;
5866
}
5967
}

includes/API/Settings_Controller.php

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,32 @@ abstract class Settings_Controller extends Controller {
2727
protected static $default_settings = array();
2828

2929
/**
30-
* @param string $key
30+
* @param string $id
3131
* @return array|mixed|WP_Error|null
3232
*/
33-
public function get_settings( string $key ) {
34-
$method_name = 'get_' . $key . '_settings';
33+
public function get_settings( string $id ) {
34+
$method_name = 'get_' . $id . '_settings';
35+
3536
if ( method_exists( $this, $method_name ) ) {
3637
return $this->$method_name();
37-
} else {
38-
return new WP_Error( 'cant-get', __( 'message', 'woocommerce-pos' ), array( 'status' => 400 ) );
3938
}
39+
40+
return new WP_Error(
41+
'woocommerce_pos_settings_error',
42+
/* translators: %s: Settings group id, ie: 'general' or 'checkout' */
43+
sprintf( __( 'Settings with id %s not found', 'woocommerce-pos' ), $id ),
44+
array( 'status' => 400 )
45+
);
4046
}
4147

4248
/**
43-
* @param string $key
49+
* @param string $id
4450
* @param array $settings
4551
* @return array|mixed|WP_Error|null
4652
*/
47-
protected function save_settings( string $key, array $settings ) {
53+
protected function save_settings( string $id, array $settings ) {
4854
$success = update_option(
49-
static::$db_prefix . $key,
55+
static::$db_prefix . $id,
5056
array_merge(
5157
$settings,
5258
array( 'date_modified_gmt' => current_time( 'mysql', true ) )
@@ -55,9 +61,14 @@ protected function save_settings( string $key, array $settings ) {
5561
);
5662

5763
if ( $success ) {
58-
return $this->get_settings( $key );
64+
return $this->get_settings( $id );
5965
}
6066

61-
return new WP_Error( 'cant-save', __( 'message', 'woocommerce-pos' ), array( 'status' => 400 ) );
67+
return new WP_Error(
68+
'woocommerce_pos_settings_error',
69+
/* translators: %s: Settings group id, ie: 'general' or 'checkout' */
70+
sprintf( __( 'Can not save settings with id %s', 'woocommerce-pos' ), $id ),
71+
array( 'status' => 400 )
72+
);
6273
}
6374
}

includes/Gateways.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function __construct() {
3232
public function payment_gateways( array $gateways ) {
3333
global $plugin_page;
3434

35-
// Remove gateways from WooCommerce settings, ie: they cannot be activated
35+
// Early exit for WooCommerce settings, ie: don't show POS gateways
3636
if ( is_admin() && 'wc-settings' == $plugin_page ) {
3737
return $gateways;
3838
}

includes/wcpos-functions.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,29 +99,31 @@ function woocommerce_pos_admin_request() {
9999
/*
100100
* Helper function to get WCPOS settings
101101
*
102-
* @param string $group
102+
* @param string $id
103103
* @param string $key
104104
* @param mixed $default
105105
*
106106
* @return mixed
107107
*/
108108
if ( ! \function_exists( 'woocommerce_pos_get_settings' ) ) {
109-
function woocommerce_pos_get_settings( $group, $key = null ) {
109+
function woocommerce_pos_get_settings( $id, $key = null ) {
110110
$api = new Settings();
111-
$settings = $api->get_settings( $group );
111+
$settings = $api->get_settings( $id );
112112

113-
if ( ! $key ) {
113+
if ( is_wp_error( $settings ) ) {
114114
return $settings;
115115
}
116116

117-
if ( isset( $settings[ $key ] ) ) {
118-
return $settings[ $key ];
117+
if ( $key && ! isset( $settings[ $key ] ) ) {
118+
return new WP_Error(
119+
'woocommerce_pos_settings_error',
120+
/* translators: 1. %s: Settings group id, ie: 'general' or 'checkout' 2. Settings key for a group, eg: 'barcode_field' */
121+
sprintf( __( 'Settings with id %s and key %s not found', 'woocommerce-pos' ), $id, $key ),
122+
array( 'status' => 400 )
123+
);
119124
}
120125

121-
return new WP_Error(
122-
'woocommerce_pos_settings_error',
123-
'Settings key not found'
124-
);
126+
return $key ? $settings[ $key ] : $settings;
125127
}
126128
}
127129

packages/settings/src/components/error.tsx

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
import * as React from 'react';
22

3+
import { get } from 'lodash';
34
import { FallbackProps } from 'react-error-boundary';
45

56
import { t } from '../translations';
67
import Notice from './notice';
78

89
const ErrorFallback = ({ error, resetErrorBoundary }: FallbackProps) => {
10+
const message = get(error, 'message', 'Unknown error');
11+
912
return (
10-
<Notice status="error" onRemove={resetErrorBoundary}>
11-
<p>
12-
{t('Something went wrong', { _tags: 'wp-admin-settings' })}: <code>{error.message}</code>
13-
</p>
14-
</Notice>
13+
<div className="wcpos-p-4">
14+
<Notice status="error" onRemove={resetErrorBoundary}>
15+
<p>
16+
{t('Something went wrong', { _tags: 'wp-admin-settings' })}: <code>{message}</code>
17+
</p>
18+
</Notice>
19+
</div>
1520
);
1621
};
1722

packages/settings/src/index.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
44
import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
55
import { render } from '@wordpress/element';
66
import { getFragment, isValidFragment } from '@wordpress/url';
7+
import { ErrorBoundary } from 'react-error-boundary';
78

9+
import Error from './components/error';
810
import { SnackbarProvider } from './components/snackbar';
911
import { NoticesProvider } from './hooks/use-notices';
1012
import useReadyState from './hooks/use-ready-state';
@@ -46,9 +48,11 @@ const App = () => {
4648
};
4749

4850
render(
49-
<QueryClientProvider client={queryClient}>
50-
<App />
51-
<ReactQueryDevtools initialIsOpen={true} />
52-
</QueryClientProvider>,
51+
<ErrorBoundary FallbackComponent={Error}>
52+
<QueryClientProvider client={queryClient}>
53+
<App />
54+
<ReactQueryDevtools initialIsOpen={true} />
55+
</QueryClientProvider>
56+
</ErrorBoundary>,
5357
document.getElementById('woocommerce-pos-settings')
5458
);

0 commit comments

Comments
 (0)