summaryrefslogtreecommitdiff
path: root/postgresqleu/plaid/backendviews.py
blob: 7210926e23402b1c8ae9cd3bd42c10e058f944de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.contrib import messages
from django.conf import settings

from postgresqleu.util.auth import authenticate_backend_group
from postgresqleu.invoices.models import InvoicePaymentMethod


def _do_balance_check(request, paymentmethod, impl):
    balances = impl.get_account_balances()
    if len(balances) == 0:
        # 0 accounts can mean we just haven't updated yet, so set up a loop
        return render(request, 'plaid/check_account.html', {
        })
    if len(balances) != 1:
        messages.error(request, 'Returned {} accounts, should be 1, cannot use this connection.'.format(len(balances)))
        impl.disconnect()
        return HttpResponseRedirect("../")
    elif balances[0]['currency'] != settings.CURRENCY_ISO:
        messages.error(request, 'Currency on account {} is {}, expected {}, cannot use this connection.'.format(balances[0]['accountid'], balances[0]['currency'], settings.CURRENCY_ISO))
        impl.disconnect()
        return HttpResponseRedirect("../")
    else:
        messages.info(request, "Account {} connected.".format(balances[0]['accountid']))
        paymentmethod.config['accountid'] = balances[0]['accountid']
        paymentmethod.save(update_fields=['config', ])
        return HttpResponseRedirect('../')


def connect_to_plaid(request, paymentmethodid):
    authenticate_backend_group(request, 'Invoice managers')

    paymentmethod = get_object_or_404(InvoicePaymentMethod, pk=paymentmethodid, classname='postgresqleu.util.payment.plaid.Plaid')

    impl = paymentmethod.get_implementation()

    if request.method == 'GET' and request.GET.get('check_account', '0') == '1':
        # We're in the check account loop
        return _do_balance_check(request, paymentmethod, impl)

    if request.method == 'POST':
        paymentmethod.config['access_token'] = impl.exchange_token(request.POST['public_token'])
        if not paymentmethod.config['access_token']:
            messages.error(request, 'Could not exchange public token for permanent token.')
            return HttpResponseRedirect('../')

        return _do_balance_check(request, paymentmethod, impl)

    token = impl.get_link_token()
    if not token:
        messages.error(request, "Could not create link token")
        return HttpResponseRedirect("../")

    return render(request, 'plaid/connectaccount.html', {
        'token': token,
    })


def refresh_plaid_connect(request, paymentmethodid):
    authenticate_backend_group(request, 'Invoice managers')

    paymentmethod = get_object_or_404(InvoicePaymentMethod, pk=paymentmethodid, classname='postgresqleu.util.payment.plaid.Plaid')

    impl = paymentmethod.get_implementation()

    token = impl.get_link_token(paymentmethod.config['access_token'])
    if not token:
        messages.error(request, "Could not create link token")
        return HttpResponseRedirect("../")

    return render(request, 'plaid/reconnectaccount.html', {
        'token': token,
    })