You are here

function uc_stripe_product_feature_validate in Ubercart Stripe 7

Same name and namespace in other branches
  1. 6 uc_stripe.module \uc_stripe_product_feature_validate()

Validate the product feature save.

Ensure everything is Stripe compatible. Some options are not Stripe compatible and we have stripped out the UI but we double check to ensure the user has not attempted to circumvent those measures.

1 string reference to 'uc_stripe_product_feature_validate'
uc_stripe_form_uc_recurring_product_feature_form_alter in ./uc_stripe.module
Implements hook_form_FORM_ID_alter().

File

./uc_stripe.module, line 173
A module used for processing payments with Stripe.

Code

function uc_stripe_product_feature_validate($form, &$form_state) {

  // First, ensure the user has selected a SKU.
  if ($form_state['values']['model'] == '') {
    form_set_error('model', t("You must select an Applicable SKU for payments to work with Stripe."));
    return FALSE;
  }
  if ($form_state['values']['regular_interval_unit'] == 'days' || $form_state['values']['regular_interval_unit'] == 'weeks') {
    form_set_error('regular_interval_unit', t("Stripe only allows intervals of months or years. Invalid value given."));
    return FALSE;
  }
  if ($form_state['values']['regular_interval_value'] > 1) {
    form_set_error('regular_interval_value', t("Stripe only allows intervals of 1 month or 1 year."));
    return FALSE;
  }
  $form_state['values']['regular_interval_value'] = 1;
  $form_state['values']['number_intervals'] = -1;
  $fee = $form_state['values']['fee_same_product'] ? intval(100 * floatval($form_state['values']['product_price'])) : intval(100 * floatval($form_state['values']['fee_amount']));
  if ($form_state['values']['fee_same_product']) {
    $form['fee']['fee_amount']['#value'] = $form_state['values']['product_price'];
  }

  // We want to programmatically create a Stripe subscription plan
  // for this item.
  if (!_uc_stripe_load_api()) {
    form_set_error('', t("There was a problem loading the Stripe API. Recurring feature could not be created. Please ensure the API is in sites/all/libraries/stripe."));
    return FALSE;
  }
  $node = node_load($form_state['values']['nid']);
  $product = uc_product_load($node);

  // We have to convert forward slashes to underscores because stripe
  // has a bug as confirmed by the stripe staff. Basically, no plans
  // with forward slashes in their plan id can be retrieved.
  // Once Stripe fixes things on their end, this can be removed.
  $plan_id = str_replace('/', '_', $product->model);

  // See if a plan like this already exists.
  $plan_exists = TRUE;
  try {
    $plan = Stripe_Plan::retrieve($plan_id);
  } catch (Exception $e) {
    $plan_exists = FALSE;
  }
  if ($plan_exists) {

    /**
     * Delete that plan (Note: Deleting a plan doesn't truly delete it.
     *  It does not affect current subscribers of that plan, it just means
     *  no new subscribers can be added to that plan. But that's fine because
     *  we will have a new plan created a few lines down to which new
     *  subscribers will be added. For more info, see the Stripe api:
     *    https://stripe.com/docs/api#delete_plan)
     *
     * For a high overview, think about it like this. You don't have
     *  authorization from users on a current plan to change the billing
     *  amount. So we cannot just "change" the amount of the plan. We are
     *  authorized to bill them $9.99/month cannot just edit it
     *  programmatically to say, "You are now charged $15.99/month".
     *  So Stripe requires you to "delete" then "create" which deletes
     *  that version of the plan so no new users get access, then create
     *  a new one, here with the same name.
     */
    try {
      $plan
        ->delete();
    } catch (Exception $e) {
      form_set_error('', t("There was a problem updating your plan with Stripe: %error", array(
        "%error" => $e
          ->getMessage(),
      )));
    }
  }

  // Convert trial period to days.
  $trial = _uc_stripe_convert_trial_period($form_state['values']['initial_charge_value'], $form_state['values']['initial_charge_unit']);

  // Create a new plan.
  try {
    $data = array(
      'amount' => $fee,
      'interval' => drupal_substr($form_state['values']['regular_interval_unit'], 0, -1),
      'name' => $node->title,
      'currency' => 'usd',
      'id' => $plan_id,
    );
    if ($trial > 0) {
      $data['trial_period_days'] = $trial;
    }
    Stripe_Plan::create($data);
  } catch (Exception $e) {
    form_set_error('', t("There was an error creating the plan in Stripe: %error", array(
      '%error' => $e
        ->getMessage(),
    )));
  }
}