Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Commit

Permalink
Add $order_status validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
brent committed Oct 30, 2024
1 parent ec68f9b commit 7bdf96b
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 1 deletion.
59 changes: 59 additions & 0 deletions src/inc/sift-object-validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,21 @@ class SiftObjectValidator {
'$other',
);

const ORDER_STATUSES = array(
'$approved',
'$canceled',
'$held',
'$fulfilled',
'$returned',
);

const CANCELLATION_REASONS = array(
'$payment_risk',
'$abuse',
'$policy',
'$other',
);

/**
* This is the main validation function.
*
Expand Down Expand Up @@ -1523,4 +1538,48 @@ public static function validate_update_password( $data ) {
}
return true;
}

/**
* Validate the order status event.
*
* @param array $data The event to validate.
*
* @return true
* @throws \Exception If the event is invalid.
*/
public static function validate_order_status( array $data ) {
$validator_map = array(
'$user_id' => array( __CLASS__, 'validate_id' ),
'$order_id' => 'is_string',
'$order_status' => self::ORDER_STATUSES,
'$reason' => self::CANCELLATION_REASONS,
'$source' => array( '$automated', '$manual_review' ),
'$analyst' => 'is_string',
'$webhook_id' => 'is_string',
'$description' => 'is_string',
'$browser' => array( __CLASS__, 'validate_browser' ),
'$app' => array( __CLASS__, 'validate_app' ),
'$brand_name' => 'is_string',
'$site_country' => array( __CLASS__, 'validate_country_code' ),
'$site_domain' => 'is_string',
);

try {
static::validate( $data, $validator_map );
// Required fields for order status: $user_id, $order_id, $order_status
if ( empty( $data['$user_id'] ) ) {
throw new \Exception( 'missing $user_id' );
}
if ( empty( $data['$order_id'] ) ) {
throw new \Exception( 'missing $order_id' );
}
if ( empty( $data['$order_status'] ) ) {
throw new \Exception( 'missing $order_status' );
}
} catch ( \Exception $e ) {
throw new \Exception( 'Invalid $order_status event: ' . esc_html( $e->getMessage() ) );
}

return true;
}
}
13 changes: 12 additions & 1 deletion src/inc/woocommerce-actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,18 @@ public static function add_session_info( string $order_id ) {}
*
* @return void
*/
public static function change_order_status( string $order_id ) {}
public static function change_order_status( string $order_id ) {
//$properties = array();
//
//try {
// SiftObjectValidator::validate_order_status( $properties );
//} catch ( \Exception $e ) {
// wc_get_logger()->error( esc_html( $e->getMessage() ) );
// return;
//}
//
//self::add( '$order_status', $properties );
}

/**
* Adds event for order update
Expand Down
57 changes: 57 additions & 0 deletions tests/SiftApi/Validate_OrderStatus_Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php declare( strict_types = 1 );

// phpcs:disable

namespace SiftApi;

use WPCOMSpecialProjects\SiftDecisions\Sift\SiftObjectValidator;

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

require_once 'SiftObjectValidatorTest.php';

class Validate_OrderStatus_Test extends SiftObjectValidatorTest {
protected static ?string $fixture_name = 'order-status.json';

protected static function validator( $data ) {
return SiftObjectValidator::validate_order_status( $data );
}

public function test_user_id_required() {
static::assert_invalid_argument_exception(
static::modify_data( [ '$user_id' => null ] ),
'missing $user_id'
);
}

public function test_order_id_required() {
static::assert_invalid_argument_exception(
static::modify_data( [ '$order_id' => null ] ),
'missing $order_id'
);
}

public function test_order_status_required() {
static::assert_invalid_argument_exception(
static::modify_data( [ '$order_status' => null ] ),
'missing $order_status'
);
}

public function test_app_browser_set() {
$data = static::load_json();
static::assert_invalid_argument_exception(
$data,
'Cannot have both $app and $browser'
);
}

public function test_site_country() {
static::assert_invalid_argument_exception(
static::modify_data( [ '$site_country' => 'US1' ] ),
'$site_country: must be an ISO 3166 country code'
);
}
}
31 changes: 31 additions & 0 deletions tests/SiftApi/fixtures/order-status.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$user_id" : "billy_jones_301",
"$order_id" : "ORDER-28168441",
"$order_status" : "$canceled",

"$reason" : "$payment_risk",
"$source" : "$manual_review",
"$analyst" : "[email protected]",
"$webhook_id" : "3ff1082a4aea8d0c58e3643ddb7a5bb87ffffeb2492dca33",
"$description" : "Canceling because multiple fraudulent users on device",
"$ip" : "54.208.214.78",


"$browser" : {
"$user_agent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36",
"$accept_language" : "en-US",
"$content_language" : "en-GB"
},

"$app" : {

"$os" : "iOS",
"$os_version" : "10.1.3",
"$device_manufacturer" : "Apple",
"$device_model" : "iPhone 4,2",
"$device_unique_id" : "A3D261E4-DE0A-470B-9E4A-720F3D3D22E6",
"$app_name" : "Calculator",
"$app_version" : "3.2.7",
"$client_language" : "en-US"
}
}

0 comments on commit 7bdf96b

Please sign in to comment.