shop.balmet.com

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

cardinity.php (10756B)


      1 <?php
      2 class ControllerExtensionPaymentCardinity extends Controller {
      3 	private $error = array();
      4 
      5 	public function index() {
      6 		$this->load->language('extension/payment/cardinity');
      7 
      8 		$this->document->setTitle($this->language->get('heading_title'));
      9 
     10 		$this->load->model('setting/setting');
     11 
     12 		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
     13 			$this->model_setting_setting->editSetting('payment_cardinity', $this->request->post);
     14 
     15 			$this->session->data['success'] = $this->language->get('text_success');
     16 
     17 			$this->response->redirect($this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=payment', true));
     18 		}
     19 
     20 		if (isset($this->error['warning'])) {
     21 			$data['error_warning'] = $this->error['warning'];
     22 		} else {
     23 			$data['error_warning'] = '';
     24 		}
     25 
     26 		if (isset($this->error['key'])) {
     27 			$data['error_key'] = $this->error['key'];
     28 		} else {
     29 			$data['error_key'] = '';
     30 		}
     31 
     32 		if (isset($this->error['secret'])) {
     33 			$data['error_secret'] = $this->error['secret'];
     34 		} else {
     35 			$data['error_secret'] = '';
     36 		}
     37 
     38 		$data['breadcrumbs'] = array();
     39 
     40 		$data['breadcrumbs'][] = array(
     41 			'text' => $this->language->get('text_home'),
     42 			'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true)
     43 		);
     44 
     45 		$data['breadcrumbs'][] = array(
     46 			'text' => $this->language->get('text_extension'),
     47 			'href' => $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=payment', true)
     48 		);
     49 
     50 		$data['breadcrumbs'][] = array(
     51 			'text' => $this->language->get('heading_title'),
     52 			'href' => $this->url->link('extension/payment/cardinity', 'user_token=' . $this->session->data['user_token'], true)
     53 		);
     54 
     55 		$data['action'] = $this->url->link('extension/payment/cardinity', 'user_token=' . $this->session->data['user_token'], true);
     56 
     57 		$data['cancel'] = $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=payment', true);
     58 
     59 		if (isset($this->request->post['payment_cardinity_key'])) {
     60 			$data['payment_cardinity_key'] = $this->request->post['payment_cardinity_key'];
     61 		} else {
     62 			$data['payment_cardinity_key'] = $this->config->get('payment_cardinity_key');
     63 		}
     64 
     65 		if (isset($this->request->post['payment_cardinity_secret'])) {
     66 			$data['payment_cardinity_secret'] = $this->request->post['payment_cardinity_secret'];
     67 		} else {
     68 			$data['payment_cardinity_secret'] = $this->config->get('payment_cardinity_secret');
     69 		}
     70 
     71 		if (isset($this->request->post['payment_cardinity_debug'])) {
     72 			$data['payment_cardinity_debug'] = $this->request->post['payment_cardinity_debug'];
     73 		} else {
     74 			$data['payment_cardinity_debug'] = $this->config->get('payment_cardinity_debug');
     75 		}
     76 
     77 		if (isset($this->request->post['payment_cardinity_total'])) {
     78 			$data['payment_cardinity_total'] = $this->request->post['payment_cardinity_total'];
     79 		} else {
     80 			$data['payment_cardinity_total'] = $this->config->get('payment_cardinity_total');
     81 		}
     82 
     83 		if (isset($this->request->post['payment_cardinity_order_status_id'])) {
     84 			$data['payment_cardinity_order_status_id'] = $this->request->post['payment_cardinity_order_status_id'];
     85 		} else {
     86 			$data['payment_cardinity_order_status_id'] = $this->config->get('payment_cardinity_order_status_id');
     87 		}
     88 
     89 		$this->load->model('localisation/order_status');
     90 
     91 		$data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();
     92 
     93 		if (isset($this->request->post['payment_cardinity_geo_zone_id'])) {
     94 			$data['payment_cardinity_geo_zone_id'] = $this->request->post['payment_cardinity_geo_zone_id'];
     95 		} else {
     96 			$data['payment_cardinity_geo_zone_id'] = $this->config->get('payment_cardinity_geo_zone_id');
     97 		}
     98 
     99 		$this->load->model('localisation/geo_zone');
    100 
    101 		$data['geo_zones'] = $this->model_localisation_geo_zone->getGeoZones();
    102 
    103 		if (isset($this->request->post['payment_cardinity_status'])) {
    104 			$data['payment_cardinity_status'] = $this->request->post['payment_cardinity_status'];
    105 		} else {
    106 			$data['payment_cardinity_status'] = $this->config->get('payment_cardinity_status');
    107 		}
    108 
    109 		if (isset($this->request->post['payment_cardinity_sort_order'])) {
    110 			$data['payment_cardinity_sort_order'] = $this->request->post['payment_cardinity_sort_order'];
    111 		} else {
    112 			$data['payment_cardinity_sort_order'] = $this->config->get('payment_cardinity_sort_order');
    113 		}
    114 		
    115 		$data['header'] = $this->load->controller('common/header');
    116 		$data['column_left'] = $this->load->controller('common/column_left');
    117 		$data['footer'] = $this->load->controller('common/footer');
    118 
    119 		$this->response->setOutput($this->load->view('extension/payment/cardinity', $data));
    120 	}
    121 
    122 	public function order() {
    123 		$this->load->language('extension/payment/cardinity');
    124 
    125 		$data['user_token'] = $this->session->data['user_token'];
    126 		$data['order_id'] = $this->request->get['order_id'];
    127 
    128 		return $this->load->view('extension/payment/cardinity_order', $data);
    129 	}
    130 
    131 	public function getPayment() {
    132 		$this->load->language('extension/payment/cardinity');
    133 
    134 		$this->load->model('extension/payment/cardinity');
    135 
    136 		$data['column_refund'] = $this->language->get('column_refund');
    137 		$data['column_date'] = $this->language->get('column_date');
    138 		$data['column_refund_history'] = $this->language->get('column_refund_history');
    139 		$data['column_action'] = $this->language->get('column_action');
    140 		$data['column_status'] = $this->language->get('column_status');
    141 		$data['column_amount'] = $this->language->get('column_amount');
    142 		$data['column_description'] = $this->language->get('column_description');
    143 
    144 		$data['button_refund'] = $this->language->get('button_refund');
    145 
    146 		$data['user_token'] = $this->session->data['user_token'];
    147 
    148 		$client = $this->model_extension_payment_cardinity->createClient(array(
    149 			'key'    => $this->config->get('payment_cardinity_key'),
    150 			'secret' => $this->config->get('payment_cardinity_secret')
    151 		));
    152 
    153 		$order = $this->model_extension_payment_cardinity->getOrder($this->request->get['order_id']);
    154 
    155 		$data['payment'] = false;
    156 
    157 		$data['refunds'] = array();
    158 
    159 		if ($order && $order['payment_id']) {
    160 			$data['payment'] = true;
    161 
    162 			$payment = $this->model_extension_payment_cardinity->getPayment($client, $order['payment_id']);
    163 
    164 			$data['refund_action'] = false;
    165 
    166 			$successful_statuses = array(
    167 				'approved'
    168 			);
    169 
    170 			if (in_array($payment->getStatus(), $successful_statuses)) {
    171 				$data['refund_action'] = true;
    172 			}
    173 
    174 			$max_refund_amount = $payment->getAmount();
    175 
    176 			$refunds = $this->model_extension_payment_cardinity->getRefunds($client, $order['payment_id']);
    177 
    178 			if ($refunds) {
    179 				foreach ($refunds as $refund) {
    180 					$successful_refund_statuses = array(
    181 						'approved'
    182 					);
    183 
    184 					if (in_array($refund->getStatus(), $successful_refund_statuses)) {
    185 						$max_refund_amount -= $refund->getAmount();
    186 					}
    187 
    188 					$data['refunds'][] = array(
    189 						'date_added'  => date($this->language->get('datetime_format'), strtotime($refund->getCreated())),
    190 						'amount'	  => $this->currency->format($refund->getAmount(), $refund->getCurrency(), '1.00000000', true),
    191 						'status'	  => $refund->getStatus(),
    192 						'description' => $refund->getDescription()
    193 					);
    194 				}
    195 			}
    196 
    197 			if (!$max_refund_amount) {
    198 				$data['refund_action'] = false;
    199 			}
    200 
    201 			$data['payment_id'] = $payment->getId();
    202 			$data['symbol_left'] = $this->currency->getSymbolLeft($payment->getCurrency());
    203 			$data['symbol_right'] = $this->currency->getSymbolRight($payment->getCurrency());
    204 
    205 			$data['max_refund_amount'] = $this->currency->format($max_refund_amount, $payment->getCurrency(), '1.00000000', false);
    206 		}
    207 
    208 		$this->response->setOutput($this->load->view('extension/payment/cardinity_order_ajax', $data));
    209 	}
    210 
    211 	public function refund() {
    212 		$this->load->language('extension/payment/cardinity');
    213 
    214 		$this->load->model('extension/payment/cardinity');
    215 
    216 		$json = array();
    217 
    218 		$success = $error = '';
    219 
    220 		$client = $this->model_extension_payment_cardinity->createClient(array(
    221 			'key'    => $this->config->get('payment_cardinity_key'),
    222 			'secret' => $this->config->get('payment_cardinity_secret')
    223 		));
    224 
    225 		$refund = $this->model_extension_payment_cardinity->refundPayment($client, $this->request->post['payment_id'], (float)number_format($this->request->post['amount'], 2), $this->request->post['description']);
    226 
    227 		if ($refund) {
    228 			$success = $this->language->get('text_success_action');
    229 		} else {
    230 			$error = $this->language->get('text_error_generic');
    231 		}
    232 
    233 		$json['success'] = $success;
    234 		$json['error'] = $error;
    235 
    236 		$this->response->addHeader('Content-Type: application/json');
    237 		$this->response->setOutput(json_encode($json));
    238 	}
    239 
    240 	protected function validate() {
    241 		$this->load->model('extension/payment/cardinity');
    242 
    243 		$check_credentials = true;
    244 
    245 		if (version_compare(phpversion(), '5.4.0', '<')) {
    246 			$this->error['warning'] = $this->language->get('error_php_version');
    247 		}
    248 
    249 		if (!$this->user->hasPermission('modify', 'extension/payment/cardinity')) {
    250 			$this->error['warning'] = $this->language->get('error_permission');
    251 
    252 			$check_credentials = false;
    253 		}
    254 
    255 		if (!$this->request->post['payment_cardinity_key']) {
    256 			$this->error['key'] = $this->language->get('error_key');
    257 
    258 			$check_credentials = false;
    259 		}
    260 
    261 		if (!$this->request->post['payment_cardinity_secret']) {
    262 			$this->error['secret'] = $this->language->get('error_secret');
    263 
    264 			$check_credentials = false;
    265 		}
    266 
    267 		if (!class_exists('Cardinity\Client')) {
    268 			$this->error['warning'] = $this->language->get('error_composer');
    269 
    270 			$check_credentials = false;
    271 		}
    272 
    273 		if ($check_credentials) {
    274 			$client = $this->model_extension_payment_cardinity->createClient(array(
    275 				'key'    => $this->request->post['payment_cardinity_key'],
    276 				'secret' => $this->request->post['payment_cardinity_secret']
    277 			));
    278 
    279 			$verify_credentials = $this->model_extension_payment_cardinity->verifyCredentials($client);
    280 
    281 			if (!$verify_credentials) {
    282 				$this->error['warning'] = $this->language->get('error_connection');
    283 			}
    284 		}
    285 
    286 		if ($this->error && !isset($this->error['warning'])) {
    287 			$this->error['warning'] = $this->language->get('error_warning');
    288 		}
    289 
    290 		return !$this->error;
    291 	}
    292 
    293 	public function install() {
    294 		$this->load->model('extension/payment/cardinity');
    295 
    296 		$this->model_extension_payment_cardinity->install();
    297 	}
    298 
    299 	public function uninstall() {
    300 		$this->load->model('extension/payment/cardinity');
    301 
    302 		$this->model_extension_payment_cardinity->uninstall();
    303 	}
    304 }