orders = $orders ?? Orders::reader(); $this->clock = $clock ?? static fn(): int => time(); } /** * The facts this provider declares. * * @param StoreCurrency $currency The store's money. * @return array> */ public static function declarations( StoreCurrency $currency ): array { return array( 'refund.amount_minor' => Orders::money( $currency, 'Refund amount' ), 'refund.gateway' => array( 'type' => 'string', 'label' => 'Refund gateway', ), 'refund.via_gateway' => array( 'type' => 'bool', 'label' => 'Refund returns money through the gateway', ), 'refund.pct_of_order' => array( 'type' => 'number', 'label' => 'Refund as a percentage of the order total', ), 'refund.order_age_days' => array( 'type' => 'number', 'label' => 'Days since the order was paid', ), 'refund.is_full' => array( 'type' => 'bool', 'label' => 'Refund of everything left to refund', ), ); } /** * Facts about a refund request. * * @param ActionDescriptor $action The classified action. * @return array */ public function __invoke( ActionDescriptor $action ): array { $order = 'create' === $action->action_cat && null !== $action->entity_id ? ( $this->orders )( $action->entity_id ) : null; if ( null === $order ) { return array(); } $decimals = $this->currency->decimals(); // The refund being created is known only at the effect hook, from // WooCommerce's own object (RefundGuard); a request body naming one // is never trusted. $in_flight = 'unclassified' === $action->source ? absint( $action->input['refund_id'] ?? 0 ) : 0; $remainder = Orders::refundable( $order, $in_flight, $decimals ); $requested = self::requested( $action->input, $decimals ); $amount = false === $requested ? null : ( $requested ?? $remainder ); if ( null !== $amount ) { $amount = max( 0, null === $remainder ? $amount : min( $amount, $remainder ) ); } if ( null !== $amount && 'unclassified' !== $action->source ) { DoorRefunds::record( (int) $action->entity_id, $amount ); } $facts = array( 'refund.via_gateway' => ! array_key_exists( 'api_refund', $action->input ) || ! is_bool( $action->input['api_refund'] ) || $action->input['api_refund'], ); $base = null === $amount ? null : $this->currency->to_base( $amount, $order ); if ( null !== $base ) { $facts['refund.amount_minor'] = $base; } if ( null !== $amount && null !== $remainder ) { $facts['refund.is_full'] = $amount === $remainder; } $total = Money::to_minor( (string) $order->get_total(), $decimals ); if ( null !== $amount && null !== $total && $total > 0 ) { $facts['refund.pct_of_order'] = round( $amount * 100 / $total, 2 ); } $gateway = (string) $order->get_payment_method(); if ( '' !== $gateway ) { $facts['refund.gateway'] = substr( $gateway, 0, 1024 ); } $since = $order->get_date_paid() ?? $order->get_date_created(); if ( null !== $since ) { $facts['refund.order_age_days'] = intdiv( max( 0, ( $this->clock )() - $since->getTimestamp() ), 86400 ); } return $facts; } /** * The amount the request names, in the order's minor units; null when * the store computes it; false when the request names one that cannot * be read, which is unknown rather than the most it could be. * * Every field that can carry the amount counts - `amount` (v1 to v3), * `total` (v4, where it overrides the line items) - and the largest * wins, so no field the request names can make the refund look smaller * than the one WooCommerce makes. * * @param array $input Request body. * @param int $decimals Minor-unit exponent. */ private static function requested( array $input, int $decimals ): int|false|null { $named = array(); foreach ( array( 'amount', 'total' ) as $field ) { if ( isset( $input[ $field ] ) ) { $named[] = Money::to_minor( $input[ $field ], $decimals, true ); } } if ( in_array( null, $named, true ) ) { return false; } $items = self::line_items( $input, $decimals ); if ( array() !== $named ) { return max( array_merge( $named, null === $items ? array() : array( $items ) ) ); } return $items; } /** * The line items' totals and taxes, summed as WooCommerce sums them; * null when the store computes the amount itself. * * @param array $input Request body. * @param int $decimals Minor-unit exponent. */ private static function line_items( array $input, int $decimals ): ?int { if ( WcRouteClassifier::truthy( $input['compute_totals'] ?? null ) || ! isset( $input['line_items'] ) || ! is_array( $input['line_items'] ) || array() === $input['line_items'] ) { return null; } $sum = 0; foreach ( $input['line_items'] as $item ) { $total = is_array( $item ) && isset( $item['refund_total'] ) ? Money::to_minor( $item['refund_total'], $decimals, true ) : null; if ( null === $total ) { return null; } $sum += $total; foreach ( is_array( $item['refund_tax'] ?? null ) ? $item['refund_tax'] : array() as $tax ) { $tax_total = Money::to_minor( is_array( $tax ) ? ( $tax['refund_total'] ?? null ) : $tax, $decimals, true ); if ( null === $tax_total ) { return null; } $sum += $tax_total; } } return $sum; } }