pp_login.php (2413B)
1 <?php 2 class ModelExtensionModulePPLogin extends Model { 3 public function getTokens($code) { 4 if ($this->config->get('module_pp_login_sandbox')) { 5 $endpoint = 'https://api.sandbox.paypal.com/v1/identity/openidconnect/tokenservice'; 6 } else { 7 $endpoint = 'https://api.paypal.com/v1/identity/openidconnect/tokenservice'; 8 } 9 10 $request = ''; 11 $request .= 'grant_type=authorization_code'; 12 $request .= '&code=' . $code; 13 $request .= '&redirect_uri=' . urlencode($this->url->link('extension/module/pp_login/login', '', true)); 14 15 $additional_opts = array( 16 CURLOPT_USERPWD => $this->config->get('module_pp_login_client_id') . ':' . $this->config->get('module_pp_login_secret'), 17 CURLOPT_POST => true, 18 CURLOPT_POSTFIELDS => $request 19 ); 20 21 $curl = $this->curl($endpoint, $additional_opts); 22 23 $this->log('cURL Response: ' . print_r($curl, 1), 1); 24 25 return $curl; 26 } 27 28 public function getUserInfo($access_token) { 29 if ($this->config->get('module_pp_login_sandbox')) { 30 $endpoint = 'https://api.sandbox.paypal.com/v1/identity/openidconnect/userinfo/?schema=openid'; 31 } else { 32 $endpoint = 'https://api.paypal.com/v1/identity/openidconnect/userinfo/?schema=openid'; 33 } 34 35 $header = array(); 36 $header[] = 'Content-Type: application/json'; 37 $header[] = 'Authorization: Bearer ' . $access_token; 38 39 $additional_opts = array( 40 CURLOPT_HTTPHEADER => $header, 41 ); 42 43 $curl = $this->curl($endpoint, $additional_opts); 44 45 $this->log('cURL Response: ' . print_r($curl, 1), 1); 46 47 return $curl; 48 } 49 50 private function curl($endpoint, $additional_opts = array()) { 51 $default_opts = array( 52 CURLOPT_PORT => 443, 53 CURLOPT_HEADER => 0, 54 CURLOPT_SSL_VERIFYPEER => 0, 55 CURLOPT_RETURNTRANSFER => 1, 56 CURLOPT_FORBID_REUSE => 1, 57 CURLOPT_FRESH_CONNECT => 1, 58 CURLOPT_URL => $endpoint, 59 ); 60 61 $ch = curl_init($endpoint); 62 63 $opts = $default_opts + $additional_opts; 64 65 curl_setopt_array($ch, $opts); 66 67 $response = json_decode(curl_exec($ch)); 68 69 curl_close($ch); 70 71 return $response; 72 } 73 74 public function log($data, $class_step = 6) { 75 if ($this->config->get('module_pp_login_debug')) { 76 $backtrace = debug_backtrace(); 77 $this->log->write('Log In with PayPal debug (' . $backtrace[$class_step]['class'] . '::' . $backtrace[6]['function'] . ') - ' . $data); 78 } 79 } 80 }