adding fakturoid basic functions
This commit is contained in:
116
rowers/fakturoid.py
Normal file
116
rowers/fakturoid.py
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
import requests
|
||||||
|
import json
|
||||||
|
from requests.auth import HTTPBasicAuth
|
||||||
|
|
||||||
|
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='+rower.user.email
|
||||||
|
|
||||||
|
res = requests.get(url, auth=auth, headers=headers)
|
||||||
|
|
||||||
|
if res.status_code != 200:
|
||||||
|
return None
|
||||||
|
|
||||||
|
if len(res.json())==1:
|
||||||
|
r = res.json()[0]
|
||||||
|
return r['id']
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
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": ""
|
||||||
|
}
|
||||||
|
|
||||||
|
res = requests.post(contacts_url, data=json.dumps(post_data), auth=auth,headers=headers)
|
||||||
|
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def create_invoice(rower,amount,braintreeid,send=False):
|
||||||
|
|
||||||
|
r_id = get_contacts(rower)
|
||||||
|
|
||||||
|
if not r_id:
|
||||||
|
return 0
|
||||||
|
|
||||||
|
post_data = {
|
||||||
|
'subject_id': r_id,
|
||||||
|
'custom_id': braintreeid,
|
||||||
|
'language':'en',
|
||||||
|
'payment_method': 'card',
|
||||||
|
'currency': 'EUR',
|
||||||
|
'paid_amount': amount,
|
||||||
|
'status': 'paid',
|
||||||
|
'lines': [{
|
||||||
|
'name': 'Rowsandall Subscription',
|
||||||
|
'quantity': '1',
|
||||||
|
'unit_price': amount,
|
||||||
|
'vat_rate': 0,
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
res = requests.post(invoices_url, data=json.dumps(post_data), auth=auth,headers=headers)
|
||||||
|
|
||||||
|
if res.status_code not in [200,201]:
|
||||||
|
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)
|
||||||
|
|
||||||
|
if res.status_code not in [200,201]:
|
||||||
|
return 0
|
||||||
|
|
||||||
|
if send:
|
||||||
|
res = requests.post(urlsend,auth=auth,headers=headers)
|
||||||
|
|
||||||
|
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
|
||||||
@@ -537,6 +537,21 @@ try:
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
PAYMENT_PROCESSING_ON = False
|
PAYMENT_PROCESSING_ON = False
|
||||||
|
|
||||||
|
try:
|
||||||
|
FAKTUROID_API_KEY = CFG['fakturoid_api_key']
|
||||||
|
except KeyError:
|
||||||
|
FAKTUROID_API_KEY = ''
|
||||||
|
|
||||||
|
try:
|
||||||
|
FAKTUROID_EMAIL = CFG['fakturoid_email']
|
||||||
|
except KeyError:
|
||||||
|
FAKTUROID_EMAIL = ''
|
||||||
|
|
||||||
|
try:
|
||||||
|
FAKTUROID_SLUG = CFG['fakturoid_slug']
|
||||||
|
except KeyError:
|
||||||
|
FAKTUROID_SLUG = ''
|
||||||
|
|
||||||
# ID obfuscation
|
# ID obfuscation
|
||||||
try:
|
try:
|
||||||
OPAQUE_SECRET_KEY = CFG['opaque_secret_key']
|
OPAQUE_SECRET_KEY = CFG['opaque_secret_key']
|
||||||
|
|||||||
Reference in New Issue
Block a user