From 75c18a690275a4dc9005e661b78b88d727b1aa74 Mon Sep 17 00:00:00 2001 From: Paul Sealock Date: Wed, 14 Aug 2024 10:08:59 +1200 Subject: [PATCH] [Beta Tester] Add WCPay test order meta data (#50467) --- .../50467-add-beta-tester-wcpay-test-order | 4 + .../woocommerce-beta-tester/src/app/app.js | 6 + .../src/payments/index.js | 160 ++++++++++++++++++ 3 files changed, 170 insertions(+) create mode 100644 plugins/woocommerce-beta-tester/changelog/50467-add-beta-tester-wcpay-test-order create mode 100644 plugins/woocommerce-beta-tester/src/payments/index.js diff --git a/plugins/woocommerce-beta-tester/changelog/50467-add-beta-tester-wcpay-test-order b/plugins/woocommerce-beta-tester/changelog/50467-add-beta-tester-wcpay-test-order new file mode 100644 index 00000000000..20882c69409 --- /dev/null +++ b/plugins/woocommerce-beta-tester/changelog/50467-add-beta-tester-wcpay-test-order @@ -0,0 +1,4 @@ +Significance: patch +Type: add + +Add WCPay test order meta data \ No newline at end of file diff --git a/plugins/woocommerce-beta-tester/src/app/app.js b/plugins/woocommerce-beta-tester/src/app/app.js index 50bd79036f9..6f0fc4bfa42 100644 --- a/plugins/woocommerce-beta-tester/src/app/app.js +++ b/plugins/woocommerce-beta-tester/src/app/app.js @@ -14,6 +14,7 @@ import { default as Features } from '../features'; import { default as RestAPIFilters } from '../rest-api-filters'; import RemoteInboxNotifications from '../remote-inbox-notifications'; import RemoteLogging from '../remote-logging'; +import Payments from '../payments'; const tabs = applyFilters( 'woocommerce_admin_test_helper_tabs', [ { @@ -51,6 +52,11 @@ const tabs = applyFilters( 'woocommerce_admin_test_helper_tabs', [ title: 'Remote Logging', content: , }, + { + name: 'woocommerce-payments', + title: 'WCPay', + content: , + }, ] ); export function App() { diff --git a/plugins/woocommerce-beta-tester/src/payments/index.js b/plugins/woocommerce-beta-tester/src/payments/index.js new file mode 100644 index 00000000000..f340b904f37 --- /dev/null +++ b/plugins/woocommerce-beta-tester/src/payments/index.js @@ -0,0 +1,160 @@ +/** + * External dependencies + */ +import { useSelect, useDispatch } from '@wordpress/data'; +import { ORDERS_STORE_NAME } from '@woocommerce/data'; +import { ToggleControl } from '@wordpress/components'; +import apiFetch from '@wordpress/api-fetch'; + +const metaKey = '_wcpay_mode'; + +const Payments = () => { + const { + orders = [], + isRequesting, + isError, + } = useSelect( ( select ) => { + const { getOrders, hasFinishedResolution, getOrdersError } = + select( ORDERS_STORE_NAME ); + + const query = { + page: 1, + per_page: 10, + }; + const orders = getOrders( query, null ); + const isRequesting = hasFinishedResolution( 'getOrders', [ query ] ); + + return { + orders, + isError: Boolean( getOrdersError( orders ) ), + isRequesting, + }; + } ); + + const { getOrderSuccess } = useDispatch( ORDERS_STORE_NAME ); + + const isTestOrder = ( order ) => + order.meta_data.find( ( metaItem ) => metaItem.key === metaKey ) + ?.value === 'test'; + + const onToggle = async ( order, isChecked ) => { + const data = { + meta_data: [ + { + key: metaKey, + value: isChecked ? 'test' : 'live', + }, + ], + }; + + try { + const updatedOrder = await apiFetch( { + path: `/wc/v3/orders/${ order.id }`, + method: 'PUT', + data: data, + headers: { + 'Content-Type': 'application/json', + }, + } ); + getOrderSuccess( order.id, updatedOrder ); + } catch ( error ) { + throw error; + } + }; + + const renderOrders = ( orders ) => { + return orders.map( ( order ) => { + return ( + + + { `${ order?.billing?.first_name } ${ order?.billing?.last_name }` } + + + { order.id } + + + { order.date_created_gmt } + + + { order.status } + + + { order.total } + + + + onToggle( order, isChecked ) + } + /> + + + ); + } ); + }; + + return ( + <> +

WooCommerce Payments

+ + + + + + + + + + + + + { ! isRequesting && + orders?.length && + renderOrders( orders ) } + +
+ Order + + ID + + Date + + Status + + Total + + WCPay Test Order +
+ { ! isRequesting && orders?.length === 0 && ( +

No orders found.

+ ) } + + ); +}; + +export default Payments;