squareup.php (5241B)
1 <?php 2 3 class ModelExtensionPaymentSquareup extends Model { 4 const RECURRING_ACTIVE = 1; 5 const RECURRING_INACTIVE = 2; 6 const RECURRING_CANCELLED = 3; 7 const RECURRING_SUSPENDED = 4; 8 const RECURRING_EXPIRED = 5; 9 const RECURRING_PENDING = 6; 10 11 public function getTransaction($squareup_transaction_id) { 12 return $this->db->query("SELECT * FROM `" . DB_PREFIX . "squareup_transaction` WHERE squareup_transaction_id='" . (int)$squareup_transaction_id . "'")->row; 13 } 14 15 public function getTransactions($data) { 16 $sql = "SELECT * FROM `" . DB_PREFIX . "squareup_transaction`"; 17 18 if (isset($data['order_id'])) { 19 $sql .= " WHERE order_id='" . (int)$data['order_id'] . "'"; 20 } 21 22 $sql .= " ORDER BY created_at DESC"; 23 24 if (isset($data['start']) && isset($data['limit'])) { 25 $sql .= " LIMIT " . $data['start'] . ', ' . $data['limit']; 26 } 27 28 return $this->db->query($sql)->rows; 29 } 30 31 public function getTotalTransactions($data) { 32 $sql = "SELECT COUNT(*) as total FROM `" . DB_PREFIX . "squareup_transaction`"; 33 34 if (isset($data['order_id'])) { 35 $sql .= " WHERE order_id='" . (int)$data['order_id'] . "'"; 36 } 37 38 return $this->db->query($sql)->row['total']; 39 } 40 41 public function updateTransaction($squareup_transaction_id, $type, $refunds = array()) { 42 $this->db->query("UPDATE `" . DB_PREFIX . "squareup_transaction` SET transaction_type='" . $this->db->escape($type) . "', is_refunded='" . (int)!empty($refunds) . "', refunds='" . $this->db->escape(json_encode($refunds)) . "' WHERE squareup_transaction_id='" . (int)$squareup_transaction_id . "'"); 43 } 44 45 public function getOrderStatusId($order_id, $transaction_status = null) { 46 if ($transaction_status) { 47 return $this->config->get('payment_squareup_status_' . strtolower($transaction_status)); 48 } else { 49 $this->load->model('sale/order'); 50 51 $order_info = $this->model_sale_order->getOrder($order_id); 52 53 return $order_info['order_status_id']; 54 } 55 } 56 57 public function editOrderRecurringStatus($order_recurring_id, $status) { 58 $this->db->query("UPDATE `" . DB_PREFIX . "order_recurring` SET `status` = '" . (int)$status . "' WHERE `order_recurring_id` = '" . (int)$order_recurring_id . "'"); 59 } 60 61 public function createTables() { 62 $this->db->query("CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "squareup_transaction` ( 63 `squareup_transaction_id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT, 64 `transaction_id` char(40) NOT NULL, 65 `merchant_id` char(32) NOT NULL, 66 `location_id` varchar(32) NOT NULL, 67 `order_id` int(11) NOT NULL, 68 `transaction_type` char(20) NOT NULL, 69 `transaction_amount` decimal(15,2) NOT NULL, 70 `transaction_currency` char(3) NOT NULL, 71 `billing_address_city` char(100) NOT NULL, 72 `billing_address_company` char(100) NOT NULL, 73 `billing_address_country` char(3) NOT NULL, 74 `billing_address_postcode` char(10) NOT NULL, 75 `billing_address_province` char(20) NOT NULL, 76 `billing_address_street_1` char(100) NOT NULL, 77 `billing_address_street_2` char(100) NOT NULL, 78 `device_browser` char(255) NOT NULL, 79 `device_ip` char(15) NOT NULL, 80 `created_at` char(29) NOT NULL, 81 `is_refunded` tinyint(1) NOT NULL, 82 `refunded_at` varchar(29) NOT NULL, 83 `tenders` text NOT NULL, 84 `refunds` text NOT NULL, 85 PRIMARY KEY (`squareup_transaction_id`), 86 KEY `order_id` (`order_id`), 87 KEY `transaction_id` (`transaction_id`) 88 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;"); 89 90 $this->db->query("CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "squareup_token` ( 91 `squareup_token_id` int(11) unsigned NOT NULL AUTO_INCREMENT, 92 `customer_id` int(11) NOT NULL, 93 `sandbox` tinyint(1) NOT NULL, 94 `token` char(40) NOT NULL, 95 `date_added` datetime NOT NULL, 96 `brand` VARCHAR(32) NOT NULL, 97 `ends_in` VARCHAR(4) NOT NULL, 98 PRIMARY KEY (`squareup_token_id`), 99 KEY `getCards` (`customer_id`, `sandbox`), 100 KEY `verifyCardCustomer` (`squareup_token_id`, `customer_id`), 101 KEY `cardExists` (`customer_id`, `brand`, `ends_in`) 102 ) ENGINE=InnoDB DEFAULT CHARSET=utf8"); 103 104 $this->db->query("CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "squareup_customer` ( 105 `customer_id` int(11) NOT NULL, 106 `sandbox` tinyint(1) NOT NULL, 107 `square_customer_id` varchar(32) NOT NULL, 108 PRIMARY KEY (`customer_id`, `sandbox`) 109 ) ENGINE=InnoDB DEFAULT CHARSET=utf8"); 110 } 111 112 public function dropTables() { 113 $this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "squareup_transaction`"); 114 $this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "squareup_token`"); 115 $this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "squareup_customer`"); 116 } 117 }