diff --git a/rowers/fakturoid.py b/rowers/fakturoid.py deleted file mode 100644 index 116021bb..00000000 --- a/rowers/fakturoid.py +++ /dev/null @@ -1,153 +0,0 @@ -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, - FAKTUROID_SLUG -) - -slug = FAKTUROID_SLUG - -invoices_url = 'https://app.fakturoid.cz/api/v2/accounts/{slug}/invoices.json'.format( - slug=slug) -contacts_url = 'https://app.fakturoid.cz/api/v2/accounts/{slug}/subjects.json'.format( - slug=slug) -contacts_search_url = 'https://app.fakturoid.cz/api/v2/accounts/{slug}/subjects/search.json'.format( - slug=slug) - -auth = HTTPBasicAuth(FAKTUROID_EMAIL, FAKTUROID_API_KEY) -headers = { - 'Content-Type': 'application/json', - 'User-Agent': 'rowsandall (admin@rowsandall.com)' -} - - -def get_contacts(rower): - res = requests.get(contacts_url, auth=auth, headers=headers) - url = contacts_search_url+'?query='+urllib.parse.quote(rower.user.email) - dologging('braintreewebhooks.log','Searching Contact url :'+str(url)) - - res = requests.get(url, auth=auth, headers=headers) - - dologging('braintreewebhooks.log','Searching Contact Status code '+str(res.status_code)+'\n') - - if res.status_code != 200: # pragma: no cover - return None - - dologging('braintreewebhooks.log','Status Code '+json.dumps(res.json())+'\n') - - if len(res.json()) >= 1: - r = res.json()[0] - return r['id'] - - return None # pragma: no cover - -# this should be triggered on braintree payment - - -def create_contact(rower): - post_data = { - "name": str(rower), - "street": rower.street_address, - "street2": None, - "city": rower.city, - "zip": rower.postal_code, - "country": rower.country.code, - "vat_no": "", - "bank_account": "", - "iban": "", - "variable_symbol": rower.id, - "full_name": rower.user.first_name+" "+rower.user.last_name, - "email": rower.user.email, - "email_copy": "", - "phone": "", - "web": "" - } - - 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) - - dologging('braintreewebhooks.log','Status Code '+str(res.status_code)+'\n') - - if res.status_code not in [200, 201]: # pragma: no cover - return 0 - - dologging('braintreewebhooks.log','Contact ID'+str(res.json()['id'])+'\n') - - return res.json()['id'] - -# this should be triggered by a Braintree webhook - - -def create_invoice(rower, amount, braintreeid, dosend=True, - contact_id=None, name=None): - - if not contact_id: # pragma: no cover - contact_id = get_contacts(rower) - - if not name: - name = 'Rowsandall Subscription' - - dologging('braintreewebhooks.log','Creating invoice for contact iD '+str(contact_id)+'\n') - - if not contact_id: # pragma: no cover - return 0 - - post_data = { - 'subject_id': contact_id, - 'custom_id': braintreeid, - 'language': 'en', - 'payment_method': 'card', - 'currency': 'EUR', - 'paid_amount': str(amount), - 'status': 'paid', - 'lines': [{ - 'name': name, - 'quantity': '1', - 'unit_price': str(amount), - 'vat_rate': 0, - } - ] - } - - res = requests.post(invoices_url, data=json.dumps( - post_data), auth=auth, headers=headers) - 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 - - # url = res.json()['url'] - id = res.json()['id'] - - urlpay = 'https://app.fakturoid.cz/api/v2/accounts/{slug}/invoices/{id}/fire.json?event=pay'.format( - id=id, slug=slug - ) - urlsend = 'https://app.fakturoid.cz/api/v2/accounts/{slug}/invoices/{id}/fire.json?event=deliver'.format( - id=id, slug=slug - ) - - res = requests.post(urlpay, auth=auth, headers=headers) - - 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 - return 0 - - if dosend: - res = requests.post(urlsend, auth=auth, headers=headers) - - dologging('braintreewebhooks.log','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)' \ - # -H 'Content-Type: application/json' \ - # -X POST -d '{ "subject_id": "28" }' \ - # https://app.fakturoid.cz/api/v2/accounts/{slug}/invoices.json diff --git a/rowers/tests/test_braintree.py b/rowers/tests/test_braintree.py index 7d199aa0..5ba9204b 100644 --- a/rowers/tests/test_braintree.py +++ b/rowers/tests/test_braintree.py @@ -75,18 +75,6 @@ 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.braintreestuff.gateway', side_effect=MockBraintreeGateway) - @patch('rowers.braintreestuff.myqueue') - def test_process_webhook(self,mock_get,mockpost,mocked_gateway,mocked_myqueue): - n = notification() - res = process_webhook(n) - self.assertEqual(res,1) - - n = notification(kind='subscription_canceled') - res = process_webhook(n) - self.assertEqual(res,1) def test_create_customer(self): with patch('rowers.braintreestuff.gateway') as mocked_gateway: diff --git a/rowers/tests/testdata/testdata.tcx.gz b/rowers/tests/testdata/testdata.tcx.gz index 3bc0b8ef..742f06bc 100644 Binary files a/rowers/tests/testdata/testdata.tcx.gz and b/rowers/tests/testdata/testdata.tcx.gz differ