public static function formatError($errorMessage)
 {
     if (preg_match(Connector::ERRORS_TAG, $errorMessage) > 0) {
         $errorMessage = MasterPassHelper::formatXML($errorMessage);
     }
     return $errorMessage;
 }
 public function getCheckoutData()
 {
     $this->appData->checkoutData = $this->service->GetPaymentShippingResource($this->appData->checkoutResourceUrl, $this->appData->accessToken);
     $checkoutObject = MasterPassHelper::formatResource($this->appData->checkoutData);
     $this->appData->transactionId = (string) $checkoutObject->TransactionId;
     return $this->appData;
 }
 public function index()
 {
     if ($this->request->server['HTTPS']) {
         $server = $this->config->get('config_ssl');
     } else {
         $server = $this->config->get('config_url');
     }
     require_once DIR_SYSTEM . 'library/masterpass/WalletWebContent/Controller/MasterPassController.php';
     require_once DIR_SYSTEM . 'library/masterpass/WalletWebContent/Controller/MasterPassHelper.php';
     $sad = unserialize($this->session->data['sad']);
     $controller = new MasterPassController($sad);
     $sad = $controller->setCallbackParameters($this->request->get);
     $connect = false;
     if (isset($this->request->get['connect'])) {
         $connect = $this->request->get['connect'] === "true";
     }
     if (isset($this->request->get[MasterPassService::PAIRING_TOKEN]) && isset($this->request->get[MasterPassService::PAIRING_VERIFIER])) {
         $pairing = true;
         $sad = $controller->setPairingToken($this->request->get[MasterPassService::PAIRING_TOKEN]);
         $sad = $controller->setPairingVerifier($this->request->get[MasterPassService::PAIRING_VERIFIER]);
         $sad = $controller->getLongAccessToken();
         if (!empty($sad->longAccessToken)) {
             setcookie('longAccessToken', $sad->longAccessToken, time() + 60 * 60 * 24 * 7);
         }
     }
     // GetaccessToken
     try {
         $sad = $controller->getAccessToken();
     } catch (Exception $e) {
         $errorMessage = MasterPassHelper::formatError($e->getMessage());
     }
     // ProccessCheckout
     $checkoutObject = "";
     try {
         $sad = $controller->getCheckoutData();
         $checkoutObject = MasterPassHelper::formatResource($sad->checkoutData);
     } catch (Exception $e) {
         $errorMessage = MasterPassHelper::formatError($e->getMessage());
     }
     $this->load->model('account/customer');
     //Verifica se o email ja possui cadastro
     $customer_email = $this->model_account_customer->getTotalCustomersByEmail($checkoutObject->Contact->EmailAddress);
     //Verifica se o CPF ja possui cadastro
     $customer_cpf = $this->model_account_customer->getTotalCustomersByCpfCnjpj($checkoutObject->Contact->NationalID);
     $cpf_detected = false;
     if ($customer_email) {
         $this->model_account_customer->updateAddressMasterPass($checkoutObject, $checkoutObject->Contact->EmailAddress);
     } else {
         if ($customer_cpf['total']) {
             $cpf_detected = $customer_cpf['email'];
             $this->model_account_customer->updateAddressMasterPass($checkoutObject, $customer_cpf['email']);
         } else {
             if (!empty($checkoutObject->Contact->EmailAddress)) {
                 $password = substr(sha1(date('dmYHis')), 0, 8);
                 $this->model_account_customer->addCustomerMasterPass($checkoutObject, $password);
                 //EMAIL
                 $template = new Template();
                 $template->data['url_store'] = $this->config->get('config_url');
                 $template->data['name_store'] = $this->config->get('config_name');
                 $template->data['logo_store'] = $template->data['url_store'] . 'image/' . $this->config->get('config_logo');
                 $template->data['url_password'] = $server . URL_PASSWORD;
                 $template->data['password'] = $password;
                 $this->load->model('tool/image');
                 if ($this->config->get('config_mail_header') && is_file(DIR_IMAGE . $this->config->get('config_mail_header'))) {
                     $template->data['config_mail_header'] = $this->model_tool_image->resize($this->config->get('config_mail_header'), 600, 120);
                 } else {
                     $template->data['config_mail_header'] = $this->model_tool_image->resize('no_image.png', 600, 120);
                 }
                 if ($this->config->get('config_mail_footer') && is_file(DIR_IMAGE . $this->config->get('config_mail_footer'))) {
                     $template->data['config_mail_footer'] = $this->model_tool_image->resize($this->config->get('config_mail_footer'), 600, 120);
                 } else {
                     $template->data['config_mail_footer'] = $this->model_tool_image->resize('no_image.png', 600, 120);
                 }
                 if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/mail/customer.tpl')) {
                     $html = $template->fetch($this->config->get('config_template') . '/template/mail/customer.tpl');
                 } else {
                     $html = $template->fetch('default/template/mail/customer.tpl');
                 }
                 $mail = new Mail($this->config->get('config_mail'));
                 $mail->setTo($checkoutObject->Contact->EmailAddress);
                 $mail->setFrom($this->config->get('config_email'));
                 $mail->setSender($this->config->get('config_name'));
                 $mail->setSubject("Seu cadastro foi concluido com sucesso!");
                 $mail->setHtml(html_entity_decode($html, ENT_QUOTES, 'UTF-8'));
                 $mail->send();
                 if ($this->config->get('config_account_mail')) {
                     $mail->setTo($this->config->get('config_email'));
                     $mail->send();
                     $emails = explode(',', $this->config->get('config_mail_alert'));
                     if ($emails) {
                         foreach ($emails as $email) {
                             if ($email && preg_match('/^[^\\@]+@.*.[a-z]{2,15}$/i', $email)) {
                                 $mail->setTo($email);
                                 $mail->send();
                             }
                         }
                     }
                 }
                 //EMAIL
             }
         }
     }
     $data['script_script'] = $this->load->controller('script/script');
     $data['sad'] = serialize($sad);
     $data['EmailCpfDetected'] = $cpf_detected;
     $data['CardHolderName'] = $checkoutObject->Card->CardHolderName;
     $data['AccountNumber'] = $checkoutObject->Card->AccountNumber;
     $data['ExpiryMonth'] = $checkoutObject->Card->ExpiryMonth;
     $data['ExpiryYear'] = $checkoutObject->Card->ExpiryYear;
     $data['BrandName'] = $checkoutObject->Card->BrandName;
     $data['EmailAddress'] = $checkoutObject->Contact->EmailAddress;
     $data['TransactionId'] = $checkoutObject->TransactionId;
     if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/wallet/masterpass_callback.tpl')) {
         $this->response->setOutput($this->load->view($this->config->get('config_template') . '/template/wallet/masterpass_callback.tpl', $data));
     } else {
         $this->response->setOutput($this->load->view('default/template/wallet/masterpass_callback.tpl', $data));
     }
 }