Allows unauthenticated payments. Links Credit Note Refunds to the Stripe Charge.

This commit is contained in:
Christopher Neugebauer 2016-09-23 16:42:47 +10:00
parent 2d434432d9
commit fd5754e679
4 changed files with 43 additions and 11 deletions

View file

@ -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',),
),
]

View file

@ -8,3 +8,7 @@ from pinax.stripe.models import Charge
class StripePayment(commerce.PaymentBase): class StripePayment(commerce.PaymentBase):
charge = models.ForeignKey(Charge) charge = models.ForeignKey(Charge)
class StripeCreditNoteRefund(commerce.CreditNoteRefund):
charge = models.ForeignKey(Charge)

View file

@ -9,6 +9,7 @@ from pinax.stripe.views import (
urlpatterns = [ urlpatterns = [
url(r"^card/([0-9]*)/$", views.card, name="registripe_card"), 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"^pubkey/$", views.pubkey_script, name="registripe_pubkey"),
url(r"^refund/([0-9]*)/$", views.refund, name="registripe_refund"), url(r"^refund/([0-9]*)/$", views.refund, name="registripe_refund"),
url(r"^webhook/$", Webhook.as_view(), name="pinax_stripe_webhook"), url(r"^webhook/$", Webhook.as_view(), name="pinax_stripe_webhook"),

View file

@ -39,12 +39,14 @@ def pubkey_script(request):
return HttpResponse(script, content_type="text/javascript") 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 ''' View that shows and processes a Stripe CreditCardForm to pay the given
invoice. Redirects back to the invoice once the invoice is fully paid. invoice. Redirects back to the invoice once the invoice is fully paid.
Arguments: Arguments:
invoice_id (castable to str): The invoice id for the invoice to pay. 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)) 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() raise Http404()
to_invoice = redirect("invoice", inv.invoice.id) to_invoice = redirect("invoice", inv.invoice.id, access_code)
if inv.invoice.balance_due() <= 0: if inv.invoice.balance_due() <= 0:
return to_invoice return to_invoice
@ -92,13 +94,13 @@ def process_card(request, form, inv):
conference = Conference.objects.get(id=CONFERENCE_ID) conference = Conference.objects.get(id=CONFERENCE_ID)
amount_to_pay = inv.invoice.balance_due() amount_to_pay = inv.invoice.balance_due()
user = inv.invoice.user
token = form.cleaned_data["stripe_token"] 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: if not customer:
customer = actions.customers.create(request.user) customer = actions.customers.create(user)
card = actions.sources.create_card(customer, token) card = actions.sources.create_card(customer, token)
@ -114,10 +116,8 @@ def process_card(request, form, inv):
capture=True, capture=True,
) )
receipt = charge.stripe_charge.receipt_number receipt = charge.stripe_charge.id
if not receipt: reference = "Paid with Stripe reference: " + receipt
receipt = charge.stripe_charge.id
reference = "Paid with Stripe receipt number: " + receipt
# Create the payment object # Create the payment object
models.StripePayment.objects.create( models.StripePayment.objects.create(
@ -185,8 +185,9 @@ def process_refund(cn, form):
refund = actions.refunds.create(charge, to_refund) refund = actions.refunds.create(charge, to_refund)
commerce.CreditNoteRefund.objects.create( models.StripeCreditNoteRefund.objects.create(
parent=cn.credit_note, parent=cn.credit_note,
charge=charge,
reference="Refunded %s to Stripe charge %s" % ( reference="Refunded %s to Stripe charge %s" % (
to_refund, stripe_charge_id to_refund, stripe_charge_id
) )