shop.balmet.com

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

cardconnect.php (13393B)


      1 <?php
      2 class ControllerExtensionPaymentCardConnect extends Controller {
      3 	public function index() {
      4 		$this->load->language('extension/payment/cardconnect');
      5 
      6 		$this->load->model('extension/payment/cardconnect');
      7 
      8 		$data['card_types'] = $this->model_extension_payment_cardconnect->getCardTypes();
      9 
     10 		$data['months'] = $this->model_extension_payment_cardconnect->getMonths();
     11 
     12 		$data['years'] = $this->model_extension_payment_cardconnect->getYears();
     13 
     14 		if ($this->customer->isLogged() && $this->config->get('cardconnect_store_cards')) {
     15 			$data['store_cards'] = true;
     16 
     17 			$data['cards'] = $this->model_extension_payment_cardconnect->getCards($this->customer->getId());
     18 		} else {
     19 			$data['store_cards'] = false;
     20 
     21 			$data['cards'] = array();
     22 		}
     23 
     24 		$data['echeck'] = $this->config->get('cardconnect_echeck');
     25 
     26 		$data['action'] = $this->url->link('extension/payment/cardconnect/send', '', true);
     27 
     28 		return $this->load->view('extension/payment/cardconnect', $data);
     29 	}
     30 
     31 	public function send()	{
     32 		$this->load->language('extension/payment/cardconnect');
     33 
     34 		$this->load->model('extension/payment/cardconnect');
     35 
     36 		$this->model_extension_payment_cardconnect->log('Posting order to CardConnect');
     37 
     38 		$json = array();
     39 
     40 		$json['error'] = '';
     41 
     42 		if ($this->config->get('cardconnect_status')) {
     43 			if ($this->request->server['REQUEST_METHOD'] == 'POST') {
     44 				$error = $this->validate();
     45 
     46 				if (!$error) {
     47 					$this->load->model('checkout/order');
     48 
     49 					$order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
     50 
     51 					if ($order_info) {
     52 						$this->model_extension_payment_cardconnect->log('Order ID: ' . $order_info['order_id']);
     53 
     54 						$accttype = $account = $expiry = $cvv2 = $profile = $capture = $bankaba = '';
     55 
     56 						$existing_card = false;
     57 
     58 						if (!isset($this->request->post['method']) || $this->request->post['method'] == 'card') {
     59 							$this->model_extension_payment_cardconnect->log('Method is card');
     60 
     61 							if ($this->request->post['card_new'] && isset($this->request->post['card_save']) && $this->config->get('cardconnect_store_cards') && $this->customer->isLogged()) {
     62 								$profile = 'Y';
     63 							} else if (!$this->request->post['card_new'] && $this->customer->isLogged()) {
     64 								$existing_card = $this->model_extension_payment_cardconnect->getCard($this->request->post['card_choice'], $this->customer->getId());
     65 
     66 								$profile = $existing_card['profileid'];
     67 							}
     68 
     69 							if ($existing_card) {
     70 								$accttype = $existing_card['type'];
     71 
     72 								$account = $existing_card['token'];
     73 
     74 								$expiry = $existing_card['expiry'];
     75 
     76 								$cvv2 = '';
     77 							} else {
     78 								$accttype = $this->request->post['card_type'];
     79 
     80 								$account = $this->request->post['card_number'];
     81 
     82 								$expiry = $this->request->post['card_expiry_month'] . $this->request->post['card_expiry_year'];
     83 
     84 								$cvv2 = $this->request->post['card_cvv2'];
     85 							}
     86 						} else {
     87 							$this->model_extension_payment_cardconnect->log('Method is Echeck');
     88 
     89 							$account = $this->request->post['account_number'];
     90 
     91 							$bankaba = $this->request->post['routing_number'];
     92 						}
     93 
     94 						if ($this->config->get('cardconnect_transaction') == 'payment') {
     95 							$capture = 'Y';
     96 
     97 							$type = 'payment';
     98 
     99 							$status = 'New';
    100 
    101 							$order_status_id = $this->config->get('cardconnect_order_status_id_processing');
    102 						} else {
    103 							$capture = 'N';
    104 
    105 							$type = 'auth';
    106 
    107 							$status = 'New';
    108 
    109 							$order_status_id = $this->config->get('cardconnect_order_status_id_pending');
    110 						}
    111 
    112 						$data = array(
    113 							'merchid'    => $this->config->get('payment_cardconnect_merchant_id'),
    114 							'accttype'   => $accttype,
    115 							'account'    => $account,
    116 							'expiry'     => $expiry,
    117 							'cvv2'       => $cvv2,
    118 							'amount'     => round(floatval($order_info['total']), 2, PHP_ROUND_HALF_DOWN),
    119 							'currency'   => $order_info['currency_code'],
    120 							'orderid'    => $order_info['order_id'],
    121 							'name'       => $order_info['payment_firstname'] . ' ' . $order_info['payment_lastname'],
    122 							'address'    => $order_info['payment_address_1'],
    123 							'city'       => $order_info['payment_city'],
    124 							'region'     => $order_info['payment_zone'],
    125 							'country'    => $order_info['payment_iso_code_2'],
    126 							'postal'     => $order_info['payment_postcode'],
    127 							'email'      => $order_info['email'],
    128 							'phone'      => $order_info['telephone'],
    129 							'ecomind'    => 'E',
    130 							'tokenize'   => 'Y',
    131 							'profile'    => $profile,
    132 							'capture'    => $capture,
    133 							'bankaba'    => $bankaba,
    134 							'userfields' => array('secret_token' => $this->config->get('cardconnect_token')),
    135 							'frontendid' => '26'
    136 						);
    137 
    138 						$data_json = json_encode($data);
    139 
    140 						$url = 'https://' . $this->config->get('cardconnect_site') . '.cardconnect.com:' . (($this->config->get('cardconnect_environment') == 'live') ? 8443 : 6443) . '/cardconnect/rest/auth';
    141 
    142 						$header = array();
    143 
    144 						$header[] = 'Content-type: application/json';
    145 						$header[] = 'Content-length: ' . strlen($data_json);
    146 						$header[] = 'Authorization: Basic ' . base64_encode($this->config->get('cardconnect_api_username') . ':' . $this->config->get('cardconnect_api_password'));
    147 
    148 						$this->model_extension_payment_cardconnect->log('Header: ' . print_r($header, true));
    149 
    150 						$this->model_extension_payment_cardconnect->log('Post Data: ' . print_r($data, true));
    151 
    152 						$this->model_extension_payment_cardconnect->log('URL: ' . $url);
    153 
    154 						$ch = curl_init();
    155 						curl_setopt($ch, CURLOPT_URL, $url);
    156 						curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    157 						curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
    158 						curl_setopt($ch, CURLOPT_POSTFIELDS, $data_json);
    159 						curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    160 						curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    161 						curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    162 						$response_data = curl_exec($ch);
    163 						if (curl_errno($ch)) {
    164 							$this->model_extension_payment_cardconnect->log('cURL error: ' . curl_errno($ch));
    165 						}
    166 						curl_close($ch);
    167 
    168 						$response_data = json_decode($response_data, true);
    169 
    170 						$this->model_extension_payment_cardconnect->log('Response: ' . print_r($response_data, true));
    171 
    172 					 	if (isset($response_data['respstat']) && $response_data['respstat'] == 'A') {
    173 							$this->load->model('checkout/order');
    174 
    175 							// if a cheque
    176 							if ($bankaba) {
    177 								$payment_method = 'echeck';
    178 
    179 								$type = 'payment';
    180 							} else {
    181 								$payment_method = 'card';
    182 							}
    183 
    184 							$this->model_checkout_order->addOrderHistory($order_info['order_id'], $order_status_id);
    185 
    186 							$order_info = array_merge($order_info, $response_data);
    187 
    188 							$cardconnect_order_id = $this->model_extension_payment_cardconnect->addOrder($order_info, $payment_method);
    189 
    190 							$this->model_extension_payment_cardconnect->addTransaction($cardconnect_order_id, $type, $status, $order_info);
    191 
    192 							if (isset($response_data['profileid']) && $this->config->get('cardconnect_store_cards') && $this->customer->isLogged()) {
    193 								$this->model_extension_payment_cardconnect->log('Saving card');
    194 
    195 								$this->model_extension_payment_cardconnect->addCard($cardconnect_order_id, $this->customer->getId(), $response_data['profileid'], $response_data['token'], $this->request->post['card_type'], $response_data['account'], $this->request->post['card_expiry_month'] . $this->request->post['card_expiry_year']);
    196 							}
    197 
    198 							$this->model_extension_payment_cardconnect->log('Success');
    199 
    200 							$json['success'] = $this->url->link('checkout/success', '', true);
    201 						} else {
    202 							$this->model_extension_payment_cardconnect->log($response_data['resptext']);
    203 
    204 							$json['error']['warning'] = $response_data['resptext'];
    205 						}
    206 					} else {
    207 						$this->model_extension_payment_cardconnect->log('No matching order');
    208 
    209 						$json['error']['warning'] = $this->language->get('error_no_order');
    210 					}
    211 				} else {
    212 					$this->model_extension_payment_cardconnect->log('Failed validation');
    213 
    214 					$json['error'] = $error;
    215 				}
    216 			} else {
    217 				$this->model_extension_payment_cardconnect->log('No $_POST data');
    218 
    219 				$json['error']['warning'] = $this->language->get('error_no_post_data');
    220 			}
    221 		} else {
    222 			$this->model_extension_payment_cardconnect->log('Module not enabled');
    223 
    224 			$json['error']['warning'] = $this->language->get('error_not_enabled');
    225 		}
    226 
    227 		$this->response->addHeader('Content-Type: application/json');
    228 		$this->response->setOutput(json_encode($json));
    229 	}
    230 
    231 	public function delete() {
    232 		$this->load->language('extension/payment/cardconnect');
    233 
    234 		$this->load->model('extension/payment/cardconnect');
    235 
    236 		$this->model_extension_payment_cardconnect->log('Deleting card');
    237 
    238 		$json = array();
    239 
    240 		if ($this->config->get('cardconnect_status')) {
    241 			if ($this->customer->isLogged()) {
    242 				if (isset($this->request->post['card_choice'])) {
    243 					if ($this->request->post['card_choice']) {
    244 						$card = $this->model_extension_payment_cardconnect->getCard($this->request->post['card_choice'], $this->customer->getId());
    245 
    246 						if ($card) {
    247 							$this->model_extension_payment_cardconnect->deleteCard($this->request->post['card_choice'], $this->customer->getId());
    248 						} else {
    249 							$this->model_extension_payment_cardconnect->log('No such card');
    250 
    251 							$json['error'] = $this->language->get('error_no_card');
    252 						}
    253 					} else {
    254 						$this->model_extension_payment_cardconnect->log('No card selected');
    255 
    256 						$json['error'] = $this->language->get('error_select_card');
    257 					}
    258 				} else {
    259 					$this->model_extension_payment_cardconnect->log('Data missing');
    260 
    261 					$json['error'] = $this->language->get('error_data_missing');
    262 				}
    263 			} else {
    264 				$this->model_extension_payment_cardconnect->log('Not logged in');
    265 
    266 				$json['error'] = $this->language->get('error_not_logged_in');
    267 			}
    268 		} else {
    269 			$this->model_extension_payment_cardconnect->log('Module not enabled');
    270 
    271 			$json['error']['warning'] = $this->language->get('error_not_enabled');
    272 		}
    273 
    274 		$this->response->addHeader('Content-Type: application/json');
    275 		$this->response->setOutput(json_encode($json));
    276 	}
    277 
    278 	public function cron() {
    279 		$this->load->model('extension/payment/cardconnect');
    280 
    281 		$this->model_extension_payment_cardconnect->log('Running cron');
    282 
    283 		if ($this->config->get('cardconnect_status')) {
    284 			if (isset($this->request->get['token']) && hash_equals($this->config->get('cardconnect_token'), $this->request->get['token'])) {
    285 				$date = date('md', strtotime('yesterday'));
    286 
    287 				$responses = $this->model_extension_payment_cardconnect->getSettlementStatuses($this->config->get('payment_cardconnect_merchant_id'), $date);
    288 
    289 				foreach($responses as $response) {
    290 					foreach($response['txns'] as $transaction) {
    291 						$this->model_extension_payment_cardconnect->updateTransactionStatusByRetref($transaction['retref'], $transaction['setlstat']);
    292 					}
    293 				}
    294 
    295 				$this->model_extension_payment_cardconnect->updateCronRunTime();
    296 			} else {
    297 				$this->model_extension_payment_cardconnect->log('Token does not match.');
    298 			}
    299 		} else {
    300 			$this->model_extension_payment_cardconnect->log('Module not enabled');
    301 		}
    302 	}
    303 
    304 	private function validate() {
    305 		$this->load->language('extension/payment/cardconnect');
    306 
    307 		$this->load->model('extension/payment/cardconnect');
    308 
    309 		$error = array();
    310 
    311 		if (!isset($this->request->post['method']) || $this->request->post['method'] == 'card') {
    312 			if ($this->request->post['card_new']) {
    313 				if (!isset($this->request->post['card_number']) || utf8_strlen($this->request->post['card_number']) < 1 || utf8_strlen($this->request->post['card_number']) > 19) {
    314 					$error['card_number'] = $this->language->get('error_card_number');
    315 				}
    316 
    317 				if (!isset($this->request->post['card_cvv2']) || utf8_strlen($this->request->post['card_cvv2']) < 1 || utf8_strlen($this->request->post['card_cvv2']) > 4) {
    318 					$error['card_cvv2'] = $this->language->get('error_card_cvv2');
    319 				}
    320 			} else {
    321 				if (isset($this->request->post['card_choice']) && $this->request->post['card_choice']) {
    322 					$card = $this->model_extension_payment_cardconnect->getCard($this->request->post['card_choice'], $this->customer->getId());
    323 
    324 					if (!$card) {
    325 						$error['card_choice'] = $this->language->get('error_no_card');
    326 					}
    327 				} else {
    328 					$error['card_choice'] = $this->language->get('error_select_card');
    329 				}
    330 			}
    331 		} else {
    332 			if ($this->config->get('cardconnect_echeck')) {
    333 				if (!isset($this->request->post['account_number']) || utf8_strlen($this->request->post['account_number']) < 1 || utf8_strlen($this->request->post['account_number']) > 19) {
    334 					$error['account_number'] = $this->language->get('error_account_number');
    335 				}
    336 
    337 				if (!isset($this->request->post['routing_number']) || utf8_strlen($this->request->post['routing_number']) < 1 || utf8_strlen($this->request->post['routing_number']) > 9) {
    338 					$error['routing_number'] = $this->language->get('error_routing_number');
    339 				}
    340 			} else {
    341 				$error['method'] = $this->language->get('error_no_echeck');
    342 			}
    343 		}
    344 
    345 		return $error;
    346 	}
    347 }