diff --git a/class/class.midtrans-shipping-api.php b/class/class.midtrans-shipping-api.php new file mode 100644 index 0000000..35d795f --- /dev/null +++ b/class/class.midtrans-shipping-api.php @@ -0,0 +1,76 @@ +token; + $headers['app'] = 'Midtrans SNAP Woocommerce v'.MT_PLUGIN_VERSION; + $headers['platform'] = 'Wordpress v'.$wp_version.' ::: Woocommerce v'.$woocommerce->version; + $headers['siteurl'] = get_bloginfo('url'); + + return array( + 'headers' => $headers, + 'timeout' => $this->timeout, + 'body' => json_encode($body) + ); + } + + public function geocoder( $params = array() ) { + $body['address'] = $params['address']; + $this->response = wp_remote_post( + $this->base_url.'geocoder', + $this->request_params($body) + ); + $this->process_result(); + return $this->result; + } + + public function rates( $params = array() ) { + $body = array( + 'origin' => $params['origin'], + 'destination' => $params['destination'], + 'weight_in_grams' => $params['weight_in_grams'], + 'providers' => array('gosend') + ); + $this->response = wp_remote_post( + $this->base_url.'rates', + $this->request_params($body) + ); + $this->process_result(); + return $this->result; + } + + public function process_result(){ + if ( ! is_wp_error( $this->response ) ) { + $body = json_decode( $this->response['body'], TRUE ); + if ($body['message'] === 'success'){ + $result['status'] = 'success'; + $result['data'] = $body['data']; + } + else { + $result['status'] = 'error'; + $result['message'] = $body['message']; + } + } + else { + $result['status'] = 'error'; + $result['message'] = __('Gagal', 'Midtrans'); + } + $this->result = $result; + } + } +} diff --git a/class/class.midtrans-shipping-gosend.php b/class/class.midtrans-shipping-gosend.php new file mode 100644 index 0000000..0072002 --- /dev/null +++ b/class/class.midtrans-shipping-gosend.php @@ -0,0 +1,140 @@ +id = 'midtrans_shipping_gosend'; + $this->method_title = __( 'Midtrans GoSend', 'Midtrans' ); + $this->method_description = __( 'Midtrans Shipping for GoSend', 'Midtrans' ); + $this->availability = 'including'; + $this->countries = array( + 'ID' + ); + $this->init_form_fields(); + $this->init_settings(); + + add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) ); + add_action( 'admin_enqueue_scripts', array( $this, 'midtrans_admin_scripts' )); + add_filter( 'woocommerce_checkout_fields', array( $this, 'update_fields' ), 10 ); + } + + // set default country, currently only available for ID + public function update_fields($fields) { + $fields['billing']['billing_country'] = array( + 'type' => 'select', + 'label' => __('Country', 'Midtrans'), + 'options' => array('ID' => 'Indonesia') + ); + $fields['shipping']['shipping_country'] = array( + 'type' => 'select', + 'label' => __('Country', 'Midtrans'), + 'options' => array('ID' => 'Indonesia') + ); + return $fields; +} + + function midtrans_admin_scripts() { + wp_enqueue_script( 'admin-midtrans', MT_PLUGIN_DIR . 'js/admin-scripts.js', array('jquery') ); + wp_localize_script( 'admin-midtrans', 'midtrans_params', $this->midtrans_params() ); + } + + function midtrans_params() { + return array( + 'location_selected' => $this->get_option( 'origin' ), + 'ajaxurl' => admin_url( 'admin-ajax.php' ) + ); + } + + function init_form_fields() { + $this->form_fields = array( + 'enabled' => array( + 'title' => __( 'Enable/Disable', 'Midtrans' ), + 'type' => 'checkbox', + 'label' => __( 'Enable Midtrans Shipping for GoSend', 'Midtrans' ), + 'default' => 'no' + ), + 'title' => array( + 'title' => __( 'Title', 'Midtrans' ), + 'type' => 'text', + 'description' => __( 'Title which the user sees during checkout.', 'Midtrans' ), + 'default' => __( 'GoSend via Midtrans', 'Midtrans' ), + ), + 'origin' => array( + 'title' => __( 'Origin Location', 'Midtrans' ), + 'type' => 'select', + 'default' => '', + 'description' => __( 'Select origin location to calculate shipping cost.', 'Midtrans' ), + 'class' => 'gosend_origin' + ), + ); + } + + public static function gosend_origin_location() { + $api = new MT_SHIPPR_API(); + $get = $api->geocoder(array('address' => $_REQUEST['q'])); + $results = array(); + foreach($get['data']['address'] as $row){ + $item = array( + 'address' => $row['address'] + ); + array_push($results, $item); + } + echo json_encode($results); + die(); + } + + public function calculate_shipping( $package = array() ) { + if ($package['destination']['country'] != 'ID') return false; + + $total_weight = 0; + foreach ( $package['contents'] as $values ) { + $_product = $values['data']; + $product_weight = (int) $_product->get_weight(); + $product_quantity = (int) $values['quantity']; + $weight = $product_weight * (int) $product_quantity; + $total_weight += $weight; + } + if ($total_weight == 0) return false; + + $weight_in_grams = wc_get_weight( $total_weight, 'g' ); + $state = $this->state_name_by_code( $package['destination']['state'] ); + $city = $package['destination']['city']; + $address = $package['destination']['address'].' '.$package['destination']['address_2']; + $destination = $address.' '.$city.' '.$state; + + $body = array( + 'origin' => $this->get_option('origin'), + 'destination' => $destination, + 'weight_in_grams' => $weight_in_grams, + 'providers' => array('gosend') + ); + + $api = new MT_SHIPPR_API(); + $rates = $api->rates($body); + if ($rates['status'] === 'error') return false; + + // get gosend services + $services = $rates['data']['rates'][0]['services']; + if (count($services) == 0) return false; + + foreach ($services as $service) { + $rate = array( + 'id' => $this->set_rate_id($service['name']), + 'label' => 'Gosend '.$service['name'], + 'cost' => $service['price'] + ); + $this->add_rate( $rate ); + } + } + + function state_name_by_code( $code ) { + $counties = new WC_Countries(); + $states = $counties->get_states( 'ID' ); + return $states[ $code ]; + } + + function set_rate_id( $name ) { + $name = str_replace(' ', '_', $name); + return 'gosend_'.strtolower($name); + } +} diff --git a/js/admin-scripts.js b/js/admin-scripts.js index 4867fd3..6af283e 100644 --- a/js/admin-scripts.js +++ b/js/admin-scripts.js @@ -22,4 +22,39 @@ } hideSavecard(false); }); -})(jQuery); \ No newline at end of file +})(jQuery); + +jQuery(function($){ + $('.gosend_origin').select2({ + ajax: { + url: ajaxurl, + type: 'post', + dataType: 'json', + delay: 250, + cache: true, + data: function (params) { + return { + q: params.term, + action: 'gosend_origin_location', + }; + }, + processResults: function( data ) { + var options = []; + if ( data ) { + $.each( data, function( index, text ) { + options.push( { id: text.address, text: text.address } ); + }); + } + return { + results: options + }; + } + }, + initSelection: function( element, callback ){ + var selected = []; + selected.push( { id: midtrans_params.location_selected, text: midtrans_params.location_selected } ); + return callback( selected ); + }, + minimumInputLength: 3 + }); +}); diff --git a/midtrans-gateway.php b/midtrans-gateway.php index d3e12ad..9f10037 100644 --- a/midtrans-gateway.php +++ b/midtrans-gateway.php @@ -55,8 +55,16 @@ function midtrans_gateway_init() { require_once dirname( __FILE__ ) . '/class/class.midtrans-gateway-installmentmigs.php'; require_once dirname( __FILE__ ) . '/class/class.midtrans-gateway-migs.php'; require_once dirname( __FILE__ ) . '/class/class.midtrans-gateway-promo.php'; + require_once dirname( __FILE__ ) . '/class/class.midtrans-shipping-api.php'; + require_once dirname( __FILE__ ) . '/class/class.midtrans-shipping-gosend.php'; add_filter( 'woocommerce_payment_gateways', 'add_midtrans_payment_gateway' ); + add_filter( 'woocommerce_shipping_methods', 'add_midtrans_shipping_method' ); +} + +function add_midtrans_shipping_method( $methods ) { + $methods[] = 'Midtrans_Shipping_Gosend'; + return $methods; } function add_midtrans_payment_gateway( $methods ) { @@ -78,3 +86,4 @@ function handle_finish_url_page() } } add_action( 'wp', 'handle_finish_url_page' ); +add_action( 'wp_ajax_gosend_origin_location', 'Midtrans_Shipping_Gosend::gosend_origin_location' );