new Stripe_Gateway() ) ); self::$gateways = array(); foreach ( (array) $list as $gateway ) { if ( $gateway instanceof Gateway ) { self::$gateways[ $gateway->id() ] = $gateway; } } } return self::$gateways; } /** @return Gateway|null */ public static function get( $id ) { $all = self::gateways(); return isset( $all[ $id ] ) ? $all[ $id ] : null; } /** * Gateway used for new bookings, or null when payments are off. * * @return Gateway|null */ public static function active() { $gateway = self::get( (string) apply_filters( 'tableengine_active_gateway', 'stripe' ) ); return $gateway && $gateway->is_enabled() ? $gateway : null; } public static function enabled() { return null !== self::active(); } /** * Gateway that handled a reservation's payment. * * @return Gateway|null */ public static function for_reservation( array $row ) { $id = ! empty( $row['gateway'] ) ? $row['gateway'] : ( ! empty( $row['stripe_intent'] ) ? 'stripe' : '' ); return $id ? self::get( $id ) : null; } /** * Deposit in minor units. 0 means no payment. * * @param array $context Optional date, time, table, plan_id — lets rules price by service or table. */ public static function deposit_for( $party, array $context = array() ) { if ( ! self::enabled() || (int) $party < (int) Settings::get( 'deposit_min_party' ) ) { $minor = 0; } else { $amount = (float) Settings::get( 'deposit_amount' ); if ( 'per_person' === Settings::get( 'deposit_type' ) ) { $amount *= (int) $party; } $minor = self::to_minor( $amount, Settings::get( 'currency' ) ); } return max( 0, (int) apply_filters( 'tableengine_deposit_amount', $minor, (int) $party, $context ) ); } /** * Does this booking go through the payment step? True for any deposit; extensions * can require it for a zero amount too (e.g. saving a card as a no-show guarantee). */ public static function required( $amount, $party, array $context = array() ) { return self::enabled() && (bool) apply_filters( 'tableengine_payment_required', $amount > 0, (int) $amount, (int) $party, $context ); } /** * Refund through whichever gateway took the payment. * * @return mixed|\WP_Error */ public static function refund( array $row, $amount = 0 ) { $gateway = self::for_reservation( $row ); if ( ! $gateway ) { return new \WP_Error( 'tableengine_refund', __( 'No payment to refund.', 'tableengine' ) ); } $result = $gateway->refund( $row, $amount ); if ( ! is_wp_error( $result ) ) { do_action( 'tableengine_payment_refunded', Reservations::get( $row['id'] ), (int) $amount ); } return $result; } public static function is_zero_decimal( $currency ) { return in_array( strtoupper( (string) $currency ), self::ZERO_DECIMAL, true ); } public static function to_minor( $amount, $currency ) { return self::is_zero_decimal( $currency ) ? (int) round( $amount ) : (int) round( $amount * 100 ); } }