--- payment.module.org Sat Jun 10 09:01:47 2006 +++ payment.module Fri Jul 07 22:28:55 2006 @@ -1,6 +1,8 @@ $ret['message']))); + } + else { + variable_set('payment_exchange_rate_' . $i, $ret['value']); + } + } + } +} + +/** * Implementation of hook_checkoutapi(). */ function payment_checkoutapi(&$txn, $op, $arg3 = NULL, $arg4 = NULL) { @@ -102,6 +132,11 @@ */ function payment_ec_settings() { drupal_set_title(t('Payment settings')); + + if (variable_get('payment_adv', 0) && !module_exist('currency_api')) { + drupal_set_message(t('The %currency_api module is not enable. Some advance currency formatting options will be disable.', array('%currency_api' => l('currency_api', "http://drupal.org/project/currency"))), 'error'); + } + // Begin price format section $form['price_format'] = array( '#type' => 'fieldset', @@ -109,42 +144,153 @@ '#collapsible' => TRUE, '#collapsed' => TRUE, ); - $form['price_format']['payment_symbol'] = array( - '#type' => 'textfield', - '#title' => t('Currency Symbol'), - '#default_value' => variable_get('payment_symbol', '$'), - '#size' => 3, - '#maxlength' => 5, - '#desciption' => t('Enter the currency symbol you wish to associate with your price. This will be displayed in front of the price. Default is the dollar symbol.') - ); - $form['price_format']['payment_symbol_position'] = array( - '#type' => 'radios', - '#title' => t('Currency Symbol'), - '#default_value' => variable_get('payment_symbol_position', 1), - '#options' => array(t('Right'), t('Left')), - '#desciption' => t('This option places the currency symbol of the left or right side of the price.') - ); - $form['price_format']['payment_thousands'] = array( - '#type' => 'textfield', - '#title' => t('Thousands separator'), - '#default_value' => variable_get('payment_thousands', ','), - '#size' => 3, '#maxlength' => 5, - '#desciption' => t('Enter the sign for the thousands separator.') - ); - $form['price_format']['payment_decimal'] = array( - '#type' => 'textfield', - '#title' => t('Decimal separator'), - '#default_value' => variable_get('payment_decimal', '.'), - '#size' => 3, '#maxlength' => 5, - '#desciption' => t('Enter the sign to seperate real numbers from floating numbers.') - ); - $form['price_format']['payment_decimal_places'] = array( - '#type' => 'textfield', - '#title' => t('Number of places after the decimal separator'), - '#default_value' => variable_get('payment_decimal_places', 2), - '#size' => 3, '#maxlength' => 5, - '#desciption' => t('How many slots are needed after the decimal?') - ); + $form['price_format']['payment_adv'] = array( + '#type' => 'radios', + '#title' => t('Enable Advance Currency Formatting'), + '#default_value' => variable_get('payment_adv', 0), + '#options' => array(t('Disabled'), t('Enabled')), + '#description' => t('If enabled, currency can config with advance formatting. You can handle a difference display currency format other than base format, set exchange rate by hand or update by %currency_api (optional), and customers are allowed to config their display currency format. Note that if you enable advance currency formatting with default parameters, there will be no difference with disable it. It is suggested to enable this option, if your store will use Paypal payment module but serve local customers mainly.', array('%currency_api' => l('currency_api', "http://drupal.org/project/currency"))), + ); + + if (variable_get('payment_adv', 0)) { + $form['price_format']['payment_active'] = array( + '#type' => 'select', + '#title' => t('Active Currency Format'), + '#default_value' => variable_get('payment_active', array('1')), + '#options' => payment_supported_currencies(TRUE, TRUE), + '#multiple' => TRUE, + '#description' => t('Chose the currency formats that you hope to active.'), + ); + $form['price_format']['payment_base'] = array( + '#type' => 'select', + '#title' => t('Base Currency Format'), + '#default_value' => variable_get('payment_base', 1), + '#options' => payment_supported_currencies(FALSE, TRUE), + '#description' => t('Chose the base currency format of your store. Suggest to use USD/EUR/AUD/CAD/GBP/JPY as base currency if work with paypal module.'), + ); + $form['price_format']['payment_display'] = array( + '#type' => 'select', + '#title' => t('Display Currency Format'), + '#default_value' => variable_get('payment_display', 1), + '#options' => payment_supported_currencies(FALSE, TRUE), + '#description' => t('Chose the display currency format of your store. If this is not your base currency, it will convert from base currency due to exchange rate, and the result will not save within database.'), + ); + $form['price_format']['payment_with_base'] = array( + '#type' => 'radios', + '#title' => t('Enable Display With Base Currency'), + '#default_value' => variable_get('payment_with_base' . $i, 0), + '#options' => array(t('Disabled'), t('Enabled')), + '#description' => t('If enabled, both base and display currency will show out.'), + ); + } + + for ($i = 1; $i <= PAYMENT_MAX_CURRENCIES; $i++) { + if (variable_get('payment_adv', 0)) { + $form['price_format'][$i] = array( + '#type' => 'fieldset', + '#title' => t('Currency %count Attributes', array('%count' => $i)), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + ); + if (module_exist('currency_api')) { + $form['price_format'][$i]['payment_code_' . $i] = array( + '#type' => 'select', + '#title' => t('Currency Code'), + '#default_value' => variable_get('payment_code_' . $i, 'USD'), + '#options' => currency_api_get_list(), + '#description' => t('Chose the currency code for your price.'), + ); + } + else { + $form['price_format'][$i]['payment_code_' . $i] = array( + '#type' => 'textfield', + '#title' => t('Currency Code'), + '#default_value' => variable_get('payment_code_' . $i, 'USD'), + '#size' => 3, + '#maxlength' => 3, + '#description' => t('Enter the three letter currency code for your price.'), + ); + } + $form['price_format'][$i]['format'] = array( + '#type' => 'fieldset', + '#title' => t('Formatting'), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + ); + } + + $form['price_format'][$i]['format']['payment_symbol_' . $i] = array( + '#type' => 'textfield', + '#title' => t('Currency Symbol'), + '#default_value' => variable_get('payment_symbol_' . $i, '$'), + '#size' => 3, + '#maxlength' => 5, + '#description' => t('Enter the currency symbol you wish to associate with your price. This will be displayed in front of the price. Default is the dollar symbol.'), + ); + $form['price_format'][$i]['format']['payment_symbol_position_' . $i] = array( + '#type' => 'radios', + '#title' => t('Currency Symbol'), + '#default_value' => variable_get('payment_symbol_position_' . $i, 1), + '#options' => array(t('Right'), t('Left')), + '#description' => t('This option places the currency symbol of the left or right side of the price.'), + ); + $form['price_format'][$i]['format']['payment_thousands_' . $i] = array( + '#type' => 'textfield', + '#title' => t('Thousands separator'), + '#default_value' => variable_get('payment_thousands_' . $i, ','), + '#size' => 3, + '#maxlength' => 5, + '#description' => t('Enter the sign for the thousands separator.'), + ); + $form['price_format'][$i]['format']['payment_decimal_' . $i] = array( + '#type' => 'textfield', + '#title' => t('Decimal separator'), + '#default_value' => variable_get('payment_decimal_' . $i, '.'), + '#size' => 3, + '#maxlength' => 5, + '#description' => t('Enter the sign to seperate real numbers from floating numbers.'), + ); + $form['price_format'][$i]['format']['payment_decimal_places_' . $i] = array( + '#type' => 'textfield', + '#title' => t('Number of places after the decimal separator'), + '#default_value' => variable_get('payment_decimal_places_' . $i, 2), + '#size' => 3, + '#maxlength' => 5, + '#description' => t('How many slots are needed after the decimal?'), + ); + + if (variable_get('payment_adv', 0)) { + $form['price_format'][$i]['exchange'] = array( + '#type' => 'fieldset', + '#title' => t('Exchange Rate'), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + ); + $form['price_format'][$i]['exchange']['payment_exchange_rate_' . $i] = array( + '#type' => 'textfield', + '#title' => t('Exchange Rates'), + '#default_value' => variable_get('payment_exchange_rate_' . $i, '1.00'), + '#size' => 5, + '#maxlength' => 10, + '#description' => t('Current exchange rates. This will be updated by cron if enable Dynamic Update Exhange Rates.'), + ); + $form['price_format'][$i]['exchange']['payment_dynamic_' . $i] = array( + '#type' => 'radios', + '#title' => t('Enable Dynamic Update Exchange Rates'), + '#default_value' => variable_get('payment_dynamic_' . $i, 0), + '#options' => array(t('Disabled'), t('Enabled')), + '#description' => t('If enabled, exchange rates will be updated during cron.'), + ); + if (!module_exist('currency_api')) { + $form['price_format'][$i]['exchange']['payment_dynamic_' . $i]['#description'] .= t(' Note that this requires %currency_api module, and cron to be set up correctly.', array('%currency_api' => l('currency_api', "http://drupal.org/project/currency"))); + $form['price_format'][$i]['exchange']['payment_dynamic_' . $i]['#attributes'] = array('disabled' => 'disabled'); + } + } + + if (!variable_get('payment_adv', 0)) { + break; + } + } // Begin recurring payments $form['recurring_payments'] = array( @@ -438,7 +584,7 @@ /** * Implementation of hook_user(). */ -function payment_user($type, &$edit, &$user) { +function payment_user($type, &$edit, &$user, $category = NULL) { // Only display the link for the user who owns it. if ($type == 'view' && $GLOBALS['user']->uid == $user->uid && variable_get('payment_recurring', 0)) { $items[] = array('title' => t('Subscriptions'), @@ -447,17 +593,136 @@ ); return array(t('History') => $items); } + if ($type == 'form' && $category == 'account' && variable_get('payment_adv', 0) && count(variable_get('payment_active', array('1'))) > 1) { + if ($edit['payment_display'] == '' || !array_key_exists($edit['payment_display'], variable_get('payment_active', array('1')))) { + $edit['payment_display'] = variable_get('payment_display', 1); + } + if ($edit['payment_with_base'] == '') { + $edit['payment_with_base'] = variable_get('payment_with_base', 0); + } + $form['payment'] = array( + '#type' => 'fieldset', + '#title' => t('Currency settings'), + '#weight' => 6, + '#collapsible' => TRUE, + '#attributes' => array('id' => 'currency'), + ); + $form['payment']['base'] = array( + '#type' => 'select', + '#title' => t('Base Currency Format'), + '#default_value' => variable_get('payment_base', 1), + '#options' => payment_supported_currencies(), + '#description' => t('The base currency format. All transactions will base on this currency.'), + '#attributes' => array('disabled' => 'disabled'), + ); + $form['payment']['payment_display'] = array( + '#type' => 'select', + '#title' => t('Display Currency Format'), + '#default_value' => $edit['payment_display'], + '#options' => payment_supported_currencies(), + '#description' => t('Chose your display currency format. If this is not the system base currency, it will be converted due to exchange rate. Note that display currency format is for reference only, all transactions will calculate in base currency.'), + ); + $form['payment']['payment_with_base'] = array( + '#type' => 'radios', + '#title' => t('Enable Display With Base Currency'), + '#default_value' => $edit['payment_with_base'], + '#options' => array(t('Disabled'), t('Enabled')), + '#description' => t('If enabled, both base and display currency will show out.'), + ); + return $form; + } + if ($type == 'validate') { + return array( + 'payment_display' => $edit['payment_display'], + 'payment_with_base' => $edit['payment_with_base'], + ); + } } /** * Format the price according to payment_settings() config options. */ function payment_format($price) { - $price = number_format((float) $price, - variable_get('payment_decimal_places', 2), - variable_get('payment_decimal', '.'), - variable_get('payment_thousands', ',')); - return (variable_get('payment_symbol_position', 1) == 1) ? variable_get('payment_symbol', '$') . $price : $price . variable_get('payment_symbol', '$'); + global $user; + $account = user_load(array('uid' => $user->uid)); + $active = variable_get('payment_active', array('1')); + + $base = variable_get('payment_base', 1); + $bcode = variable_get('payment_code_' . $base, 'USD'); + $bsymbol = variable_get('payment_symbol_' . $base, '$'); + $bprice = number_format((float) $price, + variable_get('payment_decimal_places_' . $base, 2), + variable_get('payment_decimal_' . $base, '.'), + variable_get('payment_thousands_' . $base, ',')); + $bformat = (variable_get('payment_symbol_position_' . $base, 1) == 1) ? $bsymbol . $bprice : $bprice . $bsymbol; + + // if we are in basic mode, or only have one active currency, can return now + if (!variable_get('payment_adv', 0) || count($active) == 1) { + return $bformat; + } + + // use user defined value, else default + $display = ($user->uid != 0 && $account->payment_display != NULL && array_key_exists($account->payment_display, $active)) ? + $account->payment_display : + variable_get('payment_display', 1); + $dcode = variable_get('payment_code_' . $display, 'USD'); + if ($dcode != $bcode) { + $rate = variable_get('payment_exchange_rate_' . $display, '1.00'); + $dsymbol = variable_get('payment_symbol_' . $display, '$'); + $dprice = number_format((float) $price * (float) $rate, + variable_get('payment_decimal_places_' . $display, 2), + variable_get('payment_decimal_' . $display, '.'), + variable_get('payment_thousands_' . $display, ',')); + $dformat = (variable_get('payment_symbol_position_' . $display, 1) == 1) ? $dsymbol . $dprice : $dprice . $dsymbol; + $dformat = '' . $dformat . ''; + } + else { + $dformat = $bformat; + } + + // point to register page if not user, else to user edit page + $return = ($user->uid == 0) ? + $dformat . '' . l('#', "user/register", array('title' => t('Register as user and chose your perfered currency formatting.'))) . '' : + $dformat . '' . l('#', "user/{$user->uid}/edit", array('title' => t('Change your perfered currency formatting.')), NULL, 'currency') . ''; + + // add base currency format if $with_base is set + if ($dcode != $bcode) { + $with_base = ($user->uid != 0 && $account->payment_with_base != NULL) ? + $account->payment_with_base : + variable_get('payment_with_base', 0); + if ($with_base) { + $return = $bformat . ' » ' . $return; + } + } + + return $return; +} + +/** + * Returns list of currency supported on this site. + * + * @param $getall Return all languages (even disabled ones) + * @param $counter Return with counter (for admin) + */ +function payment_supported_currencies($getall = FALSE, $counter = FALSE) { + for ($i = 1; $i <= PAYMENT_MAX_CURRENCIES; $i++) { + $list[$i] = ($counter) ? + t('Currency %count - ', array('%count' => $i)) : + ''; + $list[$i] .= (module_exist('currency_api')) ? + currency_api_get_desc(variable_get('payment_code_' . $i, 'USD')) : + variable_get('payment_code_' . $i, 'USD'); + } + + if ($getall) { + return $list; + } + else { + foreach (variable_get('payment_active', array('1')) as $i) { + $active[$i] = $list[$i]; + } + return $active; + } } function payment_accepted($txnid) {