Пример #1
0
class SubscriptionUpdateForm(forms.Form):
    paymethod = PathRelatedFormField(
        view_name='braintree:mozilla:paymethod-detail',
        queryset=BraintreePaymentMethod.objects.filter())
    subscription = PathRelatedFormField(
        view_name='braintree:mozilla:subscription-detail',
        queryset=BraintreeSubscription.objects.filter())

    def clean(self):
        solitude_subscription = self.cleaned_data.get('subscription')
        solitude_method = self.cleaned_data.get('paymethod')

        if solitude_subscription and not solitude_subscription.active:
            raise forms.ValidationError(
                'Cannot alter an inactive subscription', code='invalid')

        if solitude_method and not solitude_method.active:
            raise forms.ValidationError(
                'Cannot use an inactive payment method', code='invalid')

        return self.cleaned_data
Пример #2
0
class SubscriptionCancelForm(forms.Form):
    subscription = PathRelatedFormField(
        view_name='braintree:mozilla:subscription-detail',
        queryset=BraintreeSubscription.objects.filter())

    def clean(self):
        solitude_subscription = self.cleaned_data.get('subscription')

        if solitude_subscription and not solitude_subscription.active:
            raise forms.ValidationError(
                'Cannot cancel an inactive subscription', code='invalid')

        return self.cleaned_data
Пример #3
0
class SubscriptionForm(forms.Form):
    paymethod = PathRelatedFormField(
        view_name='braintree:mozilla:paymethod-detail',
        queryset=BraintreePaymentMethod.objects.filter())
    plan = forms.CharField(max_length=255)

    def clean_plan(self):
        data = self.cleaned_data['plan']

        try:
            obj = SellerProduct.objects.get(public_id=data)
        except ObjectDoesNotExist:
            log.info('no seller product with braintree plan id: {plan}'.format(
                plan=data))
            raise forms.ValidationError('Seller product does not exist.',
                                        code='does_not_exist')

        self.seller_product = obj
        return data

    def format_descriptor(self, name):
        # The rules for descriptor are:
        #
        # Company name/DBA section must be either 3, 7 or 12 characters and
        # the product descriptor can be up to 18, 14, or 9 characters
        # respectively (with an * in between for a total descriptor
        # name of 22 characters)
        return 'Mozilla*{}'.format(name)[0:22]

    def get_name(self, plan_id):
        if plan_id in products:
            return unicode(products.get(plan_id).description)
        log.warning('Unknown product for descriptor: {}'.format(plan_id))
        return 'Product'

    @property
    def braintree_data(self):
        plan_id = self.seller_product.public_id
        return {
            'payment_method_token': self.cleaned_data['paymethod'].provider_id,
            'plan_id': plan_id,
            'trial_period': False,
            'descriptor': {
                'name': self.format_descriptor(self.get_name(plan_id)),
                'url': 'mozilla.org'
            }
        }
Пример #4
0
class PayMethodDeleteForm(forms.Form):
    paymethod = PathRelatedFormField(
        view_name='braintree:mozilla:paymethod-detail',
        queryset=BraintreePaymentMethod.objects.filter())

    def clean(self):
        solitude_method = self.cleaned_data.get('paymethod')
        if not solitude_method:
            raise forms.ValidationError('Paymethod is required',
                                        code='required')

        # An attempt to delete an inactive payment.
        if not solitude_method.active:
            raise forms.ValidationError(
                'Cannot delete an inactive payment method', code='invalid')

        return self.cleaned_data
