Skip to content

Commit

Permalink
Pass merchant timezone to Reporting Payment Activity API (#8743)
Browse files Browse the repository at this point in the history
Co-authored-by: Jessy <[email protected]>
Co-authored-by: Shendy <[email protected]>
  • Loading branch information
3 people authored May 6, 2024
1 parent 0809d03 commit 1d5b91a
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 14 deletions.
5 changes: 5 additions & 0 deletions changelog/fix-8742-merchant-timezone-payment-activity
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: dev
Comment: Adding some fixes to the Payment Activity component, reporting controller aand associated classes.


7 changes: 4 additions & 3 deletions client/components/payment-activity/payment-activity-data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions client/data/payment-activity/test/hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ describe( 'usePaymentActivityData', () => {
const result = usePaymentActivityData( {
date_start: '2021-01-01',
date_end: '2021-01-31',
timezone: 'UTC',
} );

expect( result ).toEqual( {
Expand Down
3 changes: 2 additions & 1 deletion client/data/payment-activity/test/resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
);
Expand Down
2 changes: 1 addition & 1 deletion client/data/payment-activity/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
class Get_Reporting_Payment_Activity extends Request {


const REQUIRED_PARAMS = [
'date_start',
'date_end',
Expand Down Expand Up @@ -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 );
}
}

0 comments on commit 1d5b91a

Please sign in to comment.