Purchase_Gift_Certificate_Handler::handle_request( $submission )

Summary

No summary available.

Source

File: src/BigCommerce/Forms/Purchase_Gift_Certificate_Handler.php

	public function handle_request( $submission ) {

		if ( ! $this->should_handle_request( $submission ) ) {
			return;
		}

		$errors = $this->validate_submission( $submission );

		if ( count( $errors->get_error_codes() ) > 0 ) {

			/**
			 * Triggered when a form has errors that prevent completion.
			 *
			 * @param \WP_Error $errors     The message that will display to the user
			 * @param array     $submission The data submitted to the form
			 */
			do_action( 'bigcommerce/form/error', $errors, $submission );

			return;
		}

		$gift_certificate = $this->get_certificate_data( $submission[ 'bc-gift-purchase' ] );
		$cart             = new Cart( $this->api );


		try {
			$response         = $cart->add_gift_certificate( $gift_certificate );
		} catch ( ApiException $e ) {
			$url = get_permalink( get_option( Pages\Gift_Certificate_Page::NAME ) );
			if ( strpos( (string) $e->getCode(), '4' ) === 0 ) {
				$body = $e->getResponseBody();
				if ( $body && ! empty( $body->title ) ) {
					$message = sprintf( '[%d] %s', $e->getCode(), $body->title );
				} else {
					$message = $e->getMessage();
				}
				$error = new \WP_Error( 'api_error', sprintf(
					__( 'There was an error adding the gift certificate to your cart. Error message: "%s"', 'bigcommerce' ),
					$message
				), [ 'exception' => [ 'message' => $e->getMessage(), 'code' => $e->getCode() ] ] );
			} else {
				$error = new \WP_Error( 'api_error', __( 'There was an error adding the gift certificate to your cart.', 'bigcommerce' ), [ 'exception' => $e ] );
			}
			do_action( 'bigcommerce/form/error', $error, $submission, $url );
			return;
		}


		/**
		 * The message to display when a gift certificate is added to the cart
		 *
		 * @param string $message
		 */
		$message = apply_filters( 'bigcommerce/form/gift_certificate/success_message', __( 'Gift Certificate Created!', 'bigcommerce' ) );

		if ( get_option( Settings\Sections\Cart::OPTION_ENABLE_CART, true ) ) {
			$url = $cart->get_cart_url();
		} else {
			// no cart page, so redirect to bigcommerce for checkout
			$url = $cart->get_checkout_url( $response->getId() );
			wp_redirect( $url, 303 );
			exit();
		}

		/**
		 * Triggered when a form is successfully processed.
		 *
		 * @param string $message    The message that will display to the user
		 * @param array  $submission The data submitted with the form
		 * @param string $url        The URL to redirect the user to
		 */
		do_action( 'bigcommerce/form/success', $message, $submission, $url );
	}


User Contributed Notes

You must log in before being able to contribute a note or feedback.