From fd5754e679b388d30ab44712c0d084c8c3c5b140 Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Fri, 23 Sep 2016 16:42:47 +1000 Subject: [PATCH] Allows unauthenticated payments. Links Credit Note Refunds to the Stripe Charge. --- .../migrations/0002_stripecreditnoterefund.py | 26 +++++++++++++++++++ registripe/models.py | 4 +++ registripe/urls.py | 1 + registripe/views.py | 23 ++++++++-------- 4 files changed, 43 insertions(+), 11 deletions(-) create mode 100644 registripe/migrations/0002_stripecreditnoterefund.py diff --git a/registripe/migrations/0002_stripecreditnoterefund.py b/registripe/migrations/0002_stripecreditnoterefund.py new file mode 100644 index 00000000..d10f69e8 --- /dev/null +++ b/registripe/migrations/0002_stripecreditnoterefund.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.9.2 on 2016-09-23 06:36 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('pinax_stripe', '0003_make_cvc_check_blankable'), + ('registrasion', '0005_auto_20160905_0945'), + ('registripe', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='StripeCreditNoteRefund', + fields=[ + ('creditnoterefund_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='registrasion.CreditNoteRefund')), + ('charge', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='pinax_stripe.Charge')), + ], + bases=('registrasion.creditnoterefund',), + ), + ] diff --git a/registripe/models.py b/registripe/models.py index 668558e3..6adb65a6 100644 --- a/registripe/models.py +++ b/registripe/models.py @@ -8,3 +8,7 @@ from pinax.stripe.models import Charge class StripePayment(commerce.PaymentBase): charge = models.ForeignKey(Charge) + +class StripeCreditNoteRefund(commerce.CreditNoteRefund): + + charge = models.ForeignKey(Charge) diff --git a/registripe/urls.py b/registripe/urls.py index 04a249ba..f13e3473 100644 --- a/registripe/urls.py +++ b/registripe/urls.py @@ -9,6 +9,7 @@ from pinax.stripe.views import ( urlpatterns = [ url(r"^card/([0-9]*)/$", views.card, name="registripe_card"), + url(r"^card/([0-9]*)/([0-9A-Za-z]*)/$", views.card, name="registripe_card"), url(r"^pubkey/$", views.pubkey_script, name="registripe_pubkey"), url(r"^refund/([0-9]*)/$", views.refund, name="registripe_refund"), url(r"^webhook/$", Webhook.as_view(), name="pinax_stripe_webhook"), diff --git a/registripe/views.py b/registripe/views.py index 62e78170..575559fa 100644 --- a/registripe/views.py +++ b/registripe/views.py @@ -39,12 +39,14 @@ def pubkey_script(request): return HttpResponse(script, content_type="text/javascript") -def card(request, invoice_id): +def card(request, invoice_id, access_code=None): ''' View that shows and processes a Stripe CreditCardForm to pay the given invoice. Redirects back to the invoice once the invoice is fully paid. Arguments: invoice_id (castable to str): The invoice id for the invoice to pay. + access_code (str): The optional access code for the invoice (for + unauthenticated payment) ''' @@ -52,10 +54,10 @@ def card(request, invoice_id): inv = InvoiceController.for_id_or_404(str(invoice_id)) - if not inv.can_view(user=request.user): + if not inv.can_view(user=request.user, access_code=access_code): raise Http404() - to_invoice = redirect("invoice", inv.invoice.id) + to_invoice = redirect("invoice", inv.invoice.id, access_code) if inv.invoice.balance_due() <= 0: return to_invoice @@ -92,13 +94,13 @@ def process_card(request, form, inv): conference = Conference.objects.get(id=CONFERENCE_ID) amount_to_pay = inv.invoice.balance_due() - + user = inv.invoice.user token = form.cleaned_data["stripe_token"] - customer = actions.customers.get_customer_for_user(request.user) + customer = actions.customers.get_customer_for_user(user) if not customer: - customer = actions.customers.create(request.user) + customer = actions.customers.create(user) card = actions.sources.create_card(customer, token) @@ -114,10 +116,8 @@ def process_card(request, form, inv): capture=True, ) - receipt = charge.stripe_charge.receipt_number - if not receipt: - receipt = charge.stripe_charge.id - reference = "Paid with Stripe receipt number: " + receipt + receipt = charge.stripe_charge.id + reference = "Paid with Stripe reference: " + receipt # Create the payment object models.StripePayment.objects.create( @@ -185,8 +185,9 @@ def process_refund(cn, form): refund = actions.refunds.create(charge, to_refund) - commerce.CreditNoteRefund.objects.create( + models.StripeCreditNoteRefund.objects.create( parent=cn.credit_note, + charge=charge, reference="Refunded %s to Stripe charge %s" % ( to_refund, stripe_charge_id )