Skip to content

Commit

Permalink
Complete webhook handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Awoyalejohn committed Aug 5, 2022
1 parent 2b8714b commit daadf98
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@
"[python]": {
"editor.defaultFormatter": "ms-python.python"
},
"python.formatting.provider": "yapf"
"python.formatting.provider": "black"
}
6 changes: 4 additions & 2 deletions checkout/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ class OrderAdmin(admin.ModelAdmin):

readonly_fields = ('order_number', 'date',
'discount', 'order_subtotal',
'total',)
'total','original_cart',
'stripe_pid')

fields = ('order_number', 'date', 'full_name',
'email', 'phone_number', 'country',
'postcode', 'town_or_city', 'street_address1',
'street_address2', 'county', 'discount',
'order_subtotal', 'total',)
'order_subtotal', 'total', 'original_cart',
'stripe_pid')

list_display = ('order_number', 'date', 'full_name',
'order_subtotal', 'discount',
Expand Down
23 changes: 23 additions & 0 deletions checkout/migrations/0004_order_original_cart_order_stripe_pid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.0.6 on 2022-08-05 19:56

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('checkout', '0003_orderlineitem_quantity'),
]

operations = [
migrations.AddField(
model_name='order',
name='original_cart',
field=models.TextField(default=''),
),
migrations.AddField(
model_name='order',
name='stripe_pid',
field=models.CharField(default='', max_length=254),
),
]
4 changes: 2 additions & 2 deletions checkout/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class Order(models.Model):
discount = models.DecimalField(max_digits=6, decimal_places=2, null=False, default=0)
order_subtotal = models.DecimalField(max_digits=10, decimal_places=2, null=False, default=0)
total = models.DecimalField(max_digits=10, decimal_places=2, null=False, default=0)
# original_cart = models.TextField(null=False, blank=False, default='')
# stripe_pid = models.CharField(max_length=254, null=False, blank=False, default='')
original_cart = models.TextField(null=False, blank=False, default='')
stripe_pid = models.CharField(max_length=254, null=False, blank=False, default='')


def _generate_order_number(self):
Expand Down
3 changes: 2 additions & 1 deletion checkout/templates/checkout/checkout.html
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ <h2 class="section-heading">Checkout</h2>

<!-- Used to display form errors -->
<div class="mb-3 text-danger" id="card-errors" role="alert"></div>
<!-- <input type="hidden" value="{{ client_secret }}" name="client_secret"> -->
<!-- Pass the client secret to the view to get the payment intent id -->
<input type="hidden" value="{{ client_secret }}" name="client_secret">
</fieldset>

<div class="submit-button text-right mt-5 mb-2">
Expand Down
7 changes: 6 additions & 1 deletion checkout/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,12 @@ def post(self, request):
}
order_form = OrderForm(form_data)
if order_form.is_valid():
order = order_form.save()
order = order_form.save(commit=False)
pid = request.POST.get('client_secret').split('_secret')[0]
print(pid)
order.stripe_pid = pid
order.original_cart = json.dumps(cart)
order.save()
for slug, item_data in cart.items():
print(cart.items())
try:
Expand Down
82 changes: 81 additions & 1 deletion checkout/webhook_handler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
from django.http import HttpResponse

from .models import Order, OrderLineItem
from products.models import Product

import json
import time


class StripeWH_Handler:
"""Handle Stripe webhooks"""
Expand All @@ -21,8 +27,82 @@ def handle_payment_intent_succeeded(self, event):
"""
intent = event.data.object
print(intent)
pid = intent.id
cart = intent.metadata.cart
save_info = intent.metadata.save_info

billing_details = intent.charges.data[0].billing_details
shipping_details = intent.shipping
total = round(intent.charges.data[0].amount / 100, 2)

#Clean data in the shipping details
for field, value in shipping_details.address.items():
if value == "":
shipping_details.address[field] = None

order_exists = False
attempt = 1
while attempt <= 5:
try:
order = Order.objects.get(
full_name__iexact=shipping_details.name,
email__iexact=billing_details.email,
phone_number__iexact=shipping_details.phone,
country__iexact=shipping_details.address.country,
postcode__iexact=shipping_details.address.postal_code,
town_or_city__iexact=shipping_details.address.city,
street_address1__iexact=shipping_details.address.line1,
street_address2__iexact=shipping_details.address.line2,
county__iexact=shipping_details.address.state,
total=total,
original_cart=cart,
stripe_pid=pid,
)
order_exists = True
break


except Order.DoesNotExist:
attempt += 1
time.sleep(1)
if order_exists:
return HttpResponse(
content=f'Webhook received: {event["type"]} | SUCCESS: Verified order already in database',
status=200)
else:
order = None
try:
order = Order.objects.create(
full_name=shipping_details.name,
email=billing_details.email,
phone_number=shipping_details.phone,
country=shipping_details.address.country,
postcode=shipping_details.address.postal_code,
town_or_city=shipping_details.address.city,
street_address1=shipping_details.address.line1,
street_address2=shipping_details.address.line2,
county=shipping_details.address.state,
original_cart=cart,
stripe_pid=pid,
)
for slug, item_data in json.loads(cart).items():
product = Product.objects.get(slug=slug)
print(product)
order_line_item = OrderLineItem(
order=order,
product=product,
quantity=item_data
)
order_line_item.save()
except Exception as e:
if order:
order.delete()
return HttpResponse(
content=f'Webhook received: {event["type"]} | ERROR: {e}',
status=500)

return HttpResponse(
content=f'Webhook received: {event["type"]}',
content=f'Webhook received: {event["type"]} | SUCCESS: Created order in webhook',
status=200)

def handle_payment_intent_payment_failed(self, event):
Expand Down

0 comments on commit daadf98

Please sign in to comment.