forked from jbms/beancount-import
-
Notifications
You must be signed in to change notification settings - Fork 1
/
paypal.py
650 lines (592 loc) · 24.5 KB
/
paypal.py
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
"""Paypal.com transaction source.
Data format
===========
To use, first download Paypal data into a directory on the filesystem using the
finance_dl.paypal module.
You might have a directory structure like:
financial/
data/
paypal/
<account_id>/
<id>.json
<id>.html
Specifying the source to beancount_import
=========================================
Within your Python script for invoking beancount_import, you might use an
expression like the following to specify the Paypal source:
dict(module='beancount_import.source.paypal',
directory=os.path.join(journal_dir, 'data', 'paypal', '<account_id>'),
assets_account='Assets:Paypal',
fee_account='Expenses:Financial:Paypal:Fees',
prefix='paypal',
)
where `journal_dir` refers to the financial/ directory.
The `directory` specifies the directory containing the `.json` and `.html`
files. The `prefix` should be unique over all of your sources, and should
consist only of letters and underscores.. It is concatenated with '.' and the
transaction `id` to form a unique `link` to apply to the generated transaction
that associates it with the transaction data, and also serves as a prefix for
various metadata keys. The account in your Beancount journal corresponding to
your Paypal balance should be specified as the `assets_account`. The
`fee_account` is the Expense account for fees charged by Paypal.
Imported transaction format
===========================
Payment transaction with item details:
2016-11-27 * "eBay - abcdef (abcdef)" "Payment" ^paypal.Q7K91N1B9WI6F4RW6
Expenses:FIXME:A 25.98 USD
paypal_counterparty: "eBay - abcdef (abcdef)"
paypal_item_name: "Men's Jeans"
paypal_item_number: "123457890123"
paypal_item_quantity: 2
paypal_item_url: "http://cgi.ebay.com/ws/eBayISAPI.dll?ViewItem&item=123457890123"
paypal_merchant_category: "Retail"
Expenses:FIXME:A 2.34 USD
paypal_counterparty: "eBay - abcdef (abcdef)"
paypal_item_type: "salesTax"
paypal_merchant_category: "Retail"
Expenses:FIXME:A 13.00 USD
paypal_counterparty: "eBay - abcdef (abcdef)"
paypal_item_type: "shippingAmount"
paypal_merchant_category: "Retail"
Expenses:FIXME -41.32 USD
paypal_funding_source_description: "Chase Visa"
paypal_funding_source_institution: "VISA"
paypal_funding_source_last4: "1234"
Sending money:
2012-10-16 * "Payment Administrator" "Money Sent" ^paypal.JNNTICIP9966PELUB
Expenses:FIXME:A 12.70 USD
paypal_counterparty: "Payment Administrator"
paypal_counterparty_email: "[email protected]"
Assets:Paypal -12.70 USD
date: 2012-10-16
paypal_transaction_id: "JNNTICIP9966PELUB"
Transfer from bank to Paypal balance:
2012-10-16 * "Bank Account" "Transfer from Bank" ^paypal.RNPYPAWXEWBUUBNER
Assets:Paypal 12.70 USD
date: 2012-10-16
paypal_transaction_id: "RNPYPAWXEWBUUBNER"
Expenses:FIXME -12.70 USD
paypal_funding_source_institution: "My Bank"
paypal_funding_source_last4: "1234"
Transfer from Paypal balance to bank:
2014-09-09 * "Bank Account" "Transfer to Bank" ^paypal.266LYBQVWC7P8PXV0
Assets:Paypal -70.00 USD
date: 2014-09-09
paypal_transaction_id: "266LYBQVWC7P8PXV0"
Expenses:FIXME 70.00 USD
paypal_funding_source_institution: "My Bank"
paypal_funding_source_last4: "1234"
Sending money with credit card:
2016-05-15 * "John Smith" "Money Sent - Reimbursement for something" ^paypal.2RG4T9AJK3BT2DOV6
Expenses:Financial:Paypal:Fees 2.62 USD
Expenses:FIXME:A 80.00 USD
paypal_counterparty: "John Smith"
paypal_counterparty_email: "[email protected]"
paypal_note: "Reimbursement for something"
Expenses:FIXME -82.62 USD
paypal_funding_source_description: "Debit Card"
paypal_funding_source_institution: "VISA"
paypal_funding_source_last4: "1234"
"""
from typing import Dict, List, Tuple, Optional, Any
import collections
import os
import re
import json
import jsonschema
import dateutil.parser
from beancount.core.data import Transaction, Posting, Balance, Commodity, Price, EMPTY_SET, Directive, Entries, Meta
from beancount.core.amount import Amount
from beancount.core.flags import FLAG_OKAY
from beancount.core.number import ZERO, ONE, D
import beancount.core.amount
from . import ImportResult, Source, SourceResults, InvalidSourceReference, AssociatedData
from .link_based_source import LinkBasedSource
from ..posting_date import POSTING_DATE_KEY
from ..journal_editor import JournalEditor
from ..matching import FIXME_ACCOUNT, SimpleInventory
from ..amount_parsing import parse_amount
transaction_schema = {
'#schema': 'http://json-schema.org/draft-07/schema#',
'description': 'JSON schema for the transaction details.',
'type': 'object',
'properties': {
"fundingSource": {
"type": "object",
"properties": {
"fundingSourceList": {
"type": "array",
"items": {
"type": "object",
"properties": {
"type": {
"type": "string"
},
"last4": {
"type": "string"
},
"institution": {
"type": "string"
},
"issuer_product_description": {
"type": "string"
},
"statementName": {
"type": "string"
},
"amount": {
"type": "string"
},
"currencyCode": {
"type": "string"
}
},
"required": ["amount", "currencyCode", "type"]
}
},
"isMultipleFundingSourceUsed": {
"type": "boolean"
}
},
"required": ["fundingSourceList"]
},
"notesInfo": {
"type": "object",
"properties": {
"context": {
"type": "string"
},
"note": {
"type": "string"
}
},
"required": ["context", "note"]
},
"merchantCategory": {
"type": "string"
},
"invoiceId": {
"type": "string"
},
"itemDetails": {
"type": "object",
"properties": {
"itemList": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"number": {
"type": "string"
},
"price": {
"type": "string"
},
"isNegative": {
"type": "boolean"
},
"itemTotalPrice": {
"type": "string"
},
"url": {
"type": "string"
},
"quantity": {
"type": "integer"
},
"description": {
"type": "string"
},
"discounts": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"price": {
"type": "string"
}
},
"required": ["name", "price"]
}
},
},
"required": ["itemTotalPrice", "name"]
}
},
"salesTax": {
"type": "string"
},
"shippingAmount": {
"type": "string"
},
"itemTotalAmount": {
"type": "string"
},
"discount": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"value": {
"type": "string"
}
},
"required": ["name", "value"]
}
}
},
"required": ["itemList"]
},
},
"transactionType": {
"type": "string"
},
"counterparty": {
"type": "object",
"properties": {
"detailsCounterpartyText": {
"type": "string"
},
"name": {
"type": "string"
},
"email": {
"type": "string"
},
"url": {
"type": "string"
},
"phone": {
"type": "string"
},
"isBusiness": {
"type": "boolean"
}
},
"required": ["detailsCounterpartyText", "isBusiness", "name"]
},
"amount": {
"type":
"object",
"properties": {
"grossAmount": {
"type": "string"
},
"netAmount": {
"type": "string"
},
"fullPageNetAmount": {
"type": "string"
},
"feeAmount": {
"type": "string"
},
"grossExceedsNet": {
"type": "boolean"
},
"isZeroFee": {
"type": "boolean"
},
"feeLabel": {
"type": "string"
}
},
"required": [
"feeAmount", "feeLabel", "fullPageNetAmount", "grossAmount",
"grossExceedsNet", "isZeroFee", "netAmount"
]
},
"isCredit": {
"type": "boolean"
},
"required": [
"amount",
"counterparty",
"isCredit",
"date",
"transactionType",
],
}
class PaypalSource(LinkBasedSource, Source):
def __init__(self, directory: str, assets_account: str, fee_account: str, prefix: str,
**kwargs) -> None:
super().__init__(link_prefix=prefix + '.', **kwargs)
self.directory = directory
self.prefix = prefix
self.assets_account = assets_account
self.fee_account = fee_account
self.example_transaction_key_extractors[prefix + '_counterparty'] = None
self.example_posting_key_extractors[prefix + '_counterparty'] = None
self.example_posting_key_extractors[prefix + '_counterparty_note'] = None
self.example_posting_key_extractors[prefix + '_merchant_category'] = None
self.example_posting_key_extractors[prefix + '_item_name'] = None
self.example_posting_key_extractors[prefix + '_item_number'] = None
self.example_posting_key_extractors[prefix + '_item_description'] = None
self.example_posting_key_extractors[prefix + '_funding_source_institution'] = None
self.example_posting_key_extractors[prefix + '_funding_source_description'] = None
self.example_posting_key_extractors[prefix + '_funding_source_last4'] = None
self.transaction_meta_key = prefix + '_transaction_id'
def _make_import_result(self, txn_id: str, data: Dict[str, Any],
json_path: str):
if data.get('status') == 'PENDING': return None
date = dateutil.parser.parse(data['date']).date()
payee = data['counterparty']['name']
narration = data['transactionType']
txn_meta = collections.OrderedDict() # type: Meta
counterparty_metadata = [(self.prefix + '_counterparty', payee)]
funding_source_metadata = [] # type: List[Tuple[str, Any]]
for key in ('email', 'url', 'phone'):
if key in data['counterparty']:
value = data['counterparty'][key]
if not value: continue
counterparty_metadata.append((self.prefix + '_counterparty_' + key, value))
if 'merchantCategory' in data:
counterparty_metadata.append((self.prefix + '_merchant_category', data['merchantCategory']))
if 'invoiceId' in data:
counterparty_metadata.append((self.prefix + '_invoice_id', data['invoiceId']))
if 'notesInfo' in data:
note = data['notesInfo']['note']
note = re.sub(r'\s+', ' ', note)
narration += ' - ' + note
counterparty_metadata.append((self.prefix + '_note', note))
transaction = Transaction(
meta=txn_meta,
date=date,
flag='*',
payee=payee,
narration=narration,
links=frozenset([self.link_prefix + txn_id]),
tags=EMPTY_SET,
postings=[])
is_credit = data['isCredit']
counterparty_amount = parse_amount(data['amount']['grossAmount'])
funding_source_amount = parse_amount(data['amount']['netAmount'])
fee_amount = parse_amount(data['amount']['feeAmount'])
transaction_type_enum = data['transactionTypeEnum']
# Metadata added to postings to the `self.assets_account` account.
assets_account_metadata = [
(self.transaction_meta_key, txn_id),
(POSTING_DATE_KEY, date),
]
# If True, the posting legs to the funding source are negative, and all
# other legs are positive.
negate_funding_source_amounts = True
counterparty_remainder_account = FIXME_ACCOUNT + ':A'
if transaction_type_enum == 'TRANSFER_TO_BANK':
counterparty_remainder_account = self.assets_account
counterparty_metadata = []
negate_funding_source_amounts = False
elif transaction_type_enum == 'TRANSFER_FROM_BANK':
counterparty_remainder_account = self.assets_account
counterparty_metadata = []
negate_funding_source_amounts = True
elif transaction_type_enum.endswith('_SENT') or transaction_type_enum.endswith('_PURCHASE'):
negate_funding_source_amounts = True
elif transaction_type_enum.endswith('_RECEIVED'):
negate_funding_source_amounts = False
elif transaction_type_enum == 'MONEY_TRANSFER':
counterparty_remainder_account = self.assets_account
funding_source_metadata = counterparty_metadata
counterparty_metadata = []
negate_funding_source_amounts = is_credit
elif transaction_type_enum == 'REFUND':
negate_funding_source_amounts = False
else:
raise RuntimeError('Unknown transaction type: %s' % transaction_type_enum)
negate_counterparty_amounts = not negate_funding_source_amounts
if negate_funding_source_amounts:
funding_source_amount = -funding_source_amount
else:
funding_source_amount = funding_source_amount
counterparty_inventory = SimpleInventory()
counterparty_inventory += counterparty_amount
def add_counterparty_posting(amount,
extra_meta=[],
account=FIXME_ACCOUNT + ':A'):
nonlocal counterparty_inventory
if amount.number == ZERO:
return
counterparty_inventory -= amount
if negate_counterparty_amounts:
amount = -amount
meta = counterparty_metadata + extra_meta
if account == self.assets_account:
meta.extend(assets_account_metadata)
transaction.postings.append(
Posting(
meta=collections.OrderedDict(meta),
account=account,
units=amount,
cost=None,
price=None,
flag=None,
))
if fee_amount.number != ZERO:
if negate_counterparty_amounts:
amount = -fee_amount
else:
amount = fee_amount
transaction.postings.append(
Posting(
meta=collections.OrderedDict(),
account=self.fee_account,
units=amount,
cost=None,
price=None,
flag=None,
))
if 'itemDetails' in data:
for item in data['itemDetails']['itemList']:
units = parse_amount(item['itemTotalPrice'])
if 'quantity' in item:
quantity = D(item['quantity'])
else:
quantity = None
if units.number == ZERO and 'price' in item:
units = parse_amount(item['price'])
extra_meta = [
(self.prefix + '_item_name', item['name']),
]
for key in ('url', 'number', 'description'):
value = item.get(key, None)
if value:
extra_meta.append((self.prefix + '_item_%s' % key, value))
if quantity is not None:
extra_meta.append((self.prefix + '_item_quantity', quantity))
add_counterparty_posting(amount=units, extra_meta=extra_meta)
if 'discounts' in item:
for discount in item['discounts']:
units = -parse_amount(discount['price'])
add_counterparty_posting(amount=units, extra_meta=[
(self.prefix + '_item_discount', discount['name']),
])
for key in ('salesTax', 'shippingAmount'):
if key in data['itemDetails']:
units = parse_amount(data['itemDetails'][key])
add_counterparty_posting(amount=units, extra_meta=[
(self.prefix + '_item_type', key),
])
if 'discount' in data['itemDetails']:
for discount in data['itemDetails']['discount']:
units = -parse_amount(discount['value'])
add_counterparty_posting(amount=units, extra_meta=[
(self.prefix + '_item_discount', discount['name']),
])
counterparty_inventory_copy = counterparty_inventory.copy()
for currency in counterparty_inventory_copy:
add_counterparty_posting(
Amount(
currency=currency, number=counterparty_inventory_copy[currency]),
account=counterparty_remainder_account)
funding_source_inventory = SimpleInventory()
funding_source_inventory += funding_source_amount
funding_source_account = FIXME_ACCOUNT
if transaction_type_enum == 'SEND_MONEY_RECEIVED':
funding_source_account = self.assets_account
assert 'fundingSource' not in data
funding_source_metadata = assets_account_metadata
if 'fundingSource' in data:
for source in data['fundingSource']['fundingSourceList']:
meta = collections.OrderedDict() # type: Meta
account = FIXME_ACCOUNT
source_type = source['type']
if (source_type == 'BALANCE' or
(transaction_type_enum == 'SEND_MONEY_SENT' and
source_type != 'CREDIT_CARD')):
# For SEND_MONEY_SENT, sources other than CREDIT_CARD
# are actually handled by a separate transfer transaction.
account = self.assets_account
meta.update(assets_account_metadata)
else:
for key, meta_suffix in [
('issuer_product_description',
'funding_source_description'),
('institution', 'funding_source_institution'),
('last4', 'funding_source_last4'),
]:
if key in source:
meta[self.prefix + '_' + meta_suffix] = source[key]
# FIXME handle currencyCode
units = parse_amount(source['amount'])
if negate_funding_source_amounts:
units = -units
funding_source_inventory -= units
transaction.postings.append(
Posting(
meta=meta,
account=account,
units=units,
cost=None,
price=None,
flag=None,
))
for currency in funding_source_inventory:
transaction.postings.append(
Posting(
account=funding_source_account,
units=Amount(currency=currency, number=funding_source_inventory[currency]),
cost=None,
price=None,
flag=None,
meta=collections.OrderedDict(funding_source_metadata),
))
return ImportResult(
date=transaction.date,
info=dict(
type='application/json',
filename=json_path,
),
entries=[transaction])
def prepare(self, journal: JournalEditor, results: SourceResults):
transaction_json_suffix = '.json'
invoice_json_suffix = '.invoice.json'
transaction_ids = set(x[:-len(transaction_json_suffix)]
for x in os.listdir(self.directory)
if x.endswith(transaction_json_suffix) and
not x.endswith(invoice_json_suffix))
seen_in_journal = self.get_entries_with_link(
all_entries=journal.all_entries,
results=results,
valid_links=transaction_ids)
for txn_id in sorted(transaction_ids):
if txn_id in seen_in_journal: continue
path = os.path.join(self.directory,
txn_id + transaction_json_suffix)
self.log_status('paypal: processing %s' % (path, ))
with open(path, 'r', encoding='utf-8', newline='\n') as f:
txn = json.load(f)
jsonschema.validate(txn, transaction_schema)
import_result = self._make_import_result(
txn_id=txn_id, data=txn, json_path=path)
if import_result is not None:
results.add_pending_entry(import_result)
results.add_account(self.assets_account)
def is_posting_cleared(self, posting: Posting):
if posting.meta is None:
return False
return self.transaction_meta_key in posting.meta
def _get_html_path(self, txn_id):
return os.path.join(self.directory, txn_id + '.html')
@property
def name(self):
return 'paypal'
def get_associated_data_for_link(
self, entry_id: str) -> Optional[List[AssociatedData]]:
return [
AssociatedData(
description='Paypal transaction',
type='text/html',
path=self._get_html_path(entry_id),
)
]
def load(spec, log_status):
return PaypalSource(log_status=log_status, **spec)