diff --git a/rowers/braintreestuff.py b/rowers/braintreestuff.py index d0a0d49f..54eb336b 100644 --- a/rowers/braintreestuff.py +++ b/rowers/braintreestuff.py @@ -18,6 +18,7 @@ from rowers.tasks import ( from rowers.utils import myqueue import rowers.fakturoid as fakturoid from braintree.exceptions.invalid_signature_error import InvalidSignatureError +from braintree.exceptions.not_found_error import NotFoundError import time import sys, traceback @@ -73,8 +74,7 @@ def process_webhook(notification): subscription = notification.subscription rs = Rower.objects.filter(subscription_id=subscription.id) if rs.count() == 0: # pragma: no cover - with open('braintreewebhooks.log', 'a') as f: - f.write('Could not find rowers with subscription ID ' + + dologging('braintreewebhooks.log','Could not find rowers with subscription ID ' + subscription.id+'\n') return 0 r = rs[0] @@ -82,8 +82,7 @@ def process_webhook(notification): if result: dologging('braintreewebhooks.log','Subscription canceled {id}'.format(id=subscription.id)) return subscription.id - with open('braintreewebhooks.log', 'a') as f: # pragma: no cover - f.write('Could not cancel Subscription: ' + + dologging('braintreewebhooks.log','Could not cancel Subscription: ' + str(subscription.id)+'\n') return 0 # pragma: no cover if notification.kind == 'subscription_charged_unsuccessfully': @@ -112,21 +111,17 @@ def send_invoice(subscription): return 0 else: r = rs[0] - with open('braintreewebhooks.log', 'a') as f: - f.write('Rower '+str(r)+'\n') + dologging('braintreewebhooks.log','Rower '+str(r)+'\n') fakturoid_contact_id = fakturoid.get_contacts(r) - with open('braintreewebhooks.log', 'a') as f: - f.write('Fakturoid Contact ID '+str(fakturoid_contact_id)+'\n') + dologging('braintreewebhooks.log','Fakturoid Contact ID '+str(fakturoid_contact_id)+'\n') if not fakturoid_contact_id: # pragma: no cover fakturoid_contact_id = fakturoid.create_contact(r) - with open('braintreewebhooks.log', 'a') as f: - f.write('Created Fakturoid Contact ID ' + + dologging('braintreewebhooks.log','Created Fakturoid Contact ID ' + str(fakturoid_contact_id)+'\n') transactions = subscription.transactions if transactions: amount = transactions[0].amount - with open('braintreewebhooks.log', 'a') as f: - f.write('Transaction amount '+str(amount)+'\n') + dologging('braintreewebhooks.log','Transaction amount '+str(amount)+'\n') id = fakturoid.create_invoice(r, amount, subscription_id, dosend=True, contact_id=fakturoid_contact_id) return id @@ -261,6 +256,10 @@ def update_subscription(rower, data, method='up'): rower.subscription_id, gatewaydata ) + except NotFoundError: + rower.subscription_id = None + rower.save() + return create_subscription(rower, data) except BaseException as e: # pragma: no cover exc_type, exc_value, exc_traceback = sys.exc_info() dologging('braintree.log','Payment failed with error') diff --git a/rowers/fakturoid.py b/rowers/fakturoid.py index f025a319..116021bb 100644 --- a/rowers/fakturoid.py +++ b/rowers/fakturoid.py @@ -2,6 +2,7 @@ import requests import json from requests.auth import HTTPBasicAuth import urllib.parse +from rowers.utils import dologging from rowsandall_app.settings import ( FAKTUROID_EMAIL, FAKTUROID_API_KEY, @@ -27,19 +28,16 @@ headers = { def get_contacts(rower): res = requests.get(contacts_url, auth=auth, headers=headers) url = contacts_search_url+'?query='+urllib.parse.quote(rower.user.email) - with open('braintreewebhooks.log', 'a') as f: - f.write('Searching Contact url :'+str(url)+'\n') + dologging('braintreewebhooks.log','Searching Contact url :'+str(url)) res = requests.get(url, auth=auth, headers=headers) - with open('braintreewebhooks.log', 'a') as f: - f.write('Searching Contact Status code '+str(res.status_code)+'\n') + dologging('braintreewebhooks.log','Searching Contact Status code '+str(res.status_code)+'\n') if res.status_code != 200: # pragma: no cover return None - with open('braintreewebhooks.log', 'a') as f: - f.write('Status Code '+json.dumps(res.json())+'\n') + dologging('braintreewebhooks.log','Status Code '+json.dumps(res.json())+'\n') if len(res.json()) >= 1: r = res.json()[0] @@ -69,20 +67,17 @@ def create_contact(rower): "web": "" } - with open('braintreewebhooks.log', 'a') as f: - f.write('Creating fakturoid contact for '+str(rower.user.email)+'\n') + dologging('braintreewebhooks.log','Creating fakturoid contact for '+str(rower.user.email)+'\n') res = requests.post(contacts_url, data=json.dumps( post_data), auth=auth, headers=headers) - with open('braintreewebhooks.log', 'a') as f: - f.write('Status Code '+str(res.status_code)+'\n') + dologging('braintreewebhooks.log','Status Code '+str(res.status_code)+'\n') if res.status_code not in [200, 201]: # pragma: no cover return 0 - with open('braintreewebhooks.log', 'a') as f: - f.write('Contact ID'+str(res.json()['id'])+'\n') + dologging('braintreewebhooks.log','Contact ID'+str(res.json()['id'])+'\n') return res.json()['id'] @@ -98,8 +93,7 @@ def create_invoice(rower, amount, braintreeid, dosend=True, if not name: name = 'Rowsandall Subscription' - with open('braintreewebhooks.log', 'a') as f: - f.write('Creating invoice for contact iD '+str(contact_id)+'\n') + dologging('braintreewebhooks.log','Creating invoice for contact iD '+str(contact_id)+'\n') if not contact_id: # pragma: no cover return 0 @@ -123,8 +117,7 @@ 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') + dologging('braintreewebhooks.log','Invoice Created - status code '+str(res.status_code)+'\n') if res.status_code not in [200, 201]: # pragma: no cover return 0 @@ -141,8 +134,7 @@ 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 ' + + dologging('braintreewebhooks.log','Invoice Set to paid - status code ' + str(res.status_code)+'\n') if res.status_code not in [200, 201]: # pragma: no cover @@ -151,8 +143,7 @@ def create_invoice(rower, amount, braintreeid, dosend=True, 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') + dologging('braintreewebhooks.log','Invoice Sent - status code '+str(res.status_code)+'\n') return id diff --git a/rowers/tests/testdata/testdata.tcx.gz b/rowers/tests/testdata/testdata.tcx.gz index 9b4213f2..ce4576af 100644 Binary files a/rowers/tests/testdata/testdata.tcx.gz and b/rowers/tests/testdata/testdata.tcx.gz differ