Example #1
0
def route_and_process_payment(
    request,
    cd_donation_form,
    cd_user_form,
    payment_provider,
    frequency,
    stripe_redirect_url,
    payment_type,
):
    """Routes the donation to the correct payment provider, then normalizes
    its response.


    :param request: The WSGI request from Django
    :param cd_donation_form: The donation form with cleaned data
    :param cd_user_form: The user form with cleaned data
    :param payment_provider: The payment provider for the payment
    :param frequency: Whether monthly or one-time payment/donation
    :param stripe_redirect_url: Where to redirect a stripe payment after
    success
    :param payment_type: Whether it's a donation or payment

    Returns a dict with:
     - message: Any error messages that apply
     - status: The status of the payment for the database
     - payment_id: The ID of the payment
    """
    customer = None
    if payment_provider == PROVIDERS.PAYPAL:
        response = process_paypal_payment(cd_donation_form)
    elif payment_provider == PROVIDERS.CREDIT_CARD:
        stripe_token = request.POST.get("stripeToken")
        stripe_args = {"metadata": {"type": payment_type}}
        if frequency == FREQUENCIES.ONCE:
            stripe_args["card"] = stripe_token
        elif frequency == FREQUENCIES.MONTHLY:
            customer = create_stripe_customer(stripe_token,
                                              cd_user_form["email"])
            stripe_args["customer"] = customer.id
            stripe_args["metadata"].update({"recurring": True})
        else:
            raise NotImplementedError("Unknown frequency value: %s" %
                                      frequency)

        if cd_donation_form["reference"]:
            stripe_args["metadata"].update(
                {"reference": cd_donation_form["reference"]})

        # Calculate the amount in cents
        amount = int(float(cd_donation_form["amount"]) * 100)
        response = process_stripe_payment(amount, cd_user_form["email"],
                                          stripe_args, stripe_redirect_url)
    else:
        raise PaymentFailureException("Unknown/unhandled payment provider.")

    return response, customer
Example #2
0
def route_and_process_payment(request, cd_donation_form, cd_user_form,
                              payment_provider, frequency,
                              stripe_redirect_url, payment_type):
    """Routes the donation to the correct payment provider, then normalizes
    its response.


    :param request: The WSGI request from Django
    :param cd_donation_form: The donation form with cleaned data
    :param cd_user_form: The user form with cleaned data
    :param payment_provider: The payment provider for the payment
    :param frequency: Whether monthly or one-time payment/donation
    :param stripe_redirect_url: Where to redirect a stripe payment after
    success
    :param payment_type: Whether it's a donation or payment

    Returns a dict with:
     - message: Any error messages that apply
     - status: The status of the payment for the database
     - payment_id: The ID of the payment
    """
    customer = None
    if payment_provider == PROVIDERS.PAYPAL:
        response = process_paypal_payment(cd_donation_form)
    elif payment_provider == PROVIDERS.CREDIT_CARD:
        stripe_token = request.POST.get('stripeToken')
        stripe_args = {'metadata': {'type': payment_type}}
        if frequency == FREQUENCIES.ONCE:
            stripe_args['card'] = stripe_token
        elif frequency == FREQUENCIES.MONTHLY:
            customer = create_stripe_customer(stripe_token,
                                              cd_user_form['email'])
            stripe_args['customer'] = customer.id
            stripe_args['metadata'].update({'recurring': True})
        else:
            raise NotImplementedError("Unknown frequency value: %s" %
                                      frequency)

        if cd_donation_form['reference']:
            stripe_args['metadata'].update(
                {'reference': cd_donation_form['reference']})

        # Calculate the amount in cents
        amount = int(float(cd_donation_form['amount']) * 100)
        response = process_stripe_payment(
            amount, cd_user_form['email'], stripe_args, stripe_redirect_url)

    return response, customer
