shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

pp_pro_iframe.php (11002B)


      1 <?php
      2 class ModelExtensionPaymentPPProIframe extends Model {
      3 	public function install() {
      4 		$this->db->query("
      5 			CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "paypal_iframe_order` (
      6 			  `paypal_iframe_order_id` int(11) NOT NULL AUTO_INCREMENT,
      7 			  `order_id` int(11) NOT NULL,
      8 			  `date_added` DATETIME NOT NULL,
      9 			  `date_modified` DATETIME NOT NULL,
     10 			  `capture_status` ENUM('Complete','NotComplete') DEFAULT NULL,
     11 			  `currency_code` CHAR(3) NOT NULL,
     12 			  `authorization_id` VARCHAR(30) NOT NULL,
     13 			  `total` DECIMAL( 10, 2 ) NOT NULL,
     14 			  PRIMARY KEY (`paypal_iframe_order_id`)
     15 			) ENGINE=MyISAM DEFAULT COLLATE=utf8_general_ci;");
     16 
     17 		$this->db->query("
     18 			CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "paypal_iframe_order_transaction` (
     19 			  `paypal_iframe_order_transaction_id` int(11) NOT NULL AUTO_INCREMENT,
     20 			  `paypal_iframe_order_id` int(11) NOT NULL,
     21 			  `transaction_id` CHAR(20) NOT NULL,
     22 			  `parent_id` CHAR(20) NOT NULL,
     23 			  `date_added` DATETIME NOT NULL,
     24 			  `note` VARCHAR(255) NOT NULL,
     25 			  `msgsubid` CHAR(38) NOT NULL,
     26 			  `receipt_id` CHAR(20) NOT NULL,
     27 			  `payment_type` ENUM('none','echeck','instant', 'refund', 'void') DEFAULT NULL,
     28 			  `payment_status` CHAR(20) NOT NULL,
     29 			  `pending_reason` CHAR(50) NOT NULL,
     30 			  `transaction_entity` CHAR(50) NOT NULL,
     31 			  `amount` DECIMAL( 10, 2 ) NOT NULL,
     32 			  `debug_data` TEXT NOT NULL,
     33 			  `call_data` TEXT NOT NULL,
     34 			  PRIMARY KEY (`paypal_iframe_order_transaction_id`)
     35 			) ENGINE=MyISAM DEFAULT COLLATE=utf8_general_ci;");
     36 	}
     37 
     38 	public function uninstall() {
     39 		$this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "paypal_iframe_order_transaction`;");
     40 		$this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "paypal_iframe_order`;");
     41 	}
     42 
     43 	private function getTransactions($paypal_iframe_order_id) {
     44 		$qry = $this->db->query("SELECT `ot`.*, ( SELECT count(`ot2`.`paypal_iframe_order_id`) FROM `" . DB_PREFIX . "paypal_iframe_order_transaction` `ot2` WHERE `ot2`.`parent_id` = `ot`.`transaction_id` ) AS `children` FROM `" . DB_PREFIX . "paypal_iframe_order_transaction` `ot` WHERE `paypal_iframe_order_id` = '" . (int)$paypal_iframe_order_id . "'");
     45 
     46 		if ($qry->num_rows) {
     47 			return $qry->rows;
     48 		} else {
     49 			return false;
     50 		}
     51 	}
     52 
     53 	public function getTotalCaptured($paypal_iframe_order_id) {
     54 		$qry = $this->db->query("SELECT SUM(`amount`) AS `amount` FROM `" . DB_PREFIX . "paypal_iframe_order_transaction` WHERE `paypal_iframe_order_id` = '" . (int)$paypal_iframe_order_id . "' AND `pending_reason` != 'authorization' AND (`payment_status` = 'Partially-Refunded' OR `payment_status` = 'Completed' OR `payment_status` = 'Pending') AND `transaction_entity` = 'payment'");
     55 
     56 		return $qry->row['amount'];
     57 	}
     58 
     59 	public function getTotalRefunded($paypal_iframe_order_id) {
     60 		$qry = $this->db->query("SELECT SUM(`amount`) AS `amount` FROM `" . DB_PREFIX . "paypal_iframe_order_transaction` WHERE `paypal_iframe_order_id` = '" . (int)$paypal_iframe_order_id . "' AND `payment_status` = 'Refunded'");
     61 
     62 		return $qry->row['amount'];
     63 	}
     64 
     65 	public function getTotalRefundedTransaction($transaction_id) {
     66 		$qry = $this->db->query("SELECT SUM(`amount`) AS `amount` FROM `" . DB_PREFIX . "paypal_iframe_order_transaction` WHERE `parent_id` = '" . $this->db->escape($transaction_id) . "' AND `payment_type` = 'refund'");
     67 
     68 		return $qry->row['amount'];
     69 	}
     70 
     71 	public function getOrder($order_id) {
     72 		$qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "paypal_iframe_order` WHERE `order_id` = '" . (int)$order_id . "' LIMIT 1");
     73 
     74 		if ($qry->num_rows) {
     75 			$order = $qry->row;
     76 			$order['transactions'] = $this->getTransactions($order['paypal_iframe_order_id']);
     77 			$order['captured'] = $this->getTotalCaptured($order['paypal_iframe_order_id']);
     78 			return $order;
     79 		} else {
     80 			return false;
     81 		}
     82 	}
     83 
     84 	public function call($data) {
     85 
     86 		if ($this->config->get('payment_pp_pro_iframe_test') == 1) {
     87 			$api_endpoint = 'https://api-3t.sandbox.paypal.com/nvp';
     88 		} else {
     89 			$api_endpoint = 'https://api-3t.paypal.com/nvp';
     90 		}
     91 
     92 		$settings = array(
     93 			'USER' => $this->config->get('payment_pp_pro_iframe_user'),
     94 			'PWD' => $this->config->get('payment_pp_pro_iframe_password'),
     95 			'SIGNATURE' => $this->config->get('payment_pp_pro_iframe_sig'),
     96 			'VERSION' => '84',
     97 			'BUTTONSOURCE' => 'WM_PRO_OPENCART_UK_' . VERSION,
     98 		);
     99 
    100 		$this->log($data, 'Call data');
    101 
    102 		$defaults = array(
    103 			CURLOPT_POST => 1,
    104 			CURLOPT_HEADER => 0,
    105 			CURLOPT_URL => $api_endpoint,
    106 			CURLOPT_USERAGENT => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1",
    107 			CURLOPT_FRESH_CONNECT => 1,
    108 			CURLOPT_RETURNTRANSFER => 1,
    109 			CURLOPT_FORBID_REUSE => 1,
    110 			CURLOPT_TIMEOUT => 0,
    111 			CURLOPT_SSL_VERIFYPEER => 0,
    112 			CURLOPT_SSL_VERIFYHOST => 0,
    113 			CURLOPT_POSTFIELDS => http_build_query(array_merge($data, $settings), '', "&")
    114 		);
    115 
    116 		$ch = curl_init();
    117 
    118 		curl_setopt_array($ch, $defaults);
    119 
    120 		if (!$result = curl_exec($ch)) {
    121 
    122 			$log_data = array(
    123 				'curl_error' => curl_error($ch),
    124 				'curl_errno' => curl_errno($ch)
    125 			);
    126 
    127 			$this->log($log_data, 'CURL failed');
    128 
    129 			return false;
    130 		}
    131 
    132 		$this->log($result, 'Result');
    133 
    134 		curl_close($ch);
    135 
    136 		return $this->cleanReturn($result);
    137 	}
    138 
    139 	public function updateOrder($capture_status, $order_id) {
    140 		$this->db->query("UPDATE `" . DB_PREFIX . "paypal_iframe_order` SET `date_modified` = now(), `capture_status` = '" . $this->db->escape($capture_status) . "' WHERE `order_id` = '" . (int)$order_id . "'");
    141 	}
    142 
    143 	public function updateTransaction($transaction) {
    144 		$this->db->query("
    145 			UPDATE " . DB_PREFIX . "paypal_iframe_order_transaction
    146 			SET paypal_iframe_order_id = " . (int)$transaction['paypal_iframe_order_id'] . ",
    147 				transaction_id = '" . $this->db->escape($transaction['transaction_id']) . "',
    148 				parent_id = '" . $this->db->escape($transaction['parent_id']) . "',
    149 				date_added = '" . $this->db->escape($transaction['date_added']) . "',
    150 				note = '" . $this->db->escape($transaction['note']) . "',
    151 				msgsubid = '" . $this->db->escape($transaction['msgsubid']) . "',
    152 				receipt_id = '" . $this->db->escape($transaction['receipt_id']) . "',
    153 				payment_type = '" . $this->db->escape($transaction['payment_type']) . "',
    154 				payment_status = '" . $this->db->escape($transaction['payment_status']) . "',
    155 				pending_reason = '" . $this->db->escape($transaction['pending_reason']) . "',
    156 				transaction_entity = '" . $this->db->escape($transaction['transaction_entity']) . "',
    157 				amount = '" . $this->db->escape($transaction['amount']) . "',
    158 				debug_data = '" . $this->db->escape($transaction['debug_data']) . "',
    159 				call_data = '" . $this->db->escape($transaction['call_data']) . "'
    160 			WHERE paypal_iframe_order_transaction_id = " . (int)$transaction['paypal_iframe_order_transaction_id'] . "
    161 		");
    162 	}
    163 
    164 	public function addTransaction($transaction_data, $request_data = array()) {
    165 		$this->db->query("INSERT INTO `" . DB_PREFIX . "paypal_iframe_order_transaction` SET `paypal_iframe_order_id` = '" . (int)$transaction_data['paypal_iframe_order_id'] . "', `transaction_id` = '" . $this->db->escape($transaction_data['transaction_id']) . "', `parent_id` = '" . $this->db->escape($transaction_data['parent_id']) . "', `date_added` = NOW(), `note` = '" . $this->db->escape($transaction_data['note']) . "', `msgsubid` = '" . $this->db->escape($transaction_data['msgsubid']) . "', `receipt_id` = '" . $this->db->escape($transaction_data['receipt_id']) . "', `payment_type` = '" . $this->db->escape($transaction_data['payment_type']) . "', `payment_status` = '" . $this->db->escape($transaction_data['payment_status']) . "', `pending_reason` = '" . $this->db->escape($transaction_data['pending_reason']) . "', `transaction_entity` = '" . $this->db->escape($transaction_data['transaction_entity']) . "', `amount` = '" . (float)$transaction_data['amount'] . "', `debug_data` = '" . $this->db->escape($transaction_data['debug_data']) . "'");
    166 
    167 		$paypal_iframe_order_transaction_id = $this->db->getLastId();
    168 
    169 		if ($request_data) {
    170 			$serialized_data = json_encode($request_data);
    171 
    172 			$this->db->query("
    173 				UPDATE " . DB_PREFIX . "paypal_iframe_order_transaction
    174 				SET call_data = '" . $this->db->escape($serialized_data) . "'
    175 				WHERE paypal_iframe_order_transaction_id = " . (int)$paypal_iframe_order_transaction_id . "
    176 				LIMIT 1
    177 			");
    178 		}
    179 
    180 		return $paypal_iframe_order_transaction_id;
    181 	}
    182 
    183 	public function log($data, $title = null) {
    184 		if ($this->config->get('payment_pp_pro_iframe_debug')) {
    185 			$log = new Log('pp_pro_iframe.log');
    186 			$log->write($title . ': ' . json_encode($data));
    187 		}
    188 	}
    189 
    190 	public function getTransaction($transaction_id) {
    191 		$call_data = array(
    192 			'METHOD' => 'GetTransactionDetails',
    193 			'TRANSACTIONID' => $transaction_id,
    194 		);
    195 
    196 		return $this->call($call_data);
    197 	}
    198 
    199 	public function getOrderId($transaction_id) {
    200 		$qry = $this->db->query("SELECT `o`.`order_id` FROM `" . DB_PREFIX . "paypal_iframe_order_transaction` `ot` LEFT JOIN `" . DB_PREFIX . "paypal_iframe_order` `o`  ON `o`.`paypal_iframe_order_id` = `ot`.`paypal_iframe_order_id`  WHERE `ot`.`transaction_id` = '" . $this->db->escape($transaction_id) . "' LIMIT 1");
    201 
    202 		if ($qry->num_rows) {
    203 			return $qry->row['order_id'];
    204 		} else {
    205 			return false;
    206 		}
    207 	}
    208 
    209 	public function updateAuthorizationId($paypal_iframe_order_id, $authorization_id) {
    210 		$this->db->query("
    211 			UPDATE `" . DB_PREFIX . "paypal_iframe_order`
    212 			SET `authorization_id` = '" . $this->db->escape($authorization_id) . "'
    213 			WHERE `paypal_iframe_order_id` = '" . $this->db->escape($paypal_iframe_order_id) . "'
    214 		");
    215 	}
    216 
    217 	public function updateRefundTransaction($transaction_id, $transaction_type) {
    218 		$this->db->query("UPDATE `" . DB_PREFIX . "paypal_iframe_order_transaction` SET `payment_status` = '" . $this->db->escape($transaction_type) . "' WHERE `transaction_id` = '" . $this->db->escape($transaction_id) . "' LIMIT 1");
    219 	}
    220 
    221 	public function getFailedTransaction($paypl_iframe_order_transaction_id) {
    222 		$result = $this->db->query("
    223 			SELECT *
    224 			FROM " . DB_PREFIX . "paypal_iframe_order_transaction
    225 			WHERE paypal_iframe_order_transaction_id = " . (int)$paypl_iframe_order_transaction_id . "
    226 		")->row;
    227 
    228 		if ($result) {
    229 			return $result;
    230 		} else {
    231 			return false;
    232 		}
    233 	}
    234 
    235 	public function getLocalTransaction($transaction_id) {
    236 		$result = $this->db->query("
    237 			SELECT *
    238 			FROM " . DB_PREFIX . "paypal_iframe_order_transaction
    239 			WHERE transaction_id = '" . $this->db->escape($transaction_id) . "'
    240 		")->row;
    241 
    242 		if ($result) {
    243 			return $result;
    244 		} else {
    245 			return false;
    246 		}
    247 	}
    248 
    249 	protected function cleanReturn($data) {
    250 		$data = explode('&', $data);
    251 
    252 		$arr = array();
    253 
    254 		foreach ($data as $k => $v) {
    255 			$tmp = explode('=', $v);
    256 			$arr[$tmp[0]] = urldecode($tmp[1]);
    257 		}
    258 
    259 		return $arr;
    260 	}
    261 }