'Clean form to add Stripe objects via API during validation phase
This will handle ensuring a customer and subscription exist and will
raise any issues as validation errors.  This is required because part
of Stripe\'s validation happens on the API call to establish a
subscription.'

def clean(self):

    cleaned_data = super(StripeModelForm, self).clean()
    if (not self.is_valid()):
        self.clear_card_data()
        return
    try:
        self.validate_stripe()
    except stripe.error.CardError as e:
        self.clear_card_data()
        field_lookup = {'cvc': 'cc_cvv', 'number': 'cc_number', 'expiry': 'cc_expiry', 'exp_month': 'cc_expiry', 'exp_year': 'cc_expiry'}
        error_field = field_lookup.get(e.param, None)
        self.add_error(error_field, forms.ValidationError(str(e)))
    except stripe.error.StripeError as e:
        log.error('There    was    a    problem    communicating    with    Stripe:    %s', str(e), exc_info=True)
        raise forms.ValidationError(_('There    was    a    problem    communicating    with    Stripe'))
    return cleaned_data
