diff --git a/rowers/braintreestuff.py b/rowers/braintreestuff.py index 7cb162d1..df39ecd6 100644 --- a/rowers/braintreestuff.py +++ b/rowers/braintreestuff.py @@ -17,7 +17,7 @@ from rowers.tasks import ( # handle_send_email_transaction_notification, ) from rowers.utils import myqueue -import rowers.fakturoid as fakturoid +import rowers.idoklad as idoklad from braintree.exceptions.invalid_signature_error import InvalidSignatureError from braintree.exceptions.not_found_error import NotFoundError import time @@ -114,18 +114,18 @@ def send_invoice(subscription): else: r = rs[0] dologging('braintreewebhooks.log','Rower '+str(r)+'\n') - fakturoid_contact_id = fakturoid.get_contacts(r) - 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) - dologging('braintreewebhooks.log','Created Fakturoid Contact ID ' + - str(fakturoid_contact_id)+'\n') + idoklad_contact_id = idoklad.get_contacts(r) + dologging('braintreewebhooks.log','Idoklad Contact ID '+str(idoklad_contact_id)+'\n') + if not idoklad_contact_id: # pragma: no cover + idoklad_contact_id = idoklad.create_contact(r) + dologging('braintreewebhooks.log','Created Idoklad Contact ID ' + + str(idoklad_contact_id)+'\n') transactions = subscription.transactions if transactions: amount = transactions[0].amount dologging('braintreewebhooks.log','Transaction amount '+str(amount)+'\n') - id = fakturoid.create_invoice(r, amount, subscription_id, dosend=True, - contact_id=fakturoid_contact_id) + id = idoklad.create_invoice(r, amount, subscription_id, dosend=True, + contact_id=idoklad_contact_id) return id return 0 # pragma: no cover @@ -212,11 +212,13 @@ def make_payment(rower, data): l=rower.user.last_name, ) - fakturoid_contact_id = fakturoid.get_contacts(rower) - if not fakturoid_contact_id: - fakturoid_contact_id = fakturoid.create_contact(rower) - _ = fakturoid.create_invoice(rower, amount, transaction.id, dosend=True, contact_id=fakturoid_contact_id, - name=additional_text) + idoklad_contact_id = idoklad.get_contacts(rower) + if not idoklad_contact_id: + idoklad_contact_id = idoklad.create_contact(rower) + + _ = idoklad.create_invoice(rower, amount, transaction.id, dosend=True, + contact_id=idoklad_contact_id, + name=additional_text) _ = myqueue(queuehigh, handle_send_email_transaction, name, rower.user.email, amount) diff --git a/rowers/idoklad.py b/rowers/idoklad.py index 1da50022..2f74ac86 100644 --- a/rowers/idoklad.py +++ b/rowers/idoklad.py @@ -77,7 +77,6 @@ def get_contacts(rower): if res.status_code != 200: # pragma: no cover return None - data = res.json()['Data']['Items'] if len(data) >= 1: @@ -141,7 +140,7 @@ def create_invoice(rower, amount, braintreeid, dosend=True, contact_id=None, nam token = idoklad_token() if token is None: - return None + return 0 dologging('idoklad.log','Creating idoklad invoice for '+str(rower.user.email)+'\n') @@ -177,26 +176,29 @@ def create_invoice(rower, amount, braintreeid, dosend=True, contact_id=None, nam id = res.json()['Data']['Id'] if dosend: - url = invoice_url+'/'+str(id)+'/Send' - print(url) data = { - 'AttachmentIds': [id], - 'DocumentId': braintreeid, + 'AttachmentIds': [], + 'DocumentId': id, 'EmailBody': 'Dear customer, we are sending you the invoice for your subscription. Please do not hesitate to contact us if you have any questions. Best regards, Rowsandall Team', 'EmailSubject': 'Rowsandall Subscription Invoice', 'Method': 1, 'ReportLanguage': 3, - 'SendToSelf': True, + 'SendToSelf': True, + 'SendToPartner': True, + 'SendToAccountant': False, } - print(data) - print(email_url) + headers = { + 'Authorization': 'Bearer {access_token}'.format(access_token=token.access_token), + 'Content-Type': 'application/json', + 'Accept': 'application/json', + } res = requests.post(email_url, json=data, headers=headers) dologging('idoklad.log','Invoice Sent - status code '+str(res.status_code)+'\n') if res.status_code not in [200, 201]: - dologging('idoklad.log','Invoice Sent - reason '+str(res.reason)+'\n') + dologging('idoklad.log','Invoice Sent - reason '+str(res.text)+'\n') return id diff --git a/rowers/tests/mocks.py b/rowers/tests/mocks.py index 0942905b..512b2429 100644 --- a/rowers/tests/mocks.py +++ b/rowers/tests/mocks.py @@ -57,6 +57,14 @@ from rowers.dataprep import delete_strokedata from redis import StrictRedis redis_connection = StrictRedis() +def mocked_idoklad_token(*args, **kwargs): # pragma: no cover + class MockToken: + def __init__(self, *args,**kwargs): + self.access_token = "aap" + + return MockToken() + + def mocked_grpc(*args, **kwargs): # pragma: no cover class insecure_channel: @@ -773,6 +781,9 @@ def mocked_requests(*args, **kwargs): with open('rowers/tests/testdata/rp3_list.json','r') as infile: rp3workoutlist = json.load(infile) + with open('rowers/tests/testdata/idoklad_default.json','r') as infile: + idokladdefault = json.load(infile) + rp3linkready = {'data': {'download': {'id': 591621, 'status': 'ready', 'link': 'https://rp3rowing-app.com/api/workouts/591621/download?type=csv'}}} with open('rowers/tests/testdata/example-session-strokes-with-impeller-data.json','r') as infile: @@ -1117,6 +1128,7 @@ def mocked_requests(*args, **kwargs): rp3tester = re.compile(r'.*?rp3rowing-app\.com') garmintester = re.compile(r'.*?garmin\.com') fakturoidtester = re.compile(r'.*?fakturoid\.cz') + idokladtester = re.compile(r'.*?idoklad\.cz') polarlistregex = r'.*?polaraccesslink\.com\/.*\/(\d+)$' polarlisttester = re.compile(polarlistregex) @@ -1487,6 +1499,43 @@ def mocked_requests(*args, **kwargs): else: # pragma: no cover return MockResponse(c2workoutdata,200) + + if idokladtester.match(args[0]): + if 'Invoices' in args[0]: + if 'Default' in args[0]: + response_data = idokladdefault + + return MockResponse(response_data,200) + + response = { + 'Data': { + 'Id': 1, + } + } + return MockResponse(response,200) + + if 'Contacts' in args[0]: + response = { + 'Data': { + 'Items': [ + { + 'Id': 1, + 'url':'aap', + } + ] + } + } + + return MockResponse(response,200) + + response = [ + { + 'Id':1, + 'url':'aap', + } + ] + return MockResponse(response, 200) + if fakturoidtester.match(args[0]): if 'invoices' in args[0]: response = { diff --git a/rowers/tests/test_braintree.py b/rowers/tests/test_braintree.py index ca540e2b..3d32d19c 100644 --- a/rowers/tests/test_braintree.py +++ b/rowers/tests/test_braintree.py @@ -74,11 +74,12 @@ class BraintreeUnits(TestCase): self.p2 = PaidPlan.objects.create(price=25,paymentprocessor='braintree') - @patch('rowers.fakturoid.requests.get',side_effect=mocked_requests) - @patch('rowers.fakturoid.requests.post',side_effect=mocked_requests) + @patch('rowers.idoklad.idoklad_token', side_effect=mocked_idoklad_token) + @patch('rowers.idoklad.requests.get',side_effect=mocked_requests) + @patch('rowers.idoklad.requests.post',side_effect=mocked_requests) @patch('rowers.braintreestuff.gateway', side_effect=MockBraintreeGateway) @patch('rowers.braintreestuff.myqueue') - def test_process_webhook(self,mock_get,mockpost,mocked_gateway,mocked_myqueue): + def test_process_webhook(self,mock_token, mock_get,mockpost,mocked_gateway,mocked_myqueue): n = notification() res = process_webhook(n) self.assertEqual(res,1) diff --git a/rowers/tests/test_payments.py b/rowers/tests/test_payments.py index 5f1cf614..0ecd7776 100644 --- a/rowers/tests/test_payments.py +++ b/rowers/tests/test_payments.py @@ -405,7 +405,7 @@ description: "" @patch('rowers.views.braintreestuff.gateway', side_effect=MockBraintreeGateway) - @patch('rowers.fakturoid.create_invoice',side_effect=mocked_invoiceid) + @patch('rowers.idoklad.create_invoice',side_effect=mocked_invoiceid) @patch('rowers.utils.myqueue') def test_purchase_trainingplan_view(self, mocked_gateway,mocked_invoiceid, mocked_myqueue): u = UserFactory() diff --git a/rowers/tests/testdata/testdata.tcx.gz b/rowers/tests/testdata/testdata.tcx.gz index 0dcea5ec..c7efdd53 100644 Binary files a/rowers/tests/testdata/testdata.tcx.gz and b/rowers/tests/testdata/testdata.tcx.gz differ