Account_Status::handle_account_status_request()
Summary
Handle the ajax request to update the store status
Return
(void)
Source
File: src/BigCommerce/Merchant/Account_Status.php
public function handle_account_status_request() { $this->validate_ajax_nonce( $_REQUEST ); $store = get_option( Onboarding_Api::STORE_ID, 0 ); try { $response = $this->onboarding->status( $store ); } catch ( \RuntimeException $e ) { wp_send_json_error( [ 'code' => 'missing_credentials', 'message' => $e->getMessage(), ], 400 ); exit(); } $response_code = (int) wp_remote_retrieve_response_code( $response ); $response_body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( $response_code >= 400 && $response_code < 500 ) { delete_option( Onboarding_Api::ACCOUNT_ID ); delete_option( Onboarding_Api::STORE_ID ); add_settings_error( self::STATUS_AJAX, $response_code, __( 'We encountered an unexpected error connecting your account. Please try again.', 'bigcommerce' ) ); set_transient( 'settings_errors', get_settings_errors(), 30 ); wp_send_json_error( [ 'code' => 'not_found', 'message' => __( 'There was an error locating your account. Please refresh this page and try connecting again.', 'bigcommerce' ), 'data' => wp_remote_retrieve_body( $response ), 'response_code' => $response_code, /** * This filter is documented in src/BigCommerce/Merchant/Connect_Account.php. */ 'redirect' => apply_filters( 'bigcommerce/onboarding/error_redirect', admin_url() ), ], 200 ); exit(); } if ( $response_code < 200 || $response_code >= 400 || empty( $response_body ) || empty( $response_body[ 'status' ] ) ) { wp_send_json_error( [ 'code' => 'bad_gateway', 'message' => __( 'There was an error retrieving your account status.', 'bigcommerce' ), 'data' => wp_remote_retrieve_body( $response ), 'response_code' => $response_code, ], 502 ); exit(); } switch ( $response_body[ 'status' ] ) { case 'provisioning': wp_send_json_success( [ 'status' => 'processing', 'message' => __( 'Still working...', 'bigcommerce' ), ] ); exit(); case 'temporary': wp_send_json_success( [ 'popup' => esc_url_raw( $this->onboarding->installation_url( $store ) ), ] ); exit(); case 'active': $this->save_store_hash( $response_body[ 'store_hash' ] ); wp_send_json_success( [ 'popup' => esc_url_raw( $this->onboarding->installation_url( $store ) ), ] ); exit(); case 'authenticated': if ( empty( $response_body[ 'client_id' ] ) || empty( $response_body[ 'store_hash' ] ) || empty( $response_body[ 'oauth_token' ] ) ) { wp_send_json_error( [ 'code' => 'bad_gateway', 'message' => __( 'There was an error retrieving your account status.', 'bigcommerce' ), 'data' => wp_remote_retrieve_body( $response ), 'response_code' => $response_code, ], 502 ); exit(); } $this->save_store_hash( $response_body[ 'store_hash' ] ); $this->save_auth_credentials( $response_body[ 'client_id' ], $response_body[ 'oauth_token' ] ); /** * This filter is documented in src/BigCommerce/Merchant/Connect_Account.php. */ $redirect_url = apply_filters( 'bigcommerce/onboarding/success_redirect', admin_url() ); wp_send_json_success( [ 'redirect' => $redirect_url, ] ); exit(); default: wp_send_json_error( [ 'code' => 'bad_gateway', 'message' => __( 'Invalid response from the API server.', 'bigcommerce' ), ], 502 ); exit(); } }