cardinity.php (3501B)
1 <?php 2 use Cardinity\Client; 3 use Cardinity\Method\Payment; 4 use Cardinity\Exception as CardinityException; 5 6 class ModelExtensionPaymentCardinity extends Model { 7 public function addOrder($data) { 8 $this->db->query("INSERT INTO `" . DB_PREFIX . "cardinity_order` SET `order_id` = '" . (int)$data['order_id'] . "', `payment_id` = '" . $this->db->escape($data['payment_id']) . "'"); 9 } 10 11 public function getOrder($order_id) { 12 $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "cardinity_order` WHERE `order_id` = '" . (int)$order_id . "' LIMIT 1"); 13 14 return $query->row; 15 } 16 17 public function createPayment($key, $secret, $payment_data) { 18 $client = Client::create(array( 19 'consumerKey' => $key, 20 'consumerSecret' => $secret, 21 )); 22 23 $method = new Payment\Create($payment_data); 24 25 try { 26 $payment = $client->call($method); 27 28 return $payment; 29 } catch (Exception $exception) { 30 $this->exception($exception); 31 32 throw $exception; 33 } 34 } 35 36 public function finalizePayment($key, $secret, $payment_id, $pares) { 37 $client = Client::create(array( 38 'consumerKey' => $key, 39 'consumerSecret' => $secret, 40 )); 41 42 $method = new Payment\Finalize($payment_id, $pares); 43 44 try { 45 $payment = $client->call($method); 46 47 return $payment; 48 } catch (Exception $exception) { 49 $this->exception($exception); 50 51 return false; 52 } 53 } 54 55 public function getMethod($address, $total) { 56 $this->load->language('extension/payment/cardinity'); 57 58 $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "zone_to_geo_zone WHERE geo_zone_id = '" . (int)$this->config->get('payment_cardinity_geo_zone_id') . "' AND country_id = '" . (int)$address['country_id'] . "' AND (zone_id = '" . (int)$address['zone_id'] . "' OR zone_id = '0')"); 59 60 if ($this->config->get('payment_cardinity_total') > 0 && $this->config->get('payment_cardinity_total') > $total) { 61 $status = false; 62 } elseif (!$this->config->get('payment_cardinity_geo_zone_id')) { 63 $status = true; 64 } elseif ($query->num_rows) { 65 $status = true; 66 } else { 67 $status = false; 68 } 69 70 if (!in_array($this->session->data['currency'], $this->getSupportedCurrencies())) { 71 $status = false; 72 } 73 74 $method_data = array(); 75 76 if ($status) { 77 $method_data = array( 78 'code' => 'cardinity', 79 'title' => $this->language->get('text_title'), 80 'terms' => '', 81 'sort_order' => $this->config->get('payment_cardinity_sort_order') 82 ); 83 } 84 85 return $method_data; 86 } 87 88 public function getSupportedCurrencies() { 89 return array( 90 'USD', 91 'GBP', 92 'EUR' 93 ); 94 } 95 96 public function log($data, $class_step = 6, $function_step = 6) { 97 if ($this->config->get('payment_cardinity_debug')) { 98 $backtrace = debug_backtrace(); 99 $log = new Log('cardinity.log'); 100 $log->write('(' . $backtrace[$class_step]['class'] . '::' . $backtrace[$function_step]['function'] . ') - ' . print_r($data, true)); 101 } 102 } 103 104 private function exception(Exception $exception) { 105 $this->log($exception->getMessage(), 1, 2); 106 107 switch (true) { 108 case $exception instanceof CardinityException\Request: 109 if ($exception->getErrorsAsString()) { 110 $this->log($exception->getErrorsAsString(), 1, 2); 111 } 112 113 break; 114 case $exception instanceof CardinityException\InvalidAttributeValue: 115 foreach ($exception->getViolations() as $violation) { 116 $this->log($violation->getMessage(), 1, 2); 117 } 118 119 break; 120 } 121 } 122 }