Plugin Directory

Changeset 2341519


Ignore:
Timestamp:
07/16/2020 11:44:41 AM (6 years ago)
Author:
sendbox
Message:

made some changes to oauth

Location:
sendbox-shipping/trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • sendbox-shipping/trunk/README.txt

    r2335894 r2341519  
    5959
    6060
    61 <h3>How do i get my API Key </h3>
    62 <p> To get your key, you should go to <a href = "https://sendbox.co/">sendbox</a>, create an account, navigate to
    63 settings on you sendbox dashboard, scroll and find your API Key.</p>
     61<h3>How do i get my access tpken?</h3>
     62<p> To get your access token, you should go to <a href = "https://developers.sendbox.co/">sendbox</a>, create an account.</p>
     63<br/>
     64<p>Proceed to create a new app, enter app name can be your site name, description, and your site url to serve as app url. </p>
     65<p>Your access token and other important keys are generated for you. </p>
     66
    6467
    6568<h3>How do i use the API Key? </h3>
     
    112115<p>fixed some bugs with the api</p>
    113116
     117<b>3.2.1</b>
     118<p>Updated sendbox access token for user authentication</p>
     119
    114120
    115121
  • sendbox-shipping/trunk/admin/class-wooss-admin.php

    r2339399 r2341519  
    189189                    break;
    190190                }
    191             }
     191            } 
    192192
    193193            if(empty($destination_state)){
     
    236236
    237237            $api_call                   = new Wooss_Sendbox_Shipping_API();
    238             $auth_header                = get_option('wooss_basic_auth');
     238            //$auth_header                = get_option('wooss_basic_auth');
     239            $auth_header = $api_call->checkAuth();
    239240            $args                       = array(
    240241                'headers' => array(
     
    243244                ),
    244245            );
    245             $profile_api_url            = 'https://api.sendbox.ng/v1/merchant/profile';
     246            $profile_api_url            = 'https://live.sendbox.co/oauth/profile';
    246247            $profile_data_response_code = $api_call->get_api_response_code($profile_api_url, $args, 'GET');
    247248            $profile_data_response_body = $api_call->get_api_response_body($profile_api_url, $args, 'GET');
     
    299300                ?>
    300301
    301                 <div style="display:none;" id="wooss_shipments_data">
     302                <div id="wooss_shipments_data">
    302303
    303304                    <span style="display:none;"><strong><?php esc_html_e('Origin Details : ', 'wooss'); ?></strong>
     
    334335                    <br />
    335336                    <br />
    336                     <span><strong><?php esc_html_e('Destination Details : ', 'wooss'); ?></strong>
     337                    <span  style="display:none;"><strong><?php esc_html_e('Destination Details : ', 'wooss'); ?></strong>
    337338                        <i>This represents your customer details</i>
    338339                        <br />
     
    409410                   
    410411                    );
     412                    //print_r($delivery_args);
    411413                   
    412414                       
     
    420422                        $quote_api_url =  $api_call->get_sendbox_api_url('delivery_quote');
    421423                        $quote_body    = $api_call->get_api_response_body($quote_api_url, $delivery_args, 'POST');
    422                         //print_r($delivery_args);
     424                       // var_dump($quote_body);
    423425                             
    424426                        $quotes_rates = $quote_body->rates;
     
    633635                }
    634636
     637               
     638
    635639                $courier_selected = sanitize_text_field($data['wooss_selected_courier']);
    636640
     
    688692
    689693                $api_call    = new Wooss_Sendbox_Shipping_API();
    690                 $auth_header = get_option('wooss_basic_auth');
     694                //$auth_header = get_option('wooss_basic_auth');
     695                $auth_header = $api_call->checkAuth();
    691696
    692697                $payload_data_json = wp_json_encode($payload_data);
     
    699704                    'body'    => $payload_data_json,
    700705                );
    701                
     706                //var_dump($shipments_args);
    702707
    703708                $shipments_url = $api_call->get_sendbox_api_url('shipments');
     
    706711                $shipments_details = $api_call->get_api_response_body($shipments_url, $shipments_args, 'POST');
    707712               
    708                
     713                //var_dump($shipments_details);
    709714                if (isset($shipments_details)) {
    710715
  • sendbox-shipping/trunk/includes/assets/js/script.js

    r2335894 r2341519  
    1 (function($) {
     1(function ($) {
    22  "use strict";
    33
     
    2929   * practising this, we should strive to set a better example in our own work.
    3030   */
    31   $(document).ready(function() {
     31  $(document).ready(function () {
    3232    /**
    3333     * Check if we are specially on the sendbox shipping method settings page.
     
    3939    ) {
    4040      //by The Blacker
    41       $(document).on('click dbclick','#woocommerce_wooss_enabled',function(e){
    42         $('div.wooss-shipping-settings').hide();
    43         if ($(this).prop('checked')){
    44            $('div.wooss-shipping-settings').show();
     41      $(document).on("click dbclick", "#woocommerce_wooss_enabled", function (
     42        e
     43      ) {
     44        $("div.wooss-shipping-settings").hide();
     45        if ($(this).prop("checked")) {
     46          $("div.wooss-shipping-settings").show();
    4547        }
    46       });
    47 
    48 
    49 
     48      });
    5049
    5150      var wc_button_save = $("button.button-primary.woocommerce-save-button");
    5251      wc_button_save.show();
    53       var wooss_basic_auth;
     52      //var wooss_basic_auth;
     53      var sendbox_auth_token;
     54      var sendbox_refresh_token;
     55      var sendbox_app_id;
     56      var sendbox_client_secret;
    5457      var wooss_connect_btn = $("button.wooss-connect-sendbox");
    5558      var wooss_errors_message_span = $("span.wooss_errors_pages");
     
    5962       */
    6063
    61       wooss_connect_btn.on("click", function(e) {
     64      wooss_connect_btn.on("click", function (e) {
    6265        e.preventDefault();
    63         wooss_basic_auth = $("input[name='wooss_basic_auth'").val();
    64          function reloadPage() {
     66        //wooss_basic_auth = $("input[name='wooss_basic_auth'").val();
     67        sendbox_auth_token = $("input[name='sendbox_auth_token']").val();
     68        sendbox_refresh_token = $("input[name='sendbox_refresh_token']").val();
     69        sendbox_app_id = $("input[name='sendbox_app_id']").val();
     70        sendbox_client_secret = $("input[name='sendbox_client_secret']").val();
     71
     72        function reloadPage() {
    6573          window.location.reload();
    66          }
     74        }
    6775        wooss_loader.show();
    6876        var data = {
    69           wooss_basic_auth: wooss_basic_auth
     77          //wooss_basic_auth: wooss_basic_auth
     78          sendbox_auth_token: sendbox_auth_token,
     79          sendbox_refresh_token: sendbox_refresh_token,
     80          sendbox_app_id: sendbox_app_id,
     81          sendbox_client_secret: sendbox_client_secret,
    7082        };
    7183        $.post(
     
    7486            action: "connect_to_sendbox",
    7587            data: data,
    76             security: wooss_ajax_object.wooss_ajax_security
     88            security: wooss_ajax_object.wooss_ajax_security,
    7789          },
    78           function(response) {
     90          function (response) {
    7991            wooss_loader.hide();
    8092            if (1 == response) {
    8193              $(
    82                 '<div id="message" class="updated inline"><p><strong>Your API key is valid, this page will be refreshed in 10s.</strong></p></div>'
     94                '<div id="message" class="updated inline"><p><strong>Your access token is valid, this page will be refreshed in 10s.</strong></p></div>'
    8395              ).insertAfter("br.clear");
    8496              setTimeout(reloadPage, 10000);
     
    8698              wooss_errors_message_span
    8799                .append(
    88                   "Invalid API key login to sendbox and get the correct key"
     100                  "Invalid access and refresh token login to sendbox and get the correct tokens"
    89101                )
    90102                .show();
     
    99111       */
    100112      var wooss_button_save = $("button.button-primary.wooss_save_button");
    101       wooss_button_save.on("submit click", function(e) {
     113      wooss_button_save.on("submit click", function (e) {
    102114        e.preventDefault();
    103115        var wooss_state_name = $("select.wooss_state_dropdown").val();
     
    107119        var wooss_pickup_type = $("select.wooss_pickup_type").val();
    108120        var wooss_rates_type = $("select.wooss_rates_type").val();
    109         wooss_basic_auth = $("input[name='wooss_basic_auth'").val();
     121        //wooss_basic_auth = $("input[name='wooss_basic_auth']").val();
     122        sendbox_auth_token = $("input[name='sendbox_auth_token']").val();
     123        sendbox_refresh_token = $("input[name='sendbox_refresh_token']").val();
     124        sendbox_app_id = $("input[name='sendbox_app_id']").val();
     125        sendbox_client_secret = $("input[name='sendbox_client_secret']").val();
    110126        var wooss_enabled = $('input[name="woocommerce_wooss_enabled"]').val();
    111127        var wooss_extra_fees = $("input#wooss_extra_fees").val();
     
    116132          wooss_city: wooss_city,
    117133          wooss_street: wooss_street,
    118           wooss_basic_auth: wooss_basic_auth,
     134          //wooss_basic_auth: wooss_basic_auth,
     135          sendbox_auth_token: sendbox_auth_token,
     136          sendbox_refresh_token: sendbox_refresh_token,
     137          sendbox_app_id: sendbox_app_id,
     138          sendbox_client_secret: sendbox_client_secret,
    119139          wooss_enabled: wooss_enabled,
    120140          wooss_pickup_type: wooss_pickup_type,
    121141          wooss_rates_type: wooss_rates_type,
    122           wooss_extra_fees: wooss_extra_fees
     142          wooss_extra_fees: wooss_extra_fees,
    123143        };
    124144
     
    128148            action: "save_fields_by_ajax",
    129149            data: data,
    130             security: wooss_ajax_object.wooss_ajax_security
     150            security: wooss_ajax_object.wooss_ajax_security,
    131151          },
    132           function(response) {
     152          function (response) {
    133153            if (1 == response) {
    134154              $(
  • sendbox-shipping/trunk/includes/class-wooss-sendbox-shipping-api.php

    r2226186 r2341519  
    7171    {
    7272        if ('delivery_quote' == $url_type) {
    73             $url = 'https://api.sendbox.ng/v1/merchant/shipments/delivery_quote';
     73            $url = 'https://live.sendbox.co/shipping/shipment_delivery_quote';
    7474        } elseif ('countries' == $url_type) {
    7575            $url = 'https://api.sendbox.co/auth/countries?page_by={' . '"per_page"' . ':264}';
     
    7777            $url = 'https://api.sendbox.co/auth/states';
    7878        } elseif ('shipments' == $url_type) {
    79             $url = 'https://api.sendbox.ng/v1/merchant/shipments';
     79            $url = 'https://live.sendbox.co/shipping/shipments';
    8080        } elseif ('item_type' == $url_type) {
    8181            $url = 'https://api.sendbox.ng/v1/item_types';
    8282        } elseif ('profile' == $url_type) {
    83             $url = 'https://api.sendbox.ng/v1/merchant/profile';
     83            $url = 'https://live.sendbox.co/oauth/profile';
    8484        } else {
    8585            $url = '';
     
    8787        return $url;
    8888    }
     89
     90
     91    /**
     92 *
     93 * This function checks sendbox oauth
     94 */
     95
     96public function checkAuth(){
     97
     98    $api_key = get_option('sendbox_auth_token');
     99    $profile_url = "https://live.sendbox.co/oauth/profile";
     100
     101    $profile_res = wp_remote_get( $profile_url ,
     102    array( 'timeout' => 10,
     103   'headers' => array( 'Content-Type' => 'application/json',
     104                      'Authorization'=> $api_key )
     105    ));
     106    $profile_obj = json_decode($profile_res['body']);
     107
     108   if(isset($profile_obj->title)){
     109   
     110   //make a new request to oauth
     111   $s_url = 'https://live.sendbox.co/oauth/access/access_token/refresh?';
     112   $app_id = get_option('sendbox_app_id');
     113   $client_secret = get_option('sendbox_client_secret');
     114   $url_oauth = $s_url.'app_id='.$app_id.'&client_secret='.$client_secret;
     115   $refresh_token = get_option('sendbox_refresh_token');
     116
     117   $oauth_res = wp_remote_get( $url_oauth,
     118    array( 'timeout' => 10,
     119   'headers' => array( 'Content-Type' => 'application/json',
     120                      'Refresh-Token'=> $refresh_token )
     121    ));
     122    $oauth_obj = json_decode($oauth_res['body']);
     123   if(isset($oauth_obj->access_token)){
     124       $new_auth = $oauth_obj->access_token;
     125       update_option('sendbox_auth_token',  $new_auth);
     126   }
     127   if(isset($oauth_obj->refresh_token)){
     128       $new_refresh = $oauth_obj->refresh_token;
     129       update_option('sendbox_refresh_token',  $new_refresh);
     130   }
     131   
     132}
     133
     134else{
     135   $api_key = get_option('sendbox_auth_token');
     136}
     137
     138return $api_key;
     139
     140     
     141}
     142
    89143
    90144
     
    98152        $state = array('Abia', 'Adamawa', 'Akwa Ibom', 'Anambra', 'Bauchi', 'Benue', 'Borno', 'Bayelsa', 'Cross River', 'Delta', 'Ebonyi ', 'Edo', 'Ekiti', 'Enugu ', 'Federal Capital Territory', 'Gombe ', 'Jigawa ', ' Imo ', ' Kaduna', 'Kebbi ', 'Kano', ' Kogi', ' Lagos', 'Katsina', 'Kwara', 'Nasarawa', 'Niger ', 'Ogun', 'Ondo ', 'Rivers', 'Oyo', 'Osun', 'Sokoto', 'Plateau', 'Taraba', 'Yobe', 'Zamfara');
    99153        return $state;
    100     }
    101 }
     154    } 
     155
     156
     157}
     158
     159
  • sendbox-shipping/trunk/includes/class-wooss-shipping-method.php

    r2335894 r2341519  
    115115                    }
    116116
    117                     $auth_header = get_option('wooss_basic_auth');
     117                    //$auth_header = get_option('wooss_basic_auth');
     118                    $auth_header = $api_call->checkAuth();
    118119
    119120                    if (!$auth_header){
     
    176177
    177178                    if (empty($destination_state)) {
    178                         $destination_state = $package['destination']['state'];
     179                        //$destination_state = $package['destination']['state'];
     180                        $destination_state = "London";
    179181                    }
    180182                    if (empty($destination_city)) {
     
    214216                    $delivery_quotes_details = wooss_calculate_shipping($api_call,$payload_array_data,$auth_header);
    215217                   
    216                    
     218                    //var_dump($delivery_quotes_details);
     219                    //die();
    217220                    $wooss_rates_type = get_option('wooss_rates_type');
    218221                    if ("maximum" == $wooss_rates_type && isset($delivery_quotes_details->max_quoted_fee) ) {
     
    225228                   
    226229               
    227                     $destination_country = "United States of America";
     230                    /* $destination_country = "United States of America";
    228231                    $destination_state = "Washington";
    229232                    $destination_city = "New York";
    230                     $destination_postcode = "10001";
     233                    $destination_postcode = "10001"; */
    231234                    $payload_array_data['destination_country'] = $destination_country;
    232235                    $payload_array_data['destination_state'] = $destination_state;
     
    287290    if (isset($_GET['tab']) && $_GET['tab'] == 'shipping' &&  isset($_GET['section']) && $_GET['section'] == 'wooss' && $shipping_methods_enabled == 'yes') {
    288291        $api_call                   = new Wooss_Sendbox_Shipping_API();
    289         $auth_header                = esc_attr(get_option('wooss_basic_auth'));
     292        //$auth_header                = esc_attr(get_option('wooss_basic_auth'));
     293        $auth_header                = esc_attr(get_option('sendbox_auth_token'));
    290294        $args                       = array(
    291295            'headers' => array(
     
    294298            ),
    295299        );
    296         $profile_api_url            = 'https://api.sendbox.ng/v1/merchant/profile';
     300        $profile_api_url            = 'https://live.sendbox.co/oauth/profile';
    297301        $profile_data_response_code = $api_call->get_api_response_code($profile_api_url, $args, 'GET');
    298302        $profile_data_response_body = $api_call->get_api_response_body($profile_api_url, $args, 'GET');
     
    326330            $wooss_country = 'Nigeria';
    327331        }
    328         $wooss_connection_status = get_option('wooss_basic_auth');
     332        //$wooss_connection_status = get_option('wooss_basic_auth');
     333        $wooss_connection_status = get_option('sendbox_auth_token');
    329334        $custom_styles           = '';
    330335        if (null != $wooss_connection_status) {
     
    366371
    367372        <div class="wooss-shipping-settings" >
    368 
    369             <strong><label for="wooss_basic_auth"><?php esc_attr_e('API KEY :', 'wooss'); ?> </label><input type="text" class="wooss-text" placeholder="Basic X0000X0000000000AH" name="wooss_basic_auth" value="<?php esc_attr_e($wooss_basic_auth, 'wooss'); ?>"></strong> <br />
    370             <button type="submit" class="button-primary wooss-connect-sendbox wooss_fields" style="<?php esc_attr_e($custom_styles); ?>"><?php esc_attr_e('Connect to Sendbox', 'wooss'); ?></button><br />
    371 
     373        <!--Make changes and start using the new oauth--->
     374
     375            <!-- <strong><label for="wooss_basic_auth"><?php //esc_attr_e('API KEY :', 'wooss'); ?> </label><input type="text" class="wooss-text" placeholder="Basic X0000X0000000000AH" name="wooss_basic_auth" value="<?php //esc_attr_e($wooss_basic_auth, 'wooss'); ?>"></strong> <br /> -->
     376            <div style="<?php esc_attr_e($custom_styles); ?>">
     377            <strong><label for="sendbox_auth_token"><?php esc_attr_e('Access Token :', 'wooss'); ?> </label><input type="text" class="wooss-text" placeholder="Enter Your Access Token" name="sendbox_auth_token" value="<?php esc_attr_e($sendbox_auth_token, 'wooss'); ?>"></strong> <br />
     378            <strong><label for="sendbox_refresh_token"><?php esc_attr_e('Refresh Token :', 'wooss'); ?> </label><input type="text" class="wooss-text" placeholder="Enter Your Refresh Token" name="sendbox_auth_token" value="<?php esc_attr_e($sendbox_refresh_token, 'wooss'); ?>"></strong> <br />
     379            <strong><label for="sendbox_app_id"><?php esc_attr_e('App ID :', 'wooss'); ?> </label><input type="text" class="wooss-text" placeholder="Enter Your App ID" name="sendbox_app_id" value="<?php esc_attr_e($sendbox_app_id, 'wooss'); ?>"></strong> <br />
     380            <strong><label for="sendbox_client_secret"><?php esc_attr_e('Client Secret :', 'wooss'); ?> </label><input type="text" class="wooss-text" placeholder="Enter Your Client Secret" name="sendbox_app_id" value="<?php esc_attr_e($sendbox_client_secret, 'wooss'); ?>"></strong> <br />
     381            <button type="submit" class="button-primary wooss-connect-sendbox wooss_fields"><?php esc_attr_e('Connect to Sendbox', 'wooss'); ?></button><br />
     382           
     383           </div>
    372384       <div class="wooss_necessary_fields" style="<?php  $display_fields = 'display : none'; if ($wooss_display_fields) {$display_fields = 'display : inline'; echo $display_fields;} else { echo $display_fields; }?>">
    373385                <table style="width:100%">
     
    511523    if (isset($_POST['data'])) {
    512524        $data             = wp_unslash($_POST['data']);
    513         $wooss_basic_auth = $data['wooss_basic_auth'];
     525        //$wooss_basic_auth = $data['wooss_basic_auth'];
     526        $sendbox_auth_token = $data['sendbox_auth_token'];
     527        $sendbox_refresh_token = $data['sendbox_refresh_token'];
     528        $sendbox_client_secret = $data['sendbox_client_secret'];
     529        $sendbox_app_id = $data['sendbox_app_id'];
     530
    514531        $api_call               = new Wooss_Sendbox_Shipping_API();
    515532        $api_url                = $api_call->get_sendbox_api_url('profile');
     
    517534            'headers' => array(
    518535                'Content-Type'  => 'application/json',
    519                 'Authorization' => $wooss_basic_auth,
     536                'Authorization' => $sendbox_auth_token,
     537                //'Authorization' => $wooss_basic_auth,
    520538            ),
    521539        );
     
    524542            $response_code = 1;
    525543            update_option('wooss_connexion_status', $response_code);
    526             update_option('wooss_basic_auth', $wooss_basic_auth);
     544            //update_option('wooss_basic_auth', $wooss_basic_auth);
     545            update_option('sendbox_auth_token', $sendbox_auth_token);
     546            update_option('sendbox_refresh_token', $sendbox_refresh_token);
     547            update_option('sendbox_app_id', $sendbox_app_id);
     548            update_option('sendbox_client_secret', $sendbox_client_secret);
     549
    527550        }
    528551    }
     
    546569        $wooss_city        = sanitize_text_field($data['wooss_city']);
    547570        $wooss_street      = sanitize_text_field($data['wooss_street']);
    548         $wooss_basic_auth  = sanitize_text_field($data['wooss_basic_auth']);
     571        //$wooss_basic_auth  = sanitize_text_field($data['wooss_basic_auth']);
     572        $sendbox_auth_token  = sanitize_text_field($data['sendbox_auth_token']);
     573        $sendbox_refresh_token  = sanitize_text_field($data['sendbox_refresh_token']);
     574        $sendbox_client_secret  = sanitize_text_field($data['sendbox_client_secret']);
     575        $sendbox_app_id  = sanitize_text_field($data['sendbox_app_id']);
     576
    549577        $wooss_pickup_type = sanitize_text_field($data['wooss_pickup_type']);
    550578        $wooss_extra_fees  = sanitize_text_field($data['wooss_extra_fees']);
     
    571599            $operation_success = 1;
    572600        }
    573         if (isset($wooss_basic_auth)) {
     601        /* if (isset($wooss_basic_auth)) {
    574602            update_option('wooss_basic_auth', $wooss_basic_auth);
    575603            $operation_success = 1;
    576         }
     604        }  */
     605
     606        if (isset($sendbox_auth_token)) {
     607            update_option('sendbox_auth_token', $sendbox_auth_token);
     608            $operation_success = 1;
     609        }
     610
     611        if (isset($sendbox_refresh_token)) {
     612            update_option('sendbox_refresh_token', $sendbox_refresh_token);
     613            $operation_success = 1;
     614        }
     615
     616        if (isset($sendbox_client_secret)) {
     617            update_option('sendbox_client_secret', $sendbox_client_secret);
     618            $operation_success = 1;
     619        }
     620
     621        if (isset($sendbox_app_id)) {
     622            update_option('sendbox_app_id', $sendbox_app_id);
     623            $operation_success = 1;
     624        }
     625
    577626        if (isset($wooss_pickup_type)) {
    578627            update_option('wooss_pickup_type', $wooss_pickup_type);
    579628            $operation_success = 1;
    580629        }
    581 
    582630
    583631
     
    719767
    720768    $payload_data_json = json_encode($payload_data);
    721 
     769 
    722770    $delivery_args = array(
    723771        'headers' => array(
     
    727775        'body'    => $payload_data_json,
    728776    );
    729    
     777    //var_dump($delivery_args);
    730778
    731779    $delivery_quotes_url = $api_obj->get_sendbox_api_url('delivery_quote');
    732780    $delivery_quotes_details = $api_obj->get_api_response_body($delivery_quotes_url, $delivery_args, 'POST');
    733781 
    734    
     782    //var_dump($delivery_quotes_details);
    735783    return $delivery_quotes_details;
    736784}
  • sendbox-shipping/trunk/wooss.php

    r2339399 r2341519  
    1616 * Plugin URI:        #
    1717 * Description:       This is a woocommerce plugin that enables you ship from your store in Nigeria to anywhere in the world.
    18  * Version:           3.2.0
     18 * Version:           3.2.1
    1919 * Author:            sendbox
    2020 * Author URI:        https://sendbox.ng/
     
    3535 * Rename this for your plugin and update it as you release new versions.
    3636 */
    37 define('WOOSS_VERSION', '3.2.0');
     37define('WOOSS_VERSION', '3.2.1');
    3838
    3939/**
Note: See TracChangeset for help on using the changeset viewer.