|
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/angkasapuraretail.com/public_html/application/controllers/ |
Upload File : |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Midtrans extends Public_controller {
private $id_order = NULL;
function __construct() {
parent::__construct();
$this->load->model('order_m');
}
//when payment is success
public function receive_veritrans_notification() {
//Midtrans IP address allowed only
/* $allowlist = array(
'103.208.23.0/24',
'182.253.221.152/32',
'103.58.103.177'
);
if(!in_array($_SERVER['REMOTE_ADDR'], $allowlist)){
show_404();
} */
require_once APPPATH . 'third_party/Veritrans.php';
$this->db->select('veritrans_server_key, veritrans_sandbox_server_key, veritrans_production_mode')->from('configuration')->where('id_configuration', 1);
$veritrans = $this->db->get()->row();
$production_mode = $veritrans->veritrans_production_mode;
if($production_mode == 'true') {
Veritrans_Config::$isProduction = true;
Veritrans_Config::$serverKey = $veritrans->veritrans_server_key;
} else {
Veritrans_Config::$isProduction = false;
Veritrans_Config::$serverKey = $veritrans->veritrans_sandbox_server_key;
}
$notif = new Veritrans_Notification();
$transaction = $notif->transaction_status;
$type = $notif->payment_type;
$order_id_array = explode('-', $notif->order_id);
$order_id = (int) trim($order_id_array[0]);
$this->id_order = $order_id;
$fraud = $notif->fraud_status;
$data = array(
'log' => serialize($notif),
'order_id' => $order_id
);
if($notif->va_numbers[0]->va_number) {
$data['va_number'] = $notif->va_numbers[0]->va_number;
} elseif($notif->permata_va_number) {
$data['va_number'] = $notif->permata_va_number;
}
$this->db->insert('midtrans_log', $data);
//insert va_number into orders table
if($notif->va_numbers[0]->va_number || $notif->permata_va_number) {
if($notif->va_numbers[0]->va_number) {
$va_data['va_number'] = $notif->va_numbers[0]->va_number;
$va_data['payment_method'] = 'Bank Transfer BCA';
} elseif($notif->permata_va_number) {
$va_data['va_number'] = $notif->permata_va_number;
$va_data['payment_method'] = 'Bank Transfer Permata';
}
$this->db->where('id_orders', $order_id);
$this->db->update('orders', $va_data);
}
switch($transaction) {
case 'capture':
// For credit card transaction, we need to check whether transaction is challenge by FDS or not
if ($type == 'credit_card') {
if($fraud == 'challenge') {
// TODO set payment status in merchant's database to 'Challenge by FDS'
// TODO merchant should decide whether this transaction is authorized or not in MAP
$data = array(
'payment_status_message' => ucwords($transaction) . '. Fraud Status:' . $fraud,
'payment_method' => $type
);
$this->db->where('id_orders', (int) $order_id);
$this->db->update('orders', $data);
echo "Transaction order_id: " . $order_id ." is challenged by FDS";
} else {
//Fraud status accept and transaction capture. Means payment is success
//check payment status must not be 4 (process) or 5 (delivered)
$this->db->select('payment_status')->from('orders')->where('id_orders', (int) $order_id);
$current_payment_status = $this->db->get()->row()->payment_status;
if($current_payment_status != 4 && $current_payment_status != 5) {
// TODO set payment status in merchant's database to 'Success'
$data = array(
'payment_status' => 3, //paid
'payment_status_message' => $transaction,
'payment_confirm' => 1,
'payment_date' => $notif->transaction_time,
'payment_method' => $type,
'payment_type' => 'midtrans'
);
$this->db->where('id_orders', (int) $order_id);
$this->db->update('orders', $data);
//prevent adding point reward twice. Get midtrans_midtrans_already_process_status
$this->db->select('midtrans_process_finish')->from('orders')->where('id_orders', $order_id);
$midtrans_process_finish = $this->db->get()->row()->midtrans_process_finish;
if($midtrans_process_finish == 'no') {
//update current point rewards
//get customer_id
$this->db->select('customer_id')->from('orders')->where('id_orders', $order_id);
$customer_id = (int) $this->db->get()->row()->customer_id;
//get customer current point
$this->db->select('current_pointreward')->from('customers')->where('id_customers', $customer_id);
$current_point = (int) $this->db->get()->row()->current_pointreward;
//get add point from order
$this->db->select('plus_reward')->from('orders')->where('id_orders', $order_id);
$rewards = $this->db->get()->row();
$plus_point = (int) $rewards->plus_reward;
$updated_point = $current_point + $plus_point;
//update point reward
$data = array(
'current_pointreward' => $updated_point
);
$this->db->where('id_customers', $customer_id);
$this->db->update('customers', $data);
//change status of midtrans_process_finish to yes
$order_data = array(
'midtrans_process_finish' => 'yes'
);
$this->db->where('id_orders', $order_id);
$this->db->update('orders', $order_data);
//----SEND EMAIL TO CUSTOMER
//get customer data
$customer_id = $this->db->select('customer_id')->from('orders')->where('id_orders',$order_id)->get()->row()->customer_id;
$this->db->select('name,phone,type, email')->from('customers')->where('id_customers', $customer_id);
$data['customer_name'] = $this->db->get()->row();
//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;
//get order data
$this->db->select('*')->from('orders')->where('id_orders', $order_id);
$order = $this->db->get()->row();
$this->load->model('configuration_m');
$this->load->model('order_m');
$this->load->model('order_detail_m');
$data['emails'] = $this->configuration_m->get_emails();
$data['title'] = 'Order Confirmation';
//get order detail and customer detail
$data['order'] = $this->order_m->get_order($order_id);
$data['order_details'] = $this->order_detail_m->get_orders_detail($order_id);
//get vouchers detail
if($order->redeemed_voucher_code != NULL) {
$data['chosen_voucher_code'] = $order->redeemed_voucher_code;
$data['chosen_voucher_type'] = $order->redeemed_voucher_type;
$data['chosen_voucher_discount'] = $order->redeemed_voucher_value;
$data['redeemed_voucher_amount'] = $order->redeemed_voucher_amount;
}
//get shipping fee total
$data['carrier_name'] = $order->shipping_type;
$data['total_shipping_fee'] = $order->shipping_fee;
//add tax to email, if exist..
if($order->ppn > 0) {
$data['tax'] = $order->ppn;
}
//add point reward to email, if exist..
if($order->minus_reward > 0) {
$data['chosen_point'] = $order->minus_reward;
$data['chosen_point_discount'] = $order->minus_reward_amount;
}
$this->load->library('email');
//get email setting
$config['protocol'] = 'smtp';
$config['smtp_crypto'] = 'tls';
$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'] = 'utf-8';
$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($data['customer_name']->email);
$this->email->cc($data['emails']->from_email);
$this->email->subject('Order Confirmation');
$email = $this->load->view('email/indonesian/bank_transfer_indo', $data, TRUE);
$this->email->message($email);
$this->email->send();
//----end send email
}
echo "Transaction order_id: " . $order_id ." successfully captured using " . $type;
}
}
}
break;
case 'settlement':
if ($type != 'credit_card') {
//check payment status must not be 4 (process) or 5 (delivered)
$this->db->select('payment_status')->from('orders')->where('id_orders', (int) $order_id);
$current_payment_status = $this->db->get()->row()->payment_status;
if($current_payment_status != 4 && $current_payment_status != 5) {
$data = array(
'payment_status' => 3, //paid
'payment_status_message' => $transaction,
'payment_confirm' => 1,
'payment_date' => $notif->transaction_time,
'payment_type' => 'midtrans',
);
$this->db->where('id_orders', (int) $order_id);
$this->db->update('orders', $data);
//update current point rewards
//get customer_id
$this->db->select('customer_id')->from('orders')->where('id_orders', $order_id);
$customer_id = (int) $this->db->get()->row()->customer_id;
//get customer current point
$this->db->select('current_pointreward')->from('customers')->where('id_customers', $customer_id);
$current_point = (int) $this->db->get()->row()->current_pointreward;
//get add point from order
$this->db->select('plus_reward')->from('orders')->where('id_orders', $order_id);
$rewards = $this->db->get()->row();
$plus_point = (int) $rewards->plus_reward;
$updated_point = $current_point + $plus_point;
//update point reward
$data = array(
'current_pointreward' => $updated_point
);
$this->db->where('id_customers', $customer_id);
$this->db->update('customers', $data);
echo "Transaction order_id: " . $order_id ." successfully transfered using " . $type;
}
}
break;
case 'pending':
// TODO set payment status in merchant's database to 'Pending'
$data = array(
'payment_status' => 1, //not paid
'payment_status_message' => $transaction,
'payment_confirm' => 0,
'payment_type' => 'midtrans'
);
$this->db->where('id_orders', (int) $order_id);
$this->db->update('orders', $data);
if($type == 'bank_transfer') {
//get order content
$this->db->select('*')->from('orders')->where('id_orders', $order_id);
$order_data = $this->db->get()->row();
$finalshippingfee = 0;
$calculate_finalshippingfee = $order_data->shipping_fee - $order_data->free_shipping_fee;
if($calculate_finalshippingfee > 0){
$finalshippingfee = $calculate_finalshippingfee;
}
$grand_total = (($order_data->total_amount - $order_data->redeemed_voucher_amount - $order_data->minus_reward_amount) + ($finalshippingfee));
//get customer id
$customer_id = $order_data->customer_id;
//get customer handphone
$this->db->select('phone')->from('customers')->where('id_customers', $customer_id);
$phone = $this->db->get()->row()->phone;
//get va_number from log
$this->db->select('va_number')->from('midtrans_log')->where('order_id', $order_id);
$va_number = $this->db->get()->row()->va_number;
//send va number to sms
//send sms code to user's phone by sms gateway..
$url = 'http://gateway.siskomdigital.com:12010/cgi-bin/sendsms';
if($notif->va_numbers[0]->bank) {
$params = array( 'gw-username' => 'oky18003', 'gw-password' => '1qa2ws4r', 'gw-to' => '62' . $phone, 'gw-from' => 'Farmaku.com', 'gw-text' => 'Mohon transfer Rp. ' . number_format($grand_total) . ' ke ' . ucwords($notif->va_numbers[0]->bank) . ' Virtual Account ' . $va_number . ' untuk pesanan Farmaku.com No Order ' . $order_id . ' ,valid selama 24 jam. Cek email untuk detail pesanan.',
'gw-coding' => '1', 'gw-dlr-url' => base_url() . 'sms_receiver',
'gw-dlr-mask' => '1'
);
} elseif($notif->permata_va_number) {
$params = array( 'gw-username' => 'oky18003', 'gw-password' => '1qa2ws4r', 'gw-to' => '62' . $phone, 'gw-from' => 'Farmaku.com', 'gw-text' => 'Mohon transfer Rp. ' . number_format($grand_total) . ' ke ' . 'Permata' . ' Virtual Account ' . $va_number . ' untuk pesanan Farmaku.com No Order ' . $order_id . ', valid selama 24 jam. Cek email untuk detail pesanan.',
'gw-coding' => '1', 'gw-dlr-url' => base_url() . 'sms_receiver',
'gw-dlr-mask' => '1'
);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close ($ch);
//echo $response;
}
echo "Transaction order_id: " . $order_id ." pending using " . $type;
break;
case 'deny':
//check payment status must not be 4 (process) or 5 (delivered)
$this->db->select('payment_status')->from('orders')->where('id_orders', (int) $order_id);
$current_payment_status = $this->db->get()->row()->payment_status;
if($current_payment_status != 4 && $current_payment_status != 5) {
$data = array(
'payment_status' => 1, //not paid
'payment_status_message' => $transaction,
'payment_confirm' => 0
);
$this->db->where('id_orders', (int) $order_id);
$this->db->update('orders', $data);
}
break;
case 'expired':
case 'cancel':
$data = array(
'payment_status_message' => ucwords($transaction),
'payment_method' => $type
);
$this->db->where('id_orders', (int) $order_id);
$this->db->update('orders', $data);
echo "order_id: " . $order_id . " Method " . $type . " status " . $transaction;
break;
}
}
public function veritrans_payment_success() {
$order_id = $this->session->userdata('midtrans_order_id');
$this->db->select('*');
$this->db->from('orders');
$this->db->where('id_orders',$order_id);
$this->data['order'] = $this->db->get()->row();
$this->db->select('*');
$this->db->from('orders_detail');
$this->db->where('orders_id', $this->data['order']->id_orders);
$orders_detail = $this->db->get()->result();
$this->data['owa'] = 'no';
foreach($orders_detail as $item) {
//get product item
$this->db->select('product_type')->from('products')->where('id_products', $item->item_id);
$product_type_id = $this->db->get()->row()->product_type;
if($product_type_id == 1) {
$this->data['owa'] = 'yes';
}
break;
}
if($this->session->userdata('site_lang') == 'english') {
$this->lang->load('order_history', 'english');
} else {
$this->lang->load('order_history', 'indonesian');
}
$this->cart->destroy();
//get SEO
$this->data_header['browser_title'] = 'Midtrans Payment Success';
$this->data_header['meta_description'] = 'Midtrans Payment Success';
$this->data_header['meta_keywords'] = 'Midtrans Payment Success';
$this->load->view("themes/$this->theme_no/header", $this->data_header);
$this->load->view('veritrans_result/success',$this->data);
$this->load->view("themes/$this->theme_no/footer", $this->data_footer);
}
public function veritrans_payment_pending() {
$order_id = $this->session->userdata('midtrans_order_id');
$this->db->select('*');
$this->db->from('orders');
$this->db->where('id_orders',$order_id);
$this->data['order'] = $this->db->get()->row();
$this->db->select('*');
$this->db->from('orders_detail');
$this->db->where('orders_id', $this->data['order']->id_orders);
$orders_detail = $this->db->get()->result();
//get va number
$this->db->select('va_number')->from('midtrans_log')->where('order_id', $order_id);
$this->data['va_number'] = $this->db->get()->row()->va_number;
$this->data['owa'] = 'no';
foreach($orders_detail as $item) {
//get product item
$this->db->select('product_type')->from('products')->where('id_products', $item->item_id);
$product_type_id = $this->db->get()->row()->product_type;
if($product_type_id == 1) {
$this->data['owa'] = 'yes';
}
break;
}
if($this->session->userdata('site_lang') == 'english') {
$this->lang->load('order_history', 'english');
} else {
$this->lang->load('order_history', 'indonesian');
}
$this->cart->destroy();
//get SEO
$this->data_header['browser_title'] = 'Midtrans Payment Pending';
$this->data_header['meta_description'] = 'Midtrans Payment Pending';
$this->data_header['meta_keywords'] = 'Midtrans Payment Pending';
$this->load->view("themes/$this->theme_no/header", $this->data_header);
$this->load->view('veritrans_result/pending',$this->data);
$this->load->view("themes/$this->theme_no/footer", $this->data_footer);
}
public function veritrans_payment_unfinish() {
$this->cart->destroy();
//get SEO
$this->data_header['browser_title'] = 'Midtrans Payment Unfinish';
$this->data_header['meta_description'] = 'Midtrans Payment Unfinish';
$this->data_header['meta_keywords'] = 'Midtrans Payment Unfinish';
$this->load->view("themes/$this->theme_no/header", $this->data_header);
$this->load->view('veritrans_result/unfinish');
$this->load->view("themes/$this->theme_no/footer", $this->data_footer);
}
public function veritrans_payment_error() {
$this->cart->destroy();
//get SEO
$this->data_header['browser_title'] = 'Midtrans Payment Error';
$this->data_header['meta_description'] = 'Midtrans Payment Error';
$this->data_header['meta_keywords'] = 'Midtrans Payment Error';
$this->load->view("themes/$this->theme_no/header", $this->data_header);
$this->load->view('veritrans_result/error');
$this->load->view("themes/$this->theme_no/footer", $this->data_footer);
}
private function destroy_session_data() {
if($this->session->userdata('customer')['customer_type'] == 'guest') {
$this->session->unset_userdata('customer');
}
//DESTROY CART AND UNSET SOME SESSION, BUT NOT CUSTOMER SESSION
$this->cart->destroy();
$this->session->unset_userdata('midtrans_order_id');
$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_categoryproduct_promo');
$this->session->unset_userdata('redeemed_voucher_amount');
$this->session->unset_userdata('total_shipping_fee');
$this->session->unset_userdata('carrier');
$this->session->unset_userdata('carrier_name');
$this->session->unset_userdata('summary_message');
$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('tax');
$this->session->unset_userdata('productpage_to_cart');
$this->session->unset_userdata('choose_dropship_status');
$this->session->unset_userdata('destination_latitude');
$this->session->unset_userdata('destination_longitude');
$this->session->unset_userdata('current_viewed_category_id');
$this->session->unset_userdata('shipping_cart');
$this->session->unset_userdata('prescription_uniqid');
}
}