diff --git a/rowers/braintreestuff.py b/rowers/braintreestuff.py index 6c9662ad..6fde6606 100644 --- a/rowers/braintreestuff.py +++ b/rowers/braintreestuff.py @@ -13,6 +13,8 @@ queue = django_rq.get_queue('default') queuelow = django_rq.get_queue('low') queuehigh = django_rq.get_queue('low') +import time + from braintree.exceptions.invalid_signature_error import InvalidSignatureError import rowers.fakturoid as fakturoid @@ -56,23 +58,37 @@ from rowers.models import Rower,PaidPlan, CoachingGroup from rowers.utils import ProcessorCustomerError def process_webhook(notification): + with open('braintreewebhooks.log','a') as f: + t = time.localtime() + timestamp = time.strftime('%b-%d-%Y_%H%M', t) + f.write(timestamp+' '+notification.kind+'\n') if notification.kind == 'subscription_charged_successfully': return send_invoice(notification.subscription) return 0 def send_invoice(subscription): + with open('braintreewebhooks.log','a') as f: + t = time.localtime() + timestamp = time.strftime('%b-%d-%Y_%H%M', t) + f.write('Subscription ID '+subscription.id+'\n') subscription_id = subscription.id rs = Rower.objects.filter(subscription_id=subscription_id) if rs.count() == 0: return 0 else: r = rs[0] + with open('braintreewebhooks.log','a') as f: + f.write('Rower '+str(r)+'\n') fakturoid_contact_id = fakturoid.get_contacts(r) + with open('braintreewebhooks.log','a') as f: + f.write('Fakturoid ID '+str(fakturoid_contact_id)+'\n') if not fakturoid_contact_id: fakturoid_contact_id = fakturoid.create_contact(r) transactions = subscription.transactions if transactions: amount = transactions[0].amount + with open('braintreewebhooks.log','a') as f: + f.write('Transaction amount '+str(amount)+'\n') id = fakturoid.create_invoice(r,amount,subscription_id,dosend=True) return id diff --git a/rowers/fakturoid.py b/rowers/fakturoid.py index b80be655..f50cb028 100644 --- a/rowers/fakturoid.py +++ b/rowers/fakturoid.py @@ -67,6 +67,8 @@ def create_contact(rower): def create_invoice(rower,amount,braintreeid,dosend=True): r_id = get_contacts(rower) + with open('braintreewebhooks.log','a') as f: + f.write('Creating invoice for contact iD '+str(r_id)+'\n') if not r_id: return 0 @@ -90,6 +92,8 @@ def create_invoice(rower,amount,braintreeid,dosend=True): res = requests.post(invoices_url, data=json.dumps(post_data), auth=auth,headers=headers) + with open('braintreewebhooks.log','a') as f: + f.write('Invoice Created - status code '+str(res.status_code)+'\n') if res.status_code not in [200,201]: return 0 @@ -107,12 +111,18 @@ def create_invoice(rower,amount,braintreeid,dosend=True): res = requests.post(urlpay,auth=auth,headers=headers) + with open('braintreewebhooks.log','a') as f: + f.write('Invoice Set to paid - status code '+str(res.status_code)+'\n') + if res.status_code not in [200,201]: return 0 if dosend: res = requests.post(urlsend,auth=auth,headers=headers) + with open('braintreewebhooks.log','a') as f: + f.write('Invoice Sent - status code '+str(res.status_code)+'\n') + return id #curl -u vas@email.cz:API_TOKEN -H 'User-Agent: YourApp (yourname@example.com)' \ diff --git a/rowers/views/paymentviews.py b/rowers/views/paymentviews.py index d322e325..be456413 100644 --- a/rowers/views/paymentviews.py +++ b/rowers/views/paymentviews.py @@ -7,11 +7,16 @@ from rowers.views.statements import * @csrf_exempt def braintree_webhook_view(request): + with open('braintreewebhooks.log','a') as f: + t = time.localtime() + timestamp = time.strftime('%b-%d-%Y_%H%M', t) + f.write('\n') + f.write(timestamp+' /rowers/braintree/\n') if request.method == 'POST': result = braintreestuff.webhook(request) if result == 4: raise PermissionDenied("Not allowed") - + return HttpResponse('') def paidplans_view(request):