Esempio n. 1
0
def process_payment(stripe_email, stripe_token, form):
    order = Order(
        state='S',
        from_first_name=form.cleaned_data['from_first_name'],
        from_last_name=form.cleaned_data['from_last_name'],
        from_email=stripe_email,
        stripe_tx=stripe_token,
        to_first_name=form.cleaned_data['to_first_name'],
        to_last_name=form.cleaned_data['to_last_name'],
        delivery_company_name=form.cleaned_data['delivery_company_name'],
        delivery_first=form.cleaned_data['delivery_first'],
        delivery_second=form.cleaned_data['delivery_second'],
        delivery_third=form.cleaned_data['delivery_third'],
        delivery_postcode=form.cleaned_data['delivery_postcode'],
        delivery_note=form.cleaned_data['delivery_note'],
    )

    try:
        # Create a charge: this will charge the user's card
        charge = stripe.Charge.create(amount=1600,
                                      currency='gbp',
                                      source=stripe_token,
                                      description='One succulent surprise')

        order.state = 'P'
    except stripe.error.CardError as e:
        # The card has been declined
        order.state = 'E'
    finally:
        order.save()

    return order