diff --git a/shipping/uc_ups/config/uc_ups.settings.json b/shipping/uc_ups/config/uc_ups.settings.json index ea9d138b..ded93f60 100644 --- a/shipping/uc_ups/config/uc_ups.settings.json +++ b/shipping/uc_ups/config/uc_ups.settings.json @@ -1,22 +1,23 @@ { "_config_name": "uc_ups.settings", - "uc_ups_access_license": "", - "uc_ups_shipper_number": "", - "uc_ups_user_id": "", - "uc_ups_password": "", - "uc_ups_connection_address": "https://wwwcie.ups.com/ups.app/xml/", - "uc_ups_all_in_one": true, - "uc_ups_pkg_type": "02", - "uc_ups_classification": "04", + "uc_ups_client_id": "", + "uc_ups_client_secret": "", + "uc_ups_connection_address": "https://wwwcie.ups.com/", + "uc_ups_services": [], "uc_ups_negotiated_rates": false, - "uc_ups_pickup_type": "01", - "uc_ups_residential_quotes": false, - "uc_ups_insurance": true, "uc_ups_rate_markup_type": "percentage", - "uc_ups_rate_markup": 0, - "uc_ups_weight_markup_type": "percentage", - "uc_ups_weight_markup": 0, - "uc_ups_label_lifetime": "", + "uc_ups_rate_markup": "0", "uc_ups_markup": "", - "uc_ups_markup_type": "" -} \ No newline at end of file + "uc_ups_markup_type": "", + "uc_ups_label_lifetime": "0", + "uc_ups_pkg_type": "02", + "uc_ups_shipper_number": "", + "uc_ups_secret": "", + "uc_ups_insurance": true, + "uc_ups_pickup_type": "01", + "uc_ups_classification": "04", + "uc_ups_residential_quotes": false, + "uc_ups_all_in_one": true, + "uc_ups_weight_markup": "0", + "uc_ups_weight_markup_type": "percentage" +} diff --git a/shipping/uc_ups/uc_ups.admin.inc b/shipping/uc_ups/uc_ups.admin.inc index d776a008..3fd04adb 100644 --- a/shipping/uc_ups/uc_ups.admin.inc +++ b/shipping/uc_ups/uc_ups.admin.inc @@ -1,138 +1,95 @@ 'vertical_tabs', - '#attached' => array( - 'js' => array( - 'vertical-tabs' => backdrop_get_path('module', 'uc_ups') . '/uc_ups.admin.js', - ), - ), ); - // Container for credential forms + // OAuth Credentials $form['uc_ups_credentials'] = array( '#type' => 'fieldset', - '#title' => t('Credentials'), - '#description' => t('Account number and authorization information.'), - '#collapsible' => TRUE, - '#collapsed' => TRUE, + '#title' => t('OAuth Credentials'), + '#description' => t('Enter UPS OAuth credentials.'), '#group' => 'ups-settings', ); - $form['uc_ups_credentials']['uc_ups_access_license'] = array( - '#type' => 'textfield', - '#title' => t('UPS OnLine Tools XML Access Key'), - '#default_value' => $config->get('uc_ups_access_license'), - '#required' => TRUE, - ); $form['uc_ups_credentials']['uc_ups_shipper_number'] = array( '#type' => 'textfield', - '#title' => t('UPS Shipper #'), - '#description' => t('The 6-character string identifying your UPS account as a shipper.'), + '#title' => t('UPS Shipper Number'), '#default_value' => $config->get('uc_ups_shipper_number'), '#required' => TRUE, ); - $form['uc_ups_credentials']['uc_ups_user_id'] = array( + + $form['uc_ups_credentials']['uc_ups_client_id'] = array( '#type' => 'textfield', - '#title' => t('UPS.com user ID'), - '#default_value' => $config->get('uc_ups_user_id'), + '#title' => t('UPS Client ID'), + '#default_value' => $config->get('uc_ups_client_id'), '#required' => TRUE, ); - $form['uc_ups_credentials']['uc_ups_password'] = array( + + $client_secret = $config->get('uc_ups_client_secret'); + $form['uc_ups_credentials']['uc_ups_client_secret'] = array( '#type' => 'password', - '#title' => t('Password'), - '#default_value' => $config->get('uc_ups_password'), + '#title' => t('UPS Client Secret'), + '#placeholder' => empty($client_secret) ? t('Enter the client secret') : t(''), + ); + + $form['uc_ups_credentials']['uc_ups_access_token'] = array( + '#type' => 'textfield', + '#title' => t('Access Token'), + '#default_value' => state_get('uc_ups_access_token'), + '#disabled' => TRUE, + '#description' => t('This token is automatically retrieved via OAuth authentication.'), ); + $form['uc_ups_credentials']['uc_ups_connection_address'] = array( '#type' => 'select', - '#title' => t('Server mode'), - '#description' => t('Use the Testing server while developing and configuring your site. Switch to the Production server only after you have demonstrated that transactions on the Testing server are working and you are ready to go live.'), + '#title' => t('Server Mode'), + '#description' => t('Select Testing or Production mode for UPS API connection.'), '#options' => array( - 'https://wwwcie.ups.com/ups.app/xml/' => t('Testing'), - 'https://onlinetools.ups.com/ups.app/xml/' => t('Production'), + 'https://wwwcie.ups.com/' => t('Testing'), + 'https://onlinetools.ups.com/' => t('Production'), ), '#default_value' => $config->get('uc_ups_connection_address'), ); + // Service Options $form['services'] = array( '#type' => 'fieldset', - '#title' => t('Service options'), + '#title' => t('Service Options'), '#description' => t('Set the conditions that will return a UPS quote.'), - '#collapsible' => TRUE, - '#collapsed' => TRUE, '#group' => 'ups-settings', ); $form['services']['uc_ups_services'] = array( '#type' => 'checkboxes', - '#title' => t('UPS services'), + '#title' => t('UPS Services'), '#default_value' => $config->get('uc_ups_services'), '#options' => _uc_ups_service_list(), '#description' => t('Select the UPS services that are available to customers.'), ); - // Container for quote options. + // Quote Options $form['uc_ups_quote_options'] = array( '#type' => 'fieldset', - '#title' => t('Quote options'), + '#title' => t('Quote Options'), '#description' => t('Preferences that affect computation of quote.'), - '#collapsible' => TRUE, - '#collapsed' => TRUE, '#group' => 'ups-settings', ); - $form['uc_ups_quote_options']['uc_ups_all_in_one'] = array( - '#type' => 'radios', - '#title' => t('Product packages'), - '#default_value' => $config->get('uc_ups_all_in_one'), - '#options' => array( - 0 => t('Each product in its own package'), - 1 => t('All products in one package'), - ), - '#description' => t('Indicate whether each product is quoted as shipping separately or all in one package. Orders with one kind of product will still use the package quantity to determine the number of packages needed, however.'), - ); - - // Form to select package types. - $form['uc_ups_quote_options']['uc_ups_pkg_type'] = array( - '#type' => 'select', - '#title' => t('Default Package Type'), - '#default_value' => $config->get('uc_ups_pkg_type'), - '#options' => _uc_ups_pkg_types(), - '#description' => t('Type of packaging to be used. May be overridden on a per-product basis via the product node edit form.'), - ); - $form['uc_ups_quote_options']['uc_ups_classification'] = array( - '#type' => 'select', - '#title' => t('UPS Customer classification'), - '#options' => array( - '01' => t('Wholesale'), - '03' => t('Occasional'), - '04' => t('Retail'), - ), - '#default_value' => $config->get('uc_ups_classification'), - '#description' => t('The kind of customer you are to UPS. For daily pickups the default is wholesale; for customer counter pickups the default is retail; for other pickups the default is occasional.'), - ); - $form['uc_ups_quote_options']['uc_ups_negotiated_rates'] = array( '#type' => 'radios', - '#title' => t('Negotiated rates'), + '#title' => t('Negotiated Rates'), '#default_value' => $config->get('uc_ups_negotiated_rates'), '#options' => array( 1 => t('Yes'), @@ -141,64 +98,17 @@ function uc_ups_admin_settings($form, &$form_state) { '#description' => t('Is your UPS account receiving negotiated rates on shipments?'), ); - // Form to select pickup type. - $form['uc_ups_quote_options']['uc_ups_pickup_type'] = array( - '#type' => 'select', - '#title' => t('Pickup type'), - '#options' => array( - '01' => 'Daily Pickup', - '03' => 'Customer Counter', - '06' => 'One Time Pickup', - '07' => 'On Call Air', - '11' => 'Suggested Retail Rates', - '19' => 'Letter Center', - '20' => 'Air Service Center', - ), - '#default_value' => $config->get('uc_ups_pickup_type'), - ); - - $form['uc_ups_quote_options']['uc_ups_residential_quotes'] = array( - '#type' => 'radios', - '#title' => t('Assume UPS shipping quotes will be delivered to'), - '#default_value' => $config->get('uc_ups_residential_quotes'), - '#options' => array( - 0 => t('Business locations'), - 1 => t('Residential locations (extra fees)'), - ), - ); - - $form['uc_ups_quote_options']['uc_ups_unit_system'] = array( - '#type' => 'select', - '#title' => t('System of measurement'), - '#default_value' => $config->get('uc_ups_unit_system'), - '#options' => array( - 'in' => t('British'), - 'cm' => t('Metric'), - ), - '#description' => t('Choose the standard system of measurement for your country.'), - ); - - $form['uc_ups_quote_options']['uc_ups_insurance'] = array( - '#type' => 'checkbox', - '#title' => t('Package insurance'), - '#default_value' => $config->get('uc_ups_insurance'), - '#description' => t('When enabled, the quotes presented to the customer will include the cost of insurance for the full sales price of all products in the order.'), - ); - - // Container for markup forms. + // Markups $form['uc_ups_markups'] = array( '#type' => 'fieldset', '#title' => t('Markups'), '#description' => t('Modifiers to the shipping weight and quoted rate.'), - '#collapsible' => TRUE, - '#collapsed' => TRUE, '#group' => 'ups-settings', ); - // Form to select type of rate markup. $form['uc_ups_markups']['uc_ups_rate_markup_type'] = array( '#type' => 'select', - '#title' => t('Rate markup type'), + '#title' => t('Rate Markup Type'), '#default_value' => $config->get('uc_ups_rate_markup_type'), '#options' => array( 'percentage' => t('Percentage (%)'), @@ -207,148 +117,75 @@ function uc_ups_admin_settings($form, &$form_state) { ), ); - // Form to select rate markup amount. $form['uc_ups_markups']['uc_ups_rate_markup'] = array( '#type' => 'textfield', - '#title' => t('Shipping rate markup'), + '#title' => t('Shipping Rate Markup'), '#default_value' => $config->get('uc_ups_rate_markup'), '#description' => t('Markup shipping rate quote by currency amount, percentage, or multiplier.'), ); - // Form to select type of weight markup. - $form['uc_ups_markups']['uc_ups_weight_markup_type'] = array( - '#type' => 'select', - '#title' => t('Weight markup type'), - '#default_value' => $config->get('uc_ups_weight_markup_type'), - '#options' => array( - 'percentage' => t('Percentage (%)'), - 'multiplier' => t('Multiplier (×)'), - 'mass' => t('Addition (!mass)', array('!mass' => '#')), - ), - '#disabled' => TRUE, - ); - - // Form to select weight markup amount. - $form['uc_ups_markups']['uc_ups_weight_markup'] = array( - '#type' => 'textfield', - '#title' => t('Shipping weight markup'), - '#default_value' => $config->get('uc_ups_weight_markup'), - '#description' => t('Markup UPS shipping weight on a per-package basis before quote, by weight amount, percentage, or multiplier.'), - '#disabled' => TRUE, - ); - - // Container for label printing. + // Label Printing $form['uc_ups_labels'] = array( '#type' => 'fieldset', '#title' => t('Label Printing'), - '#description' => t('Preferences for UPS Shipping Label Printing. Additional permissions from UPS are required to use this feature.'), - '#collapsible' => TRUE, - '#collapsed' => TRUE, + '#description' => t('Preferences for UPS Shipping Label Printing. Additional permissions from UPS are required to use this feature.'), '#group' => 'ups-settings', ); - $period = backdrop_map_assoc(array(86400, 302400, 604800, 1209600, 2419200, 0), 'format_interval'); - $period[0] = t('Forever'); - - // Form to select how long labels stay on server. - $form['uc_ups_labels']['uc_ups_label_lifetime'] = array( - '#type' => 'select', - '#title' => t('Label lifetime'), - '#default_value' => $config->get('uc_ups_label_lifetime'), - '#options' => $period, - '#description' => t('Controls how long labels are stored on the server before being automatically deleted. Cron must be enabled for automatic deletion. Default is never delete the labels, keep them forever.'), - ); - - // Taken from system_settings_form(). Only, don't use its submit handler. $form['actions']['#type'] = 'actions'; $form['actions']['submit'] = array( '#type' => 'submit', '#value' => t('Save configuration'), ); - $form['actions']['cancel'] = array( - '#markup' => l(t('Cancel'), 'admin/store/settings/quotes'), - ); - - if (!empty($_POST) && form_get_errors()) { - backdrop_set_message(t('The settings have not been saved because of the errors.'), 'error'); - } - if (!isset($form['#theme'])) { - $form['#theme'] = 'system_settings_form'; - } return $form; } /** - * Validation handler for uc_ups_admin_settings. - * - * Requires password only if it hasn't been set. - * - * @see uc_ups_admin_settings() - * @see uc_ups_admin_settings_submit() - */ -function uc_ups_admin_settings_validate($form, &$form_state) { - $old_password = config_get('uc_ups.settings', 'uc_ups_password'); - if (!$form_state['values']['uc_ups_password']) { - if ($old_password) { - form_set_value($form['uc_ups_credentials']['uc_ups_password'], $old_password, $form_state); - } - else { - form_set_error('uc_ups_password', t('Password field is required.')); - } - } - - if (!is_numeric($form_state['values']['uc_ups_rate_markup'])) { - form_set_error('uc_ups_rate_markup', t('Rate markup must be a numeric value.')); - } - if (!is_numeric($form_state['values']['uc_ups_weight_markup'])) { - form_set_error('uc_ups_weight_markup', t('Weight markup must be a numeric value.')); - } -} - -/** - * Submit handler for uc_ups_admin_settings(). - * - * Emulates system_settings_form_submit(), but only on a subset of the - * form values. - * - * @see uc_ups_admin_settings() - * @see uc_ups_admin_settings_validate() + * Submit handler for UPS settings. */ function uc_ups_admin_settings_submit($form, &$form_state) { $fields = array( - 'uc_ups_access_license', 'uc_ups_shipper_number', - 'uc_ups_user_id', - 'uc_ups_password', - 'uc_ups_connection_address', - 'uc_ups_services', - 'uc_ups_pickup_type', - 'uc_ups_pkg_type', - 'uc_ups_classification', - 'uc_ups_negotiated_rates', - 'uc_ups_residential_quotes', - 'uc_ups_rate_markup_type', + 'uc_ups_client_id', +// 'uc_ups_client_secret', 'uc_ups_rate_markup', - 'uc_ups_weight_markup_type', - 'uc_ups_weight_markup', - 'uc_ups_label_lifetime', - 'uc_ups_all_in_one', - 'uc_ups_unit_system', - 'uc_ups_insurance', + 'uc_ups_negotiated_rates', + 'uc_ups_services', + 'uc_ups_connection_address', ); foreach ($fields as $key) { $value = $form_state['values'][$key]; - if (is_array($value) && isset($form_state['values']['array_filter'])) { $value = array_keys(array_filter($value)); } config_set('uc_ups.settings', $key, $value); } - backdrop_set_message(t('The configuration options have been saved.')); + // Update client secret if we entered a value. + $client_secret = $form_state['values']['uc_ups_client_secret']; + if (!empty($client_secret)) { + config_set('uc_ups.settings', 'uc_ups_client_secret', $client_secret); + } + + // Check token expiration + $expires_in = state_get('uc_ups_token_expiry'); + $current_time = REQUEST_TIME; - cache_clear_all(); - backdrop_theme_rebuild(); + if ($expires_in > $current_time + 300) { + backdrop_set_message(t('OAuth token is still valid. No refresh needed. Expires in @time seconds.', ['@time' => $expires_in - $current_time])); + } + else { + if (function_exists('uc_ups_get_access_token')) { + $token = uc_ups_get_access_token(); + if ($token) { + backdrop_set_message(t('OAuth token successfully retrieved.')); + } + else { + watchdog('uc_ups', 'Failed to retrieve OAuth token.', [], WATCHDOG_ERROR); + backdrop_set_message(t('Failed to retrieve OAuth token. Check the logs for details.'), 'error'); + } + } + } } diff --git a/shipping/uc_ups/uc_ups.install b/shipping/uc_ups/uc_ups.install index 1df63862..455b2f21 100644 --- a/shipping/uc_ups/uc_ups.install +++ b/shipping/uc_ups/uc_ups.install @@ -50,6 +50,35 @@ function uc_ups_schema() { return $schema; } +/** + * Implements hook_uninstall(). + */ +function uc_ups_uninstall() { + config_clear('uc_ups.settings', 'uc_ups_access_license'); + config_clear('uc_ups.settings', 'uc_ups_shipper_number'); + config_clear('uc_ups.settings', 'uc_ups_user_id'); + config_clear('uc_ups.settings', 'uc_ups_password'); + config_clear('uc_ups.settings', 'uc_ups_connection_address'); + config_clear('uc_ups.settings', 'uc_ups_services'); + config_clear('uc_ups.settings', 'uc_ups_pickup_type'); + config_clear('uc_ups.settings', 'uc_ups_pkg_type'); + config_clear('uc_ups.settings', 'uc_ups_classification'); + config_clear('uc_ups.settings', 'uc_ups_negotiated_rates'); + config_clear('uc_ups.settings', 'uc_ups_all_in_one'); + config_clear('uc_ups.settings', 'uc_ups_unit_system'); + config_clear('uc_ups.settings', 'uc_ups_insurance'); + config_clear('uc_ups.settings', 'uc_ups_rate_markup'); + config_clear('uc_ups.settings', 'uc_ups_rate_markup_type'); + config_clear('uc_ups.settings', 'uc_ups_weight_markup'); + config_clear('uc_ups.settings', 'uc_ups_weight_markup_type'); + // New variables for oauth2. + // @todo remove password, access_license and user_id. + config_clear('uc_ups.settings', 'uc_ups_client_id'); + config_clear('uc_ups.settings', 'uc_ups_secret'); + config_clear('uc_ups.settings', 'uc_ups_oauth2_token_production'); + config_clear('uc_ups.settings', 'uc_ups_oauth2_token_testing'); +} + /** * Implements hook_update_last_removed(). */ @@ -62,50 +91,68 @@ function uc_ups_update_last_removed() { */ function uc_ups_update_1000() { $config = config('uc_ups.settings'); - $config->set('uc_ups_access_license', update_variable_get('uc_ups_access_license', '')); - $config->set('uc_ups_shipper_number', update_variable_get('uc_ups_shipper_number', '')); - $config->set('uc_ups_user_id', update_variable_get('uc_ups_user_id', '')); - $config->set('uc_ups_password', update_variable_get('uc_ups_password', '')); - $config->set('uc_ups_connection_address', update_variable_get('uc_ups_connection_address', 'https://wwwcie.ups.com/ups.app/xml/')); + $config->set('uc_ups_client_id', update_variable_get('uc_ups_client_id', '')); + $config->set('uc_ups_client_secret', update_variable_get('uc_ups_client_secret', '')); + $config->set('uc_ups_connection_address', update_variable_get('uc_ups_connection_address', 'https://wwwcie.ups.com/')); $config->set('uc_ups_services', update_variable_get('uc_ups_services', array())); - $config->set('uc_ups_all_in_one', update_variable_get('uc_ups_all_in_one', TRUE)); - $config->set('uc_ups_pkg_type', update_variable_get('uc_ups_pkg_type', '02')); - $config->set('uc_ups_classification', update_variable_get('uc_ups_classification', '04')); - $config->set('uc_ups_negotiated_rates', update_variable_get('uc_ups_negotiated_rates', FALSE)); - $config->set('uc_ups_pickup_type', update_variable_get('uc_ups_pickup_type', '01')); - $config->set('uc_ups_residential_quotes', update_variable_get('uc_ups_residential_quotes', FALSE)); - $config->set('uc_ups_unit_system', update_variable_get('uc_ups_unit_system', config_get('uc_store.settings', 'uc_length_unit'))); - $config->set('uc_ups_insurance', update_variable_get('uc_ups_insurance', TRUE)); + $config->set('uc_ups_negotiated_rates', update_variable_get('uc_ups_negotiated_rates', array())); $config->set('uc_ups_rate_markup_type', update_variable_get('uc_ups_rate_markup_type', 'percentage')); $config->set('uc_ups_rate_markup', update_variable_get('uc_ups_rate_markup', '0')); - $config->set('uc_ups_weight_markup_type', update_variable_get('uc_ups_weight_markup_type', 'percentage')); - $config->set('uc_ups_weight_markup', update_variable_get('uc_ups_weight_markup', 0)); - $config->set('uc_ups_label_lifetime', update_variable_get('uc_ups_label_lifetime', '')); $config->set('uc_ups_markup', update_variable_get('uc_ups_markup', '')); $config->set('uc_ups_markup_type', update_variable_get('uc_ups_markup_type', '')); + $config->set('uc_ups_oauth_token_token_keyconnection_address', update_variable_get('uc_ups_oauth_token_token_keyconnection_address', 'dynamic variable in file /uc_ups/uc_ups.module line 65')); + $config->set('uc_ups_label_lifetime', update_variable_get('uc_ups_label_lifetime', '0')); + $config->set('uc_ups_pkg_type', update_variable_get('uc_ups_pkg_type', '02')); + $config->set('uc_ups_shipper_number', update_variable_get('uc_ups_shipper_number', '')); + $config->set('uc_ups_secret', update_variable_get('uc_ups_secret', '')); + $config->set('uc_ups_unit_system', update_variable_get('uc_ups_unit_system', 'dynamic value in file /uc_ups/uc_ups.ship.inc line 431')); + $config->set('uc_ups_insurance', update_variable_get('uc_ups_insurance', TRUE)); + $config->set('uc_ups_pickup_type', update_variable_get('uc_ups_pickup_type', '01')); + $config->set('uc_ups_classification', update_variable_get('uc_ups_classification', '04')); + $config->set('uc_ups_residential_quotes', update_variable_get('uc_ups_residential_quotes', array())); + $config->set('uc_ups_all_in_one', update_variable_get('uc_ups_all_in_one', TRUE)); + $config->set('uc_ups_weight_markup', update_variable_get('uc_ups_weight_markup', '0')); + $config->set('uc_ups_weight_markup_type', update_variable_get('uc_ups_weight_markup_type', 'percentage')); $config->save(); - update_variable_del('uc_ups_access_license'); - update_variable_del('uc_ups_shipper_number'); - update_variable_del('uc_ups_user_id'); - update_variable_del('uc_ups_password'); + state_set('uc_ups_access_token', update_variable_get('uc_ups_access_token', '')); + state_set('uc_ups_token_expiry', update_variable_get('uc_ups_token_expiry', '0')); + + update_variable_del('uc_ups_client_id'); + update_variable_del('uc_ups_client_secret'); + update_variable_del('uc_ups_access_token'); update_variable_del('uc_ups_connection_address'); update_variable_del('uc_ups_services'); - update_variable_del('uc_ups_all_in_one'); - update_variable_del('uc_ups_pkg_type'); - update_variable_del('uc_ups_classification'); update_variable_del('uc_ups_negotiated_rates'); - update_variable_del('uc_ups_pickup_type'); - update_variable_del('uc_ups_residential_quotes'); - update_variable_del('uc_ups_unit_system'); - update_variable_del('uc_ups_insurance'); update_variable_del('uc_ups_rate_markup_type'); update_variable_del('uc_ups_rate_markup'); - update_variable_del('uc_ups_weight_markup_type'); - update_variable_del('uc_ups_weight_markup'); - update_variable_del('uc_ups_label_lifetime'); + update_variable_del('uc_ups_token_expiry'); update_variable_del('uc_ups_markup'); update_variable_del('uc_ups_markup_type'); + update_variable_del('uc_ups_oauth_token_token_keyconnection_address'); + update_variable_del('uc_ups_label_lifetime'); + update_variable_del('uc_ups_pkg_type'); + update_variable_del('uc_ups_shipper_number'); + update_variable_del('uc_ups_secret'); + update_variable_del('uc_ups_unit_system'); + update_variable_del('uc_ups_insurance'); + update_variable_del('uc_ups_pickup_type'); + update_variable_del('uc_ups_classification'); + update_variable_del('uc_ups_residential_quotes'); + update_variable_del('uc_ups_all_in_one'); + update_variable_del('uc_ups_weight_markup'); + update_variable_del('uc_ups_weight_markup_type'); +} + +/** + * Remove pre-OAuth credentials from config. + */ +function uc_ups_update_1001() { + $config = config('uc_ups.settings'); + $config->clear('uc_ups_access_license'); + $config->clear('uc_ups_user_id'); + $config->clear('uc_ups_password'); + $config->save(); } /** diff --git a/shipping/uc_ups/uc_ups.module b/shipping/uc_ups/uc_ups.module index 6eb4f5a3..a6104b19 100644 --- a/shipping/uc_ups/uc_ups.module +++ b/shipping/uc_ups/uc_ups.module @@ -5,7 +5,7 @@ */ /****************************************************************************** - * Backdrop Hooks * + * Backdrop Hooks * ******************************************************************************/ /** @@ -22,6 +22,13 @@ function uc_ups_menu() { 'type' => MENU_LOCAL_TASK, 'file' => 'uc_ups.admin.inc', ); + $items['uc_ups/token_request'] = array( + 'title' => 'UPS Oauth2 Token Request', + 'page callback' => 'uc_ups_admin_token_request', + 'access arguments' => array('access content'), + 'file' => 'uc_ups.admin.inc', + 'type' => MENU_CALLBACK, + ); $items['admin/store/orders/%uc_order/shipments/ups'] = array( 'title' => 'UPS shipment', 'page callback' => 'backdrop_get_form', @@ -47,7 +54,6 @@ function uc_ups_config_info() { 'label' => t('Ubercart UPS settings'), 'group' => t('Configuration'), ); - return $prefixes; } @@ -59,7 +65,26 @@ function uc_ups_config_info() { * Default is never delete the labels, keep them forever. */ function uc_ups_cron() { - $cutoff = REQUEST_TIME - config_get('uc_ups.settings', 'uc_ups_label_lifetime'); + $config = config('uc_ups.settings'); + // Check if refresh token is expiring soon. + $token_key = [ + 'https://wwwcie.ups.com/' => 'testing', + 'https://onlinetools.ups.com/' => 'production', + ]; + $connection_address = $config->get('uc_ups_connection_address'); + if (isset($token_key[$connection_address])) { + $token = $config->get('uc_ups_oauth2_token_' . $token_key[$connection_address]); + if ($token) { + // Refresh token expires minus two hours padding. + $refresh_token_expires = round(($token['refresh_token_issued_at'] / 1000) + intval($token['refresh_token_expires_in'])) - 7200; + if ($refresh_token_expires <= REQUEST_TIME) { + // Refresh token. + uc_ups_get_access_token(); + } + } + } + + $cutoff = REQUEST_TIME - $config->get('uc_ups_label_lifetime'); if ($cutoff == REQUEST_TIME) { // Label lifetime is set to 0, meaning never delete. return; @@ -192,7 +217,13 @@ function uc_ups_node_load($nodes, $types) { $vids = array(); $shipping_type = config_get('uc_store.settings', 'uc_store_shipping_type'); - $shipping_types = db_query("SELECT id, shipping_type FROM {uc_quote_shipping_types} WHERE id_type = :type AND id IN (:ids)", array(':type' => 'product', ':ids' => array_keys($nodes)))->fetchAllKeyed(); + $shipping_types = db_query(' + SELECT id, shipping_type + FROM {uc_quote_shipping_types} + WHERE id_type = :type + AND id IN (:ids) + ', array(':type' => 'product', ':ids' => array_keys($nodes))) + ->fetchAllKeyed(); foreach ($nodes as $nid => $node) { if (!in_array($node->type, $product_types)) { @@ -212,7 +243,11 @@ function uc_ups_node_load($nodes, $types) { } if ($vids) { - $result = db_query("SELECT * FROM {uc_ups_products} WHERE vid IN (:vids)", array(':vids' => $vids), array('fetch' => PDO::FETCH_ASSOC)); + $result = db_query(' + SELECT * + FROM {uc_ups_products} + WHERE vid IN (:vids) + ', array(':vids' => $vids), array('fetch' => PDO::FETCH_ASSOC)); foreach ($result as $ups) { $nodes[$ups['nid']]->ups = $ups; } @@ -298,12 +333,11 @@ function uc_ups_uc_store_status() { $config = config('uc_ups.settings'); $messages = array(); - $access = $config->get('uc_ups_access_license') != ''; - $account = $config->get('uc_ups_shipper_number') != ''; - $user = $config->get('uc_ups_user_id') != ''; - $password = $config->get('uc_ups_password') != ''; + $account = !empty($config->get('uc_ups_shipper_number')); + $client_id = !empty($config->get('uc_ups_client_id')); + $client_secret = !empty($config->get('uc_ups_client_secret')); - if ($access && $account && $user && $password) { + if ($account && $client_id && $client_secret) { $messages[] = array( 'status' => 'ok', 'title' => t('UPS Online Tools'), @@ -328,19 +362,65 @@ function uc_ups_uc_store_status() { /** * Returns XML access request to be prepended to all requests to the * UPS webservice. + * Get stored Oauth2 access token. */ -function uc_ups_access_request() { + +/** + * @todo Please document this function. + */ +function uc_ups_get_access_token() { $config = config('uc_ups.settings'); - $access = $config->get('uc_ups_access_license'); - $user = $config->get('uc_ups_user_id'); - $password = $config->get('uc_ups_password'); - return " - - $access - $user - $password - -"; + $client_id = $config->get('uc_ups_client_id'); + $client_secret = $config->get('uc_ups_client_secret'); + $auth_url = 'https://wwwcie.ups.com/security/v1/oauth/token'; + + if (empty($client_id) || empty($client_secret)) { + watchdog('uc_ups', 'ERROR: Missing UPS Client ID or Client Secret.', [], WATCHDOG_ERROR); + return FALSE; + } + + $data = ['grant_type' => 'client_credentials']; + $options = [ + 'method' => 'POST', + 'data' => backdrop_http_build_query($data), + 'headers' => [ + 'Content-Type' => 'application/x-www-form-urlencoded', + 'Authorization' => 'Basic ' . base64_encode("$client_id:$client_secret"), + ], + ]; + + $result = backdrop_http_request($auth_url, $options); + + // Debug: Log API Response + watchdog('uc_ups', 'OAuth request response: @response', ['@response' => print_r($result, TRUE)], WATCHDOG_DEBUG); + + if ($result->code == 200) { + $response = backdrop_json_decode($result->data); + + if (!empty($response['access_token'])) { + // Store token in Backdrop state variables + state_set('uc_ups_access_token', $response['access_token']); + state_set('uc_ups_token_expiry', REQUEST_TIME + $response['expires_in']); + + // Debug: Confirm token storage + watchdog('uc_ups', '✅ Successfully retrieved and stored UPS OAuth token.', [], WATCHDOG_NOTICE); + return [ + 'access_token' => $response['access_token'], + 'expires_in' => $response['expires_in'], + ]; + } + else { + watchdog('uc_ups', '❌ UPS OAuth response missing access token. Response: @response', ['@response' => print_r($response, TRUE)], WATCHDOG_ERROR); + } + } + else { + watchdog('uc_ups', '❌ UPS OAuth request failed with HTTP Code @code. Response: @response', [ + '@code' => $result->code, + '@response' => print_r($result, TRUE), + ], WATCHDOG_ERROR); + } + + return FALSE; } /** @@ -359,21 +439,22 @@ function uc_ups_access_request() { * RatingServiceSelectionRequest XML document to send to UPS. */ function uc_ups_shipping_quote($packages, $origin, $destination, $ups_service) { - $config = config('uc_ups.settings'); + $store_config = config('uc_store.settings'); $store['name'] = uc_store_name(); - $store['owner'] = config_get('uc_store.settings', 'uc_store_owner'); + $store['owner'] = $store_config->get('uc_store_owner'); $store['email'] = uc_store_email(); $store['email_from'] = uc_store_email(); - $store['phone'] = config_get('uc_store.settings', 'uc_store_phone'); - $store['fax'] = config_get('uc_store.settings', 'uc_store_fax'); - $store['street1'] = config_get('uc_store.settings', 'uc_store_street1'); - $store['street2'] = config_get('uc_store.settings', 'uc_store_street2'); - $store['city'] = config_get('uc_store.settings', 'uc_store_city'); - $store['zone'] = config_get('uc_store.settings', 'uc_store_zone'); - $store['postal_code'] = config_get('uc_store.settings', 'uc_store_postal_code'); - $store['country'] = config_get('uc_store.settings', 'uc_store_country'); - - $account = $config->get('uc_ups_shipper_number'); + $store['phone'] = $store_config->get('uc_store_phone'); + $store['fax'] = $store_config->get('uc_store_fax'); + $store['street1'] = $store_config->get('uc_store_street1'); + $store['street2'] = $store_config->get('uc_store_street2'); + $store['city'] = $store_config->get('uc_store_city'); + $store['zone'] = $store_config->get('uc_store_zone'); + $store['postal_code'] = $store_config->get('uc_store_postal_code'); + $store['country'] = $store_config->get('uc_store_country'); + + $ups_config = config('uc_ups.settings'); + $account = $ups_config->get('uc_ups_shipper_number'); $ua = explode(' ', $_SERVER['HTTP_USER_AGENT']); $user_agent = $ua[0]; @@ -398,7 +479,7 @@ function uc_ups_shipping_quote($packages, $origin, $destination, $ups_service) { $shipfrom_country = $shipfrom_country[0]['country_iso_code_2']; $shipfrom_zip = $origin->postal_code; - $ups_units = $config->get('uc_ups_unit_system'); + $ups_units = $ups_config->get('uc_ups_unit_system'); switch ($ups_units) { case 'in': $units = 'LBS'; @@ -417,7 +498,7 @@ function uc_ups_shipping_quote($packages, $origin, $destination, $ups_service) { // Determine length conversion factor and weight conversion factor // for this shipment. - $length_factor = uc_length_conversion($package->length_units, $config->get('uc_ups_unit_system')); + $length_factor = uc_length_conversion($package->length_units, $ups_config->get('uc_ups_unit_system')); switch ($ups_units) { case 'in': $weight_factor = uc_weight_conversion($package->weight_units, 'lb'); @@ -436,53 +517,53 @@ function uc_ups_shipping_quote($packages, $origin, $destination, $ups_service) { 'code' => $package->pkg_type, 'description' => $pkg_types[$package->pkg_type], ); - $package_schema .= ""; - $package_schema .= ""; - $package_schema .= "" . $package_type['code'] . ""; - $package_schema .= ""; + $package_schema .= "\n"; + $package_schema .= " \n"; + $package_schema .= " " . $package_type['code'] . "\n"; + $package_schema .= " " . $package_type['description'] . "\n"; + $package_schema .= " \n"; if ($package->pkg_type == '02' && $package->length && $package->width && $package->height) { if ($package->length < $package->width) { list($package->length, $package->width) = array($package->width, $package->length); } - $package_schema .= ""; - $package_schema .= ""; - $package_schema .= "" . strtoupper($config->get('uc_ups_unit_system')) . ""; - $package_schema .= ""; - $package_schema .= "" . number_format($package->length * $length_factor, 2, '.', '') . ""; - $package_schema .= "" . number_format($package->width * $length_factor, 2, '.', '') . ""; - $package_schema .= "" . number_format($package->height * $length_factor, 2, '.', '') . ""; - $package_schema .= ""; + $package_schema .= " \n"; + $package_schema .= " \n"; + $package_schema .= " " . strtoupper($ups_config->get('uc_ups_unit_system')) . "\n"; + $package_schema .= " \n"; + $package_schema .= " " . number_format($package->length * $length_factor, 2, '.', '') . "\n"; + $package_schema .= " " . number_format($package->width * $length_factor, 2, '.', '') . "\n"; + $package_schema .= " " . number_format($package->height * $length_factor, 2, '.', '') . "\n"; + $package_schema .= " \n"; } $weight = max(1, $package->weight * $weight_factor); $shipment_weight += $weight; - $package_schema .= ""; - $package_schema .= ""; - $package_schema .= "" . $units . ""; - $package_schema .= "" . $unit_name . ""; - $package_schema .= ""; - $package_schema .= "" . number_format($weight, 1, '.', '') . ""; - $package_schema .= ""; + $package_schema .= " \n"; + $package_schema .= " \n"; + $package_schema .= " " . $units . "\n"; + $package_schema .= " " . $unit_name . "\n"; + $package_schema .= " \n"; + $package_schema .= " " . number_format($weight, 1, '.', '') . "\n"; + $package_schema .= " \n"; $size = $package->length * $length_factor + 2 * $length_factor * ($package->width + $package->height); if ($size > 130 && $size <= 165) { $package_schema .= ""; } - if ($config->get('uc_ups_insurance')) { - $package_schema .= ""; - $package_schema .= ""; - $package_schema .= "" . config_get('uc_store.settings', 'uc_currency_code') . ""; - $package_schema .= "" . $package->price . ""; - $package_schema .= ""; - $package_schema .= ""; + if ($ups_config->get('uc_ups_insurance')) { + $package_schema .= " \n"; + $package_schema .= " \n"; + $package_schema .= " " . config_get('uc_store.settings', 'uc_currency_code') . "\n"; + $package_schema .= " " . $package->price . "\n"; + $package_schema .= " \n"; + $package_schema .= " \n"; } - $package_schema .= ""; + $package_schema .= "\n"; } } - $schema = uc_ups_access_request() . " - + $schema = " @@ -493,14 +574,14 @@ function uc_ups_shipping_quote($packages, $origin, $destination, $ups_service) { rate - " . $config->get('uc_ups_pickup_type') . " + " . $ups_config->get('uc_ups_pickup_type') . " - " . $config->get('uc_ups_classification') . " + " . $ups_config->get('uc_ups_classification') . " - " . $config->get('uc_ups_shipper_number') . " + " . $ups_config->get('uc_ups_shipper_number') . "
" . $store['city'] . " $shipper_zone @@ -514,7 +595,7 @@ function uc_ups_shipping_quote($packages, $origin, $destination, $ups_service) { $shipto_zip $shipto_country "; - if ($config->get('uc_ups_residential_quotes')) { + if ($ups_config->get('uc_ups_residential_quotes')) { $schema .= " "; } @@ -540,7 +621,7 @@ function uc_ups_shipping_quote($packages, $origin, $destination, $ups_service) { "; $schema .= $package_schema; - if ($config->get('uc_ups_negotiated_rates')) { + if ($ups_config->get('uc_ups_negotiated_rates')) { $schema .= " "; @@ -574,7 +655,7 @@ function uc_ups_quote($products, $details, $method) { // filled out the destination address, so check to see whether the address // has all needed fields. If not, abort. $destination = (object) $details; - if (empty($destination->zone) || + if (empty($destination->zone) || empty($destination->postal_code) || empty($destination->country)) { // Skip this shipping method. @@ -736,12 +817,35 @@ function uc_ups_quote($products, $details, $method) { foreach ($packages as $key => $ship_packages) { $orig = $addresses[$key]; $orig->email = uc_store_email(); + // Retrieve token + $token_data = uc_ups_get_access_token(); + + // Debug: Log the entire token data + watchdog('uc_ups', 'Token Data Retrieved: @data', ['@data' => print_r($token_data, TRUE)], WATCHDOG_DEBUG); + + // Ensure the token exists + if (!isset($token_data['access_token']) || empty($token_data['access_token'])) { + watchdog('uc_ups', 'ERROR: Access token is missing or empty!', [], WATCHDOG_ERROR); + return array(); // Stop execution if token is missing + } + + // Debug: Log first few characters of the token + watchdog('uc_ups', 'Using token: @token', ['@token' => substr($token_data['access_token'], 0, 20) . '... (truncated)'], WATCHDOG_DEBUG); + foreach (array_keys(array_filter($config->get('uc_ups_services'))) as $ups_service) { $request = uc_ups_shipping_quote($ship_packages, $orig, $dest, $ups_service); - $resp = backdrop_http_request($config->get('uc_ups_connection_address') . 'Rate', array( + $resp = backdrop_http_request($config->get('uc_ups_connection_address') . 'api/ups.app/xml/Rate', array( 'method' => 'POST', 'data' => $request, + 'headers' => array( + 'Authorization' => 'Bearer ' . $token_data['access_token'], + 'Content-Type' => 'application/xml', + ), )); + + // Debug: Log the API response + watchdog('uc_ups', 'UPS API Response: @response', ['@response' => print_r($resp, TRUE)], WATCHDOG_DEBUG); + if (user_access('configure quotes') && config_get('uc_quote.settings', 'uc_quote_display_debug')) { if (!isset($debug_data[$ups_service]['debug'])) { $debug_data[$ups_service]['debug'] = ''; @@ -827,8 +931,7 @@ function uc_ups_quote($products, $details, $method) { * XML VoidShipmentRequest message. */ function uc_ups_void_shipment_request($shipment_number, $tracking_numbers = array()) { - $schema = uc_ups_access_request(); - $schema .= ''; + $schema = ''; $schema .= ''; $schema .= ''; $schema .= 'Void'; @@ -865,9 +968,14 @@ function uc_ups_void_shipment_request($shipment_number, $tracking_numbers = arra function uc_ups_void_shipment($shipment_number, $tracking_numbers = array()) { $success = FALSE; $request = uc_ups_void_shipment_request($shipment_number, $tracking_numbers); - $resp = backdrop_http_request(config_get('uc_ups.settings', 'uc_ups_connection_address') . 'Void', array( + $token_data = uc_ups_get_access_token(); + $resp = backdrop_http_request(config_get('uc_ups.settings', 'uc_ups_connection_address') . 'api/ups.app/xml/Void', array( 'method' => 'POST', 'data' => $request, + 'headers' => array( + 'Authorization' => 'Bearer ' . $token_data['access_token'], + 'Content-Type' => 'application/xml', + ), )); $response = new SimpleXMLElement($resp->data); if (isset($response->Response)) { @@ -931,7 +1039,7 @@ function uc_ups_rate_markup($rate) { function uc_ups_weight_markup($weight) { $config = config('uc_ups.settings'); $markup = trim($config->get('uc_ups_weight_markup')); - $type = $config->get('uc_ups_weight_markup_type'); + $type = $config->get('uc_ups_weight_markup_type'); if (is_numeric($markup)) { switch ($type) { diff --git a/shipping/uc_ups/uc_ups.ship.inc b/shipping/uc_ups/uc_ups.ship.inc index 47195c8d..9286897c 100644 --- a/shipping/uc_ups/uc_ups.ship.inc +++ b/shipping/uc_ups/uc_ups.ship.inc @@ -101,8 +101,8 @@ function uc_ups_fulfill_order($form, &$form_state, $order, $package_ids) { 'g' => t('Grams'), ), '#default_value' => isset($package->weight_units) ? - $package->weight_units : - config_get('uc_store.settings', 'uc_weight_unit'), + $package->weight_units : + config_get('uc_store.settings', 'uc_weight_unit'), ); $pkg_form['dimensions'] = array( '#type' => 'container', @@ -138,8 +138,8 @@ function uc_ups_fulfill_order($form, &$form_state, $order, $package_ids) { 'mm' => t('Millimeters'), ), '#default_value' => isset($package->length_units) ? - $package->length_units : - config_get('uc_store.settings', 'uc_length_unit'), + $package->length_units : + config_get('uc_store.settings', 'uc_length_unit'), ); $form['packages'][$id] = $pkg_form; @@ -255,7 +255,7 @@ function uc_ups_fulfill_order_validate($form, &$form_state) { // This is a total hack to work around changes made in the return value // from uc_shipping_address_form(). That function needs to be fixed, but // until then this should do the trick. - $origin = $form_state['values']['pickup_address']; + $origin = $form_state['values']['pickup_address']; $origin->phone = $form_state['values']['phone']; $origin->first_name = $form_state['values']['first_name']; $origin->last_name = $form_state['values']['last_name']; @@ -299,9 +299,14 @@ function uc_ups_fulfill_order_validate($form, &$form_state) { $_SESSION['ups']['order_id'] = $form_state['values']['order_id']; $request = uc_ups_shipment_request($_SESSION['ups']['packages'], $origin, $destination, $form_state['values']['service']); - $response_obj = backdrop_http_request($config->get('uc_ups_connection_address') . 'ShipConfirm', array( + $token_data = uc_ups_get_access_token(); + $response_obj = backdrop_http_request($config->get('uc_ups_connection_address') . 'api/ups.app/xml/ShipConfirm', array( 'method' => 'POST', 'data' => $request, + 'headers' => array( + 'Authorization' => 'Bearer ' . $token_data['access_token'], + 'Content-Type' => 'application/xml', + ), )); $response = new SimpleXMLElement($response_obj->data); if (isset($response->Response->Error)) { @@ -356,21 +361,22 @@ function uc_ups_fulfill_order_submit($form, &$form_state) { * ShipConfirm XML document to send to UPS. */ function uc_ups_shipment_request($packages, $origin, $destination, $ups_service) { - $config = config('uc_ups.settings'); $store['name'] = uc_store_name(); - $store['owner'] = config_get('uc_store.settings', 'uc_store_owner'); + $store_config = config('uc_store.settings'); + $store['owner'] = $store_config->get('uc_store_owner'); $store['email'] = uc_store_email(); $store['email_from'] = uc_store_email(); - $store['phone'] = config_get('uc_store.settings', 'uc_store_phone'); - $store['fax'] = config_get('uc_store.settings', 'uc_store_fax'); - $store['street1'] = config_get('uc_store.settings', 'uc_store_street1'); - $store['street2'] = config_get('uc_store.settings', 'uc_store_street2'); - $store['city'] = config_get('uc_store.settings', 'uc_store_city'); - $store['zone'] = config_get('uc_store.settings', 'uc_store_zone'); - $store['postal_code'] = config_get('uc_store.settings', 'uc_store_postal_code'); - $store['country'] = config_get('uc_store.settings', 'uc_store_country'); - - $account = $config->get('uc_ups_shipper_number'); + $store['phone'] = $store_config->get('uc_store_phone'); + $store['fax'] = $store_config->get('uc_store_fax'); + $store['street1'] = $store_config->get('uc_store_street1'); + $store['street2'] = $store_config->get('uc_store_street2'); + $store['city'] = $store_config->get('uc_store_city'); + $store['zone'] = $store_config->get('uc_store_zone'); + $store['postal_code'] = $store_config->get('uc_store_postal_code'); + $store['country'] = $store_config->get('uc_store_country'); + + $ups_config = config('uc_ups.settings'); + $account = $ups_config->get('uc_ups_shipper_number'); $ua = explode(' ', $_SERVER['HTTP_USER_AGENT']); $user_agent = $ua[0]; @@ -395,13 +401,13 @@ function uc_ups_shipment_request($packages, $origin, $destination, $ups_service) $shipfrom_country = $shipfrom_country[0]['country_iso_code_2']; $shipfrom_zip = $origin->postal_code; - $ups_units = $config->get('uc_ups_unit_system'); + $ups_units = $ups_config->get('uc_ups_unit_system'); $package_schema = ''; foreach ($packages as $package) { // Determine length conversion factor and weight conversion factor // for this shipment - $length_factor = uc_length_conversion($package->length_units, $config->get('uc_ups_unit_system')); + $length_factor = uc_length_conversion($package->length_units, $ups_config->get('uc_ups_unit_system')); switch ($ups_units) { case 'in': $weight_factor = uc_weight_conversion($package->weight_units, 'lb'); @@ -424,45 +430,43 @@ function uc_ups_shipment_request($packages, $origin, $destination, $ups_service) ); $package_schema .= ""; $package_schema .= ""; - $package_schema .= "" . $package_type['code'] . ""; - $package_schema .= ""; + $package_schema .= "" . $package_type['code'] . ""; + $package_schema .= ""; if ($package->pkg_type == '02' && $package->length && $package->width && $package->height) { if ($package->length < $package->width) { list($package->length, $package->width) = array($package->width, $package->length); } $package_schema .= ""; - $package_schema .= ""; - $package_schema .= "" . strtoupper($config->get('uc_ups_unit_system')) . ""; - $package_schema .= ""; - $package_schema .= "" . (floor($package->length * $length_factor) + 1) . ""; - $package_schema .= "" . (floor($package->width * $length_factor) + 1) . ""; - $package_schema .= "" . (floor($package->height * $length_factor) + 1) . ""; + $package_schema .= ""; + $package_schema .= "" . strtoupper($ups_config->get('uc_ups_unit_system')) . ""; + $package_schema .= ""; + $package_schema .= "" . (floor($package->length * $length_factor) + 1) . ""; + $package_schema .= "" . (floor($package->width * $length_factor) + 1) . ""; + $package_schema .= "" . (floor($package->height * $length_factor) + 1) . ""; $package_schema .= ""; } $size = $package->length * $length_factor + 2 * $length_factor * ($package->width + $package->height); $weight = max(1, $package->weight * $weight_factor); - $package_schema .= ""; - $package_schema .= ""; - $package_schema .= "" . $units . ""; - $package_schema .= "" . $unit_name . ""; - $package_schema .= ""; - $package_schema .= "" . number_format($weight, 1, '.', '') . ""; - $package_schema .= ""; + $package_schema .= ""; + $package_schema .= ""; + $package_schema .= "" . $units . ""; + $package_schema .= "" . $unit_name . ""; + $package_schema .= ""; + $package_schema .= "" . number_format($weight, 1, '.', '') . ""; + $package_schema .= ""; if ($size > 130 && $size <= 165) { $package_schema .= ""; } - $package_schema .= ""; - $package_schema .= ""; - $package_schema .= "" . config_get('uc_store.settings', 'uc_currency_code') . ""; - $package_schema .= "" . number_format($package->value, 2, '.', '') . ""; - $package_schema .= ""; - $package_schema .= ""; + $package_schema .= ""; + $package_schema .= ""; + $package_schema .= "" . $store_config->get('uc_currency_code', 'USD') . ""; + $package_schema .= "" . number_format($package->value, 2, '.', '') . ""; + $package_schema .= ""; + $package_schema .= ""; $package_schema .= ""; } } - - $schema = uc_ups_access_request() . " - + $schema = " @@ -475,7 +479,7 @@ function uc_ups_shipment_request($packages, $origin, $destination, $ups_service) "; $schema .= ""; $schema .= "" . $store['name'] . ""; - $schema .= "" . $config->get('uc_ups_shipper_number') . ""; + $schema .= "" . $ups_config->get('uc_ups_shipper_number') . ""; if ($store['phone']) { $schema .= "" . $store['phone'] . ""; } @@ -538,7 +542,7 @@ function uc_ups_shipment_request($packages, $origin, $destination, $ups_service) $schema .= ""; $schema .= ""; $schema .= ""; - if ($config->get('uc_ups_negotiated_rates')) { + if ($ups_config->get('uc_ups_negotiated_rates')) { $schema .= " "; @@ -646,14 +650,18 @@ function uc_ups_confirm_shipment_submit($form, &$form_state) { $order_id = $_SESSION['ups']['order_id']; $packages = array_keys($_SESSION['ups']['packages']); $request = uc_ups_request_pickup($form_state['values']['digest'], $order_id, $packages); - $result = backdrop_http_request(config_get('uc_ups.settings', 'uc_ups_connection_address') . 'ShipAccept', array( + $token_data = uc_ups_get_access_token(); + $result = backdrop_http_request(config_get('uc_ups.settings', 'uc_ups_connection_address') . 'api/ups.app/xml/ShipAccept', array( 'method' => 'POST', 'data' => $request, + 'headers' => array( + 'Authorization' => 'Bearer ' . $token_data['access_token'], + 'Content-Type' => 'application/xml', + ), )); $response = new SimpleXMLElement($result->data); $code = (string) $response->Response->ResponseStatusCode; - // failed request - if ($code == 0) { + if ($code == 0) { // failed request $error = $response->Response->Error; $error_severity = (string) $error->ErrorSeverity; $error_code = (string) $error->ErrorCode; @@ -733,8 +741,7 @@ function uc_ups_confirm_shipment_submit($form, &$form_state) { function uc_ups_request_pickup($digest, $order_id = 0, $packages = array()) { $packages = (array) $packages; - $schema = uc_ups_access_request(); - $schema .= " + $schema = " ShipAccept";