<?php








namespace Amazon\Affiliate\Admin;





/**


 * Class LicenseActivation


 *


 * @package Amazon\Affiliate\Admin


 */


class LicenseActivation {





    /**


     * License activation handler


     */


    public function license_activation() {


        $url             = $_SERVER['HTTP_HOST'];


        $purchase_code   = sanitize_text_field( $_POST['purchase_code'] );


        $response        = wp_remote_get( "https://api.affiliateproamazon.com/api/envato?product_code={$purchase_code}&url={$url}" );


        $response_body   = wp_remote_retrieve_body( $response );


        $response_decode = json_decode( $response_body );





        $info = array();





        if ( strtolower( $response_decode->status ) === strtolower( 'success' ) ) {


            update_option( 'ams_activated_status', 'success' );


            update_option( 'ams_activated_license', $purchase_code );


            update_option( 'ams_activated_site', $url );





            $info['status'] = 'success';


            $info['license_status'] = '<span class="wca-success">' . esc_html__( 'Activated', 'ams-wc-amazon' ) . '</span>';


            $info['massage']        = "<p class='wca-success'> ". esc_html( $response_decode->massage ) ." </p>";


        } else {


            update_option( 'ams_activated_status', 'failed' );





            $info['license_status'] = '<span class="wca-warning">' . esc_html__( 'Not Activated', 'ams-wc-amazon' ) . '</span>';


            $info['massage']        = "<p class='wca-warning'>". esc_html( $response_decode->massage ) ."</p>";


        }





        echo wp_json_encode( $info );


        wp_die();


    }





    /**


     * Deactivated license plugin


     */


    public function deactivated_license_plugin() {


        // Check for nonce security.


        $nonce = sanitize_text_field( $_POST['nonce'] );





        if ( ! wp_verify_nonce( $nonce, 'ams_de_activated' ) ) {


            echo esc_html__( 'Busted!', 'ams-wc-amazon' );


            wp_die();


        }





        $purchase_code   = get_option( 'ams_activated_license' );


        $response        = wp_remote_get( "https://api.affiliateproamazon.com/api/license-delete?product_code={$purchase_code}&url={$url}" );


        $response_body   = wp_remote_retrieve_body( $response );


        $response_decode = json_decode( $response_body );





        if ( strtolower( $response_decode->status ) === strtolower( 'success' ) ) {


            update_option( 'ams_activated_status', 'failed' );


            update_option( 'ams_activated_license', '' );





            $info['license_status'] = '<span class="wca-warning">' . esc_html__( 'Not Activated', 'ams-wc-amazon' ) . '</span>';


        }


        echo wp_json_encode( $info );


        wp_die();


    }


}