Example #1
0
def payment_accounts_form(request):
    webapp = get_object_or_404(Webapp, app_slug=request.GET.get('app_slug'))
    provider = get_provider(name=request.GET.get('provider'))
    account_list_formset = forms_payments.AccountListFormSet(
        provider_data=[
            {'user': request.user, 'addon': webapp, 'provider': p}
            for p in get_providers()])
    account_list_form = next(form for form in account_list_formset.forms
                             if form.provider.name == provider.name)
    return render(request,
                  'developers/payments/includes/bango_accounts_form.html',
                  {'account_list_form': account_list_form})
def payments(request, addon_id, addon, webapp=False):
    premium_form = forms_payments.PremiumForm(request.POST or None,
                                              request=request,
                                              addon=addon,
                                              user=request.user)

    region_form = forms.RegionForm(request.POST or None,
                                   product=addon,
                                   request=request)

    upsell_form = forms_payments.UpsellForm(request.POST or None,
                                            addon=addon,
                                            user=request.user)

    providers = get_providers()

    if 'form-TOTAL_FORMS' in request.POST:
        formset_data = request.POST
    else:
        formset_data = None
    account_list_formset = forms_payments.AccountListFormSet(
        data=formset_data,
        provider_data=[{
            'addon': addon,
            'user': request.user,
            'provider': provider
        } for provider in providers])

    if request.method == 'POST':

        active_forms = [premium_form, region_form, upsell_form]
        if formset_data is not None:
            active_forms.append(account_list_formset)

        success = all(form.is_valid() for form in active_forms)

        if success:
            region_form.save()

            try:
                premium_form.save()
            except client.Error as err:
                success = False
                log.error('Error setting payment information (%s)' % err)
                messages.error(
                    request,
                    _(u'We encountered a problem connecting to the '
                      u'payment server.'))
                raise  # We want to see these exceptions!

            is_free_inapp = addon.premium_type == mkt.ADDON_FREE_INAPP
            is_now_paid = (addon.premium_type in mkt.ADDON_PREMIUMS
                           or is_free_inapp)

            # If we haven't changed to a free app, check the upsell.
            if is_now_paid and success:
                try:
                    if not is_free_inapp:
                        upsell_form.save()
                    if formset_data is not None:
                        account_list_formset.save()
                except client.Error as err:
                    log.error('Error saving payment information (%s)' % err)
                    messages.error(
                        request,
                        _(u'We encountered a problem connecting to '
                          u'the payment server.'))
                    success = False
                    raise  # We want to see all the solitude errors now.

        # If everything happened successfully, give the user a pat on the back.
        if success:
            messages.success(request, _('Changes successfully saved.'))
            return redirect(addon.get_dev_url('payments'))

    # TODO: refactor this (bug 945267)
    android_pay = waffle.flag_is_active(request, 'android-payments')
    desktop_pay = waffle.flag_is_active(request, 'desktop-payments')

    # If android payments is not allowed then firefox os must
    # be 'checked' and android-mobile and android-tablet should not be.
    invalid_paid_platform_state = []

    if not android_pay:
        # When android-payments is off...
        invalid_paid_platform_state += [('android-mobile', True),
                                        ('android-tablet', True),
                                        ('firefoxos', False)]

    if not desktop_pay:
        # When desktop-payments is off...
        invalid_paid_platform_state += [('desktop', True)]

    cannot_be_paid = (addon.premium_type == mkt.ADDON_FREE
                      and any(premium_form.device_data['free-%s' % x] == y
                              for x, y in invalid_paid_platform_state))

    try:
        tier_zero = Price.objects.get(price='0.00', active=True)
        tier_zero_id = tier_zero.pk
    except Price.DoesNotExist:
        tier_zero = None
        tier_zero_id = ''

    # Get the regions based on tier zero. This should be all the
    # regions with payments enabled.
    paid_region_ids_by_name = []
    if tier_zero:
        paid_region_ids_by_name = tier_zero.region_ids_by_name()

    platforms = PAID_PLATFORMS(request)
    paid_platform_names = [unicode(platform[1]) for platform in platforms]

    provider_regions = {}
    if tier_zero:
        provider_regions = tier_zero.provider_regions()

    return render(
        request,
        'developers/payments/premium.html',
        {
            'addon':
            addon,
            'webapp':
            webapp,
            'premium':
            addon.premium,
            'form':
            premium_form,
            'upsell_form':
            upsell_form,
            'tier_zero_id':
            tier_zero_id,
            'region_form':
            region_form,
            'PLATFORMS_NAMES':
            PLATFORMS_NAMES,
            'is_paid': (addon.premium_type in mkt.ADDON_PREMIUMS
                        or addon.premium_type == mkt.ADDON_FREE_INAPP),
            'cannot_be_paid':
            cannot_be_paid,
            'paid_platform_names':
            paid_platform_names,
            'is_packaged':
            addon.is_packaged,
            # Bango values
            'account_list_forms':
            account_list_formset.forms,
            'account_list_formset':
            account_list_formset,
            # Waffles
            'api_pricelist_url':
            reverse('price-list'),
            'payment_methods': {
                PAYMENT_METHOD_ALL: _('All'),
                PAYMENT_METHOD_CARD: _('Credit card'),
                PAYMENT_METHOD_OPERATOR: _('Carrier'),
            },
            'provider_lookup':
            dict(PROVIDER_CHOICES),
            'all_paid_region_ids_by_name':
            paid_region_ids_by_name,
            'providers':
            providers,
            'provider_regions':
            provider_regions,
            'enabled_provider_ids': [
                acct.payment_account.provider
                for acct in addon.all_payment_accounts()
            ]
        })