public function getAuthorizationCode() { unset($this->_logs); $context = Context::getContext(); $is_logged = method_exists($context->customer, 'isLogged') ? $context->customer->isLogged() : $context->cookie->isLogged(); if ($is_logged) { return $this->getRefreshToken(); } $params = array('grant_type' => 'authorization_code', 'code' => Tools::getValue('code'), 'redirect_url' => PayPalLogin::getReturnLink()); $request = http_build_query($params, '', '&'); $result = $this->paypal_connect->makeConnection($this->getIdentityAPIURL(), $this->getTokenServiceEndpoint(), $request, false, false, true); if ($this->enable_log === true) { $handle = fopen(dirname(__FILE__) . '/Results.txt', 'a+'); fwrite($handle, "Request => " . print_r($request, true) . "\r\n"); fwrite($handle, "Result => " . print_r($result, true) . "\r\n"); fwrite($handle, "Journal => " . print_r($this->_logs, true . "\r\n")); fclose($handle); } $result = Tools::jsonDecode($result); if ($result) { $login = new PayPalLoginUser(); $customer = $this->getUserInformations($result->access_token, $login); if (!$customer) { return false; } $temp = PaypalLoginUser::getByIdCustomer((int) $context->customer->id); if ($temp) { $login = $temp; } $login->id_customer = $customer->id; $login->token_type = $result->token_type; $login->expires_in = (string) (time() + (int) $result->expires_in); $login->refresh_token = $result->refresh_token; $login->id_token = $result->id_token; $login->access_token = $result->access_token; $login->save(); return $login; } }
/** * Hooks methods */ public function hookHeader() { if ($this->useMobile()) { $id_hook = (int) Configuration::get('PS_MOBILE_HOOK_HEADER_ID'); if ($id_hook > 0) { $module = Hook::getModulesFromHook($id_hook, $this->id); if (!$module) { $this->registerHook('displayMobileHeader'); } } } if (isset($this->context->cart) && $this->context->cart->id) { $this->context->smarty->assign('id_cart', (int) $this->context->cart->id); } /* Added for PrestaBox */ if (method_exists($this->context->controller, 'addCSS')) { $this->context->controller->addCSS(_MODULE_DIR_ . $this->name . '/views/css/paypal.css'); } else { Tools::addCSS(_MODULE_DIR_ . $this->name . '/views/css/paypal.css'); } $smarty = $this->context->smarty; $smarty->assign(array('ssl_enabled' => Configuration::get('PS_SSL_ENABLED'), 'PAYPAL_SANDBOX' => Configuration::get('PAYPAL_SANDBOX'), 'PayPal_in_context_checkout' => Configuration::get('PAYPAL_IN_CONTEXT_CHECKOUT'), 'PayPal_in_context_checkout_merchant_id' => Configuration::get('PAYPAL_IN_CONTEXT_CHECKOUT_M_ID'))); $process = '<script type="text/javascript">' . $this->fetchTemplate('views/js/paypal.js') . '</script>'; if (Configuration::get('PAYPAL_IN_CONTEXT_CHECKOUT')) { $process .= '<script async src="//www.paypalobjects.com/api/checkout.js"></script>'; } if ((method_exists($smarty, 'getTemplateVars') && ($smarty->getTemplateVars('page_name') == 'authentication' || $smarty->getTemplateVars('page_name') == 'order-opc') || isset($smarty->_tpl_vars) && ($smarty->_tpl_vars['page_name'] == 'authentication' || $smarty->_tpl_vars['page_name'] == 'order-opc')) && (int) Configuration::get('PAYPAL_LOGIN') == 1) { $this->context->smarty->assign(array('paypal_locale' => $this->getLocale(), 'PAYPAL_LOGIN_CLIENT_ID' => Configuration::get('PAYPAL_LOGIN_CLIENT_ID'), 'PAYPAL_LOGIN_TPL' => Configuration::get('PAYPAL_LOGIN_TPL'), 'PAYPAL_RETURN_LINK' => PayPalLogin::getReturnLink())); $process .= ' <script src="https://www.paypalobjects.com/js/external/api.js"></script> <script>' . $this->fetchTemplate('views/js/paypal_login.js') . '</script>'; } return $process; }