PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/paypal/paypal_login/paypal_login.php

https://github.com/papich06/PrestaShop-modules
PHP | 247 lines | 169 code | 50 blank | 28 comment | 15 complexity | 6eccefc59a2d7aaad07c8b43b664a458 MD5 | raw file
Possible License(s): Apache-2.0, CC-BY-SA-3.0, Unlicense, BSD-3-Clause
  1. <?php
  2. /*
  3. * 2007-2014 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Academic Free License (AFL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/afl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2014 PrestaShop SA
  23. * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. class PayPalLogin
  27. {
  28. private $_logs = array();
  29. private $enable_log = false;
  30. private $paypal_connect = null;
  31. public function __construct()
  32. {
  33. $this->paypal_connect = new PayPalConnect();
  34. }
  35. public function getIdentityAPIURL()
  36. {
  37. if (Configuration::get('PAYPAL_SANDBOX'))
  38. //return 'www.sandbox.paypal.com';
  39. return 'api.sandbox.paypal.com';
  40. else
  41. return 'api.paypal.com';
  42. }
  43. public function getTokenServiceEndpoint()
  44. {
  45. if (Configuration::get('PAYPAL_SANDBOX'))
  46. // return '/webapps/auth/protocol/openidconnect/v1/tokenservice';
  47. return '/v1/identity/openidconnect/tokenservice';
  48. else
  49. return '/v1/identity/openidconnect/tokenservice';
  50. }
  51. public function getUserInfoEndpoint()
  52. {
  53. return '/v1/identity/openidconnect/userinfo';
  54. }
  55. public static function getReturnLink()
  56. {
  57. // return 'http://requestb.in/1jlaizq1';
  58. if (method_exists(Context::getContext()->shop, 'getBaseUrl'))
  59. return Context::getContext()->shop->getBaseUrl().'modules/paypal/paypal_login/paypal_login_token.php';
  60. else
  61. return 'http://'.Configuration::get('PS_SHOP_DOMAIN').'/modules/paypal/paypal_login/paypal_login_token.php';
  62. }
  63. public function getAuthorizationCode()
  64. {
  65. unset($this->_logs);
  66. if (Context::getContext()->cookie->isLogged())
  67. return $this->getRefreshToken();
  68. $params = array(
  69. 'grant_type' => 'authorization_code',
  70. 'code' => Tools::getValue('code'),
  71. 'redirect_url' => PayPalLogin::getReturnLink()
  72. );
  73. $request = http_build_query($params, '', '&');
  74. $result = $this->paypal_connect->makeConnection($this->getIdentityAPIURL(), $this->getTokenServiceEndpoint(), $request, false, false, true);
  75. if ($this->enable_log === true)
  76. {
  77. $handle = fopen(dirname(__FILE__).'/Results.txt', 'a+');
  78. fwrite($handle, "Request => ".print_r($request, true)."\r\n");
  79. fwrite($handle, "Result => ".print_r($result, true)."\r\n");
  80. fwrite($handle, "Journal => ".print_r($this->_logs, true."\r\n"));
  81. fclose($handle);
  82. }
  83. $result = Tools::jsonDecode($result);
  84. if ($result)
  85. {
  86. $login = new PayPalLoginUser();
  87. $customer = $this->getUserInformations($result->access_token, $login);
  88. if (!$customer)
  89. return false;
  90. $temp = PaypalLoginUser::getByIdCustomer((int)Context::getContext()->customer->id);
  91. if ($temp)
  92. $login = $temp;
  93. $login->id_customer = $customer->id;
  94. $login->token_type = $result->token_type;
  95. $login->expires_in = (string)(time() + (int)$result->expires_in);
  96. $login->refresh_token = $result->refresh_token;
  97. $login->id_token = $result->id_token;
  98. $login->access_token = $result->access_token;
  99. $login->save();
  100. return $login;
  101. }
  102. }
  103. public function getRefreshToken()
  104. {
  105. unset($this->_logs);
  106. $login = PaypalLoginUser::getByIdCustomer((int)Context::getContext()->customer->id);
  107. if (!is_object($login))
  108. return false;
  109. $params = array(
  110. 'grant_type' => 'refresh_token',
  111. 'refresh_token' => $login->refresh_token
  112. );
  113. $request = http_build_query($params, '', '&');
  114. $result = $this->paypal_connect->makeConnection($this->getIdentityAPIURL(), $this->getTokenServiceEndpoint(), $request, false, false, true);
  115. if ($this->enable_log === true)
  116. {
  117. $handle = fopen(dirname(__FILE__).'/Results.txt', 'a+');
  118. fwrite($handle, "Request => ".print_r($request, true) . "\r\n");
  119. fwrite($handle, "Result => ".print_r($result, true) . "\r\n");
  120. fwrite($handle, "Journal => ".print_r($this->_logs, true."\r\n"));
  121. fclose($handle);
  122. }
  123. $result = Tools::jsonDecode($result);
  124. if ($result)
  125. {
  126. $login->access_token = $result->access_token;
  127. $login->expires_in = (string)(time() + $result->expires_in);
  128. $login->save();
  129. return $login;
  130. }
  131. return false;
  132. }
  133. private function getUserInformations($access_token, &$login)
  134. {
  135. unset($this->_logs);
  136. $headers = array(
  137. // 'Content-Type:application/json',
  138. 'Authorization: Bearer '.$access_token
  139. );
  140. $params = array(
  141. 'schema' => 'openid'
  142. );
  143. $request = http_build_query($params, '', '&');
  144. $result = $this->paypal_connect->makeConnection($this->getIdentityAPIURL(), $this->getUserInfoEndpoint(), $request, false, $headers, true);
  145. if ($this->enable_log === true)
  146. {
  147. $handle = fopen(dirname(__FILE__).'/Results.txt', 'a+');
  148. fwrite($handle, "Request => ".print_r($request, true) . "\r\n");
  149. fwrite($handle, "Result => ".print_r($result, true) . "\r\n");
  150. fwrite($handle, "Headers => ".print_r($headers, true) . "\r\n");
  151. fwrite($handle, "Journal => ".print_r($this->_logs, true."\r\n"));
  152. fclose($handle);
  153. }
  154. $result = Tools::jsonDecode($result);
  155. if ($result)
  156. {
  157. $customer = new Customer();
  158. $customer = $customer->getByEmail($result->email);
  159. if (!$customer)
  160. {
  161. $customer = $this->setCustomer($result);
  162. }
  163. $login->account_type = $result->account_type;
  164. $login->user_id = $result->user_id;
  165. $login->verified_account = $result->verified_account;
  166. $login->zoneinfo = $result->zoneinfo;
  167. $login->age_range = $result->age_range;
  168. return $customer;
  169. }
  170. return false;
  171. }
  172. private function setCustomer($result)
  173. {
  174. $customer = new Customer();
  175. $customer->firstname = $result->given_name;
  176. $customer->lastname = $result->family_name;
  177. if (version_compare(_PS_VERSION_, '1.5.0', '>'))
  178. $customer->id_lang = Language::getIdByIso(strstr($result->language, '_', true));
  179. $customer->birthday = $result->birthday;
  180. $customer->email = $result->email;
  181. $customer->passwd = Tools::encrypt(Tools::passwdGen());
  182. $customer->save();
  183. $result_address = $result->address;
  184. $address = new Address();
  185. $address->id_customer = $customer->id;
  186. $address->id_country = Country::getByIso($result_address->country);
  187. $address->alias = 'My address';
  188. $address->lastname = $customer->lastname;
  189. $address->firstname = $customer->firstname;
  190. $address->address1 = $result_address->street_address;
  191. $address->postcode = $result_address->postal_code;
  192. $address->city = $result_address->locality;
  193. $address->phone = $result->phone_number;
  194. $address->save();
  195. return $customer;
  196. }
  197. }