shop.balmet.com

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

pp_braintree.php (49179B)


      1 <?php
      2 class ControllerExtensionPaymentPPBraintree extends Controller {
      3 	private $error = array();
      4 	private $gateway = null;
      5 	private $opencart_connect_url = 'https://www.opencart.com/index.php?route=external/braintree_auth/connect';
      6 	private $opencart_retrieve_url = 'https://www.opencart.com/index.php?route=external/braintree_auth/retrieve';
      7 
      8 	public function index() {
      9 		$this->load->language('extension/payment/pp_braintree');
     10 
     11 		$this->document->setTitle($this->language->get('heading_title'));
     12 
     13 		$this->load->model('setting/setting');
     14 
     15 		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
     16 			foreach ($this->request->post['payment_pp_braintree_account'] as $currency => $account) {
     17 				if (!isset($account['status'])) {
     18 					$this->request->post['payment_pp_braintree_account'][$currency]['status'] = 0;
     19 				}
     20 			}
     21 
     22 			$this->model_setting_setting->editSetting('payment_pp_braintree', $this->request->post);
     23 
     24 			$this->session->data['success'] = $this->language->get('text_success');
     25 
     26 			$this->response->redirect($this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=payment', true));
     27 		}
     28 
     29 		$data['button_configure'] = $this->url->link('extension/module/pp_braintree_button/configure', 'user_token=' . $this->session->data['user_token'], true);
     30 
     31 		$data['user_token'] = $this->session->data['user_token'];
     32 
     33 		$this->load->model('localisation/order_status');
     34 
     35 		$data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();
     36 
     37 		$this->load->model('localisation/geo_zone');
     38 
     39 		$data['geo_zones'] = $this->model_localisation_geo_zone->getGeoZones();
     40 
     41 		$this->load->model('localisation/currency');
     42 
     43 		$data['currencies'] = $this->model_localisation_currency->getCurrencies();
     44 
     45 		if (isset($this->error['warning'])) {
     46 			$data['error_warning'] = $this->error['warning'];
     47 		} else {
     48 			$data['error_warning'] = '';
     49 		}
     50 
     51 		if (isset($this->error['account'])) {
     52 			$data['error_account'] = $this->error['account'];
     53 		} else {
     54 			$data['error_account'] = array();
     55 		}
     56 
     57 		$data['breadcrumbs'] = array();
     58 
     59 		$data['breadcrumbs'][] = array(
     60 			'text' => $this->language->get('text_home'),
     61 			'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true)
     62 		);
     63 
     64 		$data['breadcrumbs'][] = array(
     65 			'text' => $this->language->get('text_extension'),
     66 			'href' => $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=payment', true)
     67 		);
     68 
     69 		$data['breadcrumbs'][] = array(
     70 			'text' => $this->language->get('heading_title'),
     71 			'href' => $this->url->link('extension/payment/pp_braintree', 'user_token=' . $this->session->data['user_token'], true)
     72 		);
     73 
     74 		$data['action'] = $this->url->link('extension/payment/pp_braintree', 'user_token=' . $this->session->data['user_token'], true);
     75 
     76 		$data['cancel'] = $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=payment', true);
     77 
     78 		if (isset($this->request->post['payment_pp_braintree_merchant_id'])) {
     79 			$data['payment_pp_braintree_merchant_id'] = $this->request->post['payment_pp_braintree_merchant_id'];
     80 		} else {
     81 			$data['payment_pp_braintree_merchant_id'] = $this->config->get('payment_pp_braintree_merchant_id');
     82 		}
     83 
     84 		if (isset($this->request->post['payment_pp_braintree_public_key'])) {
     85 			$data['payment_pp_braintree_public_key'] = $this->request->post['payment_pp_braintree_public_key'];
     86 		} else {
     87 			$data['payment_pp_braintree_public_key'] = $this->config->get('payment_pp_braintree_public_key');
     88 		}
     89 
     90 		if (isset($this->request->post['payment_pp_braintree_private_key'])) {
     91 			$data['payment_pp_braintree_private_key'] = $this->request->post['payment_pp_braintree_private_key'];
     92 		} else {
     93 			$data['payment_pp_braintree_private_key'] = $this->config->get('payment_pp_braintree_private_key');
     94 		}
     95 
     96 		if (isset($this->request->post['payment_pp_braintree_access_token'])) {
     97 			$data['payment_pp_braintree_access_token'] = $this->request->post['payment_pp_braintree_access_token'];
     98 		} else {
     99 			$data['payment_pp_braintree_access_token'] = $this->config->get('payment_pp_braintree_access_token');
    100 		}
    101 
    102 		if (isset($this->request->post['payment_pp_braintree_refresh_token'])) {
    103 			$data['payment_pp_braintree_refresh_token'] = $this->request->post['payment_pp_braintree_refresh_token'];
    104 		} else {
    105 			$data['payment_pp_braintree_refresh_token'] = $this->config->get('payment_pp_braintree_refresh_token');
    106 		}
    107 
    108 		if (isset($this->request->post['payment_pp_braintree_environment'])) {
    109 			$data['payment_pp_braintree_environment'] = $this->request->post['payment_pp_braintree_environment'];
    110 		} else {
    111 			$data['payment_pp_braintree_environment'] = $this->config->get('payment_pp_braintree_environment');
    112 		}
    113 
    114 		if (isset($this->request->post['payment_pp_braintree_settlement_immediate'])) {
    115 			$data['payment_pp_braintree_settlement_immediate'] = $this->request->post['payment_pp_braintree_settlement_immediate'];
    116 		} else {
    117 			$data['payment_pp_braintree_settlement_immediate'] = $this->config->get('payment_pp_braintree_settlement_immediate');
    118 		}
    119 
    120 		if (isset($this->request->post['payment_pp_braintree_card_vault'])) {
    121 			$data['payment_pp_braintree_card_vault'] = $this->request->post['payment_pp_braintree_card_vault'];
    122 		} else {
    123 			$data['payment_pp_braintree_card_vault'] = $this->config->get('payment_pp_braintree_card_vault');
    124 		}
    125 
    126 		if (isset($this->request->post['payment_pp_braintree_card_check_vault'])) {
    127 			$data['payment_pp_braintree_card_check_vault'] = $this->request->post['payment_pp_braintree_card_check_vault'];
    128 		} else {
    129 			$data['payment_pp_braintree_card_check_vault'] = $this->config->get('payment_pp_braintree_card_check_vault');
    130 		}
    131 
    132 		if (isset($this->request->post['payment_pp_braintree_paypal_vault'])) {
    133 			$data['payment_pp_braintree_paypal_vault'] = $this->request->post['payment_pp_braintree_paypal_vault'];
    134 		} else {
    135 			$data['payment_pp_braintree_paypal_vault'] = $this->config->get('payment_pp_braintree_paypal_vault');
    136 		}
    137 
    138 		if (isset($this->request->post['payment_pp_braintree_paypal_check_vault'])) {
    139 			$data['payment_pp_braintree_paypal_check_vault'] = $this->request->post['payment_pp_braintree_paypal_check_vault'];
    140 		} else {
    141 			$data['payment_pp_braintree_paypal_check_vault'] = $this->config->get('payment_pp_braintree_paypal_check_vault');
    142 		}
    143 
    144 		if (isset($this->request->post['payment_pp_braintree_vault_cvv_3ds'])) {
    145 			$data['payment_pp_braintree_vault_cvv_3ds'] = $this->request->post['payment_pp_braintree_vault_cvv_3ds'];
    146 		} else {
    147 			$data['payment_pp_braintree_vault_cvv_3ds'] = $this->config->get('payment_pp_braintree_vault_cvv_3ds');
    148 		}
    149 
    150 		if (isset($this->request->post['payment_pp_braintree_debug'])) {
    151 			$data['payment_pp_braintree_debug'] = $this->request->post['payment_pp_braintree_debug'];
    152 		} else {
    153 			$data['payment_pp_braintree_debug'] = $this->config->get('payment_pp_braintree_debug');
    154 		}
    155 
    156 		if (isset($this->request->post['payment_pp_braintree_total'])) {
    157 			$data['payment_pp_braintree_total'] = $this->request->post['payment_pp_braintree_total'];
    158 		} else {
    159 			$data['payment_pp_braintree_total'] = $this->config->get('payment_pp_braintree_total');
    160 		}
    161 
    162 		if (isset($this->request->post['payment_pp_braintree_geo_zone_id'])) {
    163 			$data['payment_pp_braintree_geo_zone_id'] = $this->request->post['payment_pp_braintree_geo_zone_id'];
    164 		} else {
    165 			$data['payment_pp_braintree_geo_zone_id'] = $this->config->get('payment_pp_braintree_geo_zone_id');
    166 		}
    167 
    168 		if (isset($this->request->post['payment_pp_braintree_status'])) {
    169 			$data['payment_pp_braintree_status'] = $this->request->post['payment_pp_braintree_status'];
    170 		} else {
    171 			$data['payment_pp_braintree_status'] = $this->config->get('payment_pp_braintree_status');
    172 		}
    173 
    174 		if (isset($this->request->post['payment_pp_braintree_sort_order'])) {
    175 			$data['payment_pp_braintree_sort_order'] = $this->request->post['payment_pp_braintree_sort_order'];
    176 		} else {
    177 			$data['payment_pp_braintree_sort_order'] = $this->config->get('payment_pp_braintree_sort_order');
    178 		}
    179 
    180 		if (isset($this->request->post['payment_pp_braintree_account'])) {
    181 			$data['payment_pp_braintree_account'] = $this->request->post['payment_pp_braintree_account'];
    182 		} else {
    183 			$data['payment_pp_braintree_account'] = $this->config->get('payment_pp_braintree_account');
    184 		}
    185 
    186 		if (isset($this->request->post['payment_pp_braintree_authorization_expired_id'])) {
    187 			$data['payment_pp_braintree_authorization_expired_id'] = $this->request->post['payment_pp_braintree_authorization_expired_id'];
    188 		} else {
    189 			$data['payment_pp_braintree_authorization_expired_id'] = $this->config->get('payment_pp_braintree_authorization_expired_id');
    190 		}
    191 
    192 		if (isset($this->request->post['payment_pp_braintree_authorized_id'])) {
    193 			$data['payment_pp_braintree_authorized_id'] = $this->request->post['payment_pp_braintree_authorized_id'];
    194 		} else {
    195 			$data['payment_pp_braintree_authorized_id'] = $this->config->get('payment_pp_braintree_authorized_id');
    196 		}
    197 
    198 		if (isset($this->request->post['payment_pp_braintree_authorizing_id'])) {
    199 			$data['payment_pp_braintree_authorizing_id'] = $this->request->post['payment_pp_braintree_authorizing_id'];
    200 		} else {
    201 			$data['payment_pp_braintree_authorizing_id'] = $this->config->get('payment_pp_braintree_authorizing_id');
    202 		}
    203 
    204 		if (isset($this->request->post['payment_pp_braintree_settlement_pending_id'])) {
    205 			$data['payment_pp_braintree_settlement_pending_id'] = $this->request->post['payment_pp_braintree_settlement_pending_id'];
    206 		} else {
    207 			$data['payment_pp_braintree_settlement_pending_id'] = $this->config->get('payment_pp_braintree_settlement_pending_id');
    208 		}
    209 
    210 		if (isset($this->request->post['payment_pp_braintree_failed_id'])) {
    211 			$data['payment_pp_braintree_failed_id'] = $this->request->post['payment_pp_braintree_failed_id'];
    212 		} else {
    213 			$data['payment_pp_braintree_failed_id'] = $this->config->get('payment_pp_braintree_failed_id');
    214 		}
    215 
    216 		if (isset($this->request->post['payment_pp_braintree_gateway_rejected_id'])) {
    217 			$data['payment_pp_braintree_gateway_rejected_id'] = $this->request->post['payment_pp_braintree_gateway_rejected_id'];
    218 		} else {
    219 			$data['payment_pp_braintree_gateway_rejected_id'] = $this->config->get('payment_pp_braintree_gateway_rejected_id');
    220 		}
    221 
    222 		if (isset($this->request->post['payment_pp_braintree_processor_declined_id'])) {
    223 			$data['payment_pp_braintree_processor_declined_id'] = $this->request->post['payment_pp_braintree_processor_declined_id'];
    224 		} else {
    225 			$data['payment_pp_braintree_processor_declined_id'] = $this->config->get('payment_pp_braintree_processor_declined_id');
    226 		}
    227 
    228 		if (isset($this->request->post['payment_pp_braintree_settled_id'])) {
    229 			$data['payment_pp_braintree_settled_id'] = $this->request->post['payment_pp_braintree_settled_id'];
    230 		} else {
    231 			$data['payment_pp_braintree_settled_id'] = $this->config->get('payment_pp_braintree_settled_id');
    232 		}
    233 
    234 		if (isset($this->request->post['payment_pp_braintree_settling_id'])) {
    235 			$data['payment_pp_braintree_settling_id'] = $this->request->post['payment_pp_braintree_settling_id'];
    236 		} else {
    237 			$data['payment_pp_braintree_settling_id'] = $this->config->get('payment_pp_braintree_settling_id');
    238 		}
    239 
    240 		if (isset($this->request->post['payment_pp_braintree_submitted_for_settlement_id'])) {
    241 			$data['payment_pp_braintree_submitted_for_settlement_id'] = $this->request->post['payment_pp_braintree_submitted_for_settlement_id'];
    242 		} else {
    243 			$data['payment_pp_braintree_submitted_for_settlement_id'] = $this->config->get('payment_pp_braintree_submitted_for_settlement_id');
    244 		}
    245 
    246 		if (isset($this->request->post['payment_pp_braintree_voided_id'])) {
    247 			$data['payment_pp_braintree_voided_id'] = $this->request->post['payment_pp_braintree_voided_id'];
    248 		} else {
    249 			$data['payment_pp_braintree_voided_id'] = $this->config->get('payment_pp_braintree_voided_id');
    250 		}
    251 
    252 		if (isset($this->request->post['payment_pp_braintree_3ds_status'])) {
    253 			$data['payment_pp_braintree_3ds_status'] = $this->request->post['payment_pp_braintree_3ds_status'];
    254 		} else {
    255 			$data['payment_pp_braintree_3ds_status'] = $this->config->get('payment_pp_braintree_3ds_status');
    256 		}
    257 
    258 		if (isset($this->request->post['payment_pp_braintree_3ds_unsupported_card'])) {
    259 			$data['payment_pp_braintree_3ds_unsupported_card'] = $this->request->post['payment_pp_braintree_3ds_unsupported_card'];
    260 		} else {
    261 			$data['payment_pp_braintree_3ds_unsupported_card'] = $this->config->get('payment_pp_braintree_3ds_unsupported_card');
    262 		}
    263 
    264 		if (isset($this->request->post['payment_pp_braintree_3ds_lookup_error'])) {
    265 			$data['payment_pp_braintree_3ds_lookup_error'] = $this->request->post['payment_pp_braintree_3ds_lookup_error'];
    266 		} else {
    267 			$data['payment_pp_braintree_3ds_lookup_error'] = $this->config->get('payment_pp_braintree_3ds_lookup_error');
    268 		}
    269 
    270 		if (isset($this->request->post['payment_pp_braintree_3ds_lookup_enrolled'])) {
    271 			$data['payment_pp_braintree_3ds_lookup_enrolled'] = $this->request->post['payment_pp_braintree_3ds_lookup_enrolled'];
    272 		} else {
    273 			$data['payment_pp_braintree_3ds_lookup_enrolled'] = $this->config->get('payment_pp_braintree_3ds_lookup_enrolled');
    274 		}
    275 
    276 		if (isset($this->request->post['payment_pp_braintree_3ds_lookup_not_enrolled'])) {
    277 			$data['payment_pp_braintree_3ds_lookup_not_enrolled'] = $this->request->post['payment_pp_braintree_3ds_lookup_not_enrolled'];
    278 		} else {
    279 			$data['payment_pp_braintree_3ds_lookup_not_enrolled'] = $this->config->get('payment_pp_braintree_3ds_lookup_not_enrolled');
    280 		}
    281 
    282 		if (isset($this->request->post['payment_pp_braintree_3ds_not_participating'])) {
    283 			$data['payment_pp_braintree_3ds_not_participating'] = $this->request->post['payment_pp_braintree_3ds_not_participating'];
    284 		} else {
    285 			$data['payment_pp_braintree_3ds_not_participating'] = $this->config->get('payment_pp_braintree_3ds_not_participating');
    286 		}
    287 
    288 		if (isset($this->request->post['payment_pp_braintree_3ds_unavailable'])) {
    289 			$data['payment_pp_braintree_3ds_unavailable'] = $this->request->post['payment_pp_braintree_3ds_unavailable'];
    290 		} else {
    291 			$data['payment_pp_braintree_3ds_unavailable'] = $this->config->get('payment_pp_braintree_3ds_unavailable');
    292 		}
    293 
    294 		if (isset($this->request->post['payment_pp_braintree_3ds_signature_failed'])) {
    295 			$data['payment_pp_braintree_3ds_signature_failed'] = $this->request->post['payment_pp_braintree_3ds_signature_failed'];
    296 		} else {
    297 			$data['payment_pp_braintree_3ds_signature_failed'] = $this->config->get('payment_pp_braintree_3ds_signature_failed');
    298 		}
    299 
    300 		if (isset($this->request->post['payment_pp_braintree_3ds_successful'])) {
    301 			$data['payment_pp_braintree_3ds_successful'] = $this->request->post['payment_pp_braintree_3ds_successful'];
    302 		} else {
    303 			$data['payment_pp_braintree_3ds_successful'] = $this->config->get('payment_pp_braintree_3ds_successful');
    304 		}
    305 
    306 		if (isset($this->request->post['payment_pp_braintree_3ds_attempt_successful'])) {
    307 			$data['payment_pp_braintree_3ds_attempt_successful'] = $this->request->post['payment_pp_braintree_3ds_attempt_successful'];
    308 		} else {
    309 			$data['payment_pp_braintree_3ds_attempt_successful'] = $this->config->get('payment_pp_braintree_3ds_attempt_successful');
    310 		}
    311 
    312 		if (isset($this->request->post['payment_pp_braintree_3ds_failed'])) {
    313 			$data['payment_pp_braintree_3ds_failed'] = $this->request->post['payment_pp_braintree_3ds_failed'];
    314 		} else {
    315 			$data['payment_pp_braintree_3ds_failed'] = $this->config->get('payment_pp_braintree_3ds_failed');
    316 		}
    317 
    318 		if (isset($this->request->post['payment_pp_braintree_3ds_unable_to_auth'])) {
    319 			$data['payment_pp_braintree_3ds_unable_to_auth'] = $this->request->post['payment_pp_braintree_3ds_unable_to_auth'];
    320 		} else {
    321 			$data['payment_pp_braintree_3ds_unable_to_auth'] = $this->config->get('payment_pp_braintree_3ds_unable_to_auth');
    322 		}
    323 
    324 		if (isset($this->request->post['payment_pp_braintree_3ds_error'])) {
    325 			$data['payment_pp_braintree_3ds_error'] = $this->request->post['payment_pp_braintree_3ds_error'];
    326 		} else {
    327 			$data['payment_pp_braintree_3ds_error'] = $this->config->get('payment_pp_braintree_3ds_error');
    328 		}
    329 
    330 		if (isset($this->request->post['payment_pp_braintree_paypal_option'])) {
    331 			$data['payment_pp_braintree_paypal_option'] = $this->request->post['payment_pp_braintree_paypal_option'];
    332 		} else {
    333 			$data['payment_pp_braintree_paypal_option'] = $this->config->get('payment_pp_braintree_paypal_option');
    334 		}
    335 
    336 		if (isset($this->request->post['payment_pp_braintree_paypal_button_colour'])) {
    337 			$data['payment_pp_braintree_paypal_button_colour'] = $this->request->post['payment_pp_braintree_paypal_button_colour'];
    338 		} else {
    339 			$data['payment_pp_braintree_paypal_button_colour'] = $this->config->get('payment_pp_braintree_paypal_button_colour');
    340 		}
    341 
    342 		if (isset($this->request->post['payment_pp_braintree_paypal_button_size'])) {
    343 			$data['payment_pp_braintree_paypal_button_size'] = $this->request->post['payment_pp_braintree_paypal_button_size'];
    344 		} else {
    345 			$data['payment_pp_braintree_paypal_button_size'] = $this->config->get('payment_pp_braintree_paypal_button_size');
    346 		}
    347 
    348 		if (isset($this->request->post['payment_pp_braintree_paypal_button_shape'])) {
    349 			$data['payment_pp_braintree_paypal_button_shape'] = $this->request->post['payment_pp_braintree_paypal_button_shape'];
    350 		} else {
    351 			$data['payment_pp_braintree_paypal_button_shape'] = $this->config->get('payment_pp_braintree_paypal_button_shape');
    352 		}
    353 
    354 		if (isset($this->request->post['payment_pp_braintree_billing_agreement'])) {
    355 			$data['payment_pp_braintree_billing_agreement'] = $this->request->post['payment_pp_braintree_billing_agreement'];
    356 		} else {
    357 			$data['payment_pp_braintree_billing_agreement'] = $this->config->get('payment_pp_braintree_billing_agreement');
    358 		}
    359 
    360 		$data['transaction_statuses'] = array(
    361 			'authorization_expired',
    362 			'authorized',
    363 			'authorizing',
    364 			'settlement_pending',
    365 			'failed',
    366 			'gateway_rejected',
    367 			'processor_declined',
    368 			'settled',
    369 			'settling',
    370 			'submitted_for_settlement',
    371 			'voided'
    372 		);
    373 
    374 		$data['card_types'] = array(
    375 			'Visa',
    376 			'MasterCard',
    377 			'American Express',
    378 			'Discover',
    379 			'JCB',
    380 			'Maestro'
    381 		);
    382 
    383 		if (isset($this->request->get['retrieve_code'])) {
    384 			$data['retrieve_code'] = $this->request->get['retrieve_code'];
    385 
    386 			$curl = curl_init($this->opencart_retrieve_url);
    387 
    388 			$post_data = array(
    389 				'return_url' => $this->url->link('extension/payment/pp_braintree', 'user_token=' . $this->session->data['user_token'], true),
    390 				'retrieve_code' => $this->request->get['retrieve_code'],
    391 				'store_version' => VERSION,
    392 			);
    393 
    394 			curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    395 			curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    396 			curl_setopt($curl, CURLOPT_FORBID_REUSE, 1);
    397 			curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);
    398 			curl_setopt($curl, CURLOPT_POST, true);
    399 			curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
    400 
    401 			$curl_response = curl_exec($curl);
    402 			$config_response = json_decode($curl_response, true);
    403 			curl_close($curl);
    404 
    405 			if (isset($config_response['merchant_id']) && isset($config_response['access_token']) && isset($config_response['refresh_token'])) {
    406 				$braintree_settings = $this->model_setting_setting->getSetting('payment_pp_braintree');
    407 				$braintree_settings['payment_pp_braintree_merchant_id'] = $config_response['merchant_id'];
    408 				$braintree_settings['payment_pp_braintree_access_token'] = $config_response['access_token'];
    409 				$braintree_settings['payment_pp_braintree_refresh_token'] = $config_response['refresh_token'];
    410 				$braintree_settings['payment_pp_braintree_environment'] = $config_response['environment'];
    411 				$braintree_settings['payment_pp_braintree_public_key'] = '';
    412 				$braintree_settings['payment_pp_braintree_private_key'] = '';
    413 
    414 				$this->model_setting_setting->editSetting('payment_pp_braintree', $braintree_settings);
    415 
    416 				$data['payment_pp_braintree_merchant_id'] = $config_response['merchant_id'];
    417 				$data['payment_pp_braintree_access_token'] = $config_response['access_token'];
    418 				$data['payment_pp_braintree_refresh_token'] = $config_response['refresh_token'];
    419 				$data['payment_pp_braintree_environment'] = $config_response['environment'];
    420 				$data['payment_pp_braintree_public_key'] = '';
    421 				$data['payment_pp_braintree_private_key'] = '';
    422 
    423 				$data['success'] = $this->language->get('text_success_connect');
    424 			}
    425 		}
    426 
    427 		$data['auth_connect_url'] = '';
    428 
    429 		// If Braintree is not setup yet, request auth token for merchant on-boarding flow
    430 		if ($data['payment_pp_braintree_merchant_id'] == '') {
    431 			$curl = curl_init($this->opencart_connect_url);
    432 
    433 			$this->load->model('localisation/country');
    434 			$country = $this->model_localisation_country->getCountry($this->config->get('config_country_id'));
    435 
    436 			$post_data = array(
    437 				'return_url' => $this->url->link('extension/payment/pp_braintree', 'user_token=' . $this->session->data['user_token'], true),
    438 				'store_url' => HTTPS_CATALOG,
    439 				'store_version' => VERSION,
    440 				'store_country' => (isset($country['iso_code_3']) ? $country['iso_code_3'] : ''),
    441 			);
    442 
    443 			curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    444 			curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    445 			curl_setopt($curl, CURLOPT_FORBID_REUSE, 1);
    446 			curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);
    447 			curl_setopt($curl, CURLOPT_POST, true);
    448 			curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
    449 
    450 			$curl_response = curl_exec($curl);
    451 
    452 			$curl_response = json_decode($curl_response, true);
    453 
    454 			curl_close($curl);
    455 
    456 			if ($curl_response['url']) {
    457 				$data['auth_connect_url'] = $curl_response['url'];
    458 			}
    459 		}
    460 
    461 		$data['braintree_config'] = array();
    462 		$data['braintree_config']['three_d_secure_enabled'] = 0;
    463 		$data['braintree_config']['paypal_enabled'] = 0;
    464 
    465 		$data['error_braintree_account_3ds'] = $this->language->get('error_braintree_account_3ds');
    466 		$data['error_braintree_account_paypal'] = $this->language->get('error_braintree_account_paypal');
    467 
    468 		// load the account info from braintree if the config has been added yet.
    469 		if (!empty($data['payment_pp_braintree_access_token']) || (!empty($data['payment_pp_braintree_environment']) && !empty($data['payment_pp_braintree_merchant_id']) && !empty($data['payment_pp_braintree_public_key']) && !empty($data['payment_pp_braintree_private_key']))) {
    470 			$this->initialise($data['payment_pp_braintree_access_token'], array(
    471 				'payment_pp_braintree_environment' => $data['payment_pp_braintree_environment'],
    472 				'payment_pp_braintree_merchant_id' => $data['payment_pp_braintree_merchant_id'],
    473 				'payment_pp_braintree_public_key'	=> $data['payment_pp_braintree_public_key'],
    474 				'payment_pp_braintree_private_key' => $data['payment_pp_braintree_private_key'],
    475 			));
    476 
    477 			$verify_credentials = $this->model_extension_payment_pp_braintree->verifyCredentials($this->gateway);
    478 
    479 			if (!$verify_credentials) {
    480 				$this->error['warning'] = $this->language->get('error_connection');
    481 			} else {
    482 				$merchant_config = json_decode(base64_decode($verify_credentials), true);
    483 
    484 				if (isset($merchant_config['threeDSecureEnabled']) && $merchant_config['threeDSecureEnabled'] == 1) {
    485 					$data['braintree_config']['three_d_secure_enabled'] = 1;
    486 				}
    487 
    488 				if (isset($merchant_config['paypalEnabled']) && $merchant_config['paypalEnabled'] == 1) {
    489 					$data['braintree_config']['paypal_enabled'] = 1;
    490 				}
    491 			}
    492 		}
    493 
    494 		$data['header'] = $this->load->controller('common/header');
    495 		$data['column_left'] = $this->load->controller('common/column_left');
    496 		$data['footer'] = $this->load->controller('common/footer');
    497 
    498 		$this->response->setOutput($this->load->view('extension/payment/pp_braintree', $data));
    499 	}
    500 
    501 	public function install() {
    502 		$this->load->model('setting/setting');
    503 
    504 		$defaults = array();
    505 
    506 		// 3D secure defaults
    507 		$defaults['payment_pp_braintree_3ds_unsupported_card'] = 1;
    508 		$defaults['payment_pp_braintree_3ds_lookup_error'] = 1;
    509 		$defaults['payment_pp_braintree_3ds_lookup_enrolled'] = 1;
    510 		$defaults['payment_pp_braintree_3ds_lookup_not_enrolled'] = 1;
    511 		$defaults['payment_pp_braintree_3ds_not_participating'] = 1;
    512 		$defaults['payment_pp_braintree_3ds_unavailable'] = 1;
    513 		$defaults['payment_pp_braintree_3ds_signature_failed'] = 0;
    514 		$defaults['payment_pp_braintree_3ds_successful'] = 1;
    515 		$defaults['payment_pp_braintree_3ds_attempt_successful'] = 1;
    516 		$defaults['payment_pp_braintree_3ds_failed'] = 0;
    517 		$defaults['payment_pp_braintree_3ds_unable_to_auth'] = 1;
    518 		$defaults['payment_pp_braintree_3ds_error'] = 1;
    519 
    520 		// Order Status defaults
    521 		$defaults['payment_pp_braintree_authorization_expired_id'] = 14;
    522 		$defaults['payment_pp_braintree_authorized_id'] = 2;
    523 		$defaults['payment_pp_braintree_authorizing_id'] = 1;
    524 		$defaults['payment_pp_braintree_failed_id'] = 10;
    525 		$defaults['payment_pp_braintree_gateway_rejected_id'] = 8;
    526 		$defaults['payment_pp_braintree_processor_declined_id'] = 8;
    527 		$defaults['payment_pp_braintree_settled_id'] = 2;
    528 		$defaults['payment_pp_braintree_settling_id'] = 2;
    529 		$defaults['payment_pp_braintree_settlement_pending_id'] = 2;
    530 		$defaults['payment_pp_braintree_submitted_for_settlement_id'] = 2;
    531 		$defaults['payment_pp_braintree_voided_id'] = 16;
    532 
    533 		$defaults['payment_pp_braintree_settlement_immediate'] = 1;
    534 
    535 		// PayPal options
    536 		$defaults['payment_pp_braintree_paypal_option'] = 1;
    537 		$defaults['payment_pp_braintree_paypal_button_size'] = 'small';
    538 		$defaults['payment_pp_braintree_paypal_button_shape'] = 'rect';
    539 
    540 		// Vault options
    541 		$defaults['payment_pp_braintree_card_vault'] = 1;
    542 		$defaults['payment_pp_braintree_paypal_vault'] = 0;
    543 		$defaults['payment_pp_braintree_card_check_vault'] = 1;
    544 		$defaults['payment_pp_braintree_paypal_check_vault'] = 0;
    545 
    546 		$this->model_setting_setting->editSetting('payment_pp_braintree', $defaults);
    547 	}
    548 
    549 	public function order() {
    550 		$this->load->language('extension/payment/pp_braintree');
    551 
    552 		$data['user_token'] = $this->session->data['user_token'];
    553 		
    554 		$data['order_id'] = $this->request->get['order_id'];
    555 
    556 		return $this->load->view('extension/payment/pp_braintree_order', $data);
    557 	}
    558 
    559 	public function getTransaction() {
    560 		$this->load->language('extension/payment/pp_braintree');
    561 
    562 		$this->load->model('extension/payment/pp_braintree');
    563 		$this->load->model('sale/order');
    564 
    565 		if (!$this->config->get('payment_pp_braintree_status') || (!isset($this->request->get['order_id']) && !isset($this->request->get['transaction_id']))) {
    566 			return;
    567 		}
    568 
    569 		$this->initialise($this->config->get('payment_pp_braintree_access_token'), array(
    570 			'payment_pp_braintree_environment' => $this->config->get('payment_pp_braintree_environment'),
    571 			'payment_pp_braintree_merchant_id' => $this->config->get('payment_pp_braintree_merchant_id'),
    572 			'payment_pp_braintree_public_key'	=> $this->config->get('payment_pp_braintree_public_key'),
    573 			'payment_pp_braintree_private_key' => $this->config->get('payment_pp_braintree_private_key')
    574 		));
    575 
    576 		if (isset($this->request->get['order_id'])) {
    577 			$search = array(
    578 				Braintree_TransactionSearch::orderId()->is($this->request->get['order_id'])
    579 			);
    580 		} elseif (isset($this->request->get['transaction_id'])) {
    581 			$search = array(
    582 				Braintree_TransactionSearch::id()->is($this->request->get['transaction_id'])
    583 			);
    584 		}
    585 
    586 		$search_transactions = $this->model_extension_payment_pp_braintree->getTransactions($this->gateway, $search);
    587 
    588 		$transaction = array();
    589 
    590 		foreach ($search_transactions as $search_transaction) {
    591 			$transaction = $search_transaction;
    592 		}
    593 
    594 		$data['transaction'] = array();
    595 
    596 		if ($transaction) {
    597 			$data['transaction_id'] = $transaction->id;
    598 			
    599 			$data['user_token'] = $this->session->data['user_token'];
    600 
    601 			$data['void_action'] = $data['settle_action'] = $data['refund_action'] = false;
    602 
    603 			switch ($transaction->status) {
    604 				case 'authorized':
    605 					$data['void_action'] = true;
    606 					$data['settle_action'] = true;
    607 					break;
    608 				case 'submitted_for_settlement':
    609 					$data['void_action'] = true;
    610 					break;
    611 				case 'settling':
    612 					$data['refund_action'] = true;
    613 					break;
    614 				case 'settled':
    615 					$data['refund_action'] = true;
    616 					break;
    617 			}
    618 
    619 			$statuses = array();
    620 
    621 			foreach ($transaction->statusHistory as $status_history) {
    622 				$created_at = $status_history->timestamp;
    623 
    624 				$statuses[] = array(
    625 					'status'     => $status_history->status,
    626 					'date_added' => date($this->language->get('datetime_format'), strtotime($created_at->format('Y-m-d H:i:s e')))
    627 				);
    628 			}
    629 
    630 			$data['statuses'] = $statuses;
    631 
    632 			$max_settle_amount = $transaction->amount;
    633 
    634 			$max_refund_amount = $transaction->amount;
    635 
    636 			$data['refunds'] = array();
    637 
    638 			foreach (array_reverse($transaction->refundIds) as $refund_id) {
    639 				$refund = $this->model_extension_payment_pp_braintree->getTransaction($this->gateway, $refund_id);
    640 
    641 				$successful_statuses = array(
    642 					'authorized',
    643 					'authorizing',
    644 					'settlement_pending',
    645 					'settlement_confirmed',
    646 					'settled',
    647 					'settling',
    648 					'submitted_for_settlement'
    649 				);
    650 
    651 				if (in_array($refund->status, $successful_statuses)) {
    652 					$max_refund_amount -= $refund->amount;
    653 				}
    654 
    655 				$created_at = $refund->createdAt;
    656 
    657 				$data['refunds'][] = array(
    658 					'date_added' => date($this->language->get('datetime_format'), strtotime($created_at->format('Y-m-d H:i:s e'))),
    659 					'amount'	 => $this->currency->format($refund->amount, $refund->currencyIsoCode, '1.00000000', true),
    660 					'status'	 => $refund->status
    661 				);
    662 			}
    663 
    664 			//If nothing left to refund, disable refund action
    665 			if (!$max_refund_amount) {
    666 				$data['refund_action'] = false;
    667 			}
    668 
    669 			$data['max_settle_amount'] = $this->currency->format($max_settle_amount, $transaction->currencyIsoCode, '1.00000000', false);
    670 
    671 			$data['max_refund_amount'] = $this->currency->format($max_refund_amount, $transaction->currencyIsoCode, '1.00000000', false);
    672 
    673 			$amount = $this->currency->format($transaction->amount, $transaction->currencyIsoCode, '1.00000000', true);
    674 
    675 			$data['symbol_left'] = $this->currency->getSymbolLeft($transaction->currencyIsoCode);
    676 			$data['symbol_right'] = $this->currency->getSymbolRight($transaction->currencyIsoCode);
    677 
    678 			$created_at = $transaction->createdAt;
    679 
    680 			if ($transaction->threeDSecureInfo) {
    681 				if ($transaction->threeDSecureInfo->liabilityShifted) {
    682 					$liability_shifted = $this->language->get('text_yes');
    683 				} else {
    684 					$liability_shifted = $this->language->get('text_no');
    685 				}
    686 			}
    687 
    688 			if ($transaction->threeDSecureInfo) {
    689 				if ($transaction->threeDSecureInfo->liabilityShiftPossible) {
    690 					$liability_shift_possible = $this->language->get('text_yes');
    691 				} else {
    692 					$liability_shift_possible = $this->language->get('text_no');
    693 				}
    694 			}
    695 
    696 			$data['transaction'] = array(
    697 				'status'			  => $transaction->status,
    698 				'transaction_id'	  => $transaction->id,
    699 				'type'				  => $transaction->type,
    700 				'date_added'		  => date($this->language->get('datetime_format'), strtotime($created_at->format('Y-m-d H:i:s e'))),
    701 				'merchant_account_id' => $transaction->merchantAccountId,
    702 				'payment_type'		  => $transaction->paymentInstrumentType,
    703 				'currency'			  => $transaction->currencyIsoCode,
    704 				'amount'			  => $amount,
    705 				'order_id'			  => $transaction->orderId,
    706 				'processor_code'	  => $transaction->processorAuthorizationCode,
    707 				'cvv_response'		  => $transaction->cvvResponseCode,
    708 				'avs_response'		  => sprintf($this->language->get('text_avs_response'), $transaction->avsStreetAddressResponseCode, $transaction->avsPostalCodeResponseCode),
    709 				'threeds_enrolled'		  => ($transaction->threeDSecureInfo ? $transaction->threeDSecureInfo->enrolled : ''),
    710 				'threeds_status'		  => ($transaction->threeDSecureInfo ? $transaction->threeDSecureInfo->status : ''),
    711 				'threeds_shifted'		  => ($transaction->threeDSecureInfo ? $liability_shifted : ''),
    712 				'threeds_shift_possible'  => ($transaction->threeDSecureInfo ? $liability_shift_possible : '')
    713 			);
    714 
    715 			$data['text_confirm_void'] = $this->language->get('text_confirm_void');
    716 			$data['text_confirm_settle'] = $this->language->get('text_confirm_settle');
    717 			$data['text_confirm_refund'] = $this->language->get('text_confirm_refund');
    718 
    719 			$this->response->setOutput($this->load->view('extension/payment/pp_braintree_order_ajax', $data));
    720 		}
    721 	}
    722 
    723 	public function transactionCommand() {
    724 		$this->load->language('extension/payment/pp_braintree');
    725 
    726 		$this->load->model('extension/payment/pp_braintree');
    727 
    728 		$this->initialise($this->config->get('payment_pp_braintree_access_token'), array(
    729 			'payment_pp_braintree_environment' => $this->config->get('payment_pp_braintree_environment'),
    730 			'payment_pp_braintree_merchant_id' => $this->config->get('payment_pp_braintree_merchant_id'),
    731 			'payment_pp_braintree_public_key'	=> $this->config->get('payment_pp_braintree_public_key'),
    732 			'payment_pp_braintree_private_key' => $this->config->get('payment_pp_braintree_private_key')
    733 		));
    734 
    735 		$json = array();
    736 
    737 		$success = $error = '';
    738 
    739 		if ($this->request->post['type'] == 'void') {
    740 			$action = $this->model_extension_payment_pp_braintree->voidTransaction($this->gateway, $this->request->post['transaction_id']);
    741 		} elseif ($this->request->post['type'] == 'settle' && $this->request->post['amount']) {
    742 			$action = $this->model_extension_payment_pp_braintree->settleTransaction($this->gateway, $this->request->post['transaction_id'], $this->request->post['amount']);
    743 		} elseif ($this->request->post['type'] == 'refund' && $this->request->post['amount']) {
    744 			$action = $this->model_extension_payment_pp_braintree->refundTransaction($this->gateway, $this->request->post['transaction_id'], $this->request->post['amount']);
    745 		} else {
    746 			$error = true;
    747 		}
    748 
    749 		if (!$error && $action && $action->success) {
    750 			$success = $this->language->get('text_success_action');
    751 		} elseif (!$error && $action && isset($action->message)) {
    752 			$error = sprintf($this->language->get('text_error_settle'), $action->message);
    753 		} else {
    754 			$error = $this->language->get('text_error_generic');
    755 		}
    756 
    757 		$json['success'] = $success;
    758 		$json['error'] = $error;
    759 
    760 		$this->response->addHeader('Content-Type: application/json');
    761 		$this->response->setOutput(json_encode($json));
    762 	}
    763 
    764 	public function search() {
    765 		$this->load->language('extension/payment/pp_braintree');
    766 
    767 		$this->load->model('extension/payment/pp_braintree');
    768 		$this->load->model('customer/customer');
    769 		$this->load->model('sale/order');
    770 
    771 		$this->initialise($this->config->get('payment_pp_braintree_access_token'), array(
    772 			'payment_pp_braintree_environment' => $this->config->get('payment_pp_braintree_environment'),
    773 			'payment_pp_braintree_merchant_id' => $this->config->get('payment_pp_braintree_merchant_id'),
    774 			'payment_pp_braintree_public_key'	=> $this->config->get('payment_pp_braintree_public_key'),
    775 			'payment_pp_braintree_private_key' => $this->config->get('payment_pp_braintree_private_key')
    776 		));
    777 
    778 		$json = array();
    779 
    780 		$success = $error = '';
    781 
    782 		if (isset($this->request->get['filter_transaction_id'])) {
    783 			$filter_transaction_id = $this->request->get['filter_transaction_id'];
    784 		} else {
    785 			$filter_transaction_id = null;
    786 		}
    787 
    788 		if (isset($this->request->get['filter_transaction_type'])) {
    789 			$filter_transaction_type = $this->request->get['filter_transaction_type'];
    790 		} else {
    791 			$filter_transaction_type = null;
    792 		}
    793 
    794 		if (isset($this->request->get['filter_payment_type'])) {
    795 			$filter_payment_type = $this->request->get['filter_payment_type'];
    796 		} else {
    797 			$filter_payment_type = null;
    798 		}
    799 
    800 		if (isset($this->request->get['filter_card_type'])) {
    801 			$filter_card_type = $this->request->get['filter_card_type'];
    802 		} else {
    803 			$filter_card_type = null;
    804 		}
    805 
    806 		if (isset($this->request->get['filter_merchant_account_id'])) {
    807 			$filter_merchant_account_id = $this->request->get['filter_merchant_account_id'];
    808 		} else {
    809 			$filter_merchant_account_id = null;
    810 		}
    811 
    812 		if (isset($this->request->get['filter_transaction_status'])) {
    813 			$filter_transaction_status = $this->request->get['filter_transaction_status'];
    814 		} else {
    815 			$filter_transaction_status = null;
    816 		}
    817 
    818 		if (isset($this->request->get['filter_date_from'])) {
    819 			$filter_date_from = $this->request->get['filter_date_from'];
    820 		} else {
    821 			$filter_date_from = null;
    822 		}
    823 
    824 		if (isset($this->request->get['filter_date_to'])) {
    825 			$filter_date_to = $this->request->get['filter_date_to'];
    826 		} else {
    827 			$filter_date_to = null;
    828 		}
    829 
    830 		if (isset($this->request->get['filter_amount_from'])) {
    831 			$filter_amount_from = $this->request->get['filter_amount_from'];
    832 		} else {
    833 			$filter_amount_from = null;
    834 		}
    835 
    836 		if (isset($this->request->get['filter_amount_to'])) {
    837 			$filter_amount_to = $this->request->get['filter_amount_to'];
    838 		} else {
    839 			$filter_amount_to = null;
    840 		}
    841 
    842 		$json['transactions'] = array();
    843 
    844 		$search = array();
    845 
    846 		if ($filter_transaction_id) {
    847 			$search[] = Braintree_TransactionSearch::id()->is($filter_transaction_id);
    848 		}
    849 
    850 		if ($filter_transaction_type) {
    851 			if ($filter_transaction_type == 'sale') {
    852 				$transaction_type = Braintree_Transaction::SALE;
    853 			} elseif ($filter_transaction_type == 'credit') {
    854 				$transaction_type = Braintree_Transaction::CREDIT;
    855 			}
    856 
    857 			$search[] = Braintree_TransactionSearch::type()->is($transaction_type);
    858 		}
    859 
    860 		if ($filter_payment_type) {
    861 			if ($filter_payment_type == 'Credit Card') {
    862 				$payment_type = 'CreditCardDetail';
    863 			} elseif ($filter_payment_type == 'PayPal') {
    864 				$payment_type = 'PayPalDetail';
    865 			}
    866 
    867 			$search[] = Braintree_TransactionSearch::paymentInstrumentType()->is($payment_type);
    868 		}
    869 
    870 		if ($filter_card_type) {
    871 			switch ($filter_card_type) {
    872 				case 'Visa':
    873 					$card_type = Braintree_CreditCard::VISA;
    874 					break;
    875 				case 'MasterCard':
    876 					$card_type = Braintree_CreditCard::MASTER_CARD;
    877 					break;
    878 				case 'American Express':
    879 					$card_type = Braintree_CreditCard::AMEX;
    880 					break;
    881 				case 'Discover':
    882 					$card_type = Braintree_CreditCard::DISCOVER;
    883 					break;
    884 				case 'JCB':
    885 					$card_type = Braintree_CreditCard::JCB;
    886 					break;
    887 				case 'Maestro':
    888 					$card_type = Braintree_CreditCard::MAESTRO;
    889 					break;
    890 			}
    891 
    892 			$search[] = Braintree_TransactionSearch::creditCardCardType()->is($card_type);
    893 		}
    894 
    895 		if ($filter_merchant_account_id) {
    896 			$search[] = Braintree_TransactionSearch::merchantAccountId()->is($filter_merchant_account_id);
    897 		}
    898 
    899 		if ($filter_transaction_status) {
    900 			$search[] = Braintree_TransactionSearch::status()->in($filter_transaction_status);
    901 		}
    902 
    903 		if ($filter_date_from || $filter_date_to) {
    904 			if ($filter_date_from) {
    905 				$date_from = new DateTime($filter_date_from);
    906 			} else {
    907 				$date_from = new DateTime('2012-01-01 00:00');
    908 			}
    909 
    910 			if ($filter_date_to) {
    911 				$date_to = new DateTime($filter_date_to . ' +1 day -1 minute');
    912 			} else {
    913 				$date_to = new DateTime('tomorrow -1 minute');
    914 			}
    915 
    916 			$search[] = Braintree_TransactionSearch::createdAt()->between($date_from, $date_to);
    917 		}
    918 
    919 		if ($filter_amount_from) {
    920 			$amount_from = $filter_amount_from;
    921 		} else {
    922 			$amount_from = 0;
    923 		}
    924 
    925 		if ($filter_amount_to) {
    926 			$amount_to = $filter_amount_to;
    927 		} else {
    928 			$amount_to = 9999999;
    929 		}
    930 
    931 		$search[] = Braintree_TransactionSearch::amount()->between((float)$amount_from, (float)$amount_to);
    932 
    933 		$transactions = $this->model_extension_payment_pp_braintree->getTransactions($this->gateway, $search);
    934 
    935 		if ($transactions) {
    936 			foreach ($transactions as $transaction) {
    937 				$customer_url = false;
    938 
    939 				if ($transaction->customer['id']) {
    940 					$braintree_customer_id = explode('_', $transaction->customer['id']);
    941 
    942 					if (isset($braintree_customer_id[2]) && is_numeric($braintree_customer_id[2])) {
    943 						$customer_info = $this->model_customer_customer->getCustomer($braintree_customer_id[2]);
    944 
    945 						if ($customer_info && $customer_info['email'] == $transaction->customer['email']) {
    946 							$customer_url = $this->url->link('sale/customer/edit', 'user_token=' . $this->session->data['user_token'] . '&customer_id=' . (int)$braintree_customer_id[2], true);
    947 						}
    948 					}
    949 				}
    950 
    951 				$order = false;
    952 
    953 				if ($transaction->orderId) {
    954 					$order_info = $this->model_sale_order->getOrder($transaction->orderId);
    955 
    956 					if ($order_info && $order_info['email'] == $transaction->customer['email']) {
    957 						$order = $this->url->link('sale/order/info', 'user_token=' . $this->session->data['user_token'] . '&order_id=' . (int)$transaction->orderId, true);
    958 					}
    959 				}
    960 
    961 				$created_at = $transaction->createdAt;
    962 
    963 				$json['transactions'][] = array(
    964 					'transaction_id'	  => $transaction->id,
    965 					'amount'			  => $transaction->amount,
    966 					'currency_iso'		  => $transaction->currencyIsoCode,
    967 					'status'			  => $transaction->status,
    968 					'type'				  => $transaction->type,
    969 					'merchant_account_id' => $transaction->merchantAccountId,
    970 					'customer'            => $transaction->customer['firstName'] . ' ' . $transaction->customer['lastName'],
    971 					'customer_url'		  => $customer_url,
    972 					'order'				  => $order,
    973 					'date_added'		  => date($this->language->get('datetime_format'), strtotime($created_at->format('Y-m-d H:i:s e')))
    974 				);
    975 			}
    976 		}
    977 
    978 		$this->response->addHeader('Content-Type: application/json');
    979 		$this->response->setOutput(json_encode($json));
    980 	}
    981 
    982 	public function connectRedirect() {
    983 		if ($this->user->hasPermission('modify', 'extension/extension/payment')) {
    984 			// Install the module before doing the redirect
    985 			$this->load->model('setting/extension');
    986 
    987 			$this->model_setting_extension->install('payment', 'pp_braintree');
    988 
    989 			$this->install();
    990 
    991 			$curl = curl_init($this->opencart_connect_url);
    992 
    993 			$this->load->model('localisation/country');
    994 			$country = $this->model_localisation_country->getCountry($this->config->get('config_country_id'));
    995 
    996 			$post_data = array(
    997 				'return_url' => $this->url->link('extension/payment/pp_braintree', 'user_token=' . $this->session->data['user_token'], true),
    998 				'store_url' => HTTPS_CATALOG,
    999 				'store_version' => VERSION,
   1000 				'store_country' => (isset($country['iso_code_3']) ? $country['iso_code_3'] : ''),
   1001 			);
   1002 
   1003 			curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
   1004 			curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
   1005 			curl_setopt($curl, CURLOPT_FORBID_REUSE, 1);
   1006 			curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);
   1007 			curl_setopt($curl, CURLOPT_POST, true);
   1008 			curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
   1009 
   1010 			$curl_response = curl_exec($curl);
   1011 
   1012 			$curl_response = json_decode($curl_response, true);
   1013 
   1014 			curl_close($curl);
   1015 
   1016 			if ($curl_response['url']) {
   1017 				$this->response->redirect($curl_response['url']);
   1018 			} else {
   1019 				$this->response->redirect($this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'], true));
   1020 			}
   1021 		} else {
   1022 			$this->response->redirect($this->url->link('error/permission', 'user_token=' . $this->session->data['user_token'], true));
   1023 		}
   1024 	}
   1025 
   1026 	public function preferredSolution() {
   1027 		$this->load->language('extension/payment/pp_braintree');
   1028 
   1029 		$data['connect_link'] = '';
   1030 		$data['module_link'] = '';
   1031 
   1032 		if ($this->config->get('payment_pp_braintree_status') || $this->config->get('payment_pp_braintree_merchant_id') || $this->config->get('payment_pp_braintree_access_token')) {
   1033 			$data['module_link'] = $this->url->link('extension/payment/pp_braintree', 'user_token=' . $this->session->data['user_token'], true);
   1034 		} else {
   1035 			if ($this->user->hasPermission('modify', 'extension/extension/payment')) {
   1036 				$data['connect_link'] = $this->url->link('extension/payment/pp_braintree/connectRedirect', 'user_token=' . $this->session->data['user_token'], true);
   1037 			}
   1038 		}
   1039 
   1040 		if ($this->config->get("payment_pp_braintree_status") == 1) {
   1041 			$data['payment_pp_braintree_status'] = "enabled";
   1042 		} elseif ($this->config->get("payment_pp_braintree_status") == null) {
   1043 			$data['payment_pp_braintree_status'] = "";
   1044 		} else {
   1045 			$data['payment_pp_braintree_status'] = "disabled";
   1046 		}
   1047 
   1048 		return $this->load->view('extension/payment/pp_braintree_preferred', $data);
   1049 	}
   1050 
   1051 	protected function validate() {
   1052 		$this->load->model('extension/payment/pp_braintree');
   1053 
   1054 		$check_credentials = true;
   1055 
   1056 		if (version_compare(phpversion(), '5.4.0', '<')) {
   1057 			$this->error['warning'] = $this->language->get('error_php_version');
   1058 		}
   1059 
   1060 		if (!$this->user->hasPermission('modify', 'extension/payment/pp_braintree')) {
   1061 			$this->error['warning'] = $this->language->get('error_permission');
   1062 
   1063 			$check_credentials = false;
   1064 		}
   1065 
   1066 		if ($check_credentials && $this->request->post['payment_pp_braintree_status'] == 1) {
   1067 			$this->initialise($this->request->post['payment_pp_braintree_access_token'], array(
   1068 				'payment_pp_braintree_environment' => $this->request->post['payment_pp_braintree_environment'],
   1069 				'payment_pp_braintree_merchant_id' => $this->request->post['payment_pp_braintree_merchant_id'],
   1070 				'payment_pp_braintree_public_key'	=> $this->request->post['payment_pp_braintree_public_key'],
   1071 				'payment_pp_braintree_private_key' => $this->request->post['payment_pp_braintree_private_key'],
   1072 			));
   1073 
   1074 			$verify_credentials = $this->model_extension_payment_pp_braintree->verifyCredentials($this->gateway);
   1075 
   1076 			if (!$verify_credentials) {
   1077 				$this->error['warning'] = $this->language->get('error_connection');
   1078 			} else {
   1079 				foreach ($this->request->post['payment_pp_braintree_account'] as $currency => $pp_braintree_account) {
   1080 					if (!empty($pp_braintree_account['merchant_account_id'])) {
   1081 						$verify_merchant_account_id = $this->model_extension_payment_pp_braintree->verifyMerchantAccount($this->gateway, $pp_braintree_account['merchant_account_id']);
   1082 
   1083 						if (!$verify_merchant_account_id) {
   1084 							$this->error['account'][$currency] = $this->language->get('error_account');
   1085 						}
   1086 					}
   1087 				}
   1088 
   1089 				$merchant_config = json_decode(base64_decode($verify_credentials), true);
   1090 
   1091 				// verify the Braintree account is ready to accept 3DS transactions
   1092 				if (isset($merchant_config['threeDSecureEnabled']) && ($this->request->post['payment_pp_braintree_3ds_status'] == 1 && $merchant_config['threeDSecureEnabled'] != 1)) {
   1093 					$this->error['warning'] = $this->language->get('error_3ds_not_ready');
   1094 				}
   1095 
   1096 				// verify the Braintree account is ready to use PayPal Billing Agreements
   1097 				if (isset($merchant_config['paypal']['billingAgreementEnabled']) && ($this->request->post['payment_pp_braintree_billing_agreement'] == 1 && $merchant_config['paypal']['billingAgreementEnabled'] != 1)) {
   1098 					$this->error['warning'] = $this->language->get('error_paypal_billing_not_ready');
   1099 				}
   1100 
   1101 				// verify the Braintree account is ready to accept PayPal transactions
   1102 				if (isset($merchant_config['paypalEnabled']) && ($this->request->post['payment_pp_braintree_paypal_option'] == 1 && $merchant_config['paypalEnabled'] != 1)) {
   1103 					$this->error['warning'] = $this->language->get('error_paypal_not_ready');
   1104 				}
   1105 
   1106 				// verify the environment matches with the token the system is using
   1107 				if (isset($merchant_config['environment']) && ($this->request->post['payment_pp_braintree_environment'] != $merchant_config['environment'])) {
   1108 					$this->error['warning'] = sprintf($this->language->get('error_environment'), $this->request->post['payment_pp_braintree_environment'], $merchant_config['environment']);
   1109 				}
   1110 			}
   1111 		}
   1112 
   1113 		if ($this->error && !isset($this->error['warning'])) {
   1114 			$this->error['warning'] = $this->language->get('error_warning');
   1115 		}
   1116 
   1117 		return !$this->error;
   1118 	}
   1119 
   1120 	private function initialise($access_token = '', $credentials = array()) {
   1121 		$this->load->model('extension/payment/pp_braintree');
   1122 
   1123 		if ($access_token != '') {
   1124 			$this->gateway = $this->model_extension_payment_pp_braintree->setGateway($access_token);
   1125 		} else {
   1126 			Braintree_Configuration::environment(isset($credentials['payment_pp_braintree_environment']) ? $credentials['payment_pp_braintree_environment'] : '');
   1127 			Braintree_Configuration::merchantId(isset($credentials['payment_pp_braintree_merchant_id']) ? $credentials['payment_pp_braintree_merchant_id'] : '');
   1128 			Braintree_Configuration::publicKey(isset($credentials['payment_pp_braintree_public_key']) ? $credentials['payment_pp_braintree_public_key'] : '');
   1129 			Braintree_Configuration::privateKey(isset($credentials['payment_pp_braintree_private_key']) ? $credentials['payment_pp_braintree_private_key'] : '');
   1130 		}
   1131 	}
   1132 }