laybuy.php (9264B)
1 <?php 2 class ModelExtensionPaymentLaybuy extends Model { 3 public function addTransaction($data = array(), $status) { 4 $this->log('Report: ' . print_r($data, true), '1'); 5 6 $this->log('Status: ' . $status, '1'); 7 8 $this->db->query("INSERT INTO `" . DB_PREFIX . "laybuy_transaction` SET `order_id` = '" . (int)$data['order_id'] . "', `firstname` = '" . $this->db->escape($data['firstname']) . "', `lastname` = '" . $this->db->escape($data['lastname']) . "', `address` = '" . $this->db->escape($data['address']) . "', `suburb` = '" . $this->db->escape($data['suburb']) . "', `state` = '" . $this->db->escape($data['state']) . "', `country` = '" . $this->db->escape($data['country']) . "', `postcode` = '" . $this->db->escape($data['postcode']) . "', `email` = '" . $this->db->escape($data['email']) . "', `amount` = '" . (float)$data['amount'] . "', `currency` = '" . $this->db->escape($data['currency']) . "', `downpayment` = '" . $this->db->escape($data['downpayment']) . "', `months` = '" . (int)$data['months'] . "', `downpayment_amount` = '" . (float)$data['downpayment_amount'] . "', `payment_amounts` = '" . (float)$data['payment_amounts'] . "', `first_payment_due` = '" . $this->db->escape($data['first_payment_due']) . "', `last_payment_due` = '" . $this->db->escape($data['last_payment_due']) . "', `store_id` = '" . (int)$data['store_id'] . "', `status` = '" . (int)$status . "', `report` = '" . $this->db->escape($data['report']) . "', `paypal_profile_id` = '" . $this->db->escape($data['paypal_profile_id']) . "', `laybuy_ref_no` = '" . (int)$data['laybuy_ref_no'] . "', `date_added` = NOW()"); 9 } 10 11 public function deleteRevisedTransaction($id) { 12 $this->db->query("DELETE FROM `" . DB_PREFIX . "laybuy_revise_request` WHERE `laybuy_revise_request_id` = '" . (int)$id . "'"); 13 } 14 15 public function deleteTransactionByOrderId($order_id) { 16 $this->db->query("DELETE FROM `" . DB_PREFIX . "laybuy_transaction` WHERE `order_id` = '" . (int)$order_id . "'"); 17 18 $this->db->query("DELETE FROM `" . DB_PREFIX . "laybuy_revise_request` WHERE `order_id` = '" . (int)$order_id . "'"); 19 } 20 21 public function getInitialPayments() { 22 $minimum = $this->config->get('payment_laybuy_min_deposit') ? $this->config->get('payment_laybuy_min_deposit') : 20; 23 24 $maximum = $this->config->get('payment_laybuy_max_deposit') ? $this->config->get('payment_laybuy_max_deposit') : 50; 25 26 $initial_payments = array(); 27 28 for ($i = $minimum; $i <= $maximum; $i += 10) { 29 $initial_payments[] = $i; 30 } 31 32 return $initial_payments; 33 } 34 35 public function getMethod($address, $total) { 36 $this->load->language('extension/payment/laybuy'); 37 38 $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "zone_to_geo_zone` WHERE `geo_zone_id` = '" . (int)$this->config->get('payment_laybuy_geo_zone_id') . "' AND `country_id` = '" . (int)$address['country_id'] . "' AND (`zone_id` = '" . (int)$address['zone_id'] . "' OR `zone_id` = '0')"); 39 40 if ($this->config->get('payment_laybuy_total') > 0 && $this->config->get('payment_laybuy_total') > $total) { 41 $status = false; 42 } elseif (!$this->config->get('payment_laybuy_geo_zone_id')) { 43 $status = true; 44 } elseif ($query->num_rows) { 45 $status = true; 46 } else { 47 $status = false; 48 } 49 50 /* Condition for customer group */ 51 if ($status && $this->config->get('payment_laybuy_customer_group')) { 52 if (isset($this->session->data['guest']) && in_array(0, $this->config->get('payment_laybuy_customer_group'))) { 53 $status = true; 54 } elseif ($this->customer->isLogged() && $this->session->data['customer_id']) { 55 $this->load->model('account/customer'); 56 57 $customer = $this->model_account_customer->getCustomer($this->session->data['customer_id']); 58 59 if (in_array($customer['customer_group_id'], $this->config->get('payment_laybuy_customer_group'))) { 60 $this->session->data['customer_group_id'] = $customer['customer_group_id']; 61 62 $status = true; 63 } else { 64 $status = false; 65 } 66 } else { 67 $status = false; 68 } 69 } 70 71 /* Condition for categories and products */ 72 if ($status && $this->config->get('payment_laybuy_category')) { 73 $allowed_categories = $this->config->get('payment_laybuy_category'); 74 75 $xproducts = explode(',', $this->config->get('payment_laybuy_xproducts')); 76 77 $cart_products = $this->cart->getProducts(); 78 79 foreach ($cart_products as $cart_product) { 80 $product = array(); 81 82 if ($xproducts && in_array($cart_product['product_id'], $xproducts)) { 83 $status = false; 84 break; 85 } else { 86 $product = $this->db->query("SELECT GROUP_CONCAT(`category_id`) as `categories` FROM `" . DB_PREFIX . "product_to_category` WHERE `product_id` = '" . (int)$cart_product['product_id'] . "'"); 87 88 $product = $product->row; 89 90 $product = explode(',', $product['categories']); 91 92 if ($product && count(array_diff($product, $allowed_categories)) > 0) { 93 $status = false; 94 break; 95 } 96 } 97 } 98 } 99 100 $method_data = array(); 101 102 if ($status) { 103 $method_data = array( 104 'code' => 'laybuy', 105 'title' => $this->language->get('text_title'), 106 'terms' => '', 107 'sort_order' => $this->config->get('payment_laybuy_sort_order') 108 ); 109 } 110 111 return $method_data; 112 } 113 114 public function getMonths() { 115 $this->load->language('extension/payment/laybuy'); 116 117 $max_months = $this->config->get('payment_laybuy_max_months'); 118 119 if (!$max_months) { 120 $max_months = 3; 121 } 122 123 if ($max_months < 1) { 124 $max_months = 1; 125 } 126 127 $months = array(); 128 129 for ($i = 1; $i <= $max_months; $i++) { 130 $months[] = array( 131 'value' => $i, 132 'label' => $i . ' ' . (($i > 1) ? $this->language->get('text_months') : $this->language->get('text_month')) 133 ); 134 } 135 136 return $months; 137 } 138 139 public function getPayPalProfileIds() { 140 $query = $this->db->query("SELECT `paypal_profile_id` FROM `" . DB_PREFIX . "laybuy_transaction` WHERE `status` = '1'"); 141 142 return $query->rows; 143 } 144 145 public function getRevisedTransaction($id) { 146 $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "laybuy_revise_request` WHERE `laybuy_revise_request_id` = '" . (int)$id . "'"); 147 148 return $query->row; 149 } 150 151 public function getTransaction($id) { 152 $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "laybuy_transaction` WHERE `laybuy_transaction_id` = '" . (int)$id . "'"); 153 154 return $query->row; 155 } 156 157 public function getTransactionByLayBuyRefId($laybuy_ref_id) { 158 $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "laybuy_transaction` WHERE `laybuy_ref_no` = '" . (int)$laybuy_ref_id . "'"); 159 160 return $query->row; 161 } 162 163 public function log($data, $step = 6) { 164 if ($this->config->get('payment_laybuy_logging')) { 165 $backtrace = debug_backtrace(); 166 167 $log = new Log('laybuy.log'); 168 169 $log->write('(' . $backtrace[$step]['class'] . '::' . $backtrace[$step]['function'] . ') - ' . $data); 170 } 171 } 172 173 public function prepareTransactionReport($post_data) { 174 $this->load->model('checkout/order'); 175 176 $this->load->language('extension/payment/laybuy'); 177 178 $data = array_change_key_case($post_data, CASE_LOWER); 179 180 $data['order_id'] = $data['custom']; 181 182 $order_info = $this->model_checkout_order->getOrder($data['order_id']); 183 184 $date_added = date($this->language->get('date_format_short'), strtotime($order_info['date_added'])); 185 186 $data['store_id'] = $order_info['store_id']; 187 188 $data['date_added'] = $order_info['date_added']; 189 190 $data['first_payment_due'] = date('Y-m-d h:i:s', strtotime(str_replace('/', '-', $data['first_payment_due']))); 191 192 $data['last_payment_due'] = date('Y-m-d h:i:s', strtotime(str_replace('/', '-', $data['last_payment_due']))); 193 194 $months = (int)$data['months']; 195 196 $report_content = array(); 197 198 $report_content[] = array( 199 'instalment' => 0, 200 'amount' => $this->currency->format($data['downpayment_amount'], $data['currency']), 201 'date' => $date_added, 202 'pp_trans_id' => $data['dp_paypal_txn_id'], 203 'status' => 'Completed' 204 ); 205 206 for ($month = 1; $month <= $months; $month++) { 207 $date = date("Y-m-d h:i:s", strtotime($data['first_payment_due'] . " +" . ($month -1) . " month")); 208 $date = date($this->language->get('date_format_short'), strtotime($date)); 209 210 $report_content[] = array( 211 'instalment' => $month, 212 'amount' => $this->currency->format($data['payment_amounts'], $data['currency']), 213 'date' => $date, 214 'pp_trans_id' => '', 215 'status' => 'Pending' 216 ); 217 } 218 219 $data['report'] = json_encode($report_content); 220 221 return $data; 222 } 223 224 public function updateCronRunTime() { 225 $this->db->query("DELETE FROM `" . DB_PREFIX . "setting` WHERE `key` = 'laybuy_cron_time'"); 226 227 $this->db->query("INSERT INTO `" . DB_PREFIX . "setting` SET `store_id` = '0', `code` = 'laybuy', `key` = 'laybuy_cron_time', `value` = NOW(), `serialized` = '0'"); 228 } 229 230 public function updateTransaction($id, $status, $report, $transaction) { 231 $this->db->query("UPDATE `" . DB_PREFIX . "laybuy_transaction` SET `status` = '" . (int)$status . "', `report` = '" . $this->db->escape($report) . "', `transaction` = '" . (int)$transaction . "' WHERE `laybuy_transaction_id` = '" . (int)$id . "'"); 232 } 233 }