Private
Public Access
1
0

Merge branch 'feature/fakturoid' into develop

This commit is contained in:
Sander Roosendaal
2021-02-02 20:33:21 +01:00
5 changed files with 146 additions and 0 deletions

View File

@@ -52,6 +52,17 @@ else:
from rowers.models import Rower,PaidPlan, CoachingGroup
from rowers.utils import ProcessorCustomerError
def webhook(request):
webhook_notification = gateway.webhook_notification.parse(
str(request.POST['bt_signature']),
request.POST['bt_payload'])
# Example values for webhook notification properties
print(webhook_notification.kind) # "subscription_went_past_due"
print(webhook_notification.timestamp) # "Sun Jan 1 00:00:00 UTC 2012"
return Response(status=200)
def create_customer(rower,force=False):
if not rower.customer_id or force:
result = gateway.customer.create(

115
rowers/fakturoid.py Normal file
View File

@@ -0,0 +1,115 @@
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
# 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": ""
}
res = requests.post(contacts_url, data=json.dumps(post_data), auth=auth,headers=headers)
return res
# this should be triggered by a Braintree webhook
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

View File

@@ -921,6 +921,7 @@ urlpatterns = [
re_path(r'^history/$',views.history_view,name="history_view"),
re_path(r'^history/user/(?P<userid>\d+)/data/$',views.history_view_data,name="history_view_data"),
re_path(r'^history/data/$',views.history_view_data,name="history_view_data"),
re_path(r'^braintree/$',views.braintree_webhook_view,name="braintree_webhook_view"),
]
if settings.DEBUG:

View File

@@ -5,6 +5,10 @@ from __future__ import unicode_literals
from rowers.views.statements import *
def braintree_webhook_view(request):
braintreestuff.webhook(request)
return JsonResponse(status=200)
def paidplans_view(request):
if not request.user.is_anonymous:
r = request.user.rower

View File

@@ -537,6 +537,21 @@ try:
except KeyError:
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
try:
OPAQUE_SECRET_KEY = CFG['opaque_secret_key']