Exemple #1
0
    def render_confirmation(self, request, **kwargs):
        """Renders a confirmation page before processing payment.

            If forms are invalid will return to preview view for user
            to correct errors.
        """
        # Retrive form details
        plan_cost_form = forms.SubscriptionPlanCostForm(
            request.POST, subscription_plan=self.subscription_plan)
        payment_form = self.payment_form(request.POST)

        # Validate form submission
        if all([payment_form.is_valid(), plan_cost_form.is_valid()]):
            self.confirmation = True
            context = self.get_context_data(**kwargs)

            # Forms to process payment (hidden to prevent editing)
            context['plan_cost_form'] = self.hide_form(plan_cost_form)
            context['payment_form'] = self.hide_form(payment_form)

            # Add the PlanCost instance to context for use in template
            context['plan_cost'] = plan_cost_form.cleaned_data['plan_cost']

            return self.render_to_response(context)

        # Invalid form submission - render preview again
        kwargs['error'] = True
        return self.render_preview(request, **kwargs)
    def render_confirmation(self, request, **kwargs):
        """Renders a confirmation page before processing payment.

            If forms are invalid will return to preview view for user
            to correct errors.
        """
        # Retrive form details
        plan_cost_form = forms.SubscriptionPlanCostForm(
            request.POST, subscription_plan=self.subscription_plan)
        payment_form = self.payment_form(request.POST)

        # Validate form submission
        if payment_form.is_valid() and plan_cost_form.is_valid():
            self.confirmation = True
            context = self.get_context_data(**kwargs)

            # Forms to process payment (hidden to prevent editing)
            context['plan_cost_form'] = self.hide_form(plan_cost_form)
            context['payment_form'] = self.hide_form(payment_form)

            return self.render_to_response(context)

        # Invalid form submission - render preview again
        self.confirmation = False
        context = self.get_context_data(**kwargs)
        context['plan_cost_form'] = plan_cost_form
        context['payment_form'] = payment_form

        return self.render_to_response(context)
Exemple #3
0
def test_subscription_plan_cost_form_without_plan():
    """Tests that SubscriptionPlanCostForm requires a plan."""
    try:
        forms.SubscriptionPlanCostForm()
    except KeyError:
        assert True
    else:
        assert False
Exemple #4
0
def test_subscription_plan_cost_form_clean_plan_cost_invalid_uuid():
    """Tests that clean_pan_cost returns error if instance not found."""
    plan = create_plan()
    create_cost(plan=plan)
    cost_form = forms.SubscriptionPlanCostForm({'plan_cost': str(uuid4())},
                                               subscription_plan=plan)

    assert cost_form.is_valid() is False
    assert cost_form.errors == {'plan_cost': ['Invalid plan cost submitted.']}
Exemple #5
0
def test_subscription_plan_cost_form_clean_plan_cost_value():
    """Tests that clean returns PlanCost instance."""
    plan = create_plan()
    cost = create_cost(plan=plan)
    cost_form = forms.SubscriptionPlanCostForm({'plan_cost': str(cost.id)},
                                               subscription_plan=plan)

    assert cost_form.is_valid()
    assert cost_form.cleaned_data['plan_cost'] == cost
Exemple #6
0
    def render_preview(self, request, **kwargs):
        """Renders preview of subscription and collect payment details."""
        self.confirmation = False
        context = self.get_context_data(**kwargs)

        # Forms to collect subscription details
        if 'error' in kwargs:
            plan_cost_form = forms.SubscriptionPlanCostForm(
                request.POST, subscription_plan=self.subscription_plan)
            payment_form = self.payment_form(request.POST)
        else:
            plan_cost_form = forms.SubscriptionPlanCostForm(
                initial=request.POST, subscription_plan=self.subscription_plan)
            payment_form = self.payment_form(initial=request.POST)

        context['plan_cost_form'] = plan_cost_form
        context['payment_form'] = payment_form

        return self.render_to_response(context)
Exemple #7
0
def test_subscription_plan_cost_form_with_plan():
    """Tests minimal creation of SubscriptionPlanCostForm."""
    plan = create_plan()

    try:
        forms.SubscriptionPlanCostForm(subscription_plan=plan)
    except KeyError:
        assert False
    else:
        assert True
    def render_preview(self, request, **kwargs):
        """Renders preview of subscription and collect payment details."""
        self.confirmation = False
        context = self.get_context_data(**kwargs)

        # Forms to collect subscription details
        context['plan_cost_form'] = forms.SubscriptionPlanCostForm(
            subscription_plan=self.subscription_plan)
        context['payment_form'] = self.payment_form()

        return self.render_to_response(context)
Exemple #9
0
def test_subscription_plan_cost_form_proper_widget_values():
    """Tests that widget values are properly added."""
    plan = create_plan()
    create_cost(plan, period=3, unit=models.HOUR, cost='3.00')
    create_cost(plan, period=1, unit=models.SECOND, cost='1.00')
    create_cost(plan, period=2, unit=models.MINUTE, cost='2.00')

    form = forms.SubscriptionPlanCostForm(subscription_plan=plan)
    choices = form.fields['plan_cost'].widget.choices
    assert choices[0][1] == '$1.00 per second'
    assert choices[1][1] == '$2.00 every 2 minutes'
    assert choices[2][1] == '$3.00 every 3 hours'
    def process_subscription(self, request, **kwargs):
        """Moves forward with payment & subscription processing.

            If forms are invalid will move back to confirmation page
            for user to correct errors.
        """
        # Validate payment details again incase anything changed
        plan_cost_form = forms.SubscriptionPlanCostForm(
            request.POST, subscription_plan=self.subscription_plan)
        payment_form = self.payment_form(request.POST)

        if payment_form.is_valid() and plan_cost_form.is_valid():
            # Attempt to process payment
            payment_transaction = self.process_payment(
                payment_form=payment_form,
                plan_cost_form=plan_cost_form,
            )

            if payment_transaction:
                # Payment successful - can handle subscription processing
                subscription = self.setup_subscription(
                    request.user, plan_cost_form.cleaned_data['plan_cost'])

                # Record the transaction details
                self.record_transaction(
                    subscription,
                    self.retrieve_transaction_date(payment_transaction))

                return HttpResponseRedirect(self.get_success_url())

            # Payment unsuccessful, add message for confirmation page
            messages.error(request, 'Error processing payment')

        # Invalid form submission/payment - render confirmation again
        self.confirmation = True
        context = self.get_context_data(**kwargs)
        context['plan_cost_form'] = self.hide_form(plan_cost_form)
        context['payment_form'] = self.hide_form(payment_form)

        return self.render_to_response(context)