Permalink
Browse files

Introduce Log In with PayPal

  • Loading branch information...
1 parent a7d50b9 commit a841bdb710796c89e266c66ce1303fee84aaec31 @haraldpdl committed Oct 21, 2013
View
207 catalog/ext/modules/payment/paypal/login.php
@@ -0,0 +1,207 @@
+<?php
+/*
+ $Id$
+
+ osCommerce, Open Source E-Commerce Solutions
+ http://www.oscommerce.com
+
+ Copyright (c) 2013 osCommerce
+
+ Released under the GNU General Public License
+*/
+
+ chdir('../../../../');
+ require('includes/application_top.php');
+
+ require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_CREATE_ACCOUNT);
+
+ if ( file_exists(DIR_WS_LANGUAGES . $language . '/modules/content/cm_login_paypal_login.php') ) {
+ include(DIR_WS_LANGUAGES . $language . '/modules/content/cm_login_paypal_login.php');
+ }
+
+ include(DIR_WS_MODULES . 'content/cm_login_paypal_login.php');
+
+ $cm_paypal_login = new cm_login_paypal_login();
+
+ if ( $cm_paypal_login->isEnabled() ) {
+ if ( isset($HTTP_GET_VARS['code']) ) {
+ $params = array('code' => $HTTP_GET_VARS['code']);
+
+ $response_token = $cm_paypal_login->getToken($params);
+
+ if ( !isset($response_token['access_token']) && isset($response_token['refresh_token']) ) {
+ $params = array('refresh_token' => $response_token['refresh_token']);
+
+ $response_token = $cm_paypal_login->getRefreshToken($params);
+ }
+
+ if ( isset($response_token['access_token']) ) {
+ $params = array('access_token' => $response_token['access_token']);
+
+ $response = $cm_paypal_login->getUserInfo($params);
+
+ if ( isset($response['email']) ) {
+ $paypal_login_access_token = $response_token['access_token'];
+ tep_session_register('paypal_login_access_token');
+
+ $force_login = false;
+
+// check if e-mail address exists in database and login or create customer account
+ if (!tep_session_is_registered('customer_id')) {
+ $customer_id = 0;
+ $customer_default_address_id = 0;
+
+ $force_login = true;
+
+ $email_address = tep_db_prepare_input($response['email']);
+
+ $check_query = tep_db_query("select * from " . TABLE_CUSTOMERS . " where customers_email_address = '" . tep_db_input($email_address) . "' limit 1");
+ if (tep_db_num_rows($check_query)) {
+ $check = tep_db_fetch_array($check_query);
+
+ $customer_id = $check['customers_id'];
+ $customers_firstname = $check['customers_firstname'];
+ $customer_default_address_id = $check['customers_default_address_id'];
+ } else {
+ $customers_firstname = tep_db_prepare_input($response['given_name']);
+ $customers_lastname = tep_db_prepare_input($response['family_name']);
+
+ $customer_password = tep_create_random_value(max(ENTRY_PASSWORD_MIN_LENGTH, 8));
+
+ $sql_data_array = array('customers_firstname' => $customers_firstname,
+ 'customers_lastname' => $customers_lastname,
+ 'customers_email_address' => $email_address,
+ 'customers_telephone' => '',
+ 'customers_fax' => '',
+ 'customers_newsletter' => '0',
+ 'customers_password' => tep_encrypt_password($customer_password));
+
+ if (isset($response['phone_number']) && tep_not_null($response['phone_number'])) {
+ $customers_telephone = tep_db_prepare_input($response['phone_number']);
+
+ $sql_data_array['customers_telephone'] = $customers_telephone;
+ }
+
+ tep_db_perform(TABLE_CUSTOMERS, $sql_data_array);
+
+ $customer_id = tep_db_insert_id();
+
+ tep_db_query("insert into " . TABLE_CUSTOMERS_INFO . " (customers_info_id, customers_info_number_of_logons, customers_info_date_account_created) values ('" . (int)$customer_id . "', '0', now())");
+
+// build the message content
+ $name = $customers_firstname . ' ' . $customers_lastname;
+ $email_text = sprintf(EMAIL_GREET_NONE, $customers_firstname) . EMAIL_WELCOME . sprintf(MODULE_CONTENT_PAYPAL_LOGIN_EMAIL_PASSWORD, $email_address, $customer_password) . EMAIL_TEXT . EMAIL_CONTACT . EMAIL_WARNING;
+ tep_mail($name, $email_address, EMAIL_SUBJECT, $email_text, STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS);
+ }
+
+ if (SESSION_RECREATE == 'True') {
+ tep_session_recreate();
+ }
+
+ $customer_first_name = $customers_firstname;
+ tep_session_register('customer_id');
+ tep_session_register('customer_first_name');
+
+// reset session token
+ $sessiontoken = md5(tep_rand() . tep_rand() . tep_rand() . tep_rand());
+ }
+
+// check if paypal shipping address exists in the address book
+ $ship_firstname = tep_db_prepare_input($response['given_name']);
+ $ship_lastname = tep_db_prepare_input($response['family_name']);
+ $ship_address = tep_db_prepare_input($response['address']['street_address']);
+ $ship_city = tep_db_prepare_input($response['address']['locality']);
+ $ship_zone = tep_db_prepare_input($response['address']['region']);
+ $ship_zone_id = 0;
+ $ship_postcode = tep_db_prepare_input($response['address']['postal_code']);
+ $ship_country = tep_db_prepare_input($response['address']['country']);
+ $ship_country_id = 0;
+ $ship_address_format_id = 1;
+
+ $country_query = tep_db_query("select countries_id, address_format_id from " . TABLE_COUNTRIES . " where countries_iso_code_2 = '" . tep_db_input($ship_country) . "' limit 1");
+ if (tep_db_num_rows($country_query)) {
+ $country = tep_db_fetch_array($country_query);
+
+ $ship_country_id = $country['countries_id'];
+ $ship_address_format_id = $country['address_format_id'];
+ }
+
+ if ($ship_country_id > 0) {
+ $zone_query = tep_db_query("select zone_id from " . TABLE_ZONES . " where zone_country_id = '" . (int)$ship_country_id . "' and (zone_name = '" . tep_db_input($ship_zone) . "' or zone_code = '" . tep_db_input($ship_zone) . "') limit 1");
+ if (tep_db_num_rows($zone_query)) {
+ $zone = tep_db_fetch_array($zone_query);
+
+ $ship_zone_id = $zone['zone_id'];
+ }
+ }
+
+ $check_query = tep_db_query("select address_book_id from " . TABLE_ADDRESS_BOOK . " where customers_id = '" . (int)$customer_id . "' and entry_firstname = '" . tep_db_input($ship_firstname) . "' and entry_lastname = '" . tep_db_input($ship_lastname) . "' and entry_street_address = '" . tep_db_input($ship_address) . "' and entry_postcode = '" . tep_db_input($ship_postcode) . "' and entry_city = '" . tep_db_input($ship_city) . "' and (entry_state = '" . tep_db_input($ship_zone) . "' or entry_zone_id = '" . (int)$ship_zone_id . "') and entry_country_id = '" . (int)$ship_country_id . "' limit 1");
+ if (tep_db_num_rows($check_query)) {
+ $check = tep_db_fetch_array($check_query);
+
+ $sendto = $check['address_book_id'];
+ } else {
+ $sql_data_array = array('customers_id' => $customer_id,
+ 'entry_firstname' => $ship_firstname,
+ 'entry_lastname' => $ship_lastname,
+ 'entry_street_address' => $ship_address,
+ 'entry_postcode' => $ship_postcode,
+ 'entry_city' => $ship_city,
+ 'entry_country_id' => $ship_country_id);
+
+ if (ACCOUNT_STATE == 'true') {
+ if ($ship_zone_id > 0) {
+ $sql_data_array['entry_zone_id'] = $ship_zone_id;
+ $sql_data_array['entry_state'] = '';
+ } else {
+ $sql_data_array['entry_zone_id'] = '0';
+ $sql_data_array['entry_state'] = $ship_zone;
+ }
+ }
+
+ tep_db_perform(TABLE_ADDRESS_BOOK, $sql_data_array);
+
+ $address_id = tep_db_insert_id();
+
+ $sendto = $address_id;
+
+ if ($customer_default_address_id < 1) {
+ tep_db_query("update " . TABLE_CUSTOMERS . " set customers_default_address_id = '" . (int)$address_id . "' where customers_id = '" . (int)$customer_id . "'");
+ $customer_default_address_id = $address_id;
+ }
+ }
+
+ if ($force_login == true) {
+ $customer_country_id = $ship_country_id;
+ $customer_zone_id = $ship_zone_id;
+ tep_session_register('customer_default_address_id');
+ tep_session_register('customer_country_id');
+ tep_session_register('customer_zone_id');
+
+ $billto = $sendto;
+
+// restore cart contents
+ $cart->restore_contents();
+ }
+ }
+ }
+ }
+ }
+
+ if (sizeof($navigation->snapshot) > 0) {
+ $redirect_url = tep_href_link($navigation->snapshot['page'], tep_array_to_string($navigation->snapshot['get'], array(tep_session_name())), $navigation->snapshot['mode']);
+ $navigation->clear_snapshot();
+ } else {
+ $redirect_url = tep_href_link(FILENAME_DEFAULT);
+ }
+?>
+
+<script>
+window.opener.location.href = "<?php echo $redirect_url; ?>";
+
+window.close();
+</script>
+
+<?php
+ require(DIR_WS_INCLUDES . 'application_bottom.php');
+?>
View
20 catalog/includes/languages/english/modules/content/cm_login_paypal_login.php
@@ -0,0 +1,20 @@
+<?php
+/*
+ $Id$
+
+ osCommerce, Open Source E-Commerce Solutions
+ http://www.oscommerce.com
+
+ Copyright (c) 2013 osCommerce
+
+ Released under the GNU General Public License
+*/
+
+ define('MODULE_CONTENT_PAYPAL_LOGIN_TITLE', 'PayPal Login');
+ define('MODULE_CONTENT_PAYPAL_LOGIN_DESCRIPTION', 'Enable Log In with PayPal with seamless checkout for PayPal Express Checkout payments');
+
+ define('MODULE_CONTENT_PAYPAL_LOGIN_TEMPLATE_TITLE', 'Log In with PayPal');
+ define('MODULE_CONTENT_PAYPAL_LOGIN_TEMPLATE_CONTENT', 'Have a PayPal account? Securely log in with PayPal to shop even faster!');
+
+ define('MODULE_CONTENT_PAYPAL_LOGIN_EMAIL_PASSWORD', 'An account has automatically been created for you with the following e-mail address and password:' . "\n\n" . 'Store Account E-Mail Address: %s' . "\n" . 'Store Account Password: %s' . "\n\n");
+?>
View
182 catalog/includes/modules/content/cm_login_paypal_login.php
@@ -0,0 +1,182 @@
+<?php
+/*
+ $Id$
+
+ osCommerce, Open Source E-Commerce Solutions
+ http://www.oscommerce.com
+
+ Copyright (c) 2013 osCommerce
+
+ Released under the GNU General Public License
+*/
+
+ class cm_login_paypal_login {
+ var $code = 'cm_login_paypal_login';
+ var $group = 'login';
+ var $title;
+ var $description;
+ var $sort_order;
+ var $enabled = false;
+
+ function cm_login_paypal_login() {
+ $this->title = MODULE_CONTENT_PAYPAL_LOGIN_TITLE . ' (' . $this->group . ')';
+ $this->description = MODULE_CONTENT_PAYPAL_LOGIN_DESCRIPTION;
+
+ if ( defined('MODULE_CONTENT_PAYPAL_LOGIN_STATUS') ) {
+ $this->sort_order = MODULE_CONTENT_PAYPAL_LOGIN_SORT_ORDER;
+ $this->enabled = (MODULE_CONTENT_PAYPAL_LOGIN_STATUS == 'True');
+ }
+ }
+
+ function execute() {
+ global $oscTemplate;
+
+ ob_start();
+ include(DIR_WS_MODULES . 'content/templates/login_paypal_login.php');
+ $template = ob_get_clean();
+
+ $oscTemplate->addContent($template, $this->group);
+ }
+
+ function isEnabled() {
+ return $this->enabled;
+ }
+
+ function check() {
+ return defined('MODULE_CONTENT_PAYPAL_LOGIN_STATUS');
+ }
+
+ function install() {
+ tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Enable PayPal Login Module', 'MODULE_CONTENT_PAYPAL_LOGIN_STATUS', 'True', 'Do you want to enable the PayPal Login module?', '6', '1', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
+ tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Client ID', 'MODULE_CONTENT_PAYPAL_LOGIN_CLIENT_ID', '', 'Your PayPal Application Client ID.', '6', '1', now())");
+ tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Secret', 'MODULE_CONTENT_PAYPAL_LOGIN_SECRET', '', 'Your PayPal Application Secret.', '6', '1', now())");
+ tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Server Type', 'MODULE_CONTENT_PAYPAL_LOGIN_SERVER_TYPE', 'Sandbox', 'Which server should be used? Live for production or Sandbox for testing.', '6', '1', 'tep_cfg_select_option(array(\'Live\', \'Sandbox\'), ', now())");
+ tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Verify SSL Certificate', 'MODULE_CONTENT_PAYPAL_LOGIN_VERIFY_SSL', 'True', 'Verify gateway server SSL certificate on connection?', '6', '1', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
+ tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Proxy Server', 'MODULE_CONTENT_PAYPAL_LOGIN_PROXY', '', 'Send API requests through this proxy server. (host:port, eg: 123.45.67.89:8080 or proxy.example.com:8080)', '6', '1', now())");
+ tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Content Width', 'MODULE_CONTENT_PAYPAL_LOGIN_CONTENT_WIDTH', 'Full', 'Should the content be shown in a full or half width container?', '6', '1', 'tep_cfg_select_option(array(\'Full\', \'Half\'), ', now())");
+ tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_CONTENT_PAYPAL_LOGIN_SORT_ORDER', '0', 'Sort order of display. Lowest is displayed first.', '6', '0', now())");
+ }
+
+ function remove() {
+ tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
+ }
+
+ function keys() {
+ return array('MODULE_CONTENT_PAYPAL_LOGIN_STATUS', 'MODULE_CONTENT_PAYPAL_LOGIN_CLIENT_ID', 'MODULE_CONTENT_PAYPAL_LOGIN_SECRET', 'MODULE_CONTENT_PAYPAL_LOGIN_SERVER_TYPE', 'MODULE_CONTENT_PAYPAL_LOGIN_VERIFY_SSL', 'MODULE_CONTENT_PAYPAL_LOGIN_PROXY', 'MODULE_CONTENT_PAYPAL_LOGIN_CONTENT_WIDTH', 'MODULE_CONTENT_PAYPAL_LOGIN_SORT_ORDER');
+ }
+
+ function sendRequest($url, $parameters = null) {
+ $server = parse_url($url);
+
+ if ( !isset($server['port']) ) {
+ $server['port'] = ($server['scheme'] == 'https') ? 443 : 80;
+ }
+
+ if ( !isset($server['path']) ) {
+ $server['path'] = '/';
+ }
+
+ $curl = curl_init($server['scheme'] . '://' . $server['host'] . $server['path'] . (isset($server['query']) ? '?' . $server['query'] : ''));
+ curl_setopt($curl, CURLOPT_PORT, $server['port']);
+ curl_setopt($curl, CURLOPT_HEADER, false);
+ curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
+ curl_setopt($curl, CURLOPT_FORBID_REUSE, true);
+ curl_setopt($curl, CURLOPT_FRESH_CONNECT, true);
+ curl_setopt($curl, CURLOPT_POST, true);
+ curl_setopt($curl, CURLOPT_POSTFIELDS, $parameters);
+ curl_setopt($curl, CURLOPT_ENCODING, '');
+
+ if ( MODULE_CONTENT_PAYPAL_LOGIN_VERIFY_SSL == 'True' ) {
+ curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
+ curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
+
+ if ( file_exists(DIR_FS_CATALOG . 'ext/modules/payment/paypal/paypal.com.crt') ) {
+ curl_setopt($curl, CURLOPT_CAINFO, DIR_FS_CATALOG . 'ext/modules/payment/paypal/paypal.com.crt');
+ } elseif ( file_exists(DIR_FS_CATALOG . 'includes/cacert.pem') ) {
+ curl_setopt($curl, CURLOPT_CAINFO, DIR_FS_CATALOG . 'includes/cacert.pem');
+ }
+ } else {
+ curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
+ }
+
+ if ( tep_not_null(MODULE_CONTENT_PAYPAL_LOGIN_PROXY) ) {
+ curl_setopt($curl, CURLOPT_HTTPPROXYTUNNEL, true);
+ curl_setopt($curl, CURLOPT_PROXY, MODULE_CONTENT_PAYPAL_LOGIN_PROXY);
+ }
+
+ $result = curl_exec($curl);
+
+ curl_close($curl);
+
+ return $result;
+ }
+
+ function getToken($params) {
+ if ( MODULE_CONTENT_PAYPAL_LOGIN_SERVER_TYPE == 'Live' ) {
+ $api_server = 'api.paypal.com';
+ } else {
+ $api_server = 'api.sandbox.paypal.com';
+ }
+
+ $parameters = array('client_id' => MODULE_CONTENT_PAYPAL_LOGIN_CLIENT_ID,
+ 'client_secret' => MODULE_CONTENT_PAYPAL_LOGIN_SECRET,
+ 'grant_type' => 'authorization_code',
+ 'code' => $params['code']);
+
+ $post_string = '';
+
+ foreach ($parameters as $key => $value) {
+ $post_string .= $key . '=' . urlencode(utf8_encode(trim($value))) . '&';
+ }
+
+ $post_string = substr($post_string, 0, -1);
+
+ $result = $this->sendRequest('https://' . $api_server . '/v1/identity/openidconnect/tokenservice', $post_string);
+
+ $result_array = json_decode($result, true);
+
+ return $result_array;
+ }
+
+ function getRefreshToken($params) {
+ if ( MODULE_CONTENT_PAYPAL_LOGIN_SERVER_TYPE == 'Live' ) {
+ $api_server = 'api.paypal.com';
+ } else {
+ $api_server = 'api.sandbox.paypal.com';
+ }
+
+ $parameters = array('client_id' => MODULE_CONTENT_PAYPAL_LOGIN_CLIENT_ID,
+ 'client_secret' => MODULE_CONTENT_PAYPAL_LOGIN_SECRET,
+ 'grant_type' => 'refresh_token',
+ 'refresh_token' => $params['refresh_token']);
+
+ $post_string = '';
+
+ foreach ($parameters as $key => $value) {
+ $post_string .= $key . '=' . urlencode(utf8_encode(trim($value))) . '&';
+ }
+
+ $post_string = substr($post_string, 0, -1);
+
+ $result = $this->sendRequest('https://' . $api_server . '/v1/identity/openidconnect/tokenservice', $post_string);
+
+ $result_array = json_decode($result, true);
+
+ return $result_array;
+ }
+
+ function getUserInfo($params) {
+ if ( MODULE_CONTENT_PAYPAL_LOGIN_SERVER_TYPE == 'Live' ) {
+ $api_server = 'api.paypal.com';
+ } else {
+ $api_server = 'api.sandbox.paypal.com';
+ }
+
+ $result = $this->sendRequest('https://' . $api_server . '/v1/identity/openidconnect/userinfo/?schema=openid&access_token=' . $params['access_token']);
+
+ $result_array = json_decode($result, true);
+
+ return $result_array;
+ }
+ }
+?>
View
28 catalog/includes/modules/content/templates/login_paypal_login.php
@@ -0,0 +1,28 @@
+<div class="contentContainer <?php echo (MODULE_CONTENT_PAYPAL_LOGIN_CONTENT_WIDTH == 'Half') ? 'grid_8' : 'grid_16'; ?>">
+ <h2><?php echo MODULE_CONTENT_PAYPAL_LOGIN_TEMPLATE_TITLE; ?></h2>
+
+ <div class="contentText">
+ <p><?php echo MODULE_CONTENT_PAYPAL_LOGIN_TEMPLATE_CONTENT; ?></p>
+
+ <div id="PayPalLoginButton" style="text-align: right; padding-top: 5px;"></div>
+ </div>
+</div>
+
+<script type="text/javascript" src="https://www.paypalobjects.com/js/external/api.js"></script>
+<script type="text/javascript">
+paypal.use( ["login"], function(login) {
+ login.render ({
+
+<?php
+ if ( MODULE_CONTENT_PAYPAL_LOGIN_SERVER_TYPE == 'Sandbox' ) {
+ echo ' "authend": "sandbox",';
+ }
+?>
+
+ "appid": "<?php echo MODULE_CONTENT_PAYPAL_LOGIN_CLIENT_ID; ?>",
+ "scopes": "profile email address phone https://uri.paypal.com/services/paypalattributes https://uri.paypal.com/services/expresscheckout",
+ "containerid": "PayPalLoginButton",
+ "returnurl": "<?php echo tep_href_link('ext/modules/payment/paypal/login.php', '', 'SSL'); ?>"
+ });
+});
+</script>

0 comments on commit a841bdb

Please sign in to comment.