> */ public static function rules( StoreCurrency $currency, ?int $refund_threshold_minor = null ): array { $unit = 10 ** $currency->decimals(); $rules = array( self::rule( 'refund', 'create', array( self::when( 'refund.amount_minor', 'gt', $refund_threshold_minor ?? self::REFUND_THRESHOLD * $unit ) ), 'REQUIRE_APPROVAL' ), array( 'match' => array( 'entity_type' => 'refund', 'action_cat' => 'create', ), 'budget' => array( 'fact' => 'refund.amount_minor', 'max' => self::DAILY_REFUND_BUDGET * $unit, 'window' => self::DAY, ), 'decision' => 'REQUIRE_APPROVAL', ), ); foreach ( array( 'product', 'product_variation' ) as $type ) { $rules[] = self::rule( $type, 'update', array( self::when( 'product.price_change_pct', 'gt', self::PRICE_CHANGE_PCT ) ), 'REQUIRE_APPROVAL' ); $rules[] = self::rule( $type, 'update', array( self::when( 'product.price_change_pct', 'lt', -self::PRICE_CHANGE_PCT ) ), 'REQUIRE_APPROVAL' ); } foreach ( array( 'create', 'update' ) as $cat ) { $rules[] = self::rule( 'coupon', $cat, array( self::when( 'coupon.discount_type', 'eq', 'percent' ), self::when( 'coupon.amount', 'gt', self::COUPON_PERCENT ), ), 'REQUIRE_APPROVAL' ); $rules[] = self::rule( 'coupon', $cat, array( self::when( 'coupon.unlimited', 'eq', true ) ), 'REQUIRE_APPROVAL' ); } $rules[] = self::rule( 'order', 'update', array( self::when( 'order.status_to', 'in', array( 'refunded', 'cancelled' ) ) ), 'REQUIRE_APPROVAL' ); // The classifier marks a note to the customer destructive: it is an // email. $rules[] = array( 'match' => array( 'entity_type' => 'order_note', 'action_cat' => 'create', 'destructive' => true, ), 'decision' => 'REQUIRE_APPROVAL', ); foreach ( array( 'payment_gateway', 'webhook', 'api_key', 'system_tool' ) as $type ) { foreach ( array( 'create', 'update', 'delete' ) as $cat ) { $rules[] = self::rule( $type, $cat, array(), 'DENY' ); } } $rules[] = self::rule( 'customer', 'delete', array(), 'DENY' ); $rules[] = array( 'match' => array( 'entity_type' => 'order', 'action_cat' => 'delete', 'destructive' => true, ), 'decision' => 'DENY', ); return $rules; } /** * A refund threshold fitted to the store: its average order, rounded up * to a figure a person would pick (1, 2 or 5 times a power of ten), in * minor units. Null without orders. * * @param list $order_totals Recent paid orders' totals, minor units. */ public static function suggested_threshold( array $order_totals ): ?int { $totals = array_values( array_filter( $order_totals, static fn( int $total ): bool => $total > 0 ) ); if ( array() === $totals ) { return null; } $average = (int) ceil( array_sum( $totals ) / count( $totals ) ); $power = 10 ** (int) floor( log10( $average ) ); foreach ( array( 1, 2, 5, 10 ) as $step ) { if ( $step * $power >= $average ) { return $step * $power; } } return 10 * $power; // @codeCoverageIgnore } /** * A rule on one entity type and category. * * @param string $type Entity type. * @param string $cat Action category. * @param list> $when Conditions, none for an unconditional rule. * @param string $decision Decision. * @return array */ private static function rule( string $type, string $cat, array $when, string $decision ): array { $rule = array( 'match' => array( 'entity_type' => $type, 'action_cat' => $cat, ), 'decision' => $decision, ); if ( array() !== $when ) { $rule = array( 'match' => $rule['match'], 'when' => $when, 'decision' => $decision, ); } return $rule; } /** * One condition. * * @param string $fact Fact. * @param string $op Operator. * @param mixed $value Value. * @return array{fact: string, op: string, value: mixed} */ private static function when( string $fact, string $op, mixed $value ): array { return array( 'fact' => $fact, 'op' => $op, 'value' => $value, ); } }