Пример #5
0
class SubscriptionForm(forms.Form):
    paymethod = PathRelatedFormField(
        view_name='braintree:mozilla:paymethod-detail',
        queryset=BraintreePaymentMethod.objects.filter())
    plan = forms.CharField(max_length=255)
    amount = forms.DecimalField(
        required=False,
        max_value=Decimal(settings.BRAINTREE_MAX_AMOUNT),
        min_value=Decimal(settings.BRAINTREE_MIN_AMOUNT))

    def clean(self):
        cleaned_data = super(SubscriptionForm, self).clean()
        plan = cleaned_data.get('plan')
        if plan:
            product = payments_config.products.get(plan)
            if not product:
                self.add_error(
                    'plan',
                    forms.ValidationError('No product configured for plan ID',
                                          code='no_configured_product'))
            else:
                amount = self.cleaned_data.get('amount')

                # If an amount is specified then the product's
                # configured amount (if it has one) must match.
                if amount and product.amount and product.amount != amount:
                    self.add_error(
                        'amount',
                        forms.ValidationError(
                            'The payment amount for this subscription cannot '
                            'be changed',
                            code='amount_cannot_be_changed'))

                if not amount and not product.amount:
                    self.add_error(
                        'amount',
                        forms.ValidationError(
                            'This product has no default amount so you must '
                            'define an amount',
                            code='subscription_amount_missing'))

    def clean_plan(self):
        data = self.cleaned_data['plan']
        try:
            obj = SellerProduct.objects.get(public_id=data)
        except ObjectDoesNotExist:
            log.info('no seller product with braintree plan id: {plan}'.format(
                plan=data))
            raise forms.ValidationError('Seller product does not exist.',
                                        code='does_not_exist')

        self.seller_product = obj
        return data

    def format_descriptor(self, name):
        # The rules for descriptor are:
        #
        # Company name/DBA section must be either 3, 7 or 12 characters and
        # the product descriptor can be up to 18, 14, or 9 characters
        # respectively (with an * in between for a total descriptor
        # name of 22 characters)
        return 'Mozilla*{}'.format(name)[0:22]

    def get_name(self, plan_id):
        return unicode(payments_config.products.get(plan_id).description)

    @property
    def braintree_data(self):
        plan_id = self.seller_product.public_id
        data = {
            'payment_method_token': self.cleaned_data['paymethod'].provider_id,
            'plan_id': plan_id,
            'trial_period': False,
            'descriptor': {
                'name': self.format_descriptor(self.get_name(plan_id)),
                'url': 'mozilla.org'
            }
        }
        if self.cleaned_data['amount']:
            data['price'] = str(self.cleaned_data['amount'])
        return data
Пример #6
0
class SaleForm(forms.Form):
    amount = forms.DecimalField(
        max_value=Decimal(settings.BRAINTREE_MAX_AMOUNT),
        min_value=Decimal(settings.BRAINTREE_MIN_AMOUNT))
    # This will be populated by the paymethod.
    buyer = None
    nonce = forms.CharField(max_length=255, required=False)
    paymethod = PathRelatedFormField(
        view_name='braintree:mozilla:paymethod-detail',
        queryset=BraintreePaymentMethod.objects.filter(),
        required=False,
        allow_null=True)
    # Seller and seller_product are set by looking up the
    # product_id inside payments-config.
    product_id = forms.CharField()
    seller = None
    seller_product = None

    def clean_product_id(self):
        product_id = self.cleaned_data.get('product_id')

        try:
            self.seller_product = (SellerProduct.objects.get(
                public_id=product_id))
        except SellerProduct.DoesNotExist:
            raise forms.ValidationError(
                'Product does not exist: {}'.format(product_id))

        return product_id

    def clean_paymethod(self):
        paymethod = self.cleaned_data['paymethod']
        if paymethod:
            self.buyer = paymethod.braintree_buyer.buyer
        return paymethod

    def clean(self):
        nonce = self.cleaned_data.get('nonce')
        paymethod = self.cleaned_data.get('paymethod')

        if nonce and paymethod:
            raise forms.ValidationError('Cannot set both paymethod and nonce',
                                        code='invalid')

        if not nonce and not paymethod:
            raise forms.ValidationError(
                'Either nonce or paymethod must be set', code='invalid')

        product = payments_config.products.get(
            self.cleaned_data.get('product_id'))

        if not product:
            raise forms.ValidationError('Product does not exist: {}'.format(
                self.cleaned_data.get('product_id')),
                                        code='invalid')

        amount = self.cleaned_data.get('amount')
        if (product.amount and product.amount != amount):
            self.add_error(
                'amount',
                forms.ValidationError(
                    'Product has an amount specified: {} and the amount '
                    'given differs: {}'.format(product.amount,
                                               self.cleaned_data['amount']),
                    code='invalid'))

        if product.recurrence:
            self.add_error(
                'product_id',
                forms.ValidationError('Product has a recurrence of: {}, '
                                      'use the subscription API'.format(
                                          product.recurrence),
                                      code='invalid'))

        return self.cleaned_data

    @property
    def braintree_data(self):
        data = {
            'amount': self.cleaned_data['amount'],
            'options': {
                'submit_for_settlement': True
            }
        }
        if self.cleaned_data.get('paymethod'):
            data['payment_method_token'] = (
                self.cleaned_data['paymethod'].provider_id)
        elif self.cleaned_data.get('nonce'):
            data['payment_method_nonce'] = self.cleaned_data['nonce']
        return data