Example #1
0
// I assume we're even active?
if (empty($modSettings['paid_enabled'])) {
    exit;
}
// If we have some custom people who find out about problems load them here.
$notify_users = array();
if (!empty($modSettings['paid_email_to'])) {
    foreach (explode(',', $modSettings['paid_email_to']) as $email) {
        $notify_users[] = array('email' => $email, 'name' => $txt['who_member'], 'id' => 0);
    }
}
// We need to see whether we can find the correct payment gateway,
// we'll going to go through all our gateway scripts and find out
// if they are happy with what we have.
$txnType = '';
$gatewayHandles = loadPaymentGateways();
foreach ($gatewayHandles as $gateway) {
    $gatewayClass = new $gateway['payment_class']();
    if ($gatewayClass->isValid()) {
        $txnType = $gateway['code'];
        break;
    }
}
if (empty($txnType)) {
    generateSubscriptionError($txt['paid_unknown_transaction_type']);
}
// Get the subscription and member ID amoungst others...
@(list($subscription_id, $member_id) = $gatewayClass->precheck());
// Integer these just in case.
$subscription_id = (int) $subscription_id;
$member_id = (int) $member_id;
function subscriptions($memID)
{
    global $context, $txt, $sourcedir, $modSettings, $smcFunc, $scripturl;
    // Load the paid template anyway.
    loadTemplate('ManagePaid');
    loadLanguage('ManagePaid');
    // Load all of the subscriptions.
    require_once $sourcedir . '/ManagePaid.php';
    loadSubscriptions();
    $context['member']['id'] = $memID;
    // Remove any invalid ones.
    foreach ($context['subscriptions'] as $id => $sub) {
        // Work out the costs.
        $costs = @unserialize($sub['real_cost']);
        $cost_array = array();
        if ($sub['real_length'] == 'F') {
            foreach ($costs as $duration => $cost) {
                if ($cost != 0) {
                    $cost_array[$duration] = $cost;
                }
            }
        } else {
            $cost_array['fixed'] = $costs['fixed'];
        }
        if (empty($cost_array)) {
            unset($context['subscriptions'][$id]);
        } else {
            $context['subscriptions'][$id]['member'] = 0;
            $context['subscriptions'][$id]['subscribed'] = false;
            $context['subscriptions'][$id]['costs'] = $cost_array;
        }
    }
    // Work out what gateways are enabled.
    $gateways = loadPaymentGateways();
    foreach ($gateways as $id => $gateway) {
        $gateways[$id] = new $gateway['display_class']();
        if (!$gateways[$id]->gatewayEnabled()) {
            unset($gateways[$id]);
        }
    }
    // No gateways yet?
    if (empty($gateways)) {
        fatal_error($txt['paid_admin_not_setup_gateway']);
    }
    // Get the current subscriptions.
    $request = $smcFunc['db_query']('', '
		SELECT id_sublog, id_subscribe, start_time, end_time, status, payments_pending, pending_details
		FROM {db_prefix}log_subscribed
		WHERE id_member = {int:selected_member}', array('selected_member' => $memID));
    $context['current'] = array();
    while ($row = $smcFunc['db_fetch_assoc']($request)) {
        // The subscription must exist!
        if (!isset($context['subscriptions'][$row['id_subscribe']])) {
            continue;
        }
        $context['current'][$row['id_subscribe']] = array('id' => $row['id_sublog'], 'sub_id' => $row['id_subscribe'], 'hide' => $row['status'] == 0 && $row['end_time'] == 0 && $row['payments_pending'] == 0, 'name' => $context['subscriptions'][$row['id_subscribe']]['name'], 'start' => timeformat($row['start_time'], false), 'end' => $row['end_time'] == 0 ? $txt['not_applicable'] : timeformat($row['end_time'], false), 'pending_details' => $row['pending_details'], 'status' => $row['status'], 'status_text' => $row['status'] == 0 ? $row['payments_pending'] ? $txt['paid_pending'] : $txt['paid_finished'] : $txt['paid_active']);
        if ($row['status'] == 1) {
            $context['subscriptions'][$row['id_subscribe']]['subscribed'] = true;
        }
    }
    $smcFunc['db_free_result']($request);
    // Simple "done"?
    if (isset($_GET['done'])) {
        $_GET['sub_id'] = (int) $_GET['sub_id'];
        // Must exist but let's be sure...
        if (isset($context['current'][$_GET['sub_id']])) {
            // What are the details like?
            $current_pending = @unserialize($context['current'][$_GET['sub_id']]['pending_details']);
            if (!empty($current_pending)) {
                $current_pending = array_reverse($current_pending);
                foreach ($current_pending as $id => $sub) {
                    // Just find one and change it.
                    if ($sub[0] == $_GET['sub_id'] && $sub[3] == 'prepay') {
                        $current_pending[$id][3] = 'payback';
                        break;
                    }
                }
                // Save the details back.
                $pending_details = serialize($current_pending);
                $smcFunc['db_query']('', '
					UPDATE {db_prefix}log_subscribed
					SET payments_pending = payments_pending + 1, pending_details = {string:pending_details}
					WHERE id_sublog = {int:current_subscription_id}
						AND id_member = {int:selected_member}', array('current_subscription_id' => $context['current'][$_GET['sub_id']]['id'], 'selected_member' => $memID, 'pending_details' => $pending_details));
            }
        }
        $context['sub_template'] = 'paid_done';
        return;
    }
    // If this is confirmation then it's simpler...
    if (isset($_GET['confirm']) && isset($_POST['sub_id']) && is_array($_POST['sub_id'])) {
        // Hopefully just one.
        foreach ($_POST['sub_id'] as $k => $v) {
            $ID_SUB = (int) $k;
        }
        if (!isset($context['subscriptions'][$ID_SUB]) || $context['subscriptions'][$ID_SUB]['active'] == 0) {
            fatal_lang_error('paid_sub_not_active');
        }
        // Simplify...
        $context['sub'] = $context['subscriptions'][$ID_SUB];
        $period = 'xx';
        if ($context['sub']['flexible']) {
            $period = isset($_POST['cur'][$ID_SUB]) && isset($context['sub']['costs'][$_POST['cur'][$ID_SUB]]) ? $_POST['cur'][$ID_SUB] : 'xx';
        }
        // Check we have a valid cost.
        if ($context['sub']['flexible'] && $period == 'xx') {
            fatal_lang_error('paid_sub_not_active');
        }
        // Sort out the cost/currency.
        $context['currency'] = $modSettings['paid_currency_code'];
        $context['recur'] = $context['sub']['repeatable'];
        if ($context['sub']['flexible']) {
            // Real cost...
            $context['value'] = $context['sub']['costs'][$_POST['cur'][$ID_SUB]];
            $context['cost'] = sprintf($modSettings['paid_currency_symbol'], $context['value']) . '/' . $txt[$_POST['cur'][$ID_SUB]];
            // The period value for paypal.
            $context['paypal_period'] = strtoupper(substr($_POST['cur'][$ID_SUB], 0, 1));
        } else {
            // Real cost...
            $context['value'] = $context['sub']['costs']['fixed'];
            $context['cost'] = sprintf($modSettings['paid_currency_symbol'], $context['value']);
            // Recur?
            preg_match('~(\\d*)(\\w)~', $context['sub']['real_length'], $match);
            $context['paypal_unit'] = $match[1];
            $context['paypal_period'] = $match[2];
        }
        // Setup the gateway context.
        $context['gateways'] = array();
        foreach ($gateways as $id => $gateway) {
            $fields = $gateways[$id]->fetchGatewayFields($context['sub']['id'] . '+' . $memID, $context['sub'], $context['value'], $period, $scripturl . '?action=profile;u=' . $memID . ';area=subscriptions;sub_id=' . $context['sub']['id'] . ';done');
            if (!empty($fields['form'])) {
                $context['gateways'][] = $fields;
            }
        }
        // Bugger?!
        if (empty($context['gateways'])) {
            fatal_error($txt['paid_admin_not_setup_gateway']);
        }
        // Now we are going to assume they want to take this out ;)
        $new_data = array($context['sub']['id'], $context['value'], $period, 'prepay');
        if (isset($context['current'][$context['sub']['id']])) {
            // What are the details like?
            $current_pending = array();
            if ($context['current'][$context['sub']['id']]['pending_details'] != '') {
                $current_pending = @unserialize($context['current'][$context['sub']['id']]['pending_details']);
            }
            // Don't get silly.
            if (count($current_pending) > 9) {
                $current_pending = array();
            }
            $pending_count = 0;
            // Only record real pending payments as will otherwise confuse the admin!
            foreach ($current_pending as $pending) {
                if ($pending[3] == 'payback') {
                    $pending_count++;
                }
            }
            if (!in_array($new_data, $current_pending)) {
                $current_pending[] = $new_data;
                $pending_details = serialize($current_pending);
                $smcFunc['db_query']('', '
					UPDATE {db_prefix}log_subscribed
					SET payments_pending = {int:pending_count}, pending_details = {string:pending_details}
					WHERE id_sublog = {int:current_subscription_item}
						AND id_member = {int:selected_member}', array('pending_count' => $pending_count, 'current_subscription_item' => $context['current'][$context['sub']['id']]['id'], 'selected_member' => $memID, 'pending_details' => $pending_details));
            }
        } else {
            $pending_details = serialize(array($new_data));
            $smcFunc['db_insert']('', '{db_prefix}log_subscribed', array('id_subscribe' => 'int', 'id_member' => 'int', 'status' => 'int', 'payments_pending' => 'int', 'pending_details' => 'string-65534', 'start_time' => 'int', 'vendor_ref' => 'string-255'), array($context['sub']['id'], $memID, 0, 0, $pending_details, time(), ''), array('id_sublog'));
        }
        // Change the template.
        $context['sub_template'] = 'choose_payment';
        // Quit.
        return;
    } else {
        $context['sub_template'] = 'user_subscription';
    }
}
Example #3
0
    /**
     * Set any setting related to paid subscriptions,
     *
     * - i.e. modify which payment methods are to be used.
     * - It requires the moderate_forum permission
     * - Accessed from ?action=admin;area=paidsubscribe;sa=settings.
     */
    public function action_paidSettings_display()
    {
        global $context, $txt, $scripturl;
        require_once SUBSDIR . '/PaidSubscriptions.subs.php';
        // Initialize the form
        $this->_init_paidSettingsForm();
        $config_vars = $this->_paidSettings->settings();
        // Now load all the other gateway settings.
        $gateways = loadPaymentGateways();
        foreach ($gateways as $gateway) {
            $gatewayClass = new $gateway['display_class']();
            $setting_data = $gatewayClass->getGatewaySettings();
            if (!empty($setting_data)) {
                $config_vars[] = array('title', $gatewayClass->title, 'text_label' => isset($txt['paidsubs_gateway_title_' . $gatewayClass->title]) ? $txt['paidsubs_gateway_title_' . $gatewayClass->title] : $gatewayClass->title);
                $config_vars = array_merge($config_vars, $setting_data);
            }
        }
        // Some important context stuff
        $context['page_title'] = $txt['settings'];
        $context['sub_template'] = 'show_settings';
        $context['settings_message'] = replaceBasicActionUrl($txt['paid_note']);
        $context[$context['admin_menu_name']]['current_subsection'] = 'settings';
        // Get the final touches in place.
        $context['post_url'] = $scripturl . '?action=admin;area=paidsubscribe;save;sa=settings';
        $context['settings_title'] = $txt['settings'];
        // We want javascript for our currency options.
        addInlineJavascript('
		toggleCurrencyOther();', true);
        // Saving the settings?
        if (isset($_GET['save'])) {
            checkSession();
            call_integration_hook('integrate_save_subscription_settings');
            // Check that the entered email addresses are valid
            if (!empty($_POST['paid_email_to'])) {
                require_once SUBSDIR . '/DataValidator.class.php';
                $validator = new Data_Validator();
                // Some cleaning and some rules
                $validator->sanitation_rules(array('paid_email_to' => 'trim'));
                $validator->validation_rules(array('paid_email_to' => 'valid_email'));
                $validator->input_processing(array('paid_email_to' => 'csv'));
                $validator->text_replacements(array('paid_email_to' => $txt['paid_email_to']));
                if ($validator->validate($_POST)) {
                    $_POST['paid_email_to'] = $validator->paid_email_to;
                } else {
                    // Thats not an email, lets set it back in the form to be fixed and let them know its wrong
                    $config_vars[1]['value'] = $_POST['paid_email_to'];
                    $context['error_type'] = 'minor';
                    $context['settings_message'] = array();
                    foreach ($validator->validation_errors() as $id => $error) {
                        $context['settings_message'][] = $error;
                    }
                }
            }
            // No errors, then save away
            if (empty($context['error_type'])) {
                // Sort out the currency stuff.
                if ($_POST['paid_currency'] != 'other') {
                    $_POST['paid_currency_code'] = $_POST['paid_currency'];
                    $_POST['paid_currency_symbol'] = $txt[$_POST['paid_currency'] . '_symbol'];
                }
                $_POST['paid_currency_code'] = trim($_POST['paid_currency_code']);
                unset($config_vars['dummy_currency']);
                Settings_Form::save_db($config_vars);
                redirectexit('action=admin;area=paidsubscribe;sa=settings');
            }
        }
        // Prepare the settings...
        Settings_Form::prepare_db($config_vars);
    }
function ModifySubscriptionSettings($return_config = false)
{
    global $context, $txt, $modSettings, $sourcedir, $smcFunc, $scripturl;
    // If the currency is set to something different then we need to set it to other for this to work and set it back shortly.
    $modSettings['paid_currency'] = !empty($modSettings['paid_currency_code']) ? $modSettings['paid_currency_code'] : '';
    if (!empty($modSettings['paid_currency_code']) && !in_array($modSettings['paid_currency_code'], array('usd', 'eur', 'gbp'))) {
        $modSettings['paid_currency'] = 'other';
    }
    // These are all the default settings.
    $config_vars = array(array('select', 'paid_email', array(0 => $txt['paid_email_no'], 1 => $txt['paid_email_error'], 2 => $txt['paid_email_all']), 'subtext' => $txt['paid_email_desc']), array('text', 'paid_email_to', 'subtext' => $txt['paid_email_to_desc'], 'size' => 60), '', 'dummy_currency' => array('select', 'paid_currency', array('usd' => $txt['usd'], 'eur' => $txt['eur'], 'gbp' => $txt['gbp'], 'other' => $txt['other']), 'javascript' => 'onchange="toggleOther();"'), array('text', 'paid_currency_code', 'subtext' => $txt['paid_currency_code_desc'], 'size' => 5, 'force_div_id' => 'custom_currency_code_div'), array('text', 'paid_currency_symbol', 'subtext' => $txt['paid_currency_symbol_desc'], 'size' => 8, 'force_div_id' => 'custom_currency_symbol_div'), array('check', 'paidsubs_test', 'subtext' => $txt['paidsubs_test_desc'], 'onclick' => 'return document.getElementById(\'paidsubs_test\').checked ? confirm(\'' . $txt['paidsubs_test_confirm'] . '\') : true;'));
    // Now load all the other gateway settings.
    $gateways = loadPaymentGateways();
    foreach ($gateways as $gateway) {
        $gatewayClass = new $gateway['display_class']();
        $setting_data = $gatewayClass->getGatewaySettings();
        if (!empty($setting_data)) {
            $config_vars[] = array('title', $gatewayClass->title, 'text_label' => isset($txt['paidsubs_gateway_title_' . $gatewayClass->title]) ? $txt['paidsubs_gateway_title_' . $gatewayClass->title] : $gatewayClass->title);
            $config_vars = array_merge($config_vars, $setting_data);
        }
    }
    // Just searching?
    if ($return_config) {
        return $config_vars;
    }
    // Get the settings template fired up.
    require_once $sourcedir . '/ManageServer.php';
    // Some important context stuff
    $context['page_title'] = $txt['settings'];
    $context['sub_template'] = 'show_settings';
    $context['settings_message'] = '<span class="smalltext">' . $txt['paid_note'] . '</span>';
    $context[$context['admin_menu_name']]['current_subsection'] = 'settings';
    // Get the final touches in place.
    $context['post_url'] = $scripturl . '?action=admin;area=paidsubscribe;save;sa=settings';
    $context['settings_title'] = $txt['settings'];
    // We want javascript for our currency options.
    $context['settings_insert_below'] = '
		<script type="text/javascript"><!-- // --><![CDATA[
			function toggleOther()
			{
				var otherOn = document.getElementById("paid_currency").value == \'other\';
				var currencydd = document.getElementById("custom_currency_code_div_dd");

				if (otherOn)
				{
					document.getElementById("custom_currency_code_div").style.display = "";
					document.getElementById("custom_currency_symbol_div").style.display = "";

					if (currencydd)
					{
						document.getElementById("custom_currency_code_div_dd").style.display = "";
						document.getElementById("custom_currency_symbol_div_dd").style.display = "";
					}
				}
				else
				{
					document.getElementById("custom_currency_code_div").style.display = "none";
					document.getElementById("custom_currency_symbol_div").style.display = "none";

					if (currencydd)
					{
						document.getElementById("custom_currency_symbol_div_dd").style.display = "none";
						document.getElementById("custom_currency_code_div_dd").style.display = "none";
					}
				}
			}
			toggleOther();
		// ]]></script>';
    // Saving the settings?
    if (isset($_GET['save'])) {
        checkSession();
        // Sort out the currency stuff.
        if ($_POST['paid_currency'] != 'other') {
            $_POST['paid_currency_code'] = $_POST['paid_currency'];
            $_POST['paid_currency_symbol'] = $txt[$_POST['paid_currency'] . '_symbol'];
        }
        unset($config_vars['dummy_currency']);
        saveDBSettings($config_vars);
        redirectexit('action=admin;area=paidsubscribe;sa=settings');
    }
    // Prepare the settings...
    prepareDBSettingContext($config_vars);
}
 /**
  * Method for doing all the paid subscription stuff - kinda.
  */
 public function action_subscriptions()
 {
     global $context, $txt;
     // Load the paid template anyway.
     loadTemplate('ManagePaid');
     loadLanguage('ManagePaid');
     $memID = currentMemberID();
     $context['member']['id'] = $memID;
     // Load all of the subscriptions in the system
     require_once SUBSDIR . '/PaidSubscriptions.subs.php';
     loadSubscriptions();
     // Remove any invalid ones, ones not properly set up
     foreach ($context['subscriptions'] as $id => $sub) {
         // Work out the costs.
         $costs = @unserialize($sub['real_cost']);
         $cost_array = array();
         // Flexible cost to time?
         if ($sub['real_length'] == 'F') {
             foreach ($costs as $duration => $cost) {
                 if ($cost != 0) {
                     $cost_array[$duration] = $cost;
                 }
             }
         } else {
             $cost_array['fixed'] = $costs['fixed'];
         }
         // No cost associated with it, then drop it
         if (empty($cost_array)) {
             unset($context['subscriptions'][$id]);
         } else {
             $context['subscriptions'][$id]['member'] = 0;
             $context['subscriptions'][$id]['subscribed'] = false;
             $context['subscriptions'][$id]['costs'] = $cost_array;
         }
     }
     // Work out what payment gateways are enabled.
     $gateways = loadPaymentGateways();
     foreach ($gateways as $id => $gateway) {
         $gateways[$id] = new $gateway['display_class']();
         if (!$gateways[$id]->gatewayEnabled()) {
             unset($gateways[$id]);
         }
     }
     // No gateways yet, no way to pay then!
     if (empty($gateways)) {
         fatal_error($txt['paid_admin_not_setup_gateway']);
     }
     // Get the members current subscriptions.
     $context['current'] = loadMemberSubscriptions($memID, $context['subscriptions']);
     // Find the active subscribed ones
     foreach ($context['current'] as $id => $current) {
         if ($current['status'] == 1) {
             $context['subscriptions'][$id]['subscribed'] = true;
         }
     }
     // Simple "done"?
     if (isset($_GET['done'])) {
         $this->_orderDone($memID);
     } elseif (isset($_GET['confirm']) && isset($_POST['sub_id']) && is_array($_POST['sub_id'])) {
         $this->_confirmOrder($gateways, $memID);
     } else {
         $context['sub_template'] = 'user_subscription';
     }
 }