Example #3
0
def donate(request):
    """Load the donate page or process a submitted donation.

    This page has several branches. The logic is as follows:
        if GET:
            --> Load the page
        elif POST:
            if user is anonymous:
                if email address on record as a stub account:
                    --> Use it.
                elif new email address or a non-stub account:
                    --> We cannot allow anonymous people to update real
                        accounts, or this is a new email address, so create a
                        new stub account.
            elif user is logged in:
                --> associate with account.

            We now have an account. Process the payment and associate it.
    """

    message = None
    if request.method == 'POST':
        donation_form = DonationForm(request.POST)

        if request.user.is_anonymous():
            # Either this is a new account, a stubbed one, or a user that's
            # simply not logged into their account
            try:
                stub_account = User.objects.filter(
                    profile__stub_account=True).get(
                        email__iexact=request.POST.get('email'))
            except User.DoesNotExist:
                # Either a regular account or an email address we've never
                # seen before. Create a new user from the POST data.
                stub_account = False
                user_form = UserForm(request.POST)
                profile_form = ProfileForm(request.POST)
            else:
                # We use the stub account and anonymous users even are allowed
                # to update it. This is OK, because we don't care too much
                # about the accuracy of this data. Later if/when this becomes
                # a real account, anonymous users won't be able to update this
                # information -- that's what matters.
                user_form = UserForm(request.POST, instance=stub_account)
                profile_form = ProfileForm(request.POST,
                                           instance=stub_account.profile)

        else:
            user_form = UserForm(request.POST, instance=request.user)
            profile_form = ProfileForm(request.POST,
                                       instance=request.user.profile)

        if all([
                donation_form.is_valid(),
                user_form.is_valid(),
                profile_form.is_valid()
        ]):
            # Process the data in form.cleaned_data
            cd_donation_form = donation_form.cleaned_data
            cd_user_form = user_form.cleaned_data
            cd_profile_form = profile_form.cleaned_data
            stripe_token = request.POST.get('stripeToken')
            frequency = request.POST.get('frequency')

            # Route the payment to a payment provider
            try:
                if frequency == 'once':
                    response = route_and_process_donation(
                        cd_donation_form, cd_user_form, {'card': stripe_token})
                elif frequency == 'monthly':
                    customer = create_stripe_customer(stripe_token,
                                                      cd_user_form['email'])
                    response = route_and_process_donation(
                        cd_donation_form,
                        cd_user_form,
                        {
                            'customer': customer.id,
                            'metadata': {
                                'recurring': True
                            }
                        },
                    )
            except PaymentFailureException as e:
                logger.critical("Payment failed. Message was: %s", e.message)
                message = e.message
                response = {'status': 'Failed'}
            else:
                logger.info("Payment routed with response: %s", response)

            if response['status'] == Donation.AWAITING_PAYMENT:
                if request.user.is_anonymous() and not stub_account:
                    # Create a stub account with an unusable password
                    user, profile = create_stub_account(
                        cd_user_form, cd_profile_form)
                    user.save()
                    profile.save()
                else:
                    # Logged in user or an existing stub account.
                    user = user_form.save()
                    profile_form.save()

                donation = donation_form.save(commit=False)
                donation.status = response['status']
                donation.payment_id = response['payment_id']
                # Will only work for Paypal:
                donation.transaction_id = response.get('transaction_id')
                donation.donor = user
                donation.save()

                if frequency == 'monthly':
                    add_monthly_donations(cd_donation_form, user, customer)

                return HttpResponseRedirect(response['redirect'])
    else:
        # Loading the page...
        try:
            donation_form = DonationForm(
                initial={'referrer': request.GET.get('referrer')})
            user_form = UserForm(
                initial={
                    'first_name': request.user.first_name,
                    'last_name': request.user.last_name,
                    'email': request.user.email,
                })
            up = request.user.profile
            profile_form = ProfileForm(
                initial={
                    'address1': up.address1,
                    'address2': up.address2,
                    'city': up.city,
                    'state': up.state,
                    'zip_code': up.zip_code,
                    'wants_newsletter': up.wants_newsletter
                })
        except AttributeError:
            # for anonymous users, who lack profile info
            user_form = UserForm()
            profile_form = ProfileForm()

    return render(
        request, 'donate.html', {
            'donation_form': donation_form,
            'user_form': user_form,
            'profile_form': profile_form,
            'private': False,
            'message': message,
            'stripe_public_key': settings.STRIPE_PUBLIC_KEY
        })