import requests import json from requests.auth import HTTPBasicAuth import urllib.parse 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) with open('braintreewebhooks.log','a') as f: f.write('Searching Contact url :'+str(url)+'\n') 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') 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') if len(res.json())>=1: r = res.json()[0] return r['id'] return None # 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": "" } with open('braintreewebhooks.log','a') as f: f.write('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') 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') 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: contact_id = get_contacts(rower) if not name: name = 'Rowsandall Subscription' with open('braintreewebhooks.log','a') as f: f.write('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) 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]: # 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) 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]: # pragma: no cover 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)' \ # -H 'Content-Type: application/json' \ # -X POST -d '{ "subject_id": "28" }' \ # https://app.fakturoid.cz/api/v2/accounts/{slug}/invoices.json