klarna_checkout.php (9606B)
1 <?php 2 use Klarna\Rest\Transport\Connector as KCConnector; 3 use Klarna\Rest\Transport\ConnectorInterface as KCConnectorInterface; 4 use Klarna\Rest\Checkout\Order as KCOrder; 5 6 class ModelExtensionPaymentKlarnaCheckout extends Model { 7 public function orderCreate(KCConnector $connector, $order_data) { 8 try { 9 $checkout = new KCOrder($connector); 10 $checkout->create($order_data); 11 12 return $checkout->fetch(); 13 } catch (\Exception $e) { 14 $this->log($e->getMessage(), 1); 15 16 return false; 17 } 18 } 19 20 public function orderRetrieve(KCConnector $connector, $order_id) { 21 try { 22 $checkout = new KCOrder($connector, $order_id); 23 24 return $checkout->fetch(); 25 } catch (\Exception $e) { 26 $this->log($e->getMessage(), 1); 27 28 return false; 29 } 30 } 31 32 public function orderUpdate(KCConnector $connector, $order_id, $order_data) { 33 try { 34 $checkout = new KCOrder($connector, $order_id); 35 $checkout->update($order_data); 36 37 return $checkout->fetch(); 38 } catch (\Exception $e) { 39 $this->log($e->getMessage(), 1); 40 41 return false; 42 } 43 } 44 45 public function omOrderRetrieve(KCConnector $connector, $order_id) { 46 try { 47 $order = new \Klarna\Rest\OrderManagement\Order($connector, $order_id); 48 49 return $order->fetch(); 50 } catch (\Exception $e) { 51 $this->log($e->getMessage(), 1); 52 53 return false; 54 } 55 } 56 57 public function getMethod($address, $total) { 58 // Not shown in the payment method list 59 return array(); 60 } 61 62 public function getConnector($accounts, $currency) { 63 $klarna_account = false; 64 $connector = false; 65 66 if ($accounts && $currency) { 67 foreach ($accounts as $account) { 68 if ($account['currency'] == $currency) { 69 if ($account['environment'] == 'test') { 70 if ($account['api'] == 'NA') { 71 $base_url = KCConnectorInterface::NA_TEST_BASE_URL; 72 } elseif ($account['api'] == 'EU') { 73 $base_url = KCConnectorInterface::EU_TEST_BASE_URL; 74 } 75 } elseif ($account['environment'] == 'live') { 76 if ($account['api'] == 'NA') { 77 $base_url = KCConnectorInterface::NA_BASE_URL; 78 } elseif ($account['api'] == 'EU') { 79 $base_url = KCConnectorInterface::EU_BASE_URL; 80 } 81 } 82 83 $klarna_account = $account; 84 $connector = $this->connector( 85 $account['merchant_id'], 86 $account['secret'], 87 $base_url 88 ); 89 90 break; 91 } 92 } 93 } 94 95 return array($klarna_account, $connector); 96 } 97 98 public function getOrder($order_ref) { 99 return $this->db->query("SELECT * FROM `" . DB_PREFIX . "klarna_checkout_order` WHERE `order_ref` = '" . $this->db->escape($order_ref) . "' LIMIT 1")->row; 100 } 101 102 public function getOrderByOrderId($order_id) { 103 return $this->db->query("SELECT * FROM `" . DB_PREFIX . "klarna_checkout_order` WHERE `order_id` = '" . (int)$order_id . "' LIMIT 1")->row; 104 } 105 106 public function addOrder($order_id, $order_ref, $data) { 107 $this->db->query("INSERT INTO `" . DB_PREFIX . "klarna_checkout_order` SET `order_id` = '" . (int)$order_id . "', `order_ref` = '" . $this->db->escape($order_ref) . "', `data` = '" . $this->db->escape($data) . "'"); 108 } 109 110 public function updateOrder($order_id, $order_ref, $data) { 111 $this->db->query("UPDATE `" . DB_PREFIX . "klarna_checkout_order` SET `order_id` = '" . (int)$order_id . "', `data` = '" . $this->db->escape($data) . "' WHERE `order_ref` = '" . $this->db->escape($order_ref) . "'"); 112 } 113 114 public function updateOcOrder($order_id, $data) { 115 $this->db->query("UPDATE `" . DB_PREFIX . "order` SET `firstname` = '" . $this->db->escape($data['firstname']) . "', `lastname` = '" . $this->db->escape($data['lastname']) . "', `telephone` = '" . $this->db->escape($data['telephone']) . "', `payment_firstname` = '" . $this->db->escape($data['payment_firstname']) . "', `payment_lastname` = '" . $this->db->escape($data['payment_lastname']) . "', `payment_address_1` = '" . $this->db->escape($data['payment_address_1']) . "', `payment_address_2` = '" . $this->db->escape($data['payment_address_2']) . "', `payment_city` = '" . $this->db->escape($data['payment_city']) . "', `payment_postcode` = '" . $this->db->escape($data['payment_postcode']) . "', `payment_zone` = '" . $this->db->escape($data['payment_zone']) . "', `payment_zone_id` = '" . (int)$data['payment_zone_id'] . "', `payment_country` = '" . $this->db->escape($data['payment_country']) . "', `payment_country_id` = '" . (int)$data['payment_country_id'] . "', `payment_address_format` = '" . $this->db->escape($data['payment_address_format']) . "', `shipping_firstname` = '" . $this->db->escape($data['shipping_firstname']) . "', `shipping_lastname` = '" . $this->db->escape($data['shipping_lastname']) . "', `shipping_address_1` = '" . $this->db->escape($data['shipping_address_1']) . "', `shipping_address_2` = '" . $this->db->escape($data['shipping_address_2']) . "', `shipping_city` = '" . $this->db->escape($data['shipping_city']) . "', `shipping_postcode` = '" . $this->db->escape($data['shipping_postcode']) . "', `shipping_zone` = '" . $this->db->escape($data['shipping_zone']) . "', `shipping_zone_id` = '" . (int)$data['shipping_zone_id'] . "', `shipping_country` = '" . $this->db->escape($data['shipping_country']) . "', `shipping_country_id` = '" . (int)$data['shipping_country_id'] . "', `shipping_address_format` = '" . $this->db->escape($data['shipping_address_format']) . "' WHERE `order_id` = '" . (int)$order_id . "'"); 116 } 117 118 public function updateOcOrderEmail($order_id, $email) { 119 $this->db->query("UPDATE `" . DB_PREFIX . "order` SET `email` = '" . $this->db->escape($email) . "' WHERE `order_id` = '" . (int)$order_id . "'"); 120 } 121 122 public function getCountryByIsoCode2($iso_code_2) { 123 $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "country WHERE `iso_code_2` = '" . $this->db->escape($iso_code_2) . "' AND `status` = '1'"); 124 125 return $query->row; 126 } 127 128 public function getCountryByIsoCode3($iso_code_3) { 129 $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "country WHERE `iso_code_3` = '" . $this->db->escape($iso_code_3) . "' AND `status` = '1'"); 130 131 return $query->row; 132 } 133 134 public function getZoneByCode($code, $country_id) { 135 $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "zone WHERE (`code` = '" . $this->db->escape($code) . "' OR `name` = '" . $this->db->escape($code) . "') AND `country_id` = '" . (int)$country_id . "' AND `status` = '1'"); 136 137 return $query->row; 138 } 139 140 public function getCountriesByGeoZone($geo_zone_id) { 141 $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "zone_to_geo_zone WHERE geo_zone_id = '" . (int)$geo_zone_id . "' GROUP BY `country_id` ORDER BY `country_id` ASC"); 142 143 return $query->rows; 144 } 145 146 public function checkForPaymentTaxes($products = array()) { 147 foreach ($products as $product) { 148 $query = $this->db->query("SELECT COUNT(*) AS `total` FROM " . DB_PREFIX . "tax_rule WHERE `based` = 'payment' AND `tax_class_id` = '" . (int)$product['tax_class_id'] . "'"); 149 150 if ($query->row['total']) { 151 return true; 152 } 153 } 154 155 return false; 156 } 157 158 public function getDefaultShippingMethod($shipping_methods) { 159 $first_shipping_method = reset($shipping_methods); 160 161 if ($first_shipping_method && isset($first_shipping_method['quote']) && !empty($first_shipping_method['quote'])) { 162 $first_shipping_method_quote = reset($first_shipping_method['quote']); 163 164 if ($first_shipping_method_quote) { 165 $shipping = explode('.', $first_shipping_method_quote['code']); 166 167 return $shipping_methods[$shipping[0]]['quote'][$shipping[1]]; 168 } 169 } 170 171 return array(); 172 } 173 174 public function log($data, $step = 6) { 175 if ($this->config->get('klarna_checkout_debug')) { 176 $backtrace = debug_backtrace(); 177 $log = new Log('klarna_checkout.log'); 178 $log->write('(' . $backtrace[$step]['class'] . '::' . $backtrace[$step]['function'] . ') - ' . print_r($data, true)); 179 } 180 } 181 182 public function subscribeNewsletter($customer_id) { 183 $this->db->query("UPDATE " . DB_PREFIX . "customer SET newsletter = '1' WHERE customer_id = '" . (int)$customer_id . "'"); 184 } 185 186 public function getTotals() { 187 $totals = array(); 188 $taxes = $this->cart->getTaxes(); 189 $total = 0; 190 191 // Because __call can not keep var references so we put them into an array. 192 $total_data = array( 193 'totals' => &$totals, 194 'taxes' => &$taxes, 195 'total' => &$total 196 ); 197 198 $this->load->model('setting/extension'); 199 200 $sort_order = array(); 201 202 $results = $this->model_setting_extension->getExtensions('total'); 203 204 foreach ($results as $key => $value) { 205 $sort_order[$key] = $this->config->get('total_' . $value['code'] . '_sort_order'); 206 } 207 208 array_multisort($sort_order, SORT_ASC, $results); 209 210 foreach ($results as $result) { 211 if ($this->config->get('total_' . $result['code'] . '_status')) { 212 $this->load->model('extension/total/' . $result['code']); 213 214 // We have to put the totals in an array so that they pass by reference. 215 $this->{'model_extension_total_' . $result['code']}->getTotal($total_data); 216 } 217 } 218 219 $sort_order = array(); 220 221 foreach ($totals as $key => $value) { 222 $sort_order[$key] = $value['sort_order']; 223 } 224 225 array_multisort($sort_order, SORT_ASC, $totals); 226 227 return array($totals, $taxes, $total); 228 } 229 230 private function connector($merchant_id, $secret, $url) { 231 try { 232 $connector = KCConnector::create( 233 $merchant_id, 234 $secret, 235 $url 236 ); 237 238 return $connector; 239 } catch (\Exception $e) { 240 $this->log($e->getMessage(), 1); 241 242 return false; 243 } 244 } 245 }