Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions class/class.midtrans-shipping-api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

if (!defined('MT_PLUGIN_DIR')) {
exit;
}

if (!class_exists('MT_SHIPPR_API')) {

class MT_SHIPPR_API {

private $base_url = 'https://api.cekongkir.id/v2/';
private $token = 'lsS2GUT2L4XHVWOfgI0eW7BzetMfJjIC1xN3KIJkOO4N4qVCX1';
private $timeout = 10;

function request_params( $body = array() ) {
global $woocommerce;
global $wp_version;

$headers['Content-Type'] = 'application/json';
$headers['Authorization'] = 'Bearer '.$this->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;
}
}
}
140 changes: 140 additions & 0 deletions class/class.midtrans-shipping-gosend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php

class Midtrans_Shipping_Gosend extends WC_Shipping_Method {

public function __construct() {
$this->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);
}
}
37 changes: 36 additions & 1 deletion js/admin-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,39 @@
} hideSavecard(false);

});
})(jQuery);
})(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
});
});
9 changes: 9 additions & 0 deletions midtrans-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand All @@ -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' );