shop.balmet.com

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

laybuy.php (19471B)


      1 <?php
      2 class ControllerExtensionPaymentLaybuy extends Controller {
      3 	public function index() {
      4 		$this->load->language('extension/payment/laybuy');
      5 
      6 		$this->load->model('extension/payment/laybuy');
      7 
      8 		$this->load->model('checkout/order');
      9 
     10 		$data['action'] = $this->url->link('extension/payment/laybuy/postToLaybuy', '', true);
     11 
     12 		$order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
     13 
     14 		$data['order_info'] = $order_info;
     15 
     16 		$data['total'] = $order_info['total'];
     17 
     18 		$data['currency_symbol_left'] = $this->currency->getSymbolLeft($this->session->data['currency']);
     19 
     20 		$data['currency_symbol_right'] = $this->currency->getSymbolRight($this->session->data['currency']);
     21 
     22 		$data['initial_payments'] = $this->model_extension_payment_laybuy->getInitialPayments();
     23 
     24 		$data['months'] = $this->model_extension_payment_laybuy->getMonths();
     25 
     26 		return $this->load->view('extension/payment/laybuy', $data);
     27 	}
     28 
     29 	public function postToLaybuy()	{
     30 		$this->load->model('extension/payment/laybuy');
     31 
     32 		$this->model_extension_payment_laybuy->log('Posting to Laybuy');
     33 
     34 		if ($this->request->server['REQUEST_METHOD'] == 'POST') {
     35 			$this->load->model('checkout/order');
     36 
     37 			$order_info = $this->model_checkout_order->getOrder($this->session->data['order_id']);
     38 
     39 			if ($order_info) {
     40 				$this->model_extension_payment_laybuy->log('Order ID: ' . $order_info['order_id']);
     41 
     42 				$data = array();
     43 
     44 				$data['VERSION']      = '0.2';
     45 				$data['MEMBER']       = $this->config->get('payment_laybuys_membership_id');
     46 				$data['RETURNURL']    = $this->url->link('extension/payment/laybuy/callback', '', true);
     47 				$data['CANCELURL']    = $this->url->link('extension/payment/laybuy/cancel', '', true);
     48 				$data['AMOUNT']       = round(floatval($order_info['total']), 2, PHP_ROUND_HALF_DOWN);
     49 				$data['CURRENCY']     = $order_info['currency_code'];
     50 				$data['INIT']         = (int)$this->request->post['INIT'];
     51 				$data['MONTHS']       = (int)$this->request->post['MONTHS'];
     52 				$data['MIND']         = ((int)$this->config->get('payment_laybuy_min_deposit')) ? (int)$this->config->get('payment_laybuy_min_deposit') : 20;
     53 				$data['MAXD']         = ((int)$this->config->get('payment_laybuy_max_deposit')) ? (int)$this->config->get('payment_laybuy_max_deposit') : 50;
     54 				$data['CUSTOM']       = $order_info['order_id'] . ':' . md5($this->config->get('payment_laybuy_token'));
     55 				$data['EMAIL']        = $order_info['email'];
     56 
     57 				$data_string = '';
     58 
     59 				foreach ($data as $param => $value) {
     60 					$data_string .= $param . '=' . $value . '&';
     61 				}
     62 
     63 				$data_string = rtrim($data_string, '&');
     64 
     65 				$this->model_extension_payment_laybuy->log('Data String: ' . $data_string);
     66 
     67 				$this->model_extension_payment_laybuy->log('Gateway URL: ' . $this->config->get('payment_laybuy_gateway_url'));
     68 
     69 				$ch = curl_init();
     70 				curl_setopt($ch, CURLOPT_URL, $this->config->get('payment_laybuy_gateway_url'));
     71 				curl_setopt($ch, CURLOPT_POST, true);
     72 				curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
     73 				curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     74 				curl_setopt($ch, CURLOPT_HEADER, false);
     75 				curl_setopt($ch, CURLOPT_TIMEOUT, 30);
     76 				curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     77 				$result = curl_exec($ch);
     78 				if (curl_errno($ch)) {
     79 					$this->model_extension_payment_laybuy->log('cURL error: ' . curl_errno($ch));
     80 				}
     81 				curl_close($ch);
     82 
     83 				$result = json_decode($result, true);
     84 
     85 				$this->model_extension_payment_laybuy->log('Response: ' . print_r($result, true));
     86 
     87 			 	if (isset($result['ACK']) && isset($result['TOKEN']) && $result['ACK'] == 'SUCCESS') {
     88 					$this->model_extension_payment_laybuy->log('Success response. Redirecting to PayPal.');
     89 
     90 					$this->response->redirect($this->config->get('payment_laybuy_gateway_url') . '?TOKEN=' . $result['TOKEN']);
     91 				} else {
     92 					$this->model_extension_payment_laybuy->log('Failure response. Redirecting to checkout/failure.');
     93 
     94 					$this->response->redirect($this->url->link('checkout/failure', '', true));
     95 				}
     96 			} else {
     97 				$this->model_extension_payment_laybuy->log('No matching order. Redirecting to checkout/failure.');
     98 
     99 				$this->response->redirect($this->url->link('checkout/failure', '', true));
    100 			}
    101 		} else {
    102 			$this->model_extension_payment_laybuy->log('No $_POST data. Redirecting to checkout/failure.');
    103 
    104 			$this->response->redirect($this->url->link('checkout/failure', '', true));
    105 		}
    106 	}
    107 
    108 	public function callback() {
    109 		$this->load->model('extension/payment/laybuy');
    110 
    111 		$this->model_extension_payment_laybuy->log('Receiving callback');
    112 
    113 		if ($this->request->server['REQUEST_METHOD'] == 'POST' && isset($this->request->post['RESULT']) && $this->request->post['RESULT'] == 'SUCCESS') {
    114 			$this->load->model('checkout/order');
    115 
    116 			$custom = $this->request->post['CUSTOM'];
    117 
    118 			$custom = explode(':', $custom);
    119 
    120 			$order_id = $custom[0];
    121 
    122 			$token = $custom[1];
    123 
    124 			$this->model_extension_payment_laybuy->log('Received Token: ' . $token);
    125 
    126 			$this->model_extension_payment_laybuy->log('Actual Token: ' . md5($this->config->get('payment_laybuy_token')));
    127 
    128 			if (hash_equals(md5($this->config->get('payment_laybuy_token')), $token)) {
    129 				$this->model_extension_payment_laybuy->log('Order ID: ' . $order_id);
    130 
    131 				$order_info = $this->model_checkout_order->getOrder($order_id);
    132 
    133 				if ($order_info) {
    134 					$this->model_checkout_order->addOrderHistory($order_id, $this->config->get('payment_laybuy_order_status_id_pending'));
    135 
    136 					$transaction_report = $this->model_extension_payment_laybuy->prepareTransactionReport($this->request->post);
    137 
    138 					$this->model_extension_payment_laybuy->addTransaction($transaction_report, 1);
    139 
    140 					$this->model_extension_payment_laybuy->log('Success. Redirecting to checkout/success.');
    141 
    142 					$this->response->redirect($this->url->link('checkout/success', '', true));
    143 				} else {
    144 					$this->model_extension_payment_laybuy->log('No matching order. Redirecting to checkout/failure.');
    145 
    146 					$this->response->redirect($this->url->link('checkout/failure', '', true));
    147 				}
    148 			} else {
    149 				$this->model_extension_payment_laybuy->log('Token does not match. Redirecting to checkout/failure.');
    150 
    151 				$this->response->redirect($this->url->link('checkout/failure', '', true));
    152 			}
    153 		} elseif ($this->request->server['REQUEST_METHOD'] == 'POST' && isset($this->request->post['RESULT']) && $this->request->post['RESULT'] == 'FAILURE') {
    154 			$this->model_extension_payment_laybuy->log('Failure Response: ' . $this->request->post);
    155 
    156 			$this->model_extension_payment_laybuy->log('Redirecting to checkout/failure.');
    157 
    158 			$this->response->redirect($this->url->link('checkout/failure', '', true));
    159 		} else {
    160 			$this->model_extension_payment_laybuy->log('Either no $_POST data or unknown response. Redirecting to checkout/failure.');
    161 
    162 			$this->response->redirect($this->url->link('checkout/failure', '', true));
    163 		}
    164 	}
    165 
    166 	public function cancel() {
    167 		$this->load->model('extension/payment/laybuy');
    168 
    169 		$this->model_extension_payment_laybuy->log('Transaction canceled by user. Redirecting to checkout/checkout.');
    170 
    171 		$this->response->redirect($this->url->link('checkout/checkout', '', true));
    172 	}
    173 
    174 	public function reviseCallback() {
    175 		$this->load->model('extension/payment/laybuy');
    176 
    177 		$this->load->language('extension/payment/laybuy');
    178 
    179 		$this->model_extension_payment_laybuy->log('Receiving callback');
    180 
    181 		if ($this->request->server['REQUEST_METHOD'] == 'POST') {
    182 			if (isset($this->request->post['RESULT']) && $this->request->post['RESULT'] == 'SUCCESS') {
    183 				$this->load->model('checkout/order');
    184 
    185 				$custom = $this->request->post['CUSTOM'];
    186 
    187 				$custom = explode(':', $custom);
    188 
    189 				$order_id = $custom[0];
    190 
    191 				$token = $custom[1];
    192 
    193 				$this->model_extension_payment_laybuy->log('Received Token: ' . $token);
    194 
    195 				$this->model_extension_payment_laybuy->log('Actual Token: ' . md5($this->config->get('payment_laybuy_token')));
    196 
    197 				if (hash_equals(md5($this->config->get('payment_laybuy_token')), $token)) {
    198 					$this->model_extension_payment_laybuy->log('Order ID: ' . $order_id);
    199 
    200 					$order_info = $this->model_checkout_order->getOrder($order_id);
    201 
    202 					if ($order_info) {
    203 						$response = $this->request->post;
    204 
    205 						$this->model_extension_payment_laybuy->log('Response: ' . print_r($response, true));
    206 
    207 						$revised_transaction_id = $response['MERCHANTS_REF_NO'];
    208 
    209 						$revised_transaction = $this->model_extension_payment_laybuy->getRevisedTransaction($revised_transaction_id);
    210 
    211 						$this->model_extension_payment_laybuy->log('Revised transaction: ' . print_r($revised_transaction, true));
    212 
    213 						$status = 1;
    214 
    215 						$current_date = date('Y-m-d h:i:s');
    216 
    217 						if (!isset($response['DOWNPAYMENT']) && !$revised_transaction['payment_type']) {
    218 							$this->model_extension_payment_laybuy->log('Buy-Now');
    219 
    220 							$response['DOWNPAYMENT'] = 100;
    221 							$response['MONTHS'] = 0;
    222 							$response['DOWNPAYMENT_AMOUNT'] = $response['AMOUNT'];
    223 							$response['PAYMENT_AMOUNTS'] = 0;
    224 							$response['FIRST_PAYMENT_DUE'] = $current_date;
    225 							$response['LAST_PAYMENT_DUE'] = $current_date;
    226 							$response['PAYPAL_PROFILE_ID'] = '';
    227 
    228 							$status = 5;
    229 
    230 							$this->model_checkout_order->addOrderHistory($order_id, $this->config->get('payment_laybuy_order_status_id_processing'), $this->language->get('text_comment'));
    231 						} else {
    232 							$this->model_extension_payment_laybuy->log('Lay-Buy');
    233 						}
    234 
    235 						$this->session->data['order_id'] = $order_id;
    236 
    237 						$transaction_report = $this->model_extension_payment_laybuy->prepareTransactionReport($response);
    238 
    239 						$transaction_report['order_id'] = $order_id;
    240 
    241 						$this->model_extension_payment_laybuy->addTransaction($transaction_report, $status);
    242 
    243 						$old_transaction = $this->model_extension_payment_laybuy->getTransaction($revised_transaction['laybuy_transaction_id']);
    244 
    245 						$report_content = json_decode($old_transaction['report'], true);
    246 
    247 						foreach ($report_content as &$array) {
    248 							$array['status'] = str_replace('Pending', 'Canceled', $array['status']);
    249 						}
    250 
    251 						$report_content = json_encode($report_content);
    252 
    253 						if ($old_transaction['paypal_profile_id']) {
    254 							$this->model_extension_payment_laybuy->log('Canceling transaction');
    255 
    256 							$data_string = 'mid=' . $this->config->get('payment_laybuys_membership_id') . '&' . 'paypal_profile_id=' . $old_transaction['paypal_profile_id'];
    257 
    258 							$this->model_extension_payment_laybuy->log('Data String: ' . $data_string);
    259 
    260 							$ch = curl_init();
    261 							$url = 'https://lay-buys.com/vtmob/deal5cancel.php';
    262 							curl_setopt($ch, CURLOPT_URL, $url);
    263 							curl_setopt($ch, CURLOPT_POST, true);
    264 							curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    265 							curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    266 							curl_setopt($ch, CURLOPT_HEADER, false);
    267 							curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    268 							curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    269 							$result = curl_exec($ch);
    270 							if (curl_errno($ch)) {
    271 								$this->model_extension_payment_laybuy->log('cURL error: ' . curl_errno($ch));
    272 							}
    273 							curl_close($ch);
    274 
    275 							$this->model_extension_payment_laybuy->log('Response: ' . $result);
    276 
    277 							if ($result == 'success') {
    278 								$this->model_extension_payment_laybuy->log('Success');
    279 							} else {
    280 								$this->model_extension_payment_laybuy->log('Failure');
    281 							}
    282 						} else {
    283 							$this->model_extension_payment_laybuy->log('Transaction has no paypal_profile_id');
    284 						}
    285 
    286 						$this->model_extension_payment_laybuy->updateTransaction($old_transaction['laybuy_transaction_id'], '51', $report_content, $old_transaction['transaction']);
    287 
    288 						$this->model_extension_payment_laybuy->deleteRevisedTransaction($revised_transaction['laybuy_revise_request_id']);
    289 
    290 						$this->response->redirect($this->url->link('checkout/success', '', true));
    291 					} else {
    292 						$this->model_extension_payment_laybuy->log('No matching order. Redirecting to checkout/failure.');
    293 
    294 						$this->response->redirect($this->url->link('checkout/failure', '', true));
    295 					}
    296 				} else {
    297 					$this->model_extension_payment_laybuy->log('Token does not match. Redirecting to checkout/failure.');
    298 
    299 					$this->response->redirect($this->url->link('checkout/failure', '', true));
    300 				}
    301 			} else {
    302 				$this->model_extension_payment_laybuy->log('No success response');
    303 
    304 				$this->response->redirect($this->url->link('checkout/failure', '', true));
    305 			}
    306 		} else {
    307 			$this->model_extension_payment_laybuy->log('No $_POST data');
    308 
    309 			$this->response->redirect($this->url->link('checkout/failure', '', true));
    310 		}
    311 	}
    312 
    313 	public function reviseCancel() {
    314 		$this->load->model('extension/payment/laybuy');
    315 
    316 		$this->model_extension_payment_laybuy->log('Revise canceled. Redirecting to checkout/checkout.');
    317 
    318 		$this->response->redirect($this->url->link('checkout/checkout', '', true));
    319 	}
    320 
    321 	public function deleteOrder($route = '', $output = '', $order_id = 0, $order_status_id = 0) {
    322 		$this->load->model('extension/payment/laybuy');
    323 
    324 		if (isset($this->session->data['api_id'])) {
    325 			$this->model_extension_payment_laybuy->log('Deleting order #' . $order_id);
    326 
    327 			$this->model_extension_payment_laybuy->deleteTransactionByOrderId($order_id);
    328 		} else {
    329 			$this->model_extension_payment_laybuy->log('No API ID in session');
    330 		}
    331 	}
    332 
    333 	public function cron() {
    334 		$this->load->model('extension/payment/laybuy');
    335 
    336 		$this->load->language('extension/payment/laybuy');
    337 
    338 		$this->model_extension_payment_laybuy->log('Running cron');
    339 
    340 		if (isset($this->request->get['token']) && hash_equals($this->config->get('payment_laybuy_token'), $this->request->get['token'])) {
    341 			$paypal_profile_id_array = $this->model_extension_payment_laybuy->getPayPalProfileIds();
    342 
    343 			if ($paypal_profile_id_array) {
    344 				$paypal_profile_ids = '';
    345 
    346 				foreach ($paypal_profile_id_array as $profile_id) {
    347 					$paypal_profile_ids .= $profile_id['paypal_profile_id'] . ',';
    348 				}
    349 
    350 				$paypal_profile_ids = rtrim($paypal_profile_ids, ',');
    351 
    352 				$data_string = 'mid=' . $this->config->get('payment_laybuys_membership_id') . '&' . 'profileIds=' . $paypal_profile_ids;
    353 
    354 				$this->model_extension_payment_laybuy->log('Data String: ' . $data_string);
    355 
    356 				$this->model_extension_payment_laybuy->log('API URL: ' . $this->config->get('payment_laybuy_api_url'));
    357 
    358 				$ch = curl_init();
    359 				curl_setopt($ch, CURLOPT_URL, $this->config->get('payment_laybuy_api_url'));
    360 				curl_setopt($ch, CURLOPT_POST, true);
    361 				curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
    362 				curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    363 				curl_setopt($ch, CURLOPT_HEADER, false);
    364 				curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    365 				curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    366 				$result = curl_exec($ch);
    367 				if (curl_errno($ch)) {
    368 					$this->model_extension_payment_laybuy->log('cURL error: ' . curl_errno($ch));
    369 				}
    370 				curl_close($ch);
    371 
    372 				$results = json_decode($result, true);
    373 
    374 				$this->model_extension_payment_laybuy->log('Response: ' . print_r($results, true));
    375 
    376 				if ($results) {
    377 					$this->load->model('checkout/order');
    378 
    379 					foreach ($results as $laybuy_ref_id => $reports) {
    380 						$status = $reports['status'];
    381 
    382 						$report = $reports['report'];
    383 
    384 						$transaction = array();
    385 
    386 						$transaction = $this->model_extension_payment_laybuy->getTransactionByLayBuyRefId($laybuy_ref_id);
    387 
    388 						$order_id = $transaction['order_id'];
    389 
    390 						$paypal_profile_id = $transaction['paypal_profile_id'];
    391 
    392 						$months = $transaction['months'];
    393 
    394 						$report_content = array();
    395 
    396 						$pending_flag = false;
    397 
    398 						$next_payment_status = $this->language->get('text_status_1');
    399 
    400 						foreach ($report as $month => $payment) {
    401 							$payment['paymentDate'] = date('Y-m-d h:i:s', strtotime(str_replace('/', '-', $payment['paymentDate'])));
    402 							$date = date($this->language->get('date_format_short'), strtotime($payment['paymentDate']));
    403 							$next_payment_date = $payment['paymentDate'];
    404 
    405 							if ($payment['type'] == 'd') {
    406 								$report_content[] = array(
    407 									'instalment'	=> 0,
    408 									'amount'		=> $this->currency->format($payment['amount'], $transaction['currency']),
    409 									'date'			=> $date,
    410 									'pp_trans_id'	=> $payment['txnID'],
    411 									'status'		=> $payment['paymentStatus']
    412 								);
    413 							} elseif ($payment['type'] == 'p') {
    414 								$pending_flag = true;
    415 
    416 								$report_content[] = array(
    417 									'instalment'	=> $month,
    418 									'amount'		=> $this->currency->format($payment['amount'], $transaction['currency']),
    419 									'date'			=> $date,
    420 									'pp_trans_id'	=> $payment['txnID'],
    421 									'status'		=> $payment['paymentStatus']
    422 								);
    423 
    424 								$next_payment_status = $payment['paymentStatus'];
    425 							}
    426 						}
    427 
    428 						if ($pending_flag) {
    429 							$start_index = $month + 1;
    430 						} else {
    431 							$start_index = $month + 2;
    432 						}
    433 
    434 						if ($month < $months) {
    435 							for ($month = 1; $month <= $months; $month++) {
    436 								$next_payment_date = date("Y-m-d h:i:s", strtotime($next_payment_date . " +1 month"));
    437 								$date = date($this->language->get('date_format_short'), strtotime($next_payment_date));
    438 
    439 								$report_content[] = array(
    440 									'instalment'	=> $month,
    441 									'amount'		=> $this->currency->format($transaction['payment_amounts'], $transaction['currency']),
    442 									'date'			=> $date,
    443 									'pp_trans_id'	=> '',
    444 									'status'		=> $next_payment_status
    445 								);
    446 							}
    447 						}
    448 
    449 						$report_content = json_encode($report_content);
    450 
    451 						switch ($status) {
    452 							case -1: // Cancel
    453 								$this->model_extension_payment_laybuy->log('Transaction #' . $transaction['laybuy_transaction_id'] . ' canceled');
    454 								$this->model_checkout_order->addOrderHistory($order_id, $this->config->get('payment_laybuy_order_status_id_canceled'), $this->language->get('text_comment'), false, false);
    455 								$this->model_extension_payment_laybuy->updateTransaction($transaction['laybuy_transaction_id'], '7', $report_content, $start_index);
    456 								break;
    457 							case 0: // Pending
    458 								$this->model_extension_payment_laybuy->log('Transaction #' . $transaction['laybuy_transaction_id'] . ' still pending');
    459 								$this->model_extension_payment_laybuy->updateTransaction($transaction['laybuy_transaction_id'], $transaction['status'], $report_content, $start_index);
    460 								break;
    461 							case 1: // Paid
    462 								$this->model_extension_payment_laybuy->log('Transaction #' . $transaction['laybuy_transaction_id'] . ' paid');
    463 								$this->model_checkout_order->addOrderHistory($order_id, $this->config->get('payment_laybuy_order_status_id_processing'), $this->language->get('text_comment'), false, false);
    464 								$this->model_extension_payment_laybuy->updateTransaction($transaction['laybuy_transaction_id'], '5', $report_content, $start_index);
    465 								break;
    466 						}
    467 					}
    468 				}
    469 			} else {
    470 				$this->model_extension_payment_laybuy->log('No PayPal Profile IDs to update');
    471 			}
    472 
    473 			$this->model_extension_payment_laybuy->updateCronRunTime();
    474 		} else {
    475 			$this->model_extension_payment_laybuy->log('Token does not match.');
    476 		}
    477 	}
    478 }