php – register_rest_route, Compare purchase codes and send a response

Question

I have a question, I would be grateful if someone could help me
I am working on WordPress plugins, I have 2 files, the first file is Verification which is for users and the second file is the Dashboard which is for admin and now I want to check the user’s purchase code and if it is correct it will show a success message .
I got all the list of purchase codes and compared the user’s purchase code with our list in Dashboardfile, solved this part and now I want to send a response to Verificationfile.
what should i do ?

-VERIFICATION FILE

    public function register_routes(): void {
        register_rest_route(
            $this->namespace,
            "https://wordpress.stackexchange.com/" . $this->rest_base,
            array(
                array(
                    'methods'             => WP_REST_Server::CREATABLE,
                    'callback'            => array( $this, 'dispatch' ),
                    'args'                => $this->get_collection_params(),
                    'permission_callback' => '__return_true',
                ),
                'schema' => array( $this, 'get_public_item_schema' ),
            )
        );
    }

public function dispatch( $request ) {
  // Get Verification Request
  $verifyCode = $request['code'];
  $baseUrl = get_site_url();

  $args = array(
    'headers'     => array(
      'Content-Type' => 'application/json',
    ),
    'timeout'     => 60,
    'blocking'    => true,
    'redirection' => 5,
    'httpversion' => '1.1',
    'sslverify'   => false,
  );

  //all linceses url
  $licenses_url   = $baseUrl . '/wp-json/lmfwc/v2/licenses?consumer_key=ck_050fcef370dca50c1db92ca63d4&consumer_secret=cs_81eb12c82568c5f2b4c16d42b3e19c';
    $response   = wp_remote_get( $licenses_url, $args );

  // Check the response
  if ( 200 <= $res_code || 300 > $res_code ) {
    // Get All Licenses
    $data = json_decode( wp_remote_retrieve_body( $response ) );
    if ( is_object( $data ) ) {
      $allData = $data->data;
      foreach ( $allData as $data ) {
        if ( $verifyCode === $data->licenseKey ) {
          write_log( 'License Active' );
          return true;
        } else {
          write_log( 'Code is Wrong!' );
        }
      }
    } else {
          echo 'An error occurred: ' . wp_remote_retrieve_response_code( $response );
    }
  }
  return callback_for_custom_rest_route( $request );
}

/**
 * Retrieves the params for the token endpoint.
 */
public function get_collection_params(): array {
  $query_params = array();

  $query_params['code'] = array(
    'description'       => __( 'Code.', 'testplugin' ),
    'required'          => true,
    'type'              => 'string',
    'sanitize_callback' => 'sanitize_text_field',
  );

  return $query_params;
}

-DASHBOARD FILE

public static $api = 'http://localhost:10034/wp-json/v1/api/testyplugin/verification/';
public function ajax_handler() {
        $purchase = isset( $_POST['purchase'] ) ? sanitize_text_field( $_POST['purchase'] ) : '';

        // Update purchase code
        update_option( 'testyplugin_purchase_code', $purchase );

        $endpoint = add_query_arg(
            array(
                'code' => $purchase,
            ),
            self::$api
        );

        $options     = array(
            'body'        => wp_json_encode(
                array(
                    'code' => $purchase,
                )
            ),
            'method'      => 'POST',
            'headers'     => array(
                'Content-Type' => 'application/json',
                'Accept' => 'application/json'
            ),
            'timeout'     => 60,
            'redirection' => 5,
            'blocking'    => true,
            'httpversion' => '1.1',
            'sslverify'   => false,
        );
        $remote_test = wp_remote_post( $endpoint , $options );
        $response = json_decode( wp_remote_retrieve_body( $remote_test ) );
        return $response;
    }

0
pooyan golkar 2 months 2023-09-27T22:26:37-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse