commit
This commit is contained in:
@@ -4,154 +4,159 @@ from requests.auth import HTTPBasicAuth
|
||||
import urllib.parse
|
||||
|
||||
from rowsandall_app.settings import (
|
||||
FAKTUROID_EMAIL, FAKTUROID_API_KEY,
|
||||
FAKTUROID_SLUG
|
||||
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)
|
||||
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)'
|
||||
'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(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)
|
||||
|
||||
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')
|
||||
|
||||
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
|
||||
|
||||
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')
|
||||
|
||||
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']
|
||||
|
||||
if len(res.json())>=1:
|
||||
r = res.json()[0]
|
||||
return r['id']
|
||||
|
||||
|
||||
return None # pragma: no cover
|
||||
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": ""
|
||||
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')
|
||||
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)
|
||||
|
||||
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')
|
||||
|
||||
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
|
||||
|
||||
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')
|
||||
|
||||
with open('braintreewebhooks.log','a') as f:
|
||||
f.write('Contact ID'+str(res.json()['id'])+'\n')
|
||||
|
||||
return res.json()['id']
|
||||
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'
|
||||
|
||||
|
||||
with open('braintreewebhooks.log','a') as f:
|
||||
f.write('Creating invoice for contact iD '+str(contact_id)+'\n')
|
||||
def create_invoice(rower, amount, braintreeid, dosend=True,
|
||||
contact_id=None, name=None):
|
||||
|
||||
if not contact_id: # pragma: no cover
|
||||
return 0
|
||||
if not contact_id: # pragma: no cover
|
||||
contact_id = get_contacts(rower)
|
||||
|
||||
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,
|
||||
}
|
||||
]
|
||||
}
|
||||
if not name:
|
||||
name = 'Rowsandall Subscription'
|
||||
|
||||
with open('braintreewebhooks.log', 'a') as f:
|
||||
f.write('Creating invoice for contact iD '+str(contact_id)+'\n')
|
||||
|
||||
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 not contact_id: # pragma: no cover
|
||||
return 0
|
||||
|
||||
if res.status_code not in [200,201]: # 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,
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
url = res.json()['url']
|
||||
id = res.json()['id']
|
||||
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')
|
||||
|
||||
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
|
||||
)
|
||||
if res.status_code not in [200, 201]: # pragma: no cover
|
||||
return 0
|
||||
|
||||
url = res.json()['url']
|
||||
id = res.json()['id']
|
||||
|
||||
res = requests.post(urlpay,auth=auth,headers=headers)
|
||||
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
|
||||
)
|
||||
|
||||
with open('braintreewebhooks.log','a') as f:
|
||||
f.write('Invoice Set to paid - status code '+str(res.status_code)+'\n')
|
||||
res = requests.post(urlpay, auth=auth, headers=headers)
|
||||
|
||||
if res.status_code not in [200,201]: # pragma: no cover
|
||||
return 0
|
||||
with open('braintreewebhooks.log', 'a') as f:
|
||||
f.write('Invoice Set to paid - status code ' +
|
||||
str(res.status_code)+'\n')
|
||||
|
||||
if dosend:
|
||||
res = requests.post(urlsend,auth=auth,headers=headers)
|
||||
if res.status_code not in [200, 201]: # pragma: no cover
|
||||
return 0
|
||||
|
||||
with open('braintreewebhooks.log','a') as f:
|
||||
f.write('Invoice Sent - status code '+str(res.status_code)+'\n')
|
||||
if dosend:
|
||||
res = requests.post(urlsend, auth=auth, headers=headers)
|
||||
|
||||
return id
|
||||
with open('braintreewebhooks.log', 'a') as f:
|
||||
f.write('Invoice Sent - status code '+str(res.status_code)+'\n')
|
||||
|
||||
#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
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user