Advertisement
Guest User

Checkout

a guest
Mar 20th, 2018
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.06 KB | None | 0 0
  1. <?php
  2. defined('BASEPATH') OR exit('No direct script access allowed');
  3.  
  4. class Checkout extends CI_Controller {
  5.  
  6.     function __construct()
  7.     {
  8.         parent::__construct();
  9.         $this->load->library(array('template', 'cart'));
  10.         $this->load->model('app');
  11.     }
  12.  
  13.     public function index()
  14.     {
  15.         if (!$this->session->userdata('user_id') || !$this->cart->contents())
  16.         {
  17.             redirect('home/login');
  18.         }
  19.  
  20.         if ($this->input->post('submit', TRUE) == 'Submit')
  21.       {
  22.             $this->load->library('form_validation');
  23.  
  24.          $this->form_validation->set_rules('prov', 'Provinsi', 'required');
  25.             $this->form_validation->set_rules('kota', 'Kota / Kabupaten', 'required');
  26.             $this->form_validation->set_rules('alamat', 'Alamat', 'required');
  27.             $this->form_validation->set_rules('kd_pos', 'Kode Pos', 'required|numeric|min_length[5]');
  28.             $this->form_validation->set_rules('kurir', 'Kurir', 'required');
  29.             $this->form_validation->set_rules('layanan', 'Layanan', 'required');
  30.             $this->form_validation->set_rules('ongkir', 'Ongkir', 'required|numeric');
  31.             $this->form_validation->set_rules('total', 'Total', 'required|numeric');
  32.  
  33.          if ($this->form_validation->run() == TRUE)
  34.          {
  35.             $get = $this->app->get_where('t_users', ['id_user' => $this->session->userdata('user_id')]);
  36.  
  37.             if ($get->num_rows() > 0)
  38.             {
  39.                //proses
  40.                $this->load->library('email');
  41.  
  42.                $config['charset'] = 'utf-8';
  43.                $config['useragent'] = 'Olshopku';
  44.                $config['protocol'] = 'smtp';
  45.                $config['mailtype'] = 'html';
  46.                $config['smtp_host'] = 'ssl://smtp.gmail.com';
  47.                $config['smtp_port'] = '465';
  48.                $config['smtp_timeout'] = '5';
  49.                $config['smtp_user'] = 'email@example'; //isi dengan email gmail
  50.                $config['smtp_pass'] = ''; //isi dengan password
  51.                $config['crlf'] = "\r\n";
  52.                $config['newline'] = "\r\n";
  53.                $config['wordwrap'] = TRUE;
  54.  
  55.                $this->email->initialize($config);
  56.  
  57.                     $user = $get->row();
  58.  
  59.                $id_order = time();
  60.                     $kota = explode(",", $this->input->post('kota', TRUE));
  61.                     $alamat = $this->input->post('alamat', TRUE);
  62.                     $pos = $this->input->post('kd_pos', TRUE);
  63.                     $kurir = $this->input->post('kurir', TRUE);
  64.                     $layanan = explode(",", $this->input->post('layanan', TRUE));
  65.                     $ongkir = $this->input->post('ongkir', TRUE);
  66.                     $total = $this->input->post('total', TRUE);
  67.                     $tgl_pesan = date("Y-m-d");
  68.                     $bts = date("Y-m-d", mktime(0,0,0, date("m"), date("d") + 3, date("Y")));
  69.  
  70.                     $table = '';
  71.                     $no = 1;
  72.                     foreach ($this->cart->contents() as $carts) {
  73.                         $table .= '<tr><td>'.$no++.'</td><td>'.$carts['name'].'</td><td>'.$carts['qty'].'</td><td style="text-align:right">'.number_format($carts['subtotal'], 0, ',', '.').'</td></tr>';
  74.                     }
  75.  
  76.                $this->email->from('email@example', "Olshopku");
  77.                $this->email->to($user->email);
  78.                $this->email->subject('Pembayaran');
  79.                $this->email->message(
  80.                   'Terima Kasih telah melakukan pemesanan di toko kami, selanjutnya silahkan anda mentransfer uang senilai <b>Rp. '.number_format($total, 0, ',', '.').',-</b> ke no. rekening <b>90xxxx</b> paling lambat '.$bts.' agar pesanan anda bisa kami proses. Detail pembayaran sebagai berikut :<br/><br/>
  81.                         <table border="1" style="width: 80%">
  82.                         <tr><th>#</th><th>Nama Barang</th><th>Jumlah</th><th>Harga</th></tr>
  83.                         '.$table.'
  84.                         <tr><td colspan="3">Ongkos Kirim</td><td style="text-align:right">'.number_format($ongkir, 0, ',', '.').'</td></tr>
  85.                         <tr><td colspan="3">Total</td><td style="text-align:right">'.number_format($total, 0, ',', '.').'</td></tr>
  86.                         </table>
  87.                         '
  88.                );
  89.  
  90.                if ($this->email->send())
  91.                {
  92.                   $data = array(
  93.                             'id_order' => $id_order,
  94.                             'id_user' => $user->id_user,
  95.                             'total' => $total,
  96.                             'tujuan' => $alamat,
  97.                             'pos' => $pos,
  98.                             'kota' => $kota[1],
  99.                             'kurir' => $kurir,
  100.                             'service' => $layanan[1],
  101.                             'tgl_pesan' => $tgl_pesan,
  102.                             'bts_bayar' => $bts,
  103.                             'status' => 'belum'
  104.                         );
  105.  
  106.                         if ($this->app->insert('t_order', $data)) {
  107.  
  108.                             foreach ($this->cart->contents() as $key) {
  109.                                 $detail = [
  110.                                     'id_order' => $id_order,
  111.                                     'id_item' => $key['id'],
  112.                                     'qty' => $key['qty'],
  113.                                     'biaya' => $key['subtotal']
  114.                                 ];
  115.  
  116.                                 $this->app->insert('t_detail_order', $detail);
  117.                             }
  118.  
  119.                             $this->cart->destroy();
  120.  
  121.                             echo '<script type="text/javascript">alert("Silahkan cek email anda untuk detail pembayaran...");window.location.replace("'.base_url().'")</script>';
  122.                         }
  123.                } else {
  124.                   echo '<script type="text/javascript">alert("Email gagal terkirim")</script>';
  125.                }
  126.  
  127.             } else {
  128.                //pesan
  129.                echo '<script type="text/javascript">alert("User tidak dikenali")</script>';
  130.             }
  131.          }
  132.       }
  133.  
  134.         $this->template->olshop('checkout');
  135.     }
  136.  
  137.    public function city()
  138.    {
  139.       $prov = $this->input->post('prov', TRUE);
  140.  
  141.       $curl = curl_init();
  142.  
  143.       curl_setopt_array($curl, array(
  144.         CURLOPT_URL => "http://api.rajaongkir.com/starter/city?province=$prov",
  145.         CURLOPT_RETURNTRANSFER => true,
  146.         CURLOPT_ENCODING => "",
  147.         CURLOPT_MAXREDIRS => 10,
  148.         CURLOPT_TIMEOUT => 30,
  149.         CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  150.         CURLOPT_CUSTOMREQUEST => "GET",
  151.         CURLOPT_HTTPHEADER => array(
  152.           "key: Your API KEY"
  153.         ),
  154.       ));
  155.  
  156.       $response = curl_exec($curl);
  157.       $err = curl_error($curl);
  158.  
  159.       curl_close($curl);
  160.  
  161.       if ($err) {
  162.         echo "cURL Error #:" . $err;
  163.       } else {
  164.          $data = json_decode($response, TRUE);
  165.  
  166.          echo '<option value="" selected disabled>Kota / Kabupaten</option>';
  167.  
  168.          for ($i=0; $i < count($data['rajaongkir']['results']); $i++) {
  169.             echo '<option value="'.$data['rajaongkir']['results'][$i]['city_id'].','.$data['rajaongkir']['results'][$i]['city_name'].'">'.$data['rajaongkir']['results'][$i]['city_name'].'</option>';
  170.          }
  171.       }
  172.    }
  173.  
  174.     public function getcost()
  175.     {
  176.         $asal = 305;
  177.         $dest = $this->input->post('dest', TRUE);
  178.         $kurir = $this->input->post('kurir', TRUE);
  179.         $berat = 0;
  180.  
  181.         foreach ($this->cart->contents() as $key) {
  182.             $berat += ($key['weight'] * $key['qty']);
  183.         }
  184.  
  185.         $curl = curl_init();
  186.  
  187.         curl_setopt_array($curl, array(
  188.           CURLOPT_URL => "http://api.rajaongkir.com/starter/cost",
  189.           CURLOPT_RETURNTRANSFER => true,
  190.           CURLOPT_ENCODING => "",
  191.           CURLOPT_MAXREDIRS => 10,
  192.           CURLOPT_TIMEOUT => 30,
  193.           CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  194.           CURLOPT_CUSTOMREQUEST => "POST",
  195.           CURLOPT_POSTFIELDS => "origin=$asal&destination=$dest&weight=$berat&courier=$kurir",
  196.           CURLOPT_HTTPHEADER => array(
  197.             "content-type: application/x-www-form-urlencoded",
  198.             "key: Your API KEY"
  199.           ),
  200.         ));
  201.  
  202.         $response = curl_exec($curl);
  203.         $err = curl_error($curl);
  204.  
  205.         curl_close($curl);
  206.  
  207.         if ($err) {
  208.           echo "cURL Error #:" . $err;
  209.         } else {
  210.           $data = json_decode($response, TRUE);
  211.  
  212.           echo '<option value="" selected disabled>Layanan yang tersedia</option>';
  213.  
  214.           for ($i=0; $i < count($data['rajaongkir']['results']); $i++) {
  215.  
  216.                 for ($l=0; $l < count($data['rajaongkir']['results'][$i]['costs']); $l++) {
  217.  
  218.                     echo '<option value="'.$data['rajaongkir']['results'][$i]['costs'][$l]['cost'][0]['value'].','.$data['rajaongkir']['results'][$i]['costs'][$l]['service'].'('.$data['rajaongkir']['results'][$i]['costs'][$l]['description'].')">';
  219.                     echo $data['rajaongkir']['results'][$i]['costs'][$l]['service'].'('.$data['rajaongkir']['results'][$i]['costs'][$l]['description'].')</option>';
  220.  
  221.                 }
  222.  
  223.           }
  224.         }
  225.     }
  226.  
  227.     public function cost()
  228.     {
  229.         $biaya = explode(',', $this->input->post('layanan', TRUE));
  230.         $total = $this->cart->total() + $biaya[0];
  231.  
  232.         echo $biaya[0].','.$total;
  233.     }
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement