rules->should_skip( $session ); } /** * Build the step descriptor. * * @param BookingSession $session Session. */ public function descriptor( BookingSession $session ): StepDescriptor { return $this->build_descriptor( $session, [ 'total_minor' => $session->total_minor(), 'currency' => $session->totals()['currency'] ?? 'GBP', 'decision' => $this->rules->decide( $session )->value, ], [ [ 'key' => 'payment_method', 'type' => 'string', 'required' => true, ], ] ); } /** * Validate submitted input. * * @param BookingSession $session Session. * @param array $input Payload. */ public function validate( BookingSession $session, array $input ): ValidationResult { if ( $this->should_skip( $session ) ) { return ValidationResult::ok(); } $method = $this->string_value( $input, 'payment_method' ); if ( null === $method ) { return ValidationResult::invalid( [ 'payment_method' => 'Please choose a payment method.' ] ); } $allowed = [ 'card', 'deposit', 'on_site' ]; if ( ! in_array( $method, $allowed, true ) ) { return ValidationResult::invalid( [ 'payment_method' => 'That payment method is not available.' ] ); } return ValidationResult::ok(); } /** * Apply validated input. * * @param BookingSession $session Session. * @param array $input Payload. */ public function apply( BookingSession $session, array $input ): BookingSession { $method = $this->string_value( $input, 'payment_method' ) ?? 'on_site'; $totals = $session->totals(); $totals['payment_method'] = $method; return $session->with_totals( $totals ); } }