Advertisement
timonte

cekout controller

Apr 21st, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 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_home');
  11. $this->load->library('form_validation');
  12. }
  13.  
  14. public function index()
  15. {
  16. $this->template->alfatihmart("checkout");
  17. }
  18.  
  19. public function city()
  20. {
  21.  
  22. $prov = $this->input->post('prov', TRUE);
  23. $curl = curl_init();
  24.  
  25. curl_setopt_array($curl, array(
  26. CURLOPT_URL => "http://api.rajaongkir.com/starter/city?province=$prov",
  27. CURLOPT_RETURNTRANSFER => true,
  28. CURLOPT_ENCODING => "",
  29. CURLOPT_MAXREDIRS => 10,
  30. CURLOPT_TIMEOUT => 30,
  31. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  32. CURLOPT_CUSTOMREQUEST => "GET",
  33. CURLOPT_HTTPHEADER => array(
  34. "key: b03354ea01da154f19e1d300a9497a16"
  35. ),
  36. ));
  37.  
  38. $response = curl_exec($curl);
  39. $err = curl_error($curl);
  40.  
  41. curl_close($curl);
  42.  
  43. if ($err) {
  44. echo "cURL Error #:" . $err;
  45. } else {
  46. $data = json_decode($response, TRUE);
  47.  
  48. echo '<option value="" selected disabled>kota / kabupaten</option>';
  49.  
  50. for ($i=0; $i < count($data['rajaongkir']['results']); $i++) {
  51. echo '<option value="' .$data['rajaongkir']['results'][$i]['city_id'].','.$data['rajaongkir']['results'][$i]['city_name'].'">'.$data['rajaongkir']['results'][$i]['city_name'].'</option>';
  52. }
  53. }
  54. }
  55.  
  56. public function getcost()
  57. {
  58. $asal = 445;
  59. $dest = $this->input->post('dest', TRUE);
  60. $kurir = $this->input->post('kurir', TRUE);
  61. $berat = 0;
  62.  
  63. foreach ($this->cart->contents() as $key) {
  64. $berat += ($key['weight'] * $key['qty']);
  65. }
  66. $curl = curl_init();
  67.  
  68. curl_setopt_array($curl, array(
  69. CURLOPT_URL => "http://api.rajaongkir.com/starter/cost",
  70. CURLOPT_RETURNTRANSFER => true,
  71. CURLOPT_ENCODING => "",
  72. CURLOPT_MAXREDIRS => 10,
  73. CURLOPT_TIMEOUT => 30,
  74. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  75. CURLOPT_CUSTOMREQUEST => "POST",
  76. CURLOPT_POSTFIELDS => "origin=$asal&destination=$dest&weight=$berat&courier=$kurir",
  77. CURLOPT_HTTPHEADER => array(
  78. "content-type: application/x-www-form-urlencoded",
  79. "key: b03354ea01da154f19e1d300a9497a16"
  80. ),
  81. ));
  82.  
  83. $response = curl_exec($curl);
  84. $err = curl_error($curl);
  85.  
  86. curl_close($curl);
  87.  
  88. if ($err) {
  89. echo "cURL Error #:" . $err;
  90. } else {
  91. $data = json_decode($response, TRUE);
  92.  
  93. echo '<option value="" selected disabled>Layanan yang Tersedia</option>';
  94.  
  95. for ($i=0; $i < count($data['rajaongkir']['results']); $i++) {
  96.  
  97.  
  98. for ($l=0; $l < count($data['rajaongkir']['results'][$i]['costs']); $l++) {
  99.  
  100. echo
  101. '<option value="'.$data['rajaongkir']['results'][$i]['costs'][$l][0]['cost']['value'].','.$data['rajaongkir']['results'][$i]['costs'][$l]['service'].'('.$data['rajaongkir']['results'][$i]['costs'][$l]['description'].')">';
  102. echo $data['rajaongkir']['results'][$i]['costs'][$l]['service'].'('.$data['rajaongkir']['results'][$i]['costs'][$l]['description'].')</option>';
  103.  
  104. }
  105. }
  106. }
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement