Skip to content

Commit

Permalink
Repair the webhook handler
Browse files Browse the repository at this point in the history
  • Loading branch information
alissatroiano committed Jul 18, 2021
1 parent 3f89a7b commit 54d7947
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 25 deletions.
9 changes: 0 additions & 9 deletions checkout/# avatar = Profile.objects.get(avatar=pr.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ Hey {{order.user_full_name }}
This is a confirmation for your order:

Order Number: {{ order.order_number }}
Order Date: {{ order.order_date }}
Order Date: {{ order.date }}

{% for item in order.orderitems.all %}
Product: {{ item.product.title }}
{% if item.product_dimension %}
{{ item.product_dimension|upper }}
{% endif %}
Order Total: ${{ order.order_total }}
Special Promotion: ${{ order.special_promotion }}
Grand Total: ${{ order.grand_total }}

Quantity: {{ item.quantity }}

Expand All @@ -22,5 +20,7 @@ Price: {{ item.product.price }}
Grand Total: {{ item.subtotal }}
{% endfor %}

If you have any questions, feel free to contact us at {{ contact_email }}.

Your digital order is attached.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Confirmation for Hue Order Number {{ order.order_number }}
Empty file.
46 changes: 36 additions & 10 deletions checkout/webhook_handler.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
from django.http import HttpResponse
from django.core.mail import send_mail

from .models import Order, OrderItem
from shop.models import Product
from profiles.models import Profile

from django.template.loader import render_to_string
from django.conf import settings

import json
import time

Expand All @@ -19,6 +24,24 @@ def handle_event(self, event):
return HttpResponse(
content=f'Unhandled webhook received: {event["type"]}',
status=200)

def _send_confirmation_email(self, order):
"""Send the user a confirmation email"""
email = order.email
subject = render_to_string(
'checkout/confirmation_emails/confirmation_email_subject.txt',
{'order': order})
body = render_to_string(
'checkout/confirmation_emails/confirmation_email_body.txt',
{'order': order, 'contact_email': settings.DEFAULT_FROM_EMAIL})

send_mail(
subject,
body,
settings.DEFAULT_FROM_EMAIL,
[email]
)
print(email)

def handle_payment_intent_succeeded(self, event):
"""
Expand All @@ -40,17 +63,18 @@ def handle_payment_intent_succeeded(self, event):
# Save profile information if user selects
profile = None # set profile to none to allow guest checkout
username = intent.metadata.username
if username is 'guest':
if username != 'AnonymousUser':
profile = Profile.objects.get(user__username=username)
profile.default_phone_number = billing_details.phone
profile.default_street_address1 = billing_details.address_line1
profile.default_street_address2 = billing_details.address_line2
profile.default_town_or_city = billing_details.address.city
profile.default_county = billing_details.address.county
profile.default_country = billing_details.address.country
profile.default_postcode = billing_details.address.postal_code
profile.default_county = billing_details.address.state
profile.save()
if save_info:
profile.default_phone_number = billing_details.phone
profile.default_street_address1 = billing_details.address_line1
profile.default_street_address2 = billing_details.address_line2
profile.default_town_or_city = billing_details.address.city
profile.default_county = billing_details.address.county
profile.default_country = billing_details.address.country
profile.default_postcode = billing_details.address.postal_code
profile.default_county = billing_details.address.state
profile.save()

order_exists = False
attempt = 1
Expand All @@ -76,6 +100,7 @@ def handle_payment_intent_succeeded(self, event):
attempt += 1
time.sleep(1)
if order_exists:
self._send_confirmation_email(order)
return HttpResponse(
content=f'Webhook received: {event["type"]} | SUCCESS: Verified order already in database',
status=200)
Expand Down Expand Up @@ -121,6 +146,7 @@ def handle_payment_intent_succeeded(self, event):
return HttpResponse(
content=f'Webhook received: {event["type"]} | ERROR: {e}',
status=500)
self._send_confirmation_email(order)
return HttpResponse(
content=f'Webhook received: {event["type"]} | SUCCESS! Order was created in webhook!',
status=200)
Expand Down

0 comments on commit 54d7947

Please sign in to comment.