From 0a7bb7e96350b6cd19b7069ea187e0ab793b1bb7 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Sun, 23 Jan 2022 01:52:23 +0000 Subject: [PATCH] Store PayPal transaction type --- apiserver/apiserver/api/models.py | 3 ++- apiserver/apiserver/api/serializers.py | 1 + apiserver/apiserver/api/utils_paypal.py | 1 + apiserver/import_paypal_txn_type.py | 34 +++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 1 deletion(-) create mode 100755 apiserver/import_paypal_txn_type.py diff --git a/apiserver/apiserver/api/models.py b/apiserver/apiserver/api/models.py index 12adc00..f31e6d9 100644 --- a/apiserver/apiserver/api/models.py +++ b/apiserver/apiserver/api/models.py @@ -77,7 +77,8 @@ class Transaction(models.Model): category = models.TextField(blank=True, null=True) account_type = models.TextField(blank=True, null=True) info_source = models.TextField(blank=True, null=True) - paypal_txn_id = models.CharField(max_length=17, blank=True, null=True) + paypal_txn_id = models.CharField(max_length=17, blank=True, null=True, unique=True) + paypal_txn_type = models.CharField(max_length=64, blank=True, null=True) paypal_payer_id = models.CharField(max_length=13, blank=True, null=True) report_type = models.TextField(blank=True, null=True) diff --git a/apiserver/apiserver/api/serializers.py b/apiserver/apiserver/api/serializers.py index 1244768..a79771c 100644 --- a/apiserver/apiserver/api/serializers.py +++ b/apiserver/apiserver/api/serializers.py @@ -63,6 +63,7 @@ class TransactionSerializer(serializers.ModelSerializer): 'user', 'recorder', 'paypal_txn_id', + 'paypal_txn_type', 'paypal_payer_id', ] diff --git a/apiserver/apiserver/api/utils_paypal.py b/apiserver/apiserver/api/utils_paypal.py index cf128cc..a3676df 100644 --- a/apiserver/apiserver/api/utils_paypal.py +++ b/apiserver/apiserver/api/utils_paypal.py @@ -122,6 +122,7 @@ def build_tx(data): payment_method=data.get('payment_type', 'unknown'), paypal_payer_id=data.get('payer_id', 'unknown'), paypal_txn_id=data.get('txn_id', 'unknown'), + paypal_txn_type=data.get('txn_type', 'unknown'), reference_number=data.get('txn_id', 'unknown'), ) diff --git a/apiserver/import_paypal_txn_type.py b/apiserver/import_paypal_txn_type.py new file mode 100755 index 0000000..5fc7ccb --- /dev/null +++ b/apiserver/import_paypal_txn_type.py @@ -0,0 +1,34 @@ +import django, sys, os +os.environ['DJANGO_SETTINGS_MODULE'] = 'apiserver.settings' +django.setup() + +from urllib.parse import parse_qs + +from apiserver.api import models + +ipns = models.IPN.objects.all() +transactions = models.Transaction.objects.filter(paypal_txn_id__isnull=False) +txs = {} + +for tx in transactions: + txs[tx.paypal_txn_id] = tx + +for ipn in ipns: + data = parse_qs(ipn.data) + + if data.get('payment_status', [False])[0] != 'Completed': + continue + + txn_id = data['txn_id'][0] + txn_type = data['txn_type'][0] + + print('Processing tx id:', txn_id, '| type:', txn_type) + + txs[txn_id].paypal_txn_type = txn_type + +print('Performing bulk update...') +transactions.bulk_update(txs.values(), ['paypal_txn_type']) + +print('Processed', ipns.count(), 'IPNs.') + +print('Done.')