From 1d5b91a6b48e4d434ac1f359a283444abb7a85e3 Mon Sep 17 00:00:00 2001 From: Jessy Pappachan <32092402+jessy-p@users.noreply.github.com> Date: Mon, 6 May 2024 11:03:41 +0530 Subject: [PATCH] Pass merchant timezone to Reporting Payment Activity API (#8743) Co-authored-by: Jessy Co-authored-by: Shendy <73803630+shendy-a8c@users.noreply.github.com> --- ...ix-8742-merchant-timezone-payment-activity | 5 +++ .../payment-activity-data.tsx | 7 ++-- .../data/payment-activity/test/hooks.test.ts | 1 + .../payment-activity/test/resolver.test.ts | 3 +- client/data/payment-activity/types.d.ts | 2 +- .../class-get-reporting-payment-activity.md | 38 +++++++++++++++++++ .../class-get-reporting-payment-activity.php | 37 +++++++++++++----- 7 files changed, 79 insertions(+), 14 deletions(-) create mode 100644 changelog/fix-8742-merchant-timezone-payment-activity create mode 100644 includes/core/server/request/class-get-reporting-payment-activity.md diff --git a/changelog/fix-8742-merchant-timezone-payment-activity b/changelog/fix-8742-merchant-timezone-payment-activity new file mode 100644 index 00000000000..08dc0462916 --- /dev/null +++ b/changelog/fix-8742-merchant-timezone-payment-activity @@ -0,0 +1,5 @@ +Significance: patch +Type: dev +Comment: Adding some fixes to the Payment Activity component, reporting controller aand associated classes. + + diff --git a/client/components/payment-activity/payment-activity-data.tsx b/client/components/payment-activity/payment-activity-data.tsx index 20ca61265f3..06b085a1589 100644 --- a/client/components/payment-activity/payment-activity-data.tsx +++ b/client/components/payment-activity/payment-activity-data.tsx @@ -65,9 +65,10 @@ const getSearchParams = ( searchTerms: string[] ) => { }; const PaymentActivityData: React.FC = () => { - const { paymentActivityData, isLoading } = usePaymentActivityData( - getDateRange() - ); + const { paymentActivityData, isLoading } = usePaymentActivityData( { + ...getDateRange(), + timezone: moment( new Date() ).format( 'Z' ), + } ); const totalPaymentVolume = paymentActivityData?.total_payment_volume ?? 0; const charges = paymentActivityData?.charges ?? 0; diff --git a/client/data/payment-activity/test/hooks.test.ts b/client/data/payment-activity/test/hooks.test.ts index 03d1d2834d7..4cd6114f69a 100644 --- a/client/data/payment-activity/test/hooks.test.ts +++ b/client/data/payment-activity/test/hooks.test.ts @@ -36,6 +36,7 @@ describe( 'usePaymentActivityData', () => { const result = usePaymentActivityData( { date_start: '2021-01-01', date_end: '2021-01-31', + timezone: 'UTC', } ); expect( result ).toEqual( { diff --git a/client/data/payment-activity/test/resolver.test.ts b/client/data/payment-activity/test/resolver.test.ts index 8142b49b7c9..a9a93977180 100644 --- a/client/data/payment-activity/test/resolver.test.ts +++ b/client/data/payment-activity/test/resolver.test.ts @@ -15,12 +15,13 @@ import { getPaymentActivityData } from '../resolvers'; const query = { date_start: '2020-04-29T04:00:00', date_end: '2020-04-29T03:59:59', + timezone: '+2:30', }; describe( 'getPaymentActivityData resolver', () => { const successfulResponse: any = { amount: 3000 }; const expectedQueryString = - 'date_start=2020-04-29T04%3A00%3A00&date_end=2020-04-29T03%3A59%3A59'; + 'date_start=2020-04-29T04%3A00%3A00&date_end=2020-04-29T03%3A59%3A59&timezone=%2B2%3A30'; const errorResponse = new Error( 'Error retrieving payment activity data.' ); diff --git a/client/data/payment-activity/types.d.ts b/client/data/payment-activity/types.d.ts index ef388d2b899..76690dc459c 100644 --- a/client/data/payment-activity/types.d.ts +++ b/client/data/payment-activity/types.d.ts @@ -39,5 +39,5 @@ export interface PaymentActivityQuery { /** The date range end datetime used to calculate transaction data, e.g. 2024-04-29T16:19:29 */ date_end: string; /** The timezone used to calculate the transaction data date range, e.g. 'UTC' */ - timezone?: string; + timezone: string; } diff --git a/includes/core/server/request/class-get-reporting-payment-activity.md b/includes/core/server/request/class-get-reporting-payment-activity.md new file mode 100644 index 00000000000..53f676f6d59 --- /dev/null +++ b/includes/core/server/request/class-get-reporting-payment-activity.md @@ -0,0 +1,38 @@ +# `Get_Reporting_Payment_Activity` request class + +[ℹ️ This document is a part of __WooCommerce Payments Server Requests__](../README.md) + +## Description + +The `WCPay\Core\Server\Request\Get_Reporting_Payment_Activity` class is used to construct the request for retrieving payment activity. + +## Parameters + +| Parameter | Setter | Immutable | Required | Default value | +|-------------|-------------------------------------------|:---------:|:--------:|:-------------:| +| `date_start`| `set_date_start( string $date_start )` | No | Yes | - | +| `date_end` | `set_date_end( string $date_end )` | No | Yes | - | +| `timezone` | `set_timezone( string $timezone )` | No | Yes | - | + +The `date_start` and `date_end` parameters should be in the 'YYYY-MM-DDT00:00:00' format. +The `timezone` parameter can be passed as an offset or as a [timezone name](https://www.php.net/manual/en/timezones.php). + +## Filter + +When using this request, provide the following filter and arguments: + +- Name: `wcpay_get_payment_activity` + +## Example: + +```php +$request = Get_Reporting_Payment_Activity::create(); +$request->set_date_start( $date_start ); +$request->set_date_end( $date_end ); +$request->set_timezone( $timezone ); +$request->send(); +``` + +## Exceptions + +- `Invalid_Request_Parameter_Exception` - Thrown when the provided date or timezone is not in expected format. \ No newline at end of file diff --git a/includes/core/server/request/class-get-reporting-payment-activity.php b/includes/core/server/request/class-get-reporting-payment-activity.php index d9be6cf33eb..f8697a198e8 100644 --- a/includes/core/server/request/class-get-reporting-payment-activity.php +++ b/includes/core/server/request/class-get-reporting-payment-activity.php @@ -16,7 +16,6 @@ */ class Get_Reporting_Payment_Activity extends Request { - const REQUIRED_PARAMS = [ 'date_start', 'date_end', @@ -50,32 +49,52 @@ public function get_method(): string { /** * Sets the start date for the payment activity data. * - * @param string|null $date_start The start date in the format 'YYYY-MM-DDT00:00:00' or null. + * @param string $date_start The start date in the format 'YYYY-MM-DDT00:00:00'. * @return void + * + * @throws Invalid_Request_Parameter_Exception Exception if the date is not in valid format. */ - public function set_date_start( ?string $date_start ) { - // TBD - validation. + public function set_date_start( string $date_start ) { + $this->validate_date( $date_start, 'Y-m-d\TH:i:s' ); $this->set_param( 'date_start', $date_start ); } /** * Sets the end date for the payment activity data. * - * @param string|null $date_end The end date in the format 'YYYY-MM-DDT00:00:00' or null. + * @param string $date_end The end date in the format 'YYYY-MM-DDT00:00:00'. * @return void + * + * @throws Invalid_Request_Parameter_Exception Exception if the date is not in valid format. */ public function set_date_end( string $date_end ) { - // TBD - validation. + $this->validate_date( $date_end, 'Y-m-d\TH:i:s' ); $this->set_param( 'date_end', $date_end ); } /** * Sets the timezone for the reporting data. * - * @param string|null $timezone The timezone to set or null. + * @param string $timezone The timezone to set. * @return void + * + * @throws Invalid_Request_Parameter_Exception Exception if the timezone is not in valid format. */ - public function set_timezone( ?string $timezone ) { - $this->set_param( 'timezone', $timezone ?? 'UTC' ); + public function set_timezone( string $timezone ) { + try { + new \DateTimeZone( $timezone ); + } catch ( \Exception $e ) { + throw new Invalid_Request_Parameter_Exception( + esc_html( + sprintf( + // Translators: %s is a provided timezone. + __( '%s is not a valid timezone.', 'woocommerce-payments' ), + $timezone, + ) + ), + 'wcpay_core_invalid_request_parameter_invalid_timezone' + ); + } + $this->set_param( 'timezone', $timezone ); } }