示例#1
0
def route_and_process_donation(cd_donation_form, cd_user_form, stripe_token):
    """Routes the donation to the correct payment provider, then normalizes
    its response.

    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
    """
    if cd_donation_form['payment_provider'] == 'paypal':
        response = process_paypal_payment(cd_donation_form)
        if response['result'] == 'created':
            response = {
                'message': None,
                'status': Donation.AWAITING_PAYMENT,
                'payment_id': response['payment_id'],
                'transaction_id': response['transaction_id'],
                'redirect': response['redirect'],
            }
        else:
            response = {
                'message': 'We had an error working with PayPal. Please try '
                'another payment method.',
                'status': Donation.UNKNOWN_ERROR,
                'payment_id': None,
                'redirect': None,
            }
    elif cd_donation_form['payment_provider'] == 'cc':
        response = process_stripe_payment(cd_donation_form, cd_user_form,
                                          stripe_token)
    else:
        response = None
    return response
示例#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)
    else:
        raise PaymentFailureException("Unknown/unhandled payment provider.")

    return response, customer
示例#3
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
示例#4
0
def route_and_process_donation(cd_donation_form, cd_user_form, kwargs):
    """Routes the donation to the correct payment provider, then normalizes
    its response.

    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
    """
    if cd_donation_form['payment_provider'] == PROVIDERS.PAYPAL:
        response = process_paypal_payment(cd_donation_form)
    elif cd_donation_form['payment_provider'] == PROVIDERS.CREDIT_CARD:
        # Calculate the amount in cents
        amount = int(float(cd_donation_form['amount']) * 100)
        response = process_stripe_payment(amount, cd_user_form['email'],
                                          kwargs)
    else:
        response = None
    return response
示例#5
0
def route_and_process_donation(cd_donation_form, cd_user_form, stripe_token):
    """Routes the donation to the correct payment provider, then normalizes
    its response.

    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
    """
    if cd_donation_form['payment_provider'] == 'paypal':
        response = process_paypal_payment(cd_donation_form)
        if response['result'] == 'created':
            response = {
                'message': None,
                'status': Donation.AWAITING_PAYMENT,
                'payment_id': response['payment_id'],
                'transaction_id': response['transaction_id'],
                'redirect': response['redirect'],
            }
        else:
            response = {
                'message': 'We had an error working with PayPal. Please try '
                           'another payment method.',
                'status': Donation.UNKNOWN_ERROR,
                'payment_id': None,
                'redirect': None,
            }
    elif cd_donation_form['payment_provider'] == 'cc':
        response = process_stripe_payment(
            cd_donation_form,
            cd_user_form,
            stripe_token
        )
    else:
        response = None
    return response