--- payment.module.org Sat Jun 10 09:01:47 2006 +++ payment.module Wed Jul 05 23:55:44 2006 @@ -43,6 +43,34 @@ } /** + * Implementation of cron_hook() + */ +function payment_cron() { + if (!module_exist('currency_api')) { + return; + } + + $base = variable_get('payment_base', 1); + foreach (variable_get('payment_active', array('1')) as $i) { + if (variable_get('payment_code_' . $i, 'USD') == variable_get('payment_code_' . $base, 'USD')) { + variable_set('payment_exchange_rate_' . $i, '1.00'); + } + else if (variable_get('payment_dynamic_' . $i, 0)) { + $from = variable_get('payment_code_' . $base, 'USD'); + $to = variable_get('payment_code_' . $i, 'USD'); + $amt = 1; + $ret = currency_api_convert($from, $to, $amt); + if ($ret['status'] == FALSE) { + watchdog('ecommerce', t('%message', array('%message' => $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 +130,11 @@ */ function payment_ec_settings() { drupal_set_title(t('Payment settings')); + + if (!module_exist('currency_api')) { + drupal_set_message('The currency_api module is not enabled. Some functions within Price Formatting will be disable.', 'error'); + } + // Begin price format section $form['price_format'] = array( '#type' => 'fieldset', @@ -109,42 +142,148 @@ '#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.') + + for ($i = 1; $i <= 5; $i++) { + $list[$i] = t('Currency ' . $i . ' - '); + if (module_exist('currency_api')) { + $list[$i] .= currency_api_get_desc(variable_get('payment_code_' . $i, 'USD')); + } + else { + $list[$i] .= variable_get('payment_code_' . $i, 'USD'); + } + } + $form['price_format']['payment_active'] = array( + '#type' => 'select', + '#title' => t('Active Currency Format'), + '#default_value' => variable_get('payment_active', array('1')), + '#options' => $list, + '#multiple' => TRUE, + '#description' => t('Chose the currency format that you hope to active. Hold down CTRL to chose for multiple.'), ); - $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.') + + foreach (variable_get('payment_active', array('1')) as $i) { + $active[$i] = $list[$i]; + } + $form['price_format']['payment_base'] = array( + '#type' => 'select', + '#title' => t('Base Currency Format'), + '#default_value' => variable_get('payment_base', 1), + '#options' => $active, + '#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' => $active, + '#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_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_agreement_url'] = array( + '#type' => 'textfield', + '#title' => t('Payment Agreement Path'), + '#default_value' => variable_get('payment_agreement_url', '#'), + '#description' => t('The path to your payment agreement page. Display the detail of your site payment agreement within this page, e.g. customer need to pay in base currency, or currencies other than base currency are for display only. This path will be shown after the price, if display currency is not equal to base currency.'), + ); + + for ($i = 1; $i <= 5; $i++) { + $form['price_format'][$i] = array( + '#type' => 'fieldset', + '#title' => t('Currency ' . $i . ' Attributes'), + '#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?'), + ); + + $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('Enbaled')), + '#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 module currency_api and cron to be set up correctly.'); + $form['price_format'][$i]['exchange']['payment_dynamic_' . $i]['#attributes'] = array('disabled' => 'disabled'); + } + } // Begin recurring payments $form['recurring_payments'] = array( @@ -453,11 +592,23 @@ * 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', '$'); + $base = variable_get('payment_base', 1); + $display = variable_get('payment_display', 1); + $rate = variable_get('payment_exchange_rate_' . $display, '1.00'); + $symbol = variable_get('payment_symbol_' . $display, '$'); + $url = variable_get('payment_agreement_url', '#'); + + $price = number_format((float) $price * (float) $rate, + variable_get('payment_decimal_places_' . $display, 2), + variable_get('payment_decimal_' . $display, '.'), + variable_get('payment_thousands_' . $display, ',')); + + $return = (variable_get('payment_symbol_position_' . $display, 1) == 1) ? $symbol . $price : $price . $symbol; + if (variable_get('payment_code_' . $display, 'USD') != variable_get('payment_code_' . $base, 'USD')) { + $return = '' . $return . '#'; + } + + return $return; } function payment_accepted($txnid) {