summaryrefslogtreecommitdiff
path: root/postgresqleu/transferwise/api.py
blob: 571c31b203d6c0b2dd056933bf05f966cc10a11f (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
from django.core.serializers.json import DjangoJSONEncoder
from django.conf import settings

import requests
from datetime import datetime, timedelta
from decimal import Decimal
import json
import re
import uuid

from .models import TransferwiseRefund


class TransferwiseApi(object):
    def __init__(self, pm):
        self.pm = pm
        self.session = requests.session()
        self.session.headers.update({
            'Authorization': 'Bearer {}'.format(self.pm.config('apikey')),
        })

        self.profile = self.account = None

    def format_date(self, dt):
        return dt.strftime('%Y-%m-%dT00:00:00.000Z')

    def parse_datetime(self, s):
        return datetime.strptime(s, '%Y-%m-%dT%H:%M:%S.%fZ')

    def _get(self, suburl, params=None, stream=False):
        r = self.session.get(
            'https://api.transferwise.com/v1/{}'.format(suburl),
            params=params,
            stream=stream,
        )
        if r.status_code != 200:
            r.raise_for_status()
        return r

    def get(self, suburl, params=None):
        return self._get(suburl, params, False).json()

    def get_binary(self, suburl, params=None):
        r = self._get(suburl, params, True)
        r.raw.decode_content = True
        return r.raw

    def post(self, suburl, params):
        j = json.dumps(params, cls=DjangoJSONEncoder)
        r = self.session.post(
            'https://api.transferwise.com/v1/{}'.format(suburl),
            data=j,
            headers={
                'Content-Type': 'application/json',
            },
        )
        r.raise_for_status()
        return r.json()

    def get_profile(self):
        if not self.profile:
            try:
                self.profile = next((p['id'] for p in self.get('profiles') if p['type'] == 'business'))
            except Exception as e:
                raise Exception("Failed to get profile: {}".format(e))
            pass
        return self.profile

    def get_account(self):
        if not self.account:
            try:
                self.account = next((a for a in self.get('borderless-accounts', {'profileId': self.get_profile()}) if a['balances'][0]['currency'] == settings.CURRENCY_ABBREV))
            except Exception as e:
                raise Exception("Failed to get account: {}".format(e))
        return self.account

    def get_balance(self):
        for b in self.get_account()['balances']:
            if b['currency'] == settings.CURRENCY_ABBREV:
                return Decimal(b['amount']['value']).quantize(Decimal('0.01'))
        return None

    def get_transactions(self, startdate=None, enddate=None):
        if not enddate:
            enddate = (datetime.today() + timedelta(days=1)).date()

        if not startdate:
            startdate = enddate - timedelta(days=60)

        return self.get(
            'borderless-accounts/{0}/statement.json'.format(self.get_account()['id']),
            {
                'currency': settings.CURRENCY_ABBREV,
                'intervalStart': self.format_date(startdate),
                'intervalEnd': self.format_date(enddate),
            },
        )['transactions']

    def validate_iban(self, iban):
        return self.get('validators/iban?iban={}'.format(iban))['validation'] == 'success'

    def get_structured_amount(self, amount):
        if amount['currency'] != settings.CURRENCY_ABBREV:
            raise Exception("Invalid currency {} found, exepcted {}".format(amount['currency'], settings.CURRENCY_ABBREV))
        return Decimal(amount['value']).quantize(Decimal('0.01'))

    def refund_transaction(self, origtrans, refundid, refundamount, refundstr):
        if not origtrans.counterpart_valid_iban:
            raise Exception("Cannot refund transaction without valid counterpart IBAN!")

        # This is a many-step process, unfortunately complicated.
        twr = TransferwiseRefund(origtransaction=origtrans, uuid=uuid.uuid4(), refundid=refundid)

        (accid, quoteid, transferid) = self.make_transfer(origtrans.counterpart_name,
                                                          origtrans.counterpart_account,
                                                          refundamount,
                                                          refundstr,
                                                          twr.uuid,
        )
        twr.accid = accid
        twr.quoteid = quoteid
        twr.transferid = transferid
        twr.save()
        return twr.id

    def make_transfer(self, counterpart_name, counterpart_account, amount, reference, xuuid):
        # Create a recipient account
        acc = self.post(
            'accounts',
            {
                'profile': self.get_profile(),
                'currency': settings.CURRENCY_ABBREV,
                'accountHolderName': re.sub(r'\d+', '', counterpart_name.replace(',', ' ')),
                'type': 'iban',
                'details': {
                    'IBAN': counterpart_account,
                },
            }
        )
        accid = acc['id']

        # Create a quote (even though we're not doing currency exchange)
        quote = self.post(
            'quotes',
            {
                'profile': self.get_profile(),
                'source': settings.CURRENCY_ABBREV,
                'target': settings.CURRENCY_ABBREV,
                'rateType': 'FIXED',
                'targetAmount': amount,
                'type': 'BALANCE_PAYOUT',
            },
        )
        quoteid = quote['id']

        # Create the actual transfer
        transfer = self.post(
            'transfers',
            {
                'targetAccount': accid,
                'quote': quoteid,
                'customerTransactionId': str(xuuid),
                'details': {
                    'reference': reference,
                },
            },
        )
        transferid = transfer['id']

        # Fund the transfer from our account
        fund = self.post(
            'transfers/{0}/payments'.format(transferid),
            {
                'type': 'BALANCE',
            },
        )

        return (accid, quoteid, transferid)