From 423e8c920c0c28a66beeb647dbe77585fc73aa8e Mon Sep 17 00:00:00 2001 From: Sander Roosendaal Date: Wed, 3 Feb 2021 08:29:13 +0100 Subject: [PATCH] webhook tested locally --- rowers/braintreestuff.py | 39 ++++++++++++++++++++------------------- rowers/fakturoid.py | 13 ++++++++----- 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/rowers/braintreestuff.py b/rowers/braintreestuff.py index 67b9af75..6c9662ad 100644 --- a/rowers/braintreestuff.py +++ b/rowers/braintreestuff.py @@ -14,6 +14,7 @@ queuelow = django_rq.get_queue('low') queuehigh = django_rq.get_queue('low') from braintree.exceptions.invalid_signature_error import InvalidSignatureError +import rowers.fakturoid as fakturoid from rowers.utils import myqueue from rowers.tasks import ( @@ -55,27 +56,29 @@ from rowers.models import Rower,PaidPlan, CoachingGroup from rowers.utils import ProcessorCustomerError def process_webhook(notification): - print(notification.kind) if notification.kind == 'subscription_charged_successfully': - subscription_id = notification.subscription.id - print(subscription_id) - rs = Rower.objects.filter(subscription_id=subscription_id) - if rs.count() == 0: - return 0 - else: - r = rs[0] - print(r) - fakturoid_contact_id = fakturoid.get_contacts(r) - if not fakturoid_contact_id: - fakturoid_contact_id = fakturoid.create_contact(rower) - transactions = result.subscription.transactions - if transactions: - amount = transactions[0].amount - fakturoid.create_invoice(r,amount) - return 1 + return send_invoice(notification.subscription) + return 0 + +def send_invoice(subscription): + subscription_id = subscription.id + rs = Rower.objects.filter(subscription_id=subscription_id) + if rs.count() == 0: + return 0 + else: + r = rs[0] + fakturoid_contact_id = fakturoid.get_contacts(r) + if not fakturoid_contact_id: + fakturoid_contact_id = fakturoid.create_contact(r) + transactions = subscription.transactions + if transactions: + amount = transactions[0].amount + id = fakturoid.create_invoice(r,amount,subscription_id,dosend=True) + return id return 0 + def webhook(request): try: webhook_notification = gateway.webhook_notification.parse( @@ -84,8 +87,6 @@ def webhook(request): except InvalidSignatureError: return 4 - print(webhook_notification.kind) - result = process_webhook(webhook_notification) diff --git a/rowers/fakturoid.py b/rowers/fakturoid.py index 705fed74..b80be655 100644 --- a/rowers/fakturoid.py +++ b/rowers/fakturoid.py @@ -28,10 +28,11 @@ def get_contacts(rower): if res.status_code != 200: return None - if len(res.json())==1: + if len(res.json())>=1: r = res.json()[0] return r['id'] + return None # this should be triggered on braintree payment @@ -54,6 +55,7 @@ def create_contact(rower): "web": "" } + res = requests.post(contacts_url, data=json.dumps(post_data), auth=auth,headers=headers) if res.status_code not in [200,201]: @@ -62,7 +64,7 @@ def create_contact(rower): return res.json()['id'] # this should be triggered by a Braintree webhook -def create_invoice(rower,amount,braintreeid,send=False): +def create_invoice(rower,amount,braintreeid,dosend=True): r_id = get_contacts(rower) @@ -75,17 +77,18 @@ def create_invoice(rower,amount,braintreeid,send=False): 'language':'en', 'payment_method': 'card', 'currency': 'EUR', - 'paid_amount': amount, + 'paid_amount': str(amount), 'status': 'paid', 'lines': [{ 'name': 'Rowsandall Subscription', 'quantity': '1', - 'unit_price': amount, + 'unit_price': str(amount), 'vat_rate': 0, } ] } + res = requests.post(invoices_url, data=json.dumps(post_data), auth=auth,headers=headers) if res.status_code not in [200,201]: @@ -107,7 +110,7 @@ def create_invoice(rower,amount,braintreeid,send=False): if res.status_code not in [200,201]: return 0 - if send: + if dosend: res = requests.post(urlsend,auth=auth,headers=headers) return id