Skip to content

Commit

Permalink
update payment gateways api response
Browse files Browse the repository at this point in the history
  • Loading branch information
kilbot committed Jan 2, 2023
1 parent cf4ea9b commit c7122f1
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 45 deletions.
38 changes: 23 additions & 15 deletions includes/API/Payment_Gateways.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,28 @@

namespace WCPOS\WooCommercePOS\API;

use WC_Payment_Gateway;
use WP_REST_Request;
use WP_REST_Response;

class Payment_Gateways {
/* @var WP_REST_Request $request */
private $request;

/* @var $settings */
private $settings;

/**
* Payment Gateways constructor.
*
* @param $request WP_REST_Request
*/
public function __construct( WP_REST_Request $request ) {
$this->request = $request;
$this->settings = woocommerce_pos_get_settings( 'payment_gateways' );

add_filter( 'woocommerce_rest_check_permissions', array( $this, 'check_permissions' ), 10, 4 );
add_filter( 'woocommerce_rest_prepare_payment_gateway', array( $this, 'prepare_payment_gateway' ), 10, 3 );
}

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

/**
* Returns array of all gateway ids, titles.
*
* @param array $fields
* Filter payment gateway objects returned from the REST API.
*
* @return array|void
* @param WP_REST_Response $response The response object.
* @param WC_Payment_Gateway $gateway Payment gateway object.
* @param WP_REST_Request $request Request object.
*/
public function get_all_posts( array $fields = array() ) {
$pos_gateways = Settings::get_setting( 'checkout', 'gateways' );
$enabled_pos_gateways = array();
public function prepare_payment_gateway( WP_REST_Response $response, WC_Payment_Gateway $gateway, WP_REST_Request $request ): WP_REST_Response {
$pos_setting = $this->settings['gateways'][ $gateway->id ] ?? null;
$data = $response->get_data();

for ( $i = 0; $i < \count( $pos_gateways ); $i ++ ) {
if ( $pos_gateways[ $i ]['enabled'] ) {
$gateway = $pos_gateways[ $i ];

$gateway['order'] = $i;
$enabled_pos_gateways[] = $gateway;
}
if ( $pos_setting ) {
$data['enabled'] = $pos_setting['enabled'];
$data['order'] = $pos_setting['order'];
} else {
$data['enabled'] = false;
}

return $enabled_pos_gateways;
$response->set_data( $data );

return $response;
}
}
31 changes: 21 additions & 10 deletions includes/API/Settings_Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,32 @@ abstract class Settings_Controller extends Controller {
protected static $default_settings = array();

/**
* @param string $key
* @param string $id
* @return array|mixed|WP_Error|null
*/
public function get_settings( string $key ) {
$method_name = 'get_' . $key . '_settings';
public function get_settings( string $id ) {
$method_name = 'get_' . $id . '_settings';

if ( method_exists( $this, $method_name ) ) {
return $this->$method_name();
} else {
return new WP_Error( 'cant-get', __( 'message', 'woocommerce-pos' ), array( 'status' => 400 ) );
}

return new WP_Error(
'woocommerce_pos_settings_error',
/* translators: %s: Settings group id, ie: 'general' or 'checkout' */
sprintf( __( 'Settings with id %s not found', 'woocommerce-pos' ), $id ),
array( 'status' => 400 )
);
}

/**
* @param string $key
* @param string $id
* @param array $settings
* @return array|mixed|WP_Error|null
*/
protected function save_settings( string $key, array $settings ) {
protected function save_settings( string $id, array $settings ) {
$success = update_option(
static::$db_prefix . $key,
static::$db_prefix . $id,
array_merge(
$settings,
array( 'date_modified_gmt' => current_time( 'mysql', true ) )
Expand All @@ -55,9 +61,14 @@ protected function save_settings( string $key, array $settings ) {
);

if ( $success ) {
return $this->get_settings( $key );
return $this->get_settings( $id );
}

return new WP_Error( 'cant-save', __( 'message', 'woocommerce-pos' ), array( 'status' => 400 ) );
return new WP_Error(
'woocommerce_pos_settings_error',
/* translators: %s: Settings group id, ie: 'general' or 'checkout' */
sprintf( __( 'Can not save settings with id %s', 'woocommerce-pos' ), $id ),
array( 'status' => 400 )
);
}
}
2 changes: 1 addition & 1 deletion includes/Gateways.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct() {
public function payment_gateways( array $gateways ) {
global $plugin_page;

// Remove gateways from WooCommerce settings, ie: they cannot be activated
// Early exit for WooCommerce settings, ie: don't show POS gateways
if ( is_admin() && 'wc-settings' == $plugin_page ) {
return $gateways;
}
Expand Down
22 changes: 12 additions & 10 deletions includes/wcpos-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,29 +99,31 @@ function woocommerce_pos_admin_request() {
/*
* Helper function to get WCPOS settings
*
* @param string $group
* @param string $id
* @param string $key
* @param mixed $default
*
* @return mixed
*/
if ( ! \function_exists( 'woocommerce_pos_get_settings' ) ) {
function woocommerce_pos_get_settings( $group, $key = null ) {
function woocommerce_pos_get_settings( $id, $key = null ) {
$api = new Settings();
$settings = $api->get_settings( $group );
$settings = $api->get_settings( $id );

if ( ! $key ) {
if ( is_wp_error( $settings ) ) {
return $settings;
}

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

return new WP_Error(
'woocommerce_pos_settings_error',
'Settings key not found'
);
return $key ? $settings[ $key ] : $settings;
}
}

Expand Down
15 changes: 10 additions & 5 deletions packages/settings/src/components/error.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import * as React from 'react';

import { get } from 'lodash';
import { FallbackProps } from 'react-error-boundary';

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

const ErrorFallback = ({ error, resetErrorBoundary }: FallbackProps) => {
const message = get(error, 'message', 'Unknown error');

return (
<Notice status="error" onRemove={resetErrorBoundary}>
<p>
{t('Something went wrong', { _tags: 'wp-admin-settings' })}: <code>{error.message}</code>
</p>
</Notice>
<div className="wcpos-p-4">
<Notice status="error" onRemove={resetErrorBoundary}>
<p>
{t('Something went wrong', { _tags: 'wp-admin-settings' })}: <code>{message}</code>
</p>
</Notice>
</div>
);
};

Expand Down
12 changes: 8 additions & 4 deletions packages/settings/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
import { render } from '@wordpress/element';
import { getFragment, isValidFragment } from '@wordpress/url';
import { ErrorBoundary } from 'react-error-boundary';

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

render(
<QueryClientProvider client={queryClient}>
<App />
<ReactQueryDevtools initialIsOpen={true} />
</QueryClientProvider>,
<ErrorBoundary FallbackComponent={Error}>
<QueryClientProvider client={queryClient}>
<App />
<ReactQueryDevtools initialIsOpen={true} />
</QueryClientProvider>
</ErrorBoundary>,
document.getElementById('woocommerce-pos-settings')
);

0 comments on commit c7122f1

Please sign in to comment.