Private
Public Access
1
0

webhook tested locally

This commit is contained in:
Sander Roosendaal
2021-02-03 08:29:13 +01:00
parent ce2d16af4d
commit 423e8c920c
2 changed files with 28 additions and 24 deletions

View File

@@ -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)

View File

@@ -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