first attempt at process_webhook
This commit is contained in:
@@ -13,6 +13,8 @@ queue = django_rq.get_queue('default')
|
|||||||
queuelow = django_rq.get_queue('low')
|
queuelow = django_rq.get_queue('low')
|
||||||
queuehigh = django_rq.get_queue('low')
|
queuehigh = django_rq.get_queue('low')
|
||||||
|
|
||||||
|
from braintree.exceptions.invalid_signature_error import InvalidSignatureError
|
||||||
|
|
||||||
from rowers.utils import myqueue
|
from rowers.utils import myqueue
|
||||||
from rowers.tasks import (
|
from rowers.tasks import (
|
||||||
handle_send_email_transaction,
|
handle_send_email_transaction,
|
||||||
@@ -52,17 +54,37 @@ else:
|
|||||||
from rowers.models import Rower,PaidPlan, CoachingGroup
|
from rowers.models import Rower,PaidPlan, CoachingGroup
|
||||||
from rowers.utils import ProcessorCustomerError
|
from rowers.utils import ProcessorCustomerError
|
||||||
|
|
||||||
|
def process_webhook(notification):
|
||||||
|
if notification.kind == 'subscription_charged_successfully':
|
||||||
|
subscription_id = notification.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(rower)
|
||||||
|
transactions = result.subscription.transactions
|
||||||
|
if transactions:
|
||||||
|
amount = transactions[0].amount
|
||||||
|
fakturoid.create_invoice(r,amount)
|
||||||
|
return 1
|
||||||
|
|
||||||
|
return 0
|
||||||
|
|
||||||
def webhook(request):
|
def webhook(request):
|
||||||
try:
|
try:
|
||||||
webhook_notification = gateway.webhook_notification.parse(
|
webhook_notification = gateway.webhook_notification.parse(
|
||||||
str(request.POST['bt_signature']),
|
str(request.POST['bt_signature']),
|
||||||
request.POST['bt_payload'])
|
request.POST['bt_payload'])
|
||||||
except:
|
except InvalidSignatureError:
|
||||||
print(sys.exc_info())
|
return 4
|
||||||
|
|
||||||
# Example values for webhook notification properties
|
result = process_webhook(webhook_notification)
|
||||||
print(webhook_notification.kind) # "subscription_went_past_due"
|
|
||||||
print(webhook_notification.timestamp) # "Sun Jan 1 00:00:00 UTC 2012"
|
|
||||||
|
return result
|
||||||
|
|
||||||
def create_customer(rower,force=False):
|
def create_customer(rower,force=False):
|
||||||
if not rower.customer_id or force:
|
if not rower.customer_id or force:
|
||||||
|
|||||||
@@ -56,7 +56,10 @@ def create_contact(rower):
|
|||||||
|
|
||||||
res = requests.post(contacts_url, data=json.dumps(post_data), auth=auth,headers=headers)
|
res = requests.post(contacts_url, data=json.dumps(post_data), auth=auth,headers=headers)
|
||||||
|
|
||||||
return res
|
if res.status_code not in [200,201]:
|
||||||
|
return 0
|
||||||
|
|
||||||
|
return res.json()['id']
|
||||||
|
|
||||||
# this should be triggered by a Braintree webhook
|
# this should be triggered by a Braintree webhook
|
||||||
def create_invoice(rower,amount,braintreeid,send=False):
|
def create_invoice(rower,amount,braintreeid,send=False):
|
||||||
|
|||||||
@@ -8,7 +8,10 @@ from rowers.views.statements import *
|
|||||||
@csrf_exempt
|
@csrf_exempt
|
||||||
def braintree_webhook_view(request):
|
def braintree_webhook_view(request):
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
braintreestuff.webhook(request)
|
result = braintreestuff.webhook(request)
|
||||||
|
if result == 4:
|
||||||
|
raise PermissionDenied("Not allowed")
|
||||||
|
|
||||||
return HttpResponse('')
|
return HttpResponse('')
|
||||||
|
|
||||||
def paidplans_view(request):
|
def paidplans_view(request):
|
||||||
|
|||||||
Reference in New Issue
Block a user