shop.balmet.com

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

pp_express.php (54337B)


      1 <?php
      2 class ControllerExtensionPaymentPPExpress extends Controller {
      3 	private $error = array();
      4 	private $opencart_connect_url = 'https://www.opencart.com/index.php?route=external/paypal_auth/connect';
      5 	private $opencart_retrieve_url = 'https://www.opencart.com/index.php?route=external/paypal_auth/retrieve';
      6 
      7 	public function index() {
      8 		$this->load->language('extension/payment/pp_express');
      9 
     10 		$this->document->setTitle($this->language->get('heading_title'));
     11 
     12 		$this->load->model('setting/setting');
     13 
     14 		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
     15 			$this->model_setting_setting->editSetting('payment_pp_express', $this->request->post);
     16 
     17 			$this->session->data['success'] = $this->language->get('text_success');
     18 
     19 			$this->response->redirect($this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=payment', true));
     20 		}
     21 
     22 		$data['user_token'] = $this->session->data['user_token'];
     23 
     24 		$data['button_configure'] = $this->url->link('extension/module/pp_button/configure', 'user_token=' . $this->session->data['user_token'], true);
     25 
     26 		if (isset($this->error['warning'])) {
     27 			$data['error_warning'] = $this->error['warning'];
     28 		} else {
     29 			$data['error_warning'] = '';
     30 		}
     31 
     32 		if (isset($this->error['username'])) {
     33 			$data['error_username'] = $this->error['username'];
     34 		} else {
     35 			$data['error_username'] = '';
     36 		}
     37 
     38 		if (isset($this->error['password'])) {
     39 			$data['error_password'] = $this->error['password'];
     40 		} else {
     41 			$data['error_password'] = '';
     42 		}
     43 
     44 		if (isset($this->error['signature'])) {
     45 			$data['error_signature'] = $this->error['signature'];
     46 		} else {
     47 			$data['error_signature'] = '';
     48 		}
     49 
     50 		if (isset($this->error['sandbox_username'])) {
     51 			$data['error_sandbox_username'] = $this->error['sandbox_username'];
     52 		} else {
     53 			$data['error_sandbox_username'] = '';
     54 		}
     55 
     56 		if (isset($this->error['sandbox_password'])) {
     57 			$data['error_sandbox_password'] = $this->error['sandbox_password'];
     58 		} else {
     59 			$data['error_sandbox_password'] = '';
     60 		}
     61 
     62 		if (isset($this->error['sandbox_signature'])) {
     63 			$data['error_sandbox_signature'] = $this->error['sandbox_signature'];
     64 		} else {
     65 			$data['error_sandbox_signature'] = '';
     66 		}
     67 
     68 		$data['breadcrumbs'] = array();
     69 
     70 		$data['breadcrumbs'][] = array(
     71 			'text' => $this->language->get('text_home'),
     72 			'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true),
     73 		);
     74 
     75 		$data['breadcrumbs'][] = array(
     76 			'text' => $this->language->get('text_extension'),
     77 			'href' => $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=payment', true),
     78 		);
     79 
     80 		$data['breadcrumbs'][] = array(
     81 			'text' => $this->language->get('heading_title'),
     82 			'href' => $this->url->link('extension/payment/pp_express', 'user_token=' . $this->session->data['user_token'], true),
     83 		);
     84 
     85 		$data['action'] = $this->url->link('extension/payment/pp_express', 'user_token=' . $this->session->data['user_token'], true);
     86 
     87 		$data['cancel'] = $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=payment', true);
     88 
     89 		$data['search'] = $this->url->link('extension/payment/pp_express/search', 'user_token=' . $this->session->data['user_token'], true);
     90 
     91 		if (isset($this->request->post['payment_pp_express_username'])) {
     92 			$data['payment_pp_express_username'] = $this->request->post['payment_pp_express_username'];
     93 		} else {
     94 			$data['payment_pp_express_username'] = $this->config->get('payment_pp_express_username');
     95 		}
     96 
     97 		if (isset($this->request->post['payment_pp_express_password'])) {
     98 			$data['payment_pp_express_password'] = $this->request->post['payment_pp_express_password'];
     99 		} else {
    100 			$data['payment_pp_express_password'] = $this->config->get('payment_pp_express_password');
    101 		}
    102 
    103 		if (isset($this->request->post['payment_pp_express_signature'])) {
    104 			$data['payment_pp_express_signature'] = $this->request->post['payment_pp_express_signature'];
    105 		} else {
    106 			$data['payment_pp_express_signature'] = $this->config->get('payment_pp_express_signature');
    107 		}
    108 
    109 		if (isset($this->request->post['payment_pp_express_sandbox_username'])) {
    110 			$data['payment_pp_express_sandbox_username'] = $this->request->post['payment_pp_express_sandbox_username'];
    111 		} else {
    112 			$data['payment_pp_express_sandbox_username'] = $this->config->get('payment_pp_express_sandbox_username');
    113 		}
    114 
    115 		if (isset($this->request->post['payment_pp_express_sandbox_password'])) {
    116 			$data['payment_pp_express_sandbox_password'] = $this->request->post['payment_pp_express_sandbox_password'];
    117 		} else {
    118 			$data['payment_pp_express_sandbox_password'] = $this->config->get('payment_pp_express_sandbox_password');
    119 		}
    120 
    121 		if (isset($this->request->post['payment_pp_express_sandbox_signature'])) {
    122 			$data['payment_pp_express_sandbox_signature'] = $this->request->post['payment_pp_express_sandbox_signature'];
    123 		} else {
    124 			$data['payment_pp_express_sandbox_signature'] = $this->config->get('payment_pp_express_sandbox_signature');
    125 		}
    126 
    127 		$data['ipn_url'] = HTTPS_CATALOG . 'index.php?route=extension/payment/pp_express/ipn';
    128 
    129 		if (isset($this->request->post['payment_pp_express_test'])) {
    130 			$data['payment_pp_express_test'] = $this->request->post['payment_pp_express_test'];
    131 		} else {
    132 			$data['payment_pp_express_test'] = $this->config->get('payment_pp_express_test');
    133 		}
    134 
    135 		if (isset($this->request->post['payment_pp_express_debug'])) {
    136 			$data['payment_pp_express_debug'] = $this->request->post['payment_pp_express_debug'];
    137 		} else {
    138 			$data['payment_pp_express_debug'] = $this->config->get('payment_pp_express_debug');
    139 		}
    140 
    141 		if (isset($this->request->post['payment_pp_express_incontext_disable'])) {
    142 			$data['payment_pp_express_incontext_disable'] = $this->request->post['payment_pp_express_incontext_disable'];
    143 		} else {
    144 			$data['payment_pp_express_incontext_disable'] = $this->config->get('payment_pp_express_incontext_disable');
    145 		}
    146 
    147 		if (isset($this->request->post['payment_pp_express_currency'])) {
    148 			$data['payment_pp_express_currency'] = $this->request->post['payment_pp_express_currency'];
    149 		} else {
    150 			$data['payment_pp_express_currency'] = $this->config->get('payment_pp_express_currency');
    151 		}
    152 
    153 		$this->load->model('extension/payment/pp_express');
    154 
    155 		$data['currencies'] = $this->model_extension_payment_pp_express->getCurrencies();
    156 
    157 		if (isset($this->request->post['payment_pp_express_recurring_cancel'])) {
    158 			$data['payment_pp_express_recurring_cancel'] = $this->request->post['payment_pp_express_recurring_cancel'];
    159 		} else {
    160 			$data['payment_pp_express_recurring_cancel'] = $this->config->get('payment_pp_express_recurring_cancel');
    161 		}
    162 
    163 		if (isset($this->request->post['payment_pp_express_transaction'])) {
    164 			$data['payment_pp_express_transaction'] = $this->request->post['payment_pp_express_transaction'];
    165 		} else {
    166 			$data['payment_pp_express_transaction'] = $this->config->get('payment_pp_express_transaction');
    167 		}
    168 
    169 		if (isset($this->request->post['payment_pp_express_total'])) {
    170 			$data['payment_pp_express_total'] = $this->request->post['payment_pp_express_total'];
    171 		} else {
    172 			$data['payment_pp_express_total'] = $this->config->get('payment_pp_express_total');
    173 		}
    174 
    175 		if (isset($this->request->post['payment_pp_express_geo_zone_id'])) {
    176 			$data['payment_pp_express_geo_zone_id'] = $this->request->post['payment_pp_express_geo_zone_id'];
    177 		} else {
    178 			$data['payment_pp_express_geo_zone_id'] = $this->config->get('payment_pp_express_geo_zone_id');
    179 		}
    180 
    181 		$this->load->model('localisation/geo_zone');
    182 
    183 		$data['geo_zones'] = $this->model_localisation_geo_zone->getGeoZones();
    184 
    185 		if (isset($this->request->post['payment_pp_express_status'])) {
    186 			$data['payment_pp_express_status'] = $this->request->post['payment_pp_express_status'];
    187 		} else {
    188 			$data['payment_pp_express_status'] = $this->config->get('payment_pp_express_status');
    189 		}
    190 
    191 		if (isset($this->request->post['payment_pp_express_sort_order'])) {
    192 			$data['payment_pp_express_sort_order'] = $this->request->post['payment_pp_express_sort_order'];
    193 		} else {
    194 			$data['payment_pp_express_sort_order'] = $this->config->get('payment_pp_express_sort_order');
    195 		}
    196 
    197 		if (isset($this->request->post['payment_pp_express_canceled_reversal_status_id'])) {
    198 			$data['payment_pp_express_canceled_reversal_status_id'] = $this->request->post['payment_pp_express_canceled_reversal_status_id'];
    199 		} else {
    200 			$data['payment_pp_express_canceled_reversal_status_id'] = $this->config->get('payment_pp_express_canceled_reversal_status_id');
    201 		}
    202 
    203 		if (isset($this->request->post['payment_pp_express_completed_status_id'])) {
    204 			$data['payment_pp_express_completed_status_id'] = $this->request->post['payment_pp_express_completed_status_id'];
    205 		} else {
    206 			$data['payment_pp_express_completed_status_id'] = $this->config->get('payment_pp_express_completed_status_id');
    207 		}
    208 
    209 		if (isset($this->request->post['payment_pp_express_denied_status_id'])) {
    210 			$data['payment_pp_express_denied_status_id'] = $this->request->post['payment_pp_express_denied_status_id'];
    211 		} else {
    212 			$data['payment_pp_express_denied_status_id'] = $this->config->get('payment_pp_express_denied_status_id');
    213 		}
    214 
    215 		if (isset($this->request->post['payment_pp_express_expired_status_id'])) {
    216 			$data['payment_pp_express_expired_status_id'] = $this->request->post['payment_pp_express_expired_status_id'];
    217 		} else {
    218 			$data['payment_pp_express_expired_status_id'] = $this->config->get('payment_pp_express_expired_status_id');
    219 		}
    220 
    221 		if (isset($this->request->post['payment_pp_express_failed_status_id'])) {
    222 			$data['payment_pp_express_failed_status_id'] = $this->request->post['payment_pp_express_failed_status_id'];
    223 		} else {
    224 			$data['payment_pp_express_failed_status_id'] = $this->config->get('payment_pp_express_failed_status_id');
    225 		}
    226 
    227 		if (isset($this->request->post['payment_pp_express_pending_status_id'])) {
    228 			$data['payment_pp_express_pending_status_id'] = $this->request->post['payment_pp_express_pending_status_id'];
    229 		} else {
    230 			$data['payment_pp_express_pending_status_id'] = $this->config->get('payment_pp_express_pending_status_id');
    231 		}
    232 
    233 		if (isset($this->request->post['payment_pp_express_processed_status_id'])) {
    234 			$data['payment_pp_express_processed_status_id'] = $this->request->post['payment_pp_express_processed_status_id'];
    235 		} else {
    236 			$data['payment_pp_express_processed_status_id'] = $this->config->get('payment_pp_express_processed_status_id');
    237 		}
    238 
    239 		if (isset($this->request->post['payment_pp_express_refunded_status_id'])) {
    240 			$data['payment_pp_express_refunded_status_id'] = $this->request->post['payment_pp_express_refunded_status_id'];
    241 		} else {
    242 			$data['payment_pp_express_refunded_status_id'] = $this->config->get('payment_pp_express_refunded_status_id');
    243 		}
    244 
    245 		if (isset($this->request->post['payment_pp_express_reversed_status_id'])) {
    246 			$data['payment_pp_express_reversed_status_id'] = $this->request->post['payment_pp_express_reversed_status_id'];
    247 		} else {
    248 			$data['payment_pp_express_reversed_status_id'] = $this->config->get('payment_pp_express_reversed_status_id');
    249 		}
    250 
    251 		if (isset($this->request->post['payment_pp_express_voided_status_id'])) {
    252 			$data['payment_pp_express_voided_status_id'] = $this->request->post['payment_pp_express_voided_status_id'];
    253 		} else {
    254 			$data['payment_pp_express_voided_status_id'] = $this->config->get('payment_pp_express_voided_status_id');
    255 		}
    256 
    257 		$this->load->model('localisation/order_status');
    258 
    259 		$data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();
    260 
    261 		if (isset($this->request->post['payment_pp_express_allow_note'])) {
    262 			$data['payment_pp_express_allow_note'] = $this->request->post['payment_pp_express_allow_note'];
    263 		} else {
    264 			$data['payment_pp_express_allow_note'] = $this->config->get('payment_pp_express_allow_note');
    265 		}
    266 
    267 		if (isset($this->request->post['payment_pp_express_colour'])) {
    268 			$data['payment_pp_express_colour'] = str_replace('#', '', $this->request->post['payment_pp_express_colour']);
    269 		} else {
    270 			$data['payment_pp_express_colour'] = $this->config->get('payment_pp_express_colour');
    271 		}
    272 
    273 		if (isset($this->request->post['payment_pp_express_logo'])) {
    274 			$data['payment_pp_express_logo'] = $this->request->post['payment_pp_express_logo'];
    275 		} else {
    276 			$data['payment_pp_express_logo'] = $this->config->get('payment_pp_express_logo');
    277 		}
    278 
    279 		$this->load->model('tool/image');
    280 
    281 		if (isset($this->request->post['payment_pp_express_logo']) && is_file(DIR_IMAGE . $this->request->post['payment_pp_express_logo'])) {
    282 			$data['thumb'] = $this->model_tool_image->resize($this->request->post['payment_pp_express_logo'], 750, 90);
    283 		} elseif (is_file(DIR_IMAGE . $this->config->get('payment_pp_express_logo'))) {
    284 			$data['thumb'] = $this->model_tool_image->resize($this->config->get('payment_pp_express_logo'), 750, 90);
    285 		} else {
    286 			$data['thumb'] = $this->model_tool_image->resize('no_image.png', 750, 90);
    287 		}
    288 
    289 		$data['placeholder'] = $this->model_tool_image->resize('no_image.png', 750, 90);
    290 
    291 		if (isset($this->request->get['retrieve_code']) && isset($this->request->get['merchant_id'])) {
    292 			$curl = curl_init($this->opencart_retrieve_url);
    293 
    294 			$post_data = array(
    295 				'merchant_id' => $this->request->get['merchant_id'],
    296 				'retrieve_code' => $this->request->get['retrieve_code'],
    297 			);
    298 
    299 			curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    300 			curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    301 			curl_setopt($curl, CURLOPT_FORBID_REUSE, 1);
    302 			curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);
    303 			curl_setopt($curl, CURLOPT_POST, true);
    304 			curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
    305 
    306 			$curl_response = curl_exec($curl);
    307 			$config_response = json_decode($curl_response, true);
    308 			curl_close($curl);
    309 
    310 			if (isset($config_response['api_user_name']) && isset($config_response['api_password']) && isset($config_response['signature'])) {
    311 				$pp_express_settings = $this->model_setting_setting->getSetting('payment_pp_express');
    312 
    313 				if ($config_response['environment'] == 'sandbox') {
    314 					$pp_express_settings['payment_pp_express_sandbox_username'] = $config_response['api_user_name'];
    315 					$pp_express_settings['payment_pp_express_sandbox_password'] = $config_response['api_password'];
    316 					$pp_express_settings['payment_pp_express_sandbox_signature'] = $config_response['signature'];
    317 					$pp_express_settings['payment_pp_express_test'] = 1;
    318 
    319 					$data['payment_pp_express_sandbox_username'] = $config_response['api_user_name'];
    320 					$data['payment_pp_express_sandbox_password'] = $config_response['api_password'];
    321 					$data['payment_pp_express_sandbox_signature'] = $config_response['signature'];
    322 					$data['payment_pp_express_test'] = 1;
    323 				} else {
    324 					$pp_express_settings['payment_pp_express_username'] = $config_response['api_user_name'];
    325 					$pp_express_settings['payment_pp_express_password'] = $config_response['api_password'];
    326 					$pp_express_settings['payment_pp_express_signature'] = $config_response['signature'];
    327 					$pp_express_settings['payment_pp_express_test'] = 0;
    328 
    329 					$data['payment_pp_express_username'] = $config_response['api_user_name'];
    330 					$data['payment_pp_express_password'] = $config_response['api_password'];
    331 					$data['payment_pp_express_signature'] = $config_response['signature'];
    332 					$data['payment_pp_express_test'] = 0;
    333 				}
    334 
    335 				$data['retrieve_success'] = 1;
    336 				$data['text_retrieve'] = $this->language->get('text_retrieve');
    337 
    338 				$this->model_setting_setting->editSetting('payment_pp_express', $pp_express_settings);
    339 			}
    340 		}
    341 
    342 		$this->load->model('localisation/country');
    343 
    344 		$country = $this->model_localisation_country->getCountry($this->config->get('config_country_id'));
    345 
    346 		$post_data = array(
    347 			'return_url' => $this->url->link('extension/payment/pp_express', 'user_token=' . $this->session->data['user_token'], true),
    348 			'store_url' => HTTPS_CATALOG,
    349 			'store_version' => VERSION,
    350 			'store_country' => (isset($country['iso_code_3']) ? $country['iso_code_3'] : ''),
    351 		);
    352 
    353 		// Create sandbox link
    354 		$curl = curl_init($this->opencart_connect_url);
    355 
    356 		$post_data['environment'] = 'sandbox';
    357 
    358 		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    359 		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    360 		curl_setopt($curl, CURLOPT_FORBID_REUSE, 1);
    361 		curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);
    362 		curl_setopt($curl, CURLOPT_POST, true);
    363 		curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post_data));
    364 
    365 		$curl_response = curl_exec($curl);
    366 		$curl_response = json_decode($curl_response, true);
    367 
    368 		curl_close($curl);
    369 
    370 		$data['auth_connect_url_sandbox'] = '';
    371 		if (isset($curl_response['url']) && !empty($curl_response['url'])) {
    372 			$data['auth_connect_url_sandbox'] = $curl_response['url'];
    373 		}
    374 
    375 		// Create Live link
    376 		$curl = curl_init($this->opencart_connect_url);
    377 
    378 		$post_data['environment'] = 'live';
    379 
    380 		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    381 		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    382 		curl_setopt($curl, CURLOPT_FORBID_REUSE, 1);
    383 		curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);
    384 		curl_setopt($curl, CURLOPT_POST, true);
    385 		curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post_data));
    386 
    387 		$curl_response = curl_exec($curl);
    388 		$curl_response = json_decode($curl_response, true);
    389 
    390 		curl_close($curl);
    391 
    392 		$data['auth_connect_url_live'] = '';
    393 		if (isset($curl_response['url']) && !empty($curl_response['url'])) {
    394 			$data['auth_connect_url_live'] = $curl_response['url'];
    395 		}
    396 
    397 		$data['header'] = $this->load->controller('common/header');
    398 		$data['column_left'] = $this->load->controller('common/column_left');
    399 		$data['footer'] = $this->load->controller('common/footer');
    400 
    401 		$this->response->setOutput($this->load->view('extension/payment/pp_express', $data));
    402 	}
    403 
    404 	protected function validate() {
    405 		if (!$this->user->hasPermission('modify', 'extension/payment/pp_express')) {
    406 			$this->error['warning'] = $this->language->get('error_permission');
    407 		}
    408 
    409 		if ($this->request->post['payment_pp_express_test']) {
    410 			if (!$this->request->post['payment_pp_express_sandbox_username']) {
    411 				$this->error['sandbox_username'] = $this->language->get('error_sandbox_username');
    412 			}
    413 
    414 			if (!$this->request->post['payment_pp_express_sandbox_password']) {
    415 				$this->error['sandbox_password'] = $this->language->get('error_sandbox_password');
    416 			}
    417 
    418 			if (!$this->request->post['payment_pp_express_sandbox_signature']) {
    419 				$this->error['sandbox_signature'] = $this->language->get('error_sandbox_signature');
    420 			}
    421 		} else {
    422 			if (!$this->request->post['payment_pp_express_username']) {
    423 				$this->error['username'] = $this->language->get('error_username');
    424 			}
    425 
    426 			if (!$this->request->post['payment_pp_express_password']) {
    427 				$this->error['password'] = $this->language->get('error_password');
    428 			}
    429 
    430 			if (!$this->request->post['payment_pp_express_signature']) {
    431 				$this->error['signature'] = $this->language->get('error_signature');
    432 			}
    433 		}
    434 
    435 		return !$this->error;
    436 	}
    437 
    438 	public function install() {
    439 		$this->load->model('extension/payment/pp_express');
    440 
    441 		$this->model_extension_payment_pp_express->install();
    442 	}
    443 
    444 	public function uninstall() {
    445 		$this->load->model('extension/payment/pp_express');
    446 
    447 		$this->model_extension_payment_pp_express->uninstall();
    448 	}
    449 
    450 	public function order() {
    451 		if ($this->config->get('payment_pp_express_status')) {
    452 			$this->load->language('extension/payment/pp_express_order');
    453 
    454 			if (isset($this->request->get['order_id'])) {
    455 				$order_id = $this->request->get['order_id'];
    456 			} else {
    457 				$order_id = 0;
    458 			}
    459 
    460 			$this->load->model('extension/payment/pp_express');
    461 
    462 			$paypal_info = $this->model_extension_payment_pp_express->getPayPalOrder($order_id);
    463 
    464 			if ($paypal_info) {
    465 				$data['user_token'] = $this->session->data['user_token'];
    466 
    467 				$data['order_id'] = $this->request->get['order_id'];
    468 
    469 				$data['capture_status'] = $paypal_info['capture_status'];
    470 
    471 				$data['total'] = $paypal_info['total'];
    472 
    473 				$captured = number_format($this->model_extension_payment_pp_express->getCapturedTotal($paypal_info['paypal_order_id']), 2);
    474 
    475 				$data['captured'] = $captured;
    476 
    477 				$data['capture_remaining'] = number_format($paypal_info['total'] - $captured, 2);
    478 
    479 				$refunded = number_format($this->model_extension_payment_pp_express->getRefundedTotal($paypal_info['paypal_order_id']), 2);
    480 
    481 				$data['refunded'] = $refunded;
    482 
    483 				return $this->load->view('extension/payment/pp_express_order', $data);
    484 			}
    485 		}
    486 	}
    487 
    488 	public function transaction() {
    489 		$this->load->language('extension/payment/pp_express_order');
    490 
    491 		$data['transactions'] = array();
    492 
    493 		if (isset($this->request->get['order_id'])) {
    494 			$order_id = $this->request->get['order_id'];
    495 		} else {
    496 			$order_id = 0;
    497 		}
    498 
    499 		$this->load->model('extension/payment/pp_express');
    500 
    501 		$paypal_info = $this->model_extension_payment_pp_express->getOrder($order_id);
    502 
    503 		if ($paypal_info) {
    504 			$results = $this->model_extension_payment_pp_express->getTransactions($paypal_info['paypal_order_id']);
    505 
    506 			foreach ($results as $result) {
    507 				$data['transactions'][] = array(
    508 					'transaction_id' => $result['transaction_id'],
    509 					'amount'         => $result['amount'],
    510 					'payment_type'   => $result['payment_type'],
    511 					'payment_status' => $result['payment_status'],
    512 					'pending_reason' => $result['pending_reason'],
    513 					'date_added'     => date($this->language->get('datetime_format'), strtotime($result['date_added'])),
    514 					'view'           => $this->url->link('extension/payment/pp_express/info', 'user_token=' . $this->session->data['user_token'] . '&transaction_id=' . $result['transaction_id'], true),
    515 					'refund'         => $this->url->link('extension/payment/pp_express/refund', 'user_token=' . $this->session->data['user_token'] . '&transaction_id=' . $result['transaction_id'], true),
    516 					'resend'         => $this->url->link('extension/payment/pp_express/resend', 'user_token=' . $this->session->data['user_token'] . '&paypal_order_transaction_id=' . $result['paypal_order_transaction_id'], true)
    517 				);
    518 			}
    519 		}
    520 
    521 		$this->response->setOutput($this->load->view('extension/payment/pp_express_transaction', $data));
    522 	}
    523 
    524 	public function capture() {
    525 		$json = array();
    526 
    527 		$this->load->language('extension/payment/pp_express_order');
    528 
    529 		if (!isset($this->request->post['amount']) && $this->request->post['amount'] > 0) {
    530 			$json['error'] = $this->language->get('error_capture');
    531 		}
    532 
    533 		if (!$json) {
    534 			$this->load->model('extension/payment/pp_express');
    535 
    536 			if (isset($this->request->get['order_id'])) {
    537 				$order_id = $this->request->get['order_id'];
    538 			} else {
    539 				$order_id = 0;
    540 			}
    541 
    542 			$paypal_info = $this->model_extension_payment_pp_express->getOrder($order_id);
    543 
    544 			if ($paypal_info) {
    545 				// If this is the final amount to capture or not
    546 				if ($this->request->post['complete'] == 1) {
    547 					$complete = 'Complete';
    548 				} else {
    549 					$complete = 'NotComplete';
    550 				}
    551 
    552 				$request = array(
    553 					'METHOD'          => 'DoCapture',
    554 					'AUTHORIZATIONID' => $paypal_info['authorization_id'],
    555 					'AMT'             => number_format($this->request->post['amount'], 2),
    556 					'CURRENCYCODE'    => $paypal_info['currency_code'],
    557 					'COMPLETETYPE'    => $complete,
    558 					'MSGSUBID'        => uniqid(mt_rand(), true)
    559 				);
    560 
    561 				$response = $this->model_extension_payment_pp_express->call($request);
    562 
    563 				if (isset($response['ACK']) && ($response['ACK'] != 'Failure') && ($response['ACK'] != 'FailureWithWarning')) {
    564 					$transaction_data = array(
    565 						'paypal_order_id'       => $paypal_info['paypal_order_id'],
    566 						'transaction_id'        => $response['TRANSACTIONID'],
    567 						'parent_id'             => $paypal_info['authorization_id'],
    568 						'note'                  => '',
    569 						'msgsubid'              => $response['MSGSUBID'],
    570 						'receipt_id'            => '',
    571 						'payment_type'          => $response['PAYMENTTYPE'],
    572 						'payment_status'        => $response['PAYMENTSTATUS'],
    573 						'pending_reason'        => (isset($response['PENDINGREASON']) ? $response['PENDINGREASON'] : ''),
    574 						'transaction_entity'    => 'payment',
    575 						'amount'                => $response['AMT'],
    576 						'debug_data'            => json_encode($response)
    577 					);
    578 
    579 					$this->model_extension_payment_pp_express->addTransaction($transaction_data);
    580 
    581 					$captured = number_format($this->model_extension_payment_pp_express->getCapturedTotal($paypal_info['paypal_order_id']), 2);
    582 					$refunded = number_format($this->model_extension_payment_pp_express->getRefundedTotal($paypal_info['paypal_order_id']), 2);
    583 
    584 					$json['captured'] = $captured;
    585 					$json['refunded'] = $refunded;
    586 					$json['remaining'] = number_format($paypal_info['total'] - $captured, 2);
    587 
    588 					if ($this->request->post['complete'] == 1 || $json['remaining'] == 0.00) {
    589 						$json['capture_status'] = $this->language->get('text_complete');
    590 
    591 						$this->model_extension_payment_pp_express->editPayPalOrderStatus($order_id, 'Complete');
    592 					}
    593 
    594 					$json['success'] = $this->language->get('text_success');
    595 				} else {
    596 					$json['error'] = (isset($response_info['L_SHORTMESSAGE0']) ? $response_info['L_SHORTMESSAGE0'] : $this->language->get('error_transaction'));
    597 				}
    598 			} else {
    599 				$json['error'] = $this->language->get('error_not_found');
    600 			}
    601 		}
    602 
    603 		$this->response->addHeader('Content-Type: application/json');
    604 		$this->response->setOutput(json_encode($json));
    605 	}
    606 
    607 	public function refund() {
    608 		$this->load->language('extension/payment/pp_express_refund');
    609 
    610 		$this->document->setTitle($this->language->get('heading_title'));
    611 
    612 		$data['breadcrumbs'] = array();
    613 
    614 		$data['breadcrumbs'][] = array(
    615 			'text' => $this->language->get('text_home'),
    616 			'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true),
    617 		);
    618 
    619 		$data['breadcrumbs'][] = array(
    620 			'text' => $this->language->get('text_pp_express'),
    621 			'href' => $this->url->link('extension/payment/pp_express', 'user_token=' . $this->session->data['user_token'], true),
    622 		);
    623 
    624 		$data['breadcrumbs'][] = array(
    625 			'text' => $this->language->get('heading_title'),
    626 			'href' => $this->url->link('extension/payment/pp_express/refund', 'user_token=' . $this->session->data['user_token'], true),
    627 		);
    628 
    629 		//button actions
    630 		$data['action'] = $this->url->link('extension/payment/pp_express/doRefund', 'user_token=' . $this->session->data['user_token'], true);
    631 		$data['cancel'] = $this->url->link('extension/payment/pp_express', 'user_token=' . $this->session->data['user_token'], true);
    632 
    633 		$data['transaction_id'] = $this->request->get['transaction_id'];
    634 
    635 		$this->load->model('extension/payment/pp_express');
    636 		$pp_transaction = $this->model_extension_payment_pp_express->getTransaction($this->request->get['transaction_id']);
    637 
    638 		$data['amount_original'] = $pp_transaction['AMT'];
    639 		$data['currency_code'] = $pp_transaction['CURRENCYCODE'];
    640 
    641 		$refunded = number_format($this->model_extension_payment_pp_express->getRefundedTotalByParentId($this->request->get['transaction_id']), 2);
    642 
    643 		if ($refunded != 0.00) {
    644 			$data['refund_available'] = number_format($data['amount_original'] + $refunded, 2);
    645 			$data['attention'] = $this->language->get('text_current_refunds') . ': ' . $data['refund_available'];
    646 		} else {
    647 			$data['refund_available'] = '';
    648 			$data['attention'] = '';
    649 		}
    650 
    651 		$data['user_token'] = $this->session->data['user_token'];
    652 
    653 		if (isset($this->session->data['error'])) {
    654 			$data['error'] = $this->session->data['error'];
    655 			unset($this->session->data['error']);
    656 		} else {
    657 			$data['error'] = '';
    658 		}
    659 
    660 		$data['header'] = $this->load->controller('common/header');
    661 		$data['column_left'] = $this->load->controller('common/column_left');
    662 		$data['footer'] = $this->load->controller('common/footer');
    663 
    664 		$this->response->setOutput($this->load->view('extension/payment/pp_express_refund', $data));
    665 	}
    666 
    667 	public function doRefund() {
    668 		/**
    669 		 * used to issue a refund for a captured payment
    670 		 *
    671 		 * refund can be full or partial
    672 		 */
    673 		if (isset($this->request->post['transaction_id']) && isset($this->request->post['refund_full'])) {
    674 
    675 			$this->load->model('extension/payment/pp_express');
    676 			$this->load->language('extension/payment/pp_express_refund');
    677 
    678 			if ($this->request->post['refund_full'] == 0 && $this->request->post['amount'] == 0) {
    679 				$this->session->data['error'] = $this->language->get('error_partial_amt');
    680 			} else {
    681 				$order_id = $this->model_extension_payment_pp_express->getOrderId($this->request->post['transaction_id']);
    682 				$paypal_order = $this->model_extension_payment_pp_express->getOrder($order_id);
    683 
    684 				if ($paypal_order) {
    685 					$call_data = array();
    686 					$call_data['METHOD'] = 'RefundTransaction';
    687 					$call_data['TRANSACTIONID'] = $this->request->post['transaction_id'];
    688 					$call_data['NOTE'] = urlencode($this->request->post['refund_message']);
    689 					$call_data['MSGSUBID'] = uniqid(mt_rand(), true);
    690 
    691 					$current_transaction = $this->model_extension_payment_pp_express->getLocalTransaction($this->request->post['transaction_id']);
    692 
    693 					if ($this->request->post['refund_full'] == 1) {
    694 						$call_data['REFUNDTYPE'] = 'Full';
    695 					} else {
    696 						$call_data['REFUNDTYPE'] = 'Partial';
    697 						$call_data['AMT'] = number_format($this->request->post['amount'], 2);
    698 						$call_data['CURRENCYCODE'] = $this->request->post['currency_code'];
    699 					}
    700 
    701 					$result = $this->model_extension_payment_pp_express->call($call_data);
    702 
    703 					$transaction = array(
    704 						'paypal_order_id' => $paypal_order['paypal_order_id'],
    705 						'transaction_id' => '',
    706 						'parent_transaction_id' => $this->request->post['transaction_id'],
    707 						'note' => $this->request->post['refund_message'],
    708 						'msgsubid' => $call_data['MSGSUBID'],
    709 						'receipt_id' => '',
    710 						'payment_type' => '',
    711 						'payment_status' => 'Refunded',
    712 						'transaction_entity' => 'payment',
    713 						'pending_reason' => '',
    714 						'amount' => '-' . (isset($call_data['AMT']) ? $call_data['AMT'] : $current_transaction['amount']),
    715 						'debug_data' => json_encode($result)
    716 					);
    717 
    718 					if ($result == false) {
    719 						$transaction['payment_status'] = 'Failed';
    720 						$this->model_extension_payment_pp_express->addTransaction($transaction, $call_data);
    721 						$this->response->redirect($this->url->link('sale/order/info', 'user_token=' . $this->session->data['user_token'] . '&order_id=' . $paypal_order['order_id'], true));
    722 					} else if ($result['ACK'] != 'Failure' && $result['ACK'] != 'FailureWithWarning') {
    723 
    724 						$transaction['transaction_id'] = $result['REFUNDTRANSACTIONID'];
    725 						$transaction['payment_type'] = $result['REFUNDSTATUS'];
    726 						$transaction['pending_reason'] = $result['PENDINGREASON'];
    727 						$transaction['amount'] = '-' . $result['GROSSREFUNDAMT'];
    728 
    729 						$this->model_extension_payment_pp_express->addTransaction($transaction);
    730 
    731 						//edit transaction to refunded status
    732 						if ($result['TOTALREFUNDEDAMOUNT'] == $this->request->post['amount_original']) {
    733 							$this->db->query("UPDATE `" . DB_PREFIX . "paypal_order_transaction` SET `payment_status` = 'Refunded' WHERE `transaction_id` = '" . $this->db->escape($this->request->post['transaction_id']) . "' LIMIT 1");
    734 						} else {
    735 							$this->db->query("UPDATE `" . DB_PREFIX . "paypal_order_transaction` SET `payment_status` = 'Partially-Refunded' WHERE `transaction_id` = '" . $this->db->escape($this->request->post['transaction_id']) . "' LIMIT 1");
    736 						}
    737 
    738 						//redirect back to the order
    739 						$this->response->redirect($this->url->link('sale/order/info', 'user_token=' . $this->session->data['user_token'] . '&order_id=' . $paypal_order['order_id'], true));
    740 					} else {
    741 						$this->model_extension_payment_pp_express->log(json_encode($result));
    742 						$this->session->data['error'] = (isset($result['L_SHORTMESSAGE0']) ? $result['L_SHORTMESSAGE0'] : 'There was an error') . (isset($result['L_LONGMESSAGE0']) ? '<br />' . $result['L_LONGMESSAGE0'] : '');
    743 						$this->response->redirect($this->url->link('extension/payment/pp_express/refund', 'user_token=' . $this->session->data['user_token'] . '&transaction_id=' . $this->request->post['transaction_id'], true));
    744 					}
    745 				} else {
    746 					$this->session->data['error'] = $this->language->get('error_data_missing');
    747 					$this->response->redirect($this->url->link('extension/payment/pp_express/refund', 'user_token=' . $this->session->data['user_token'] . '&transaction_id=' . $this->request->post['transaction_id'], true));
    748 				}
    749 			}
    750 		} else {
    751 			$this->session->data['error'] = $this->language->get('error_data');
    752 			$this->response->redirect($this->url->link('extension/payment/pp_express/refund', 'user_token=' . $this->session->data['user_token'] . '&transaction_id=' . $this->request->post['transaction_id'], true));
    753 		}
    754 	}
    755 
    756 	/**
    757 	 * used to void an authorised payment
    758 	 */
    759 	public function void() {
    760 		$json = array();
    761 
    762 		$this->load->language('extension/payment/pp_express_order');
    763 
    764 		if (isset($this->request->get['order_id'])) {
    765 			$order_id = $this->request->get['order_id'];
    766 		} else {
    767 			$order_id = 0;
    768 		}
    769 
    770 		$this->load->model('extension/payment/pp_express');
    771 
    772 		$paypal_info = $this->model_extension_payment_pp_express->getOrder($order_id);
    773 
    774 		if ($paypal_info) {
    775 			$request = array(
    776 				'METHOD'          => 'DoVoid',
    777 				'AUTHORIZATIONID' => $paypal_info['authorization_id'],
    778 				'MSGSUBID'        => uniqid(mt_rand(), true)
    779 			);
    780 
    781 			$response_info = $this->model_extension_payment_pp_express->call($request);
    782 
    783 			if (isset($response_info['ACK']) && ($response_info['ACK'] != 'Failure') && ($response_info['ACK'] != 'FailureWithWarning')) {
    784 				$transaction = array(
    785 					'paypal_order_id'       => $paypal_info['paypal_order_id'],
    786 					'transaction_id'        => '',
    787 					'parent_id'             => $paypal_info['authorization_id'],
    788 					'note'                  => '',
    789 					'msgsubid'              => '',
    790 					'receipt_id'            => '',
    791 					'payment_type'          => 'void',
    792 					'payment_status'        => 'Void',
    793 					'pending_reason'        => '',
    794 					'transaction_entity'    => 'auth',
    795 					'amount'                => '',
    796 					'debug_data'            => json_encode($response_info)
    797 				);
    798 
    799 				$this->model_extension_payment_pp_express->addTransaction($transaction);
    800 
    801 				$this->model_extension_payment_pp_express->editPayPalOrderStatus($order_id, 'Complete');
    802 
    803 				$json['capture_status'] = 'Complete';
    804 
    805 				$json['success'] = $this->language->get('text_success');
    806 			} else {
    807 				$json['error'] = (isset($result['L_SHORTMESSAGE0']) ? $result['L_SHORTMESSAGE0'] : $this->language->get('error_transaction'));
    808 			}
    809 		} else {
    810 			$json['error'] = $this->language->get('error_not_found');
    811 		}
    812 
    813 		$this->response->addHeader('Content-Type: application/json');
    814 		$this->response->setOutput(json_encode($json));
    815 	}
    816 
    817 	// Cancel an active recurring
    818 	public function recurringCancel() {
    819 		$json = array();
    820 
    821 		$this->load->language('extension/recurring/pp_express');
    822 
    823 		//cancel an active recurring
    824 		$this->load->model('account/recurring');
    825 
    826 		if (isset($this->request->get['order_recurring_id'])) {
    827 			$order_recurring_id = $this->request->get['order_recurring_id'];
    828 		} else {
    829 			$order_recurring_id = 0;
    830 		}
    831 
    832 		$recurring_info = $this->model_account_recurring->getOrderRecurring($order_recurring_id);
    833 
    834 		if ($recurring_info && $recurring_info['reference']) {
    835 			if ($this->config->get('payment_pp_express_test')) {
    836 				$api_url = 'https://api-3t.sandbox.paypal.com/nvp';
    837 				$api_username = $this->config->get('payment_pp_express_sandbox_username');
    838 				$api_password = $this->config->get('payment_pp_express_sandbox_password');
    839 				$api_signature = $this->config->get('payment_pp_express_sandbox_signature');
    840 			} else {
    841 				$api_url = 'https://api-3t.paypal.com/nvp';
    842 				$api_username = $this->config->get('payment_pp_express_username');
    843 				$api_password = $this->config->get('payment_pp_express_password');
    844 				$api_signature = $this->config->get('payment_pp_express_signature');
    845 			}
    846 
    847 			$request = array(
    848 				'USER'         => $api_username,
    849 				'PWD'          => $api_password,
    850 				'SIGNATURE'    => $api_signature,
    851 				'VERSION'      => '109.0',
    852 				'BUTTONSOURCE' => 'OpenCart_2.0_EC',
    853 				'METHOD'       => 'SetExpressCheckout',
    854 				'METHOD'       => 'ManageRecurringPaymentsProfileStatus',
    855 				'PROFILEID'    => $recurring_info['reference'],
    856 				'ACTION'       => 'Cancel'
    857 			);
    858 
    859 			$curl = curl_init($api_url);
    860 
    861 			curl_setopt($curl, CURLOPT_POST, true);
    862 			curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
    863 			curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    864 			curl_setopt($curl, CURLOPT_HEADER, false);
    865 			curl_setopt($curl, CURLOPT_TIMEOUT, 30);
    866 			curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    867 
    868 			$response = curl_exec($curl);
    869 
    870 			if (!$response) {
    871 				$this->log(sprintf($this->language->get('error_curl'), curl_errno($curl), curl_error($curl)));
    872 			}
    873 
    874 			curl_close($curl);
    875 
    876 			$response_info = array();
    877 
    878 			parse_str($response, $response_info);
    879 
    880 			if (isset($response_info['PROFILEID'])) {
    881 				$this->model_account_recurring->editOrderRecurringStatus($order_recurring_id, 4);
    882 				$this->model_account_recurring->addOrderRecurringTransaction($order_recurring_id, 5);
    883 
    884 				$json['success'] = $this->language->get('text_cancelled');
    885 			} else {
    886 				$json['error'] = sprintf($this->language->get('error_not_cancelled'), $response_info['L_LONGMESSAGE0']);
    887 			}
    888 		} else {
    889 			$json['error'] = $this->language->get('error_not_found');
    890 		}
    891 
    892 		$this->response->addHeader('Content-Type: application/json');
    893 		$this->response->setOutput(json_encode($json));
    894 	}
    895 
    896 	public function resend() {
    897 		$json = array();
    898 
    899 		$this->load->language('extension/payment/pp_express');
    900 
    901 		if (isset($this->request->get['paypal_order_transaction_id'])) {
    902 			$paypal_order_transaction_id = $this->request->get['paypal_order_transaction_id'];
    903 		} else {
    904 			$paypal_order_transaction_id = 0;
    905 		}
    906 
    907 		$this->load->model('extension/payment/pp_express');
    908 
    909 		$transaction = $this->model_extension_payment_pp_express->getFailedTransaction($paypal_order_transaction_id);
    910 
    911 		if ($transaction) {
    912 
    913 			$call_data = json_decode($transaction['call_data'], true);
    914 
    915 			$result = $this->model_extension_payment_pp_express->call($call_data);
    916 
    917 			if ($result) {
    918 
    919 				$parent_transaction = $this->model_extension_payment_pp_express->getLocalTransaction($transaction['parent_id']);
    920 
    921 				if ($parent_transaction['amount'] == abs($transaction['amount'])) {
    922 					$this->db->query("UPDATE `" . DB_PREFIX . "paypal_order_transaction` SET `payment_status` = 'Refunded' WHERE `transaction_id` = '" . $this->db->escape($transaction['parent_id']) . "' LIMIT 1");
    923 				} else {
    924 					$this->db->query("UPDATE `" . DB_PREFIX . "paypal_order_transaction` SET `payment_status` = 'Partially-Refunded' WHERE `transaction_id` = '" . $this->db->escape($transaction['parent_id']) . "' LIMIT 1");
    925 				}
    926 
    927 				if (isset($result['REFUNDTRANSACTIONID'])) {
    928 					$transaction['transaction_id'] = $result['REFUNDTRANSACTIONID'];
    929 				} else {
    930 					$transaction['transaction_id'] = $result['TRANSACTIONID'];
    931 				}
    932 
    933 				if (isset($result['PAYMENTTYPE'])) {
    934 					$transaction['payment_type'] = $result['PAYMENTTYPE'];
    935 				} else {
    936 					$transaction['payment_type'] = $result['REFUNDSTATUS'];
    937 				}
    938 
    939 				if (isset($result['PAYMENTSTATUS'])) {
    940 					$transaction['payment_status'] = $result['PAYMENTSTATUS'];
    941 				} else {
    942 					$transaction['payment_status'] = 'Refunded';
    943 				}
    944 
    945 				if (isset($result['AMT'])) {
    946 					$transaction['amount'] = $result['AMT'];
    947 				} else {
    948 					$transaction['amount'] = $transaction['amount'];
    949 				}
    950 
    951 				$transaction['pending_reason'] = (isset($result['PENDINGREASON']) ? $result['PENDINGREASON'] : '');
    952 
    953 				$this->model_extension_payment_pp_express->updateTransaction($transaction);
    954 
    955 				$json['success'] = $this->language->get('success_transaction_resent');
    956 			} else {
    957 				$json['error'] = $this->language->get('error_timeout');
    958 			}
    959 		} else {
    960 			$json['error'] = $this->language->get('error_transaction_missing');
    961 		}
    962 
    963 		$this->response->addHeader('Content-Type: application/json');
    964 		$this->response->setOutput(json_encode($json));
    965 	}
    966 
    967 	public function search() {
    968 		$this->load->language('extension/payment/pp_express_search');
    969 
    970 		$this->document->setTitle($this->language->get('heading_title'));
    971 
    972 		$data['user_token'] = $this->session->data['user_token'];
    973 
    974 		$data['breadcrumbs'] = array();
    975 
    976 		$data['breadcrumbs'][] = array(
    977 			'text' => $this->language->get('text_home'),
    978 			'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true),
    979 		);
    980 
    981 		$data['breadcrumbs'][] = array(
    982 			'text' => $this->language->get('text_pp_express'),
    983 			'href' => $this->url->link('extension/payment/pp_express', 'user_token=' . $this->session->data['user_token'], true),
    984 		);
    985 
    986 		$data['breadcrumbs'][] = array(
    987 			'text' => $this->language->get('heading_title'),
    988 			'href' => $this->url->link('extension/payment/pp_express/search', 'user_token=' . $this->session->data['user_token'], true),
    989 		);
    990 
    991 		$this->load->model('extension/payment/pp_express');
    992 
    993 		$data['currency_codes'] = $this->model_extension_payment_pp_express->getCurrencies();
    994 
    995 		$data['default_currency'] = $this->config->get('payment_pp_express_currency');
    996 
    997 		$data['date_start'] = date("Y-m-d", strtotime('-30 days'));
    998 		$data['date_end'] = date("Y-m-d");
    999 		$data['view_link'] = $this->url->link('extension/payment/pp_express/info', 'user_token=' . $this->session->data['user_token'], true);
   1000 
   1001 		$data['header'] = $this->load->controller('common/header');
   1002 		$data['column_left'] = $this->load->controller('common/column_left');
   1003 		$data['footer'] = $this->load->controller('common/footer');
   1004 
   1005 		$this->response->setOutput($this->load->view('extension/payment/pp_express_search', $data));
   1006 	}
   1007 
   1008 	public function info() {
   1009 		$this->load->language('extension/payment/pp_express_view');
   1010 
   1011 		$this->document->setTitle($this->language->get('heading_title'));
   1012 		
   1013 		$data['breadcrumbs'] = array();
   1014 
   1015 		$data['breadcrumbs'][] = array(
   1016 			'text' => $this->language->get('text_home'),
   1017 			'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true),
   1018 		);
   1019 
   1020 		$data['breadcrumbs'][] = array(
   1021 			'text' => $this->language->get('text_pp_express'),
   1022 			'href' => $this->url->link('extension/payment/pp_express', 'user_token=' . $this->session->data['user_token'], true),
   1023 		);
   1024 
   1025 		$data['breadcrumbs'][] = array(
   1026 			'text' => $this->language->get('heading_title'),
   1027 			'href' => $this->url->link('extension/payment/pp_express/info', 'user_token=' . $this->session->data['user_token'] . '&transaction_id=' . $this->request->get['transaction_id'], true),
   1028 		);
   1029 
   1030 		$this->load->model('extension/payment/pp_express');
   1031 
   1032 		$data['transaction'] = $this->model_extension_payment_pp_express->getTransaction($this->request->get['transaction_id']);
   1033 		$data['lines'] = $this->formatRows($data['transaction']);
   1034 		$data['view_link'] = $this->url->link('extension/payment/pp_express/info', 'user_token=' . $this->session->data['user_token'], true);
   1035 		$data['cancel'] = $this->url->link('extension/payment/pp_express/search', 'user_token=' . $this->session->data['user_token'], true);
   1036 		$data['user_token'] = $this->session->data['user_token'];
   1037 
   1038 		$data['header'] = $this->load->controller('common/header');
   1039 		$data['column_left'] = $this->load->controller('common/column_left');
   1040 		$data['footer'] = $this->load->controller('common/footer');
   1041 
   1042 		$this->response->setOutput($this->load->view('extension/payment/pp_express_view', $data));
   1043 	}
   1044 
   1045 	public function doSearch() {
   1046 		/**
   1047 		 * used to search for transactions from a user account
   1048 		 */
   1049 		if (isset($this->request->post['date_start'])) {
   1050 
   1051 			$this->load->model('extension/payment/pp_express');
   1052 
   1053 			$call_data = array();
   1054 			$call_data['METHOD'] = 'TransactionSearch';
   1055 			$call_data['STARTDATE'] = gmdate($this->request->post['date_start'] . "\TH:i:s\Z");
   1056 
   1057 			if (!empty($this->request->post['date_end'])) {
   1058 				$call_data['ENDDATE'] = gmdate($this->request->post['date_end'] . "\TH:i:s\Z");
   1059 			}
   1060 
   1061 			if (!empty($this->request->post['transaction_class'])) {
   1062 				$call_data['TRANSACTIONCLASS'] = $this->request->post['transaction_class'];
   1063 			}
   1064 
   1065 			if (!empty($this->request->post['status'])) {
   1066 				$call_data['STATUS'] = $this->request->post['status'];
   1067 			}
   1068 
   1069 			if (!empty($this->request->post['buyer_email'])) {
   1070 				$call_data['EMAIL'] = $this->request->post['buyer_email'];
   1071 			}
   1072 
   1073 			if (!empty($this->request->post['merchant_email'])) {
   1074 				$call_data['RECEIVER'] = $this->request->post['merchant_email'];
   1075 			}
   1076 
   1077 			if (!empty($this->request->post['receipt_id'])) {
   1078 				$call_data['RECEIPTID'] = $this->request->post['receipt_id'];
   1079 			}
   1080 
   1081 			if (!empty($this->request->post['transaction_id'])) {
   1082 				$call_data['TRANSACTIONID'] = $this->request->post['transaction_id'];
   1083 			}
   1084 
   1085 			if (!empty($this->request->post['invoice_number'])) {
   1086 				$call_data['INVNUM'] = $this->request->post['invoice_number'];
   1087 			}
   1088 
   1089 			if (!empty($this->request->post['auction_item_number'])) {
   1090 				$call_data['AUCTIONITEMNUMBER'] = $this->request->post['auction_item_number'];
   1091 			}
   1092 
   1093 			if (!empty($this->request->post['amount'])) {
   1094 				$call_data['AMT'] = number_format($this->request->post['amount'], 2);
   1095 				$call_data['CURRENCYCODE'] = $this->request->post['currency_code'];
   1096 			}
   1097 
   1098 			if (!empty($this->request->post['recurring_id'])) {
   1099 				$call_data['PROFILEID'] = $this->request->post['recurring_id'];
   1100 			}
   1101 
   1102 			if (!empty($this->request->post['name_salutation'])) {
   1103 				$call_data['SALUTATION'] = $this->request->post['name_salutation'];
   1104 			}
   1105 
   1106 			if (!empty($this->request->post['name_first'])) {
   1107 				$call_data['FIRSTNAME'] = $this->request->post['name_first'];
   1108 			}
   1109 
   1110 			if (!empty($this->request->post['name_middle'])) {
   1111 				$call_data['MIDDLENAME'] = $this->request->post['name_middle'];
   1112 			}
   1113 
   1114 			if (!empty($this->request->post['name_last'])) {
   1115 				$call_data['LASTNAME'] = $this->request->post['name_last'];
   1116 			}
   1117 
   1118 			if (!empty($this->request->post['name_suffix'])) {
   1119 				$call_data['SUFFIX'] = $this->request->post['name_suffix'];
   1120 			}
   1121 
   1122 			$result = $this->model_extension_payment_pp_express->call($call_data);
   1123 
   1124 			if ($result['ACK'] != 'Failure' && $result['ACK'] != 'FailureWithWarning' && $result['ACK'] != 'Warning') {
   1125 				$response['error'] = false;
   1126 				$response['result'] = $this->formatRows($result);
   1127 			} else {
   1128 				$response['error'] = true;
   1129 				$response['error_msg'] = $result['L_LONGMESSAGE0'];
   1130 			}
   1131 
   1132 			$this->response->addHeader('Content-Type: application/json');
   1133 			$this->response->setOutput(json_encode($response));
   1134 		} else {
   1135 			$response['error'] = true;
   1136 			$response['error_msg'] = 'Enter a start date';
   1137 			$this->response->addHeader('Content-Type: application/json');
   1138 			$this->response->setOutput(json_encode($response));
   1139 		}
   1140 	}
   1141 
   1142 	public function live() {
   1143 		if (isset($this->request->get['merchantId'])) {
   1144 			$this->load->language('extension/payment/pp_express');
   1145 
   1146 			$this->load->model('extension/payment/pp_express');
   1147 			$this->load->model('setting/setting');
   1148 
   1149 			$token = $this->model_extension_payment_pp_express->getTokens('live');
   1150 
   1151 			if (isset($token->access_token)) {
   1152 				$user_info = $this->model_extension_payment_pp_express->getUserInfo($this->request->get['merchantId'], 'live', $token->access_token);
   1153 			} else {
   1154 				$this->session->data['error_api'] = $this->language->get('error_api');
   1155 			}
   1156 
   1157 			if (isset($user_info->api_user_name)) {
   1158 				$this->model_setting_setting->editSettingValue('payment_pp_express', 'payment_pp_express_username', $user_info->api_user_name);
   1159 				$this->model_setting_setting->editSettingValue('payment_pp_express', 'payment_pp_express_password', $user_info->api_password);
   1160 				$this->model_setting_setting->editSettingValue('payment_pp_express', 'payment_pp_express_signature', $user_info->signature);
   1161 			} else {
   1162 				$this->session->data['error_api'] = $this->language->get('error_api');
   1163 			}
   1164 		}
   1165 
   1166 		$this->response->redirect($this->url->link('extension/payment/pp_express', 'user_token=' . $this->session->data['user_token'], true));
   1167 	}
   1168 
   1169 	public function sandbox() {
   1170 		if (isset($this->request->get['merchantId'])) {
   1171 			$this->load->language('extension/payment/pp_express');
   1172 
   1173 			$this->load->model('extension/payment/pp_express');
   1174 			$this->load->model('setting/setting');
   1175 
   1176 			$token = $this->model_extension_payment_pp_express->getTokens('sandbox');
   1177 
   1178 			if (isset($token->access_token)) {
   1179 				$user_info = $this->model_extension_payment_pp_express->getUserInfo($this->request->get['merchantId'], 'sandbox', $token->access_token);
   1180 			} else {
   1181 				$this->session->data['error_api'] = $this->language->get('error_api_sandbox');
   1182 			}
   1183 
   1184 			if (isset($user_info->api_user_name)) {
   1185 				$this->model_setting_setting->editSettingValue('payment_pp_express', 'payment_pp_express_sandbox_username', $user_info->api_user_name);
   1186 				$this->model_setting_setting->editSettingValue('payment_pp_express', 'payment_pp_express_sandbox_password', $user_info->api_password);
   1187 				$this->model_setting_setting->editSettingValue('payment_pp_express', 'payment_pp_express_sandbox_signature', $user_info->signature);
   1188 			} else {
   1189 				$this->session->data['error_api'] = $this->language->get('error_api_sandbox');
   1190 			}
   1191 		}
   1192 		$this->response->redirect($this->url->link('extension/payment/pp_express', 'user_token=' . $this->session->data['user_token'], true));
   1193 	}
   1194 
   1195 	private function formatRows($data) {
   1196 		$return = array();
   1197 
   1198 		foreach ($data as $k => $v) {
   1199 			$elements = preg_split("/(\d+)/", $k, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
   1200 			if (isset($elements[1]) && isset($elements[0])) {
   1201 				if ($elements[0] == 'L_TIMESTAMP') {
   1202 					$v = str_replace('T', ' ', $v);
   1203 					$v = str_replace('Z', '', $v);
   1204 				}
   1205 				$return[$elements[1]][$elements[0]] = $v;
   1206 			}
   1207 		}
   1208 
   1209 		return $return;
   1210 	}
   1211 
   1212 	public function recurringButtons() {
   1213 		$this->load->model('sale/recurring');
   1214 
   1215 		$recurring = $this->model_sale_recurring->getRecurring($this->request->get['order_recurring_id']);
   1216 
   1217 		$data['buttons'] = array();
   1218 
   1219 		if ($recurring['status'] == 2 || $recurring['status'] == 3) {
   1220 			$data['buttons'][] = array(
   1221 				'text' => $this->language->get('button_cancel_recurring'),
   1222 				'link' => $this->url->link('extension/payment/pp_express/recurringCancel', 'order_recurring_id=' . $this->request->get['order_recurring_id'] . '&user_token=' . $this->request->get['user_token'], true)
   1223 			);
   1224 		}
   1225 
   1226 		return $this->load->view('sale/recurring_button', $data);
   1227 	}
   1228 
   1229 	public function connectRedirect() {
   1230 		if ($this->user->hasPermission('modify', 'extension/extension/payment') && $this->user->hasPermission('modify', 'extension/payment/pp_express')) {
   1231 			// Install the module before doing the redirect
   1232 			$this->load->model('setting/extension');
   1233 
   1234 			$this->model_setting_extension->install('payment', 'pp_express');
   1235 
   1236 			$this->install();
   1237 
   1238 			$this->load->model('localisation/country');
   1239 
   1240 			$country = $this->model_localisation_country->getCountry($this->config->get('config_country_id'));
   1241 
   1242 			$post_data = array(
   1243 				'return_url' => $this->url->link('extension/payment/pp_express', 'user_token=' . $this->session->data['user_token'], true),
   1244 				'store_url' => HTTPS_CATALOG,
   1245 				'store_version' => VERSION,
   1246 				'store_country' => (isset($country['iso_code_3']) ? $country['iso_code_3'] : ''),
   1247 			);
   1248 
   1249 			// Create Live link
   1250 			$curl = curl_init($this->opencart_connect_url);
   1251 
   1252 			$post_data['environment'] = 'live';
   1253 
   1254 			curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
   1255 			curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
   1256 			curl_setopt($curl, CURLOPT_FORBID_REUSE, 1);
   1257 			curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);
   1258 			curl_setopt($curl, CURLOPT_POST, true);
   1259 			curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post_data));
   1260 
   1261 			$curl_response = curl_exec($curl);
   1262 			$curl_response = json_decode($curl_response, true);
   1263 
   1264 			curl_close($curl);
   1265 
   1266 			if (isset($curl_response['url']) && !empty($curl_response['url'])) {
   1267 				$this->response->redirect($curl_response['url']);
   1268 			} else {
   1269 				$this->response->redirect($this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'], true));
   1270 			}
   1271 		} else {
   1272 			$this->response->redirect($this->url->link('error/permission', 'user_token=' . $this->session->data['user_token'], true));
   1273 		}
   1274 	}
   1275 
   1276 	public function preferredSolution() {
   1277 		$this->load->language('extension/payment/pp_express');
   1278 
   1279 		$data['connect_link'] = '';
   1280 		$data['module_link'] = '';
   1281 
   1282 		if ($this->config->get('payment_pp_express_username') || $this->config->get('payment_pp_express_sandbox_username')) {
   1283 			$data['module_link'] = $this->url->link('extension/payment/pp_express', 'user_token=' . $this->session->data['user_token'], true);
   1284 		} else {
   1285 			if ($this->user->hasPermission('modify', 'extension/extension/payment')) {
   1286 				$data['connect_link'] = $this->url->link('extension/payment/pp_express/connectRedirect', 'user_token=' . $this->session->data['user_token'], true);
   1287 			}
   1288 		}
   1289 
   1290 		if ($this->config->get("payment_pp_express_status") == 1) {
   1291 			$data['payment_pp_express_status'] = "enabled";
   1292 		} elseif ($this->config->get("payment_pp_express_status") == null) {
   1293 			$data['payment_pp_express_status'] = "";
   1294 		} else {
   1295 			$data['payment_pp_express_status'] = "disabled";
   1296 		}
   1297 
   1298 		return $this->load->view('extension/payment/pp_express_preferred', $data);
   1299 	}
   1300 }