https://t.me/RX1948
Server : Apache/2.4.18 (Ubuntu)
System : Linux canvaswebdesign 3.13.0-71-generic #114-Ubuntu SMP Tue Dec 1 02:34:22 UTC 2015 x86_64
User : oppastar ( 1041)
PHP Version : 7.0.33-0ubuntu0.16.04.15
Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,
Directory :  /var/www/kamariallee.com/public_html/application/controllers/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /var/www/kamariallee.com/public_html/application/controllers/Payment.php
<?php  if ( ! defined('BASEPATH')) {exit('No direct script access allowed');}

class Payment extends Customer_Controller
{
    private $curTotalShippingFee = 0;

    //Ipay88
    private $ipay88ApiUrl        = null;
    private $ipay88MerchantCode  = null;
    private $ipay88MerchantKey   = null;
    private $ipay88RedirectUrl   = null;
    private $ipay88KamariCallbackUrl  = null;
    private $ipay88KamariRedirectUrl = null;
    private $ipay88KamariResponseUrl = null;

    public function __construct()
    {
        parent::__construct();
        $this->load->model('customer_m');
        $this->load->model('order_m');
        $this->load->model('order_detail_m');
        $this->load->model('configuration_m');
        $this->load->library('cart');
        $this->load->helper('rajaongkir');
        $this->load->library('form_validation');

        $config = $this->db->select(
        'ipay88_url, ipay88_merchantcode, ipay88_merchantkey,
        ipay88_redirect_url, ipay88_kamari_callback_url,
        ipay88_kamari_redirect_url, ipay88_kamari_response_url')
        ->from('configuration')
        ->where('id_configuration', 1)
        ->get()
        ->row_array();
      
        $this->ipay88ApiUrl = $config['ipay88_url'];
        $this->ipay88MerchantCode = $config['ipay88_merchantcode'];
        $this->ipay88MerchantKey = $config['ipay88_merchantkey'];
        $this->ipay88RedirectUrl = $config['ipay88_redirect_url'];
        $this->ipay88KamariCallbackUrl = $config['ipay88_kamari_callback_url'];
        $this->ipay88KamariRedirectUrl = $config['ipay88_kamari_redirect_url'];
        $this->ipay88KamariResponseUrl = $config['ipay88_kamari_response_url'];
    }

    public function index()
    {
        if (!$this->cart->contents())
        {
            redirect('cart');
        }
        else
        {
          $total_amount = $this->getOrderInfo();
          $data['grand_total'] = $total_amount['grand_total'];
          $data['subtotals'] = $total_amount['subtotals'];
          $data['shipping'] = $total_amount['shipping'];
          $data['total_shipping_fee'] = $total_amount['total_shipping_fee'];
        }

        //get SEO
        $this->db->select('website_name, meta_keywords, bank')->from('configuration')->where('id_configuration', 1);
        $website_name = $this->db->get()->row();

        $data['bank_account'] =  $website_name->bank;
        
        $this->data_header['browser_title'] =  ucwords($website_name->website_name) . ' - Checkout';
        $this->data_header['meta_description'] = ucwords($website_name->website_name) . ' - Checkout';
        $this->data_header['meta_keywords'] = $website_name->meta_keywords;
        $this->load->view('template/header', $this->data_header);
        $this->load->view('payment', $data);
        $this->load->view('template/footer', $this->data_footer);
    }

    public function process_payment()
    {
        if (!isset($_POST['process_payment']))
        {
            redirect('payment');
        }

        switch ($this->input->post('payment_option'))
        {
            default:
            case 'manual transfer':
                $data['payment_type'] = 'bank transfer';
                $data['payment_id'] = 0;
                break;
            case 'credit card':
                $data['payment_type'] = 'credit card';
                $data['payment_id'] = 105;
                break;
            case 'BNI VA':
                $data['payment_type'] = 'BNI VA';
                $data['payment_id'] = 83;
                break;
            case 'BRI VA':
                $data['payment_type'] = 'BRI VA';
                $data['payment_id'] = 84;
                break;
            case 'CIMB VA':
                $data['payment_type'] = 'CIMB VA';
                $data['payment_id'] = 86;
                break;
            case 'Mandiri VA':
                $data['payment_type'] = 'Mandiri VA';
                $data['payment_id'] = 119;
                break;
            case 'Permata VA':
                $data['payment_type'] = 'Permata VA';
                $data['payment_id'] = 112;
                break;
            case 'Maybank VA':
                $data['payment_type'] = 'Maybank VA';
                $data['payment_id'] = 9;
                break;
            case 'OVO':
                $data['payment_type'] = 'OVO';
                $data['payment_id'] = 63;
                break;
            case 'Shopee Pay':
                $data['payment_type'] = 'Shopee Pay';
                $data['payment_id'] = 76;
                break;
            case 'Dana':
                $data['payment_type'] = 'Dana';
                $data['payment_id'] = 77;
                break;
            case 'Qris':
                $data['payment_type'] = 'Qris';
                $data['payment_id'] = 78;
                break;
        }

        $this->insertNewOrder($data['payment_type'], $data['payment_id']);

        //send email
        $this->sendEmail($data['payment_type'], $data['payment_id']);

        if($data['payment_id'] == 0)
        {
            //SEO browser title and meta details homepage
            $this->db->select('browser_title, meta_description, meta_keywords')
            ->from('configuration')->where('id_configuration', 1);
            $website_name = $this->db->get()->row();
            $this->data_header['browser_title'] = $website_name->browser_title;
            $this->data_header['meta_description'] = $website_name->meta_description;
            $this->data_header['meta_keywords'] = $website_name->meta_keywords;
            $data['email'] = $this->session->userdata('customer')['customer_email'];

            $this->load->view('template/header', $this->data_header);
            $this->load->view('banktransfer.php', $data);
            $this->load->view('template/footer', $this->data_footer);
            $this->destroySessionData();
        }
        else
        {
            $this->destroySessionData();
            redirect('account/order_history');
        }
    }

    private function sendEmail($payment_type, $payment_id)
    {
        $data = array();
        $data['payment_type'] = $payment_type;
        $data['payment_id'] = $payment_id;

        //----SEND EMAIL TO CUSTOMER
        //get customer name
        $customer_id = (int) $this->session->userdata('customer')['customer_id'];
        $this->db->select('name')->from('customers')->where('id_customers', $customer_id);
        $data['customer_name'] = $this->db->get()->row()->name;

        //get website data
        $this->db->select('logo, from_email, website_name,
        email_smtp_host, email_smtp_port, email_smtp_password, email_smtp')
        ->from('configuration')
        ->where('id_configuration', 1);
        $website_data = $this->db->get()->row();
        $data['logo'] = $website_data->logo;
        $data['website_name'] = $website_data->website_name;

        $data['emails'] = $this->configuration_m->get_emails();
        $data['bank'] = $this->configuration_m->get_bank();
        $data['title'] = 'Payment';

        //get order detail and customer detail
        $data['order'] = $this->order_m->get_order($this->session->userdata('order_id'));
        $data['order_details'] = $this->order_detail_m->get_orders_detail($this->session->userdata('order_id'));

        //get vouchers detail
        if ($this->session->userdata('chosen_voucher_code'))
        {
            $data['chosen_voucher_code'] = $this->session->userdata('chosen_voucher_code');
            $data['chosen_voucher_type'] = $this->session->userdata('chosen_voucher_type');
            $data['chosen_voucher_discount'] = $this->session->userdata('chosen_voucher_discount');
            $data['redeemed_voucher_amount'] = $this->session->userdata('redeemed_voucher_amount');
        }

        //get shipping fee total
        $data['carrier_name'] = $this->session->userdata('carrier_name');
        $data['total_shipping_fee'] = $this->curTotalShippingFee;

        //add tax to email, if exist..
        if($this->session->userdata('tax'))
        {
            $data['tax'] = $this->session->userdata('tax');
        }

        //add point reward to email, if exist..
        if($this->session->userdata('chosen_point'))
        {
            $data['chosen_point'] = $this->session->userdata('chosen_point');
            $data['chosen_point_discount'] = $this->session->userdata('chosen_point_discount');
        }

        $this->load->library('email');
        //get email setting
        $config['protocol'] = 'smtp';
        $config['smtp_host'] = $website_data->email_smtp_host;
        $config['smtp_port'] = $website_data->email_smtp_port;
        $config['smtp_user'] = $website_data->email_smtp;
        $config['smtp_pass'] = $website_data->email_smtp_password;
        $config['mailtype'] = 'html';
        $config['charset'] = 'iso-8859-1';
        $config['wordwrap'] = true;
        $config['newline'] = "\r\n"; //use double quotes to comply with RFC 822 standard
        $this->email->initialize($config);
        $this->email->from($data['emails']->from_email, $data['emails']->website_name);
        $this->email->to($this->session->userdata('customer')['customer_email']);
        $this->email->cc($data['emails']->from_email);
        $this->email->subject('Order Confirmation');
        $email = $this->load->view('email/payment', $data, true);
        $this->email->message($email);
        $this->email->send();
        //----end send email
    }

    private function getOrderInfo()
    {
        //calculate grand total
        $data['grand_total'] = 0;
        $data['subtotals'] = 0;
        $total_weight_gram = 0;
        
        foreach($this->cart->contents() as $item)
        {
            $data['subtotals'] = $data['subtotals'] + $item['subtotal'];

            if(isset($item['options']['greetingcard_price_int']))
            {
                $data['subtotals'] = $data['subtotals'] + $item['options']['greetingcard_price_int'];
            }

            if(isset($item['options']['chosenHeelsTypePrice']))
            {
                $data['subtotals'] = $data['subtotals'] +  ($item['qty'] * $item['options']['chosenHeelsTypePrice']);
            }

            if(isset($item['options']['chosenHeelsHeightPrice']))
            {
                $data['subtotals'] = $data['subtotals'] +  ($item['qty'] * $item['options']['chosenHeelsHeightPrice']);
            }

            if(isset($item['options']['chosenPlatformStylePrice']))
            {
                $data['subtotals'] = $data['subtotals'] +  ($item['qty']
                * $item['options']['chosenPlatformStylePrice']);
            }

            if(isset($item['options']['chosenPlatformHeightPrice']))
            {
                $data['subtotals'] = $data['subtotals'] +  ($item['qty']
                * $item['options']['chosenPlatformHeightPrice']);
            }

            if(isset($item['options']['chosenInitialEngravedPrice']))
            {
                $data['subtotals'] = $data['subtotals'] +  ($item['qty']
                * $item['options']['chosenInitialEngravedPrice']);
            }

            //get product weight
            $this->db->select('weight')->from('product_details')
            ->where('product_id', $item['id'])
            ->where('sku', $item['options']['sku']);
            $item_weight = $this->db->get()->row()->weight * $item['qty'];
            $total_weight_gram = $total_weight_gram + $item_weight;
        }
      
        $id_customer = (int) $this->session->userdata('customer')['customer_id'];

        //get customer shipping address detail
        $data['shipping'] = $this->customer_m->get_shipping($id_customer);

        //calculate shipping fee
        //get active carrier from configuration table..
        // $this->db->select('jne_reg, jne_yes, jne_oke, tiki_eco, tiki_reg,
        // tiki_ons, pos_regular, pos_express, rpx_regular, rpx_retail,
        // shop_delivery, shopdelivery_duration, shopdelivery_fee')
        // ->from('configuration')->where('id_configuration', 1);
        // $data['active_carrier'] = $this->db->get()->row();
        // $data['shopdelivery_duration'] = $data['active_carrier']->shopdelivery_duration;

        //get subdistrict shop ID from configuration table
        $this->db->select('shop_subdistrict_id')->from('configuration')->where('id_configuration', 1);
        $shop_subdistrict_id = (int) $this->db->get()->row()->shop_subdistrict_id;

        //shipping to indonesia
        //shipping to outside indonesia
        // $this->session->set_userdata('carrier', 'jne');
        // $this->session->set_userdata('carrier_name', 'JNE REGULAR');

        if($data['shipping']->shipping_id_province == 6)
        {
            $data['carrier'] = 'jne_yes'; 
        }
        else
        {
            $data['carrier'] = 'jne_regular'; 
        } 

        //get subdistrict destination ID
        $destination_subdistrict_id = (int) $data['shipping']->shipping_id_subdistrict;

        //set shipping
        switch ($data['carrier']) {
          case 'jne_regular':
            $data['total_shipping_fee'] = $this->get_carrier_cost(
              'jne', 'JNE REGULAR', $shop_subdistrict_id, 
              $destination_subdistrict_id, $total_weight_gram, 'REG', 'CTC');
            break;
          case 'jne_yes':
            $data['total_shipping_fee'] = $this->get_carrier_cost(
              'jne', 'JNE YES', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, 'YES', 'CTCYES');
            break;
          case 'jne_oke':
            $data['total_shipping_fee'] = $this->get_carrier_cost(
              'jne', 'JNE OKE', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, 'OKE');
            break; 
          case 'tiki_reg':
            $data['total_shipping_fee'] = $this->get_carrier_cost(
              'tiki', 'TIKI REGULAR', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, 'REG');
            break; 
          case 'tiki_ons':
            $data['total_shipping_fee'] = $this->get_carrier_cost(
              'tiki', 'TIKI ONS', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, 'ONS');
            break; 
          case 'tiki_eco':
            $data['total_shipping_fee'] = $this->get_carrier_cost(
              'tiki', 'TIKI ECO', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, 'ECO');
            break; 
          case 'pos_regular':
            $data['total_shipping_fee'] = $this->get_carrier_cost(
              'pos', 'POS KILAT KHUSUS', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, 
              'Surat Kilat Khusus');
            break; 
          case 'pos_express':
            $data['total_shipping_fee'] = $this->get_carrier_cost(
              'pos', 'POS EXPRESS', $shop_subdistrict_id, 
              $destination_subdistrict_id, $total_weight_gram, 'Express Next Day');
            break; 
          case 'rpx_regular':
            $data['total_shipping_fee'] = $this->get_carrier_cost(
              'rpx', 'RPX REGULAR', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, 'RGP');
            break; 
          case 'rpx_retail':
            $data['total_shipping_fee'] = $this->get_carrier_cost(
              'rpx', 'RPX RETAIL', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, 'REP');
            break; 
          case 'shop_delivery':
            $this->session->set_userdata('carrier', 'shop_delivery');
            $this->session->set_userdata('carrier_name', 'SHOP DELIVERY');
            $data['total_shipping_fee'] = $data['active_carrier']->shopdelivery_fee;
            break;
          default:
            if($this->session->userdata('carrier')) {
              
              if($this->session->userdata('carrier_name') == 'JNE REGULAR') {
                $data['total_shipping_fee'] = $this->get_carrier_cost(
                  'jne', 'JNE REGULAR', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, 'REG', 'CTC');
              } elseif($this->session->userdata('carrier_name') == 'JNE YES') {
                $data['total_shipping_fee'] = $this->get_carrier_cost(
                  'jne', 'JNE YES', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, 'YES', 'CTCYES');
                break;
              } elseif($this->session->userdata('carrier_name') == 'JNE OKE') {
                $data['total_shipping_fee'] = $this->get_carrier_cost(
                  'jne', 'JNE OKE', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, 'OKE');
                break; 
              } elseif($this->session->userdata('carrier_name') == 'TIKI REGULAR') {
                $data['total_shipping_fee'] = $this->get_carrier_cost(
                  'tiki', 'TIKI REGULAR', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, 'REG');
                break; 
              } elseif($this->session->userdata('carrier_name') == 'TIKI ONS') {
                $data['total_shipping_fee'] = $this->get_carrier_cost(
                  'tiki', 'TIKI ONS', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, 'ONS');
                break; 
              } elseif($this->session->userdata('carrier_name') == 'TIKI ECO') {
                $data['total_shipping_fee'] = $this->get_carrier_cost(
                  'tiki', 'TIKI ECO', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, 'ECO');
                break; 
              } elseif($this->session->userdata('carrier_name') == 'POS KILAT KHUSUS') {
                $data['total_shipping_fee'] = $this->get_carrier_cost(
                  'pos', 'POS KILAT KHUSUS', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, 
                  'Surat Kilat Khusus');
                break; 
              } elseif($this->session->userdata('carrier_name') == 'POS EXPRESS') {
                $data['total_shipping_fee'] = $this->get_carrier_cost(
                  'pos', 'POS EXPRESS', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram,
                  'Express Next Day');
                break; 
              } elseif($this->session->userdata('carrier_name') == 'RPX REGULAR') {
                $data['total_shipping_fee'] = $this->get_carrier_cost(
                  'rpx', 'RPX REGULAR', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, 'RGP');
                break; 
              } elseif($this->session->userdata('carrier_name') == 'RPX RETAIL') {
                $data['total_shipping_fee'] = $this->get_carrier_cost(
                  'rpx', 'RPX RETAIL', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, 'REP');
                break; 
              } elseif($this->session->userdata('carrier_name') == 'SHOP DELIVERY') {
                $this->session->set_userdata('carrier', 'shop_delivery');
                $this->session->set_userdata('carrier_name', 'SHOP DELIVERY');
                $data['total_shipping_fee'] = $data['active_carrier']->shopdelivery_fee;
                break; 
              }	

            } else {

              if($data['active_carrier']->jne_reg == 1) {
                $data['total_shipping_fee'] = $this->get_carrier_cost(
                  'jne', 'JNE REGULAR', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, 'REG', 'CTC');
                break;
              } elseif($data['active_carrier']->jne_yes == 1) {
                $data['total_shipping_fee'] = $this->get_carrier_cost(
                  'jne', 'JNE YES', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, 'YES', 'CTCYES');
                break;
              } elseif($data['active_carrier']->jne_oke == 1) {
                $data['total_shipping_fee'] = $this->get_carrier_cost(
                  'jne', 'JNE OKE', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, 'OKE');
                break; 
              } elseif($data['active_carrier']->tiki_reg == 1) {
                $data['total_shipping_fee'] = $this->get_carrier_cost(
                  'tiki', 'TIKI REGULAR', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, 'REG');
                break; 
              } elseif($data['active_carrier']->tiki_ons == 1) {
                $data['total_shipping_fee'] = $this->get_carrier_cost(
                  'tiki', 'TIKI ONS', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, 'ONS');
                break; 
              } elseif($data['active_carrier']->tiki_eco == 1) {
                $data['total_shipping_fee'] = $this->get_carrier_cost(
                  'tiki', 'TIKI ECO', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, 'ECO');
                break; 
              } elseif($data['active_carrier']->pos_regular == 1) {
                $data['total_shipping_fee'] = $this->get_carrier_cost(
                  'pos', 'POS KILAT KHUSUS', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, 'Surat Kilat Khusus');
                break; 
              } elseif($data['active_carrier']->pos_express == 1) {
                $data['total_shipping_fee'] = $this->get_carrier_cost(
                  'pos', 'POS EXPRESS', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, 'Express Next Day');
                break; 
              } elseif($data['active_carrier']->rpx_regular == 1) {
                $data['total_shipping_fee'] = $this->get_carrier_cost(
                  'rpx', 'RPX REGULAR', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, 'RGP');
                break; 
              } elseif($data['active_carrier']->rpx_retail == 1) {
                $data['total_shipping_fee'] = $this->get_carrier_cost(
                  'rpx', 'RPX RETAIL', $shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, 'REP');
                break; 
              } elseif($data['active_carrier']->shop_delivery == 1) {
                $this->session->set_userdata('carrier', 'shop_delivery');
                $this->session->set_userdata('carrier_name', 'SHOP DELIVERY');
                $data['total_shipping_fee'] = $data['active_carrier']->shopdelivery_fee;
                break; 
              }	
            }		
        } 

        //for testing only
        //$data['total_shipping_fee'] = 0;

        //check if product has international custom order, then shipping fee == 0
        $cart = $this->cart->contents();

        $international_custom_shipping = false;

        foreach ($cart as $item) 
        {    
            //get product international_custom_order status
            $this->db->select('international_custom_order')->from('products')->where('id_products', (int) $item['id']);
            $is_international = $this->db->get()->row()->international_custom_order;

            if($is_international == 'yes')
            {
              $international_custom_shipping = true;
              break;
            }
        }

        if($international_custom_shipping)
        {
            $data['total_shipping_fee'] = 0;
        }

        $this->curTotalShippingFee = $data['total_shipping_fee'];

        $data['grand_total'] = $data['subtotals']  + 
        $data['total_shipping_fee'] - $this->session->userdata('chosen_point_discount');

        if($this->session->userdata('chosen_voucher_type') == 'amount')
        {
            $data['grand_total'] = $data['grand_total'] - $this->session->userdata('chosen_voucher_discount');
            $this->session->set_userdata(
              'redeemed_voucher_amount',
              $this->session->userdata('chosen_voucher_discount')
            );
        }
        elseif($this->session->userdata('chosen_voucher_type') == 'percentage')
        {
            $data['grand_total'] = $data['grand_total'] -
            ($data['product_grand_total'] * $this->session->userdata('chosen_voucher_discount')/100);
            $this->session->set_userdata('redeemed_voucher_amount',
            $data['product_grand_total'] * $this->session->userdata('chosen_voucher_discount')/100);
        }
        else
        {
            //voucher type is free shipping
            //do nothing... 
        }

        return $data;
    }

    private function get_carrier_cost($carrier, $carrier_name,
    $shop_subdistrict_id, $destination_subdistrict_id,
    $total_weight_gram, $service_code1, $service_code2 = null)
    {
        $this->session->set_userdata('carrier', $carrier);
        $this->session->set_userdata('carrier_name', $carrier_name);
        
        $rajaongkir_cost = get_rajaongkir_ongkos($shop_subdistrict_id, $destination_subdistrict_id, $total_weight_gram, $carrier); 

        // echo '<pre>';
        // print_r($rajaongkir_cost);
        // echo '</pre>'; 
        // exit;

        //check if weight is zero. If zero, then rajaongkir cannot proceed.
        if($total_weight_gram > 0) {

          //check which key has carrier name
          if($service_code2 != NULL) {
            if(isset($rajaongkir_cost['rajaongkir'])) {
              if(count($rajaongkir_cost['rajaongkir']['results'][0]['costs']) != 0) {
                foreach($rajaongkir_cost['rajaongkir']['results'][0]['costs'] as $key => $result) {
                  if($result['service'] == $service_code1 || $result['service'] == $service_code2) {
                    $total_shipping_fee = $result['cost'][0]['value'];
                    break;
                  } else {
                    $total_shipping_fee = NULL; //service is not available
                  }
                }
              } else {
                $total_shipping_fee = NULL; //service is not available
              }
            }	else {
              $total_shipping_fee = NULL; //service is not available
            }
          } else {
            
            if(count($rajaongkir_cost['rajaongkir']['results'][0]['costs']) != 0) {

              foreach($rajaongkir_cost['rajaongkir']['results'][0]['costs'] as $key => $result) {
                if($result['service'] == $service_code1) {
                  $total_shipping_fee = $result['cost'][0]['value'];
                  break;
                } else {
                  $total_shipping_fee = NULL; //service is not available
                } 
              }
            } else {
              $total_shipping_fee = NULL; //service is not available
            }
          }

        } else {

          //total weight gram is zero
          $total_shipping_fee = NULL; //service is not available
        }
        return $total_shipping_fee;
    }

    private function insertNewOrder($payment_type, $payment_id)
    {
        $order_info = $this->getOrderInfo();
        $grand_total = $order_info['grand_total'];
        $shipping = $order_info['shipping'];
        $total_shipping_fee = $order_info['total_shipping_fee'];

        $this->db->trans_start();

        //insert new order to orders table
        $data = array(
            'customer_id' => (int) $this->session->userdata('customer')['customer_id'],
            'total_amount' => (int) $grand_total,
            'payment_type' => $payment_type,
            'recipient_name' => $shipping->recipient_name,
            'address' => $shipping->shipping_address,
            'country' => 'Indonesia',
            'postcode' => $shipping->shipping_postcode,
            'phone' => $shipping->shipping_phone,
            'order_date' => date('Y-m-d H:i:s'),
            'shipping_type' => $this->session->userdata('carrier_name'),
            'shipping_fee' => $total_shipping_fee,
        );

        //add purchase notes
        if ($this->session->userdata('purchase_notes'))
        {
            $data['purchase_notes'] = $this->session->userdata('purchase_notes');
        }

        //add voucher
        if ($this->session->userdata('chosen_voucher_code')) {
          $data['redeemed_voucher_code'] = $this->session->userdata('chosen_voucher_code');
          $data['redeemed_voucher_amount'] = $this->session->userdata('redeemed_voucher_amount');
        }

        //add tax
        if($this->session->userdata('tax')) {
          $data['ppn'] = $this->session->userdata('tax');
        }

        //add point rewards
        if($this->session->userdata('add_point_reward')) {
          $data['plus_reward'] = $this->session->userdata('add_point_reward');
        }
        
        //minus point rewards
        if($this->session->userdata('minus_point_reward')) {
          $data['minus_reward'] = $this->session->userdata('minus_point_reward');
          $data['minus_reward_amount'] = $this->session->userdata('chosen_point_discount');
        }

        //get district & province
        $this->db->select('province, district, subdistrict')
        ->from('customers')->where('id_customers', (int) $this->session->userdata('customer')['customer_id']);
        $region = $this->db->get()->row();
        $data['district'] = $region->district;
        $data['subdistrict'] = $region->subdistrict;
        $data['province'] = $region->province;

        //insert to orders table
        $data['order_id'] = $this->order_m->save($data, $id = null);

        //put order_id into session, to use for email or payment return page
        $this->session->set_userdata('order_id', $data['order_id']);

        if($this->session->userdata('minus_point_reward'))
        {
          //deduct minus_rewards to customer current point rewards..
          //get customer current point reward
          $this->db->select('current_pointreward')->from('customers')
          ->where('id_customers', (int) $this->session->userdata('customer')['customer_id']);
          $current_point_reward = (int) $this->db->get()->row()->current_pointreward;
          $new_point_reward = $current_point_reward - (int) $data['minus_reward'];

          //add new point back to customers table
          $data = array(
            'current_pointreward' => $new_point_reward
          );
          $this->db->where('id_customers', (int) $this->session->userdata('customer')['customer_id']);
          $this->db->update('customers', $data);
        }

        //insert new order details to order details table
        $cart_items = $this->cart->contents();

        foreach ($cart_items as $cart_item)
        {
            $item_data = array(
                'orders_id' => (int) $data['order_id'],
                'item_id' => (int) $cart_item['id'],
                'item_name' => $cart_item['name'],
                'item_price' => (int) $cart_item['price'],
                'quantity' => (int) $cart_item['qty'],
                'subtotal' => (int) $cart_item['price'] * (int) $cart_item['qty'],
                'sku' => $cart_item['options']['sku'],
                'attributes' => $cart_item['options']['size']
            );

            $attributes = array();
            $attributes['color'] = $cart_item['options']['color'];
            $attributes['size'] = $cart_item['options']['size'];

            if(isset($cart_item['options']['greetingcard_text']))
            {
                $attributes['greetingcard_text'] = $cart_item['options']['greetingcard_text'];
                $attributes['greetingcard_price_int'] = $cart_item['options']['greetingcard_price_int'];
            }

            if(isset($cart_item['options']['chosenHeelsType']))
            {
                $attributes['chosenHeelsType'] = $cart_item['options']['chosenHeelsType'];
                $attributes['chosenHeelsTypePrice'] = $cart_item['options']['chosenHeelsTypePrice'];
            }

            if(isset($cart_item['options']['chosenHeelsHeight']))
            {
                $attributes['chosenHeelsHeight'] = $cart_item['options']['chosenHeelsHeight'];
                $attributes['chosenHeelsHeightPrice'] = $cart_item['options']['chosenHeelsHeightPrice'];
            }

            if(isset($cart_item['options']['chosenPlatformStyle']))
            {
                $attributes['chosenPlatformStyle'] = $cart_item['options']['chosenPlatformStyle'];
                $attributes['chosenPlatformStylePrice'] = $cart_item['options']['chosenPlatformStylePrice'];
            }

            if(isset($cart_item['options']['chosenPlatformHeight']))
            {
                $attributes['chosenPlatformHeight'] = $cart_item['options']['chosenPlatformHeight'];
                $attributes['chosenPlatformHeightPrice'] = $cart_item['options']['chosenPlatformHeightPrice']; 
            }

            if(isset($cart_item['options']['chosenInitialEngraved']))
            {
                $attributes['chosenInitialEngraved'] = $cart_item['options']['chosenInitialEngraved'];
                $attributes['chosenInitialEngravedPrice'] = $cart_item['options']['chosenInitialEngravedPrice'];
                $attributes['chosenInitialEngravedImage'] = $cart_item['options']['chosenInitialEngravedImage'];
            }

            $item_data['attributes'] = json_encode($attributes);
        
            $this->order_detail_m->save($item_data, $id = null);

            //DEDUCT VOUCHERS FROM VOUCHER TABLE
            if ($this->session->userdata('chosen_voucher_code')) {
              
              $this->db->select('qty_ready')->from('vouchers')->where('voucher_code', $this->session->userdata('chosen_voucher_code'));
              $qty_ready = $this->db->get()->row()->qty_ready;

              if ($qty_ready != NULL) {
                $new_qty_ready = $qty_ready - 1;
                $data = array(
                'qty_ready' => $new_qty_ready,
              );

              $this->db->where('voucher_code', $this->session->userdata('chosen_voucher_code'));
              $this->db->update('vouchers', $data); 

              }
            }

            //UPDATE VOUCHER USER TABLE
            //get voucher id
            if ($this->session->userdata('chosen_voucher_code')) {

              $this->db->select('id_vouchers')->from('vouchers')->where('voucher_code', $this->session->userdata('chosen_voucher_code'));
                $voucher_id = (int) $this->db->get()->row()->id_vouchers;

              //check if this voucher already been used before in voucher user table
              $this->db->select('id_voucher_users')->from('voucher_users')->where('voucher_id', $voucher_id)->where('customer_id', (int) $this->session->userdata('customer')['customer_id']);
                $count_voucher = $this->db->get()->num_rows();

              if ($count_voucher == 0) {

                //voucher not exist yet, insert new voucher
                
                $data = array(
                  'voucher_id' => $voucher_id,
                  'customer_id' => (int) $this->session->userdata('customer')['customer_id'],
                  'voucher_used' => 1,
                );
                $this->db->insert('voucher_users', $data); 

              } else {

                //voucher already exist

                //get current used voucher quantity, and add 1
                $this->db->select('id_voucher_users, voucher_used')->from('voucher_users')->where('voucher_id', (int) $voucher_id)->where('customer_id', (int) $this->session->userdata('customer')['customer_id']);

                $voucher_user = $this->db->get()->row();
                
                $new_voucher_used = $voucher_user->voucher_used + 1;

                $data = array(
                  'voucher_used' => (int) $new_voucher_used,	
                );

                $this->db->where('id_voucher_users', $voucher_user->id_voucher_users);
                $this->db->update('voucher_users', $data); 
              }
            }

            //DEDUCT CURRENT STOCK WITH PURCHASE QUANTITY
            //get current stock froms stocks table
            $this->db->select('stock');
            $this->db->from('product_details');
            $this->db->where('product_id', (int) $cart_item['id']);
            $this->db->where('sku', $cart_item['options']['sku']);
            $query = $this->db->get();
            $stock = $query->row();
            $current_stock = (int) $stock->stock;

            $new_item_stock = $current_stock - (int) $cart_item['qty'];

            $stock_data = array(
              'stock' => $new_item_stock,
            );

            //update the product item stock in database
            $this->db->where('product_id', (int) $cart_item['id']);
            $this->db->where('sku', $cart_item['options']['sku']);
            $this->db->update('product_details', $stock_data);
        }

        $this->db->trans_complete();

        if($payment_id != 0)
        {
            $this->ipay88Processpayment($grand_total, $total_shipping_fee,
            $data['order_id'],$cart_items,
            (int) $this->session->userdata('customer')['customer_id'],
            $payment_id, $payment_type);
        }
    }

    //IPAY88 process payment
    private function ipay88Processpayment($amount, $total_shipping_fee, $refNo,
    $products, $customer_id, $payment_id, $payment_type)
    {
        //generate signature
        $signature = hash('sha256', '||' . $this->ipay88MerchantKey .
        '||' . $this->ipay88MerchantCode . '||' . $refNo . '||' . $amount .
        '||' . 'IDR' . '||');

        $itemTransactions = array();
        $sellers = array();

        //add seller data to $sellers
        $seller_arr = array();
        $seller_arr['Id']       = 'kamari';
        $seller_arr['Name']     = 'Kamari Allee';
        $seller_arr['LegalId']  = null;
        $seller_arr['SellerIdNumber']  = null;
        $seller_arr['Email']  = 'info@kamariallee.com';
        $seller_arr['Url']  = 'www.kamariallee.com';
        $seller_arr['address']['FirstName'] = 'Kamari';
        $seller_arr['address']['LastName'] = 'Allee';
        $seller_arr['address']['Address'] = 'Cluster Amerika Latin. Green Lake City';
        $seller_arr['address']['City'] = 'Tangerang';
        $seller_arr['address']['State'] = 'Banten';
        $seller_arr['address']['PostalCode'] = '15147';
        $seller_arr['address']['Phone'] = '081293359527';
        $seller_arr['address']['CountryCode'] = 'IDN';

        array_push($sellers, $seller_arr);

        //add products data to $itemTransactions
        foreach ($products as $cart_item)
        {
            $product_arr = array();
            $product_arr['Id']   = $cart_item['id'];
            $product_arr['Name'] = $cart_item['name'];
            $product_arr['Quantity'] = $cart_item['qty'];
            $product_arr['Amount'] = (int) $cart_item['price'] * $cart_item['qty'];
            $product_arr['ParentType'] = 'SELLER';
            $product_arr['ParentId'] = 'kamari';

            array_push($itemTransactions, $product_arr);

            //add total shipping price to $itemTransactions
            array_push($itemTransactions, array(
                'Id'          => 'shipping-kamari',
                'Name'        => 'Shipping Fee',
                'Quantity'    => 1,
                'Amount'      => (int) $total_shipping_fee,
                'ParentType'  => 'SELLER',
                'ParentId'    => 'kamari'
            ));

            //get customer data
            //need name, phone, address, city, province, postcode (shipping address & billing address)
            $customer = $this->db->select('*')->from ('customers')
            ->where('id_customers', $customer_id)->get()->row_array();    

            $endpoint_url  = $this->ipay88ApiUrl . 'ePayment/WebService/PaymentAPI/Checkout';

            $request_body = array(
                'APIVersion'        => '2.0',
                'MerchantCode'      => $this->ipay88MerchantCode,
                'PaymentId'         => $payment_id,
                'Currency'          => 'IDR',
                'RefNo'             => $refNo,
                'Amount'            => $amount,
                'ProdDesc'          => 'shoes',
                'RequestType'       => 'REDIRECT',
                'UserName'          => ucwords($customer['name']),
                'UserEmail'         => $customer['email'],
                'UserContact'       => $customer['phone'],
                'Remark'            => '',
                'Lang'              => 'iso-8859-1',
                'ResponseURL'       => $this->ipay88KamariResponseUrl,
                'BackendURL'        => $this->ipay88KamariCallbackUrl,
                'Signature'         => $signature,
                'ItemTransactions'  => $itemTransactions,
                'BillingAddress'    => array(
                        'FirstName'     => ucwords($customer['name']),
                        'LastName'      => '',
                        'Address'       => $customer['address'],
                        'City'          => $customer['district'],
                        'State'         => $customer['province'],
                        'PostalCode'    => $customer['postcode'],
                        'Phone'         => $customer['phone'],
                        'CountryCode'   => '1'
                ),
                'Sellers'           => $sellers
            );

            $request_body['ShippingAddress']['FirstName'] = ucwords($customer['shipping_name']);
            $request_body['ShippingAddress']['LastName'] = '';
            $request_body['ShippingAddress']['Address'] = $customer['shipping_address'];
            $request_body['ShippingAddress']['City'] = $customer['shipping_district'];
            $request_body['ShippingAddress']['State'] = $customer['shipping_province'];
            $request_body['ShippingAddress']['PostalCode'] = $customer['shipping_postcode'];
            $request_body['ShippingAddress']['Phone'] = $customer['shipping_phone'];
            $request_body['ShippingAddress']['CountryCode'] = '1';

            $method = 'POST';
            $headers = array();
            $headers[] = 'Content-Type: application/json';

            //store request log
            $request_data = array(
              'ipay88_request_log' => json_encode(array(
                  'headers' => $headers,
                  'body'    => $request_body
              )),
            );
            $this->db->where('id_orders', $refNo);
            $this->db->update('orders', $request_data);
            
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $endpoint_url );
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_ENCODING, '');
            curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
            curl_setopt($ch, CURLOPT_TIMEOUT, 0);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($request_body));
            $result = curl_exec($ch);

            curl_close($ch);

            $response_data = json_decode($result);
            $response_data->redirect_url = $this->ipay88KamariRedirectUrl . '?url='
            . $this->ipay88RedirectUrl . '&signature=' . $response_data->Signature .
            '&checkoutid=' . $response_data->CheckoutID;

            //store response log
            $update_data = array(
                'ipay88_response_log' => $result
            );
            $this->db->where('id_orders', $refNo);
            $this->db->update('orders', $update_data);

            //send email
            $this->sendEmail($payment_type, $payment_id);

            redirect($response_data->redirect_url);
        }
    }

    private function destroySessionData()
    {
        //DESTROY CART AND UNSET SOME SESSION, BUT NOT CUSTOMER SESSION
        $this->cart->destroy();
        $this->session->unset_userdata('grand_total');
        $this->session->unset_userdata('recipient_name');
        $this->session->unset_userdata('address');
        $this->session->unset_userdata('id_district');
        $this->session->unset_userdata('id_subdistrict');
        $this->session->unset_userdata('id_province');
        $this->session->unset_userdata('district');
        $this->session->unset_userdata('subdistrict');
        $this->session->unset_userdata('province');
        $this->session->unset_userdata('country');
        $this->session->unset_userdata('postcode');
        $this->session->unset_userdata('phone');
        $this->session->unset_userdata('order_id');
        $this->session->unset_userdata('is_from_cart');
        $this->session->unset_userdata('chosen_voucher_type');
        $this->session->unset_userdata('chosen_voucher_discount');
        $this->session->unset_userdata('chosen_voucher_code');
        $this->session->unset_userdata('total_shipping_fee');
        $this->session->unset_userdata('carrier');
        $this->session->unset_userdata('carrier_name');
        $this->session->unset_userdata('add_point_reward');
        $this->session->unset_userdata('minus_point_reward');
        $this->session->unset_userdata('chosen_point');
        $this->session->unset_userdata('chosen_point_discount');
        $this->session->unset_userdata('chosen_payment_type');
        $this->session->unset_userdata('redeemed_voucher_amount');
        $this->session->unset_userdata('tax');
        $this->session->unset_userdata('purchase_notes');
    }
}

https://t.me/RX1948 - 2025