Skip to content

Commit

Permalink
Implement new MobilePay Feed API (#423)
Browse files Browse the repository at this point in the history
* Refactor existing method and draft new method

* Add safeguard in import

* Implement feed-API

* Acknowledge Mobilepay docs is terrible

Replace missing parameter by simply looking for first empty response.
  • Loading branch information
krestenlaust authored Apr 12, 2024
1 parent 42823d0 commit 3bc41ba
Showing 1 changed file with 62 additions and 4 deletions.
66 changes: 62 additions & 4 deletions stregsystem/management/commands/importmobilepaypayments.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime, timedelta, date

from django.core.management.base import BaseCommand
from django.utils.dateparse import parse_datetime
from pathlib import Path
Expand Down Expand Up @@ -97,7 +98,12 @@ def refresh_ledger_id(self):
self.tokens['ledger_id'] = self.get_ledger_id(self.myshop_number)

# Fetches the transactions for a given payment-point (MobilePay phone-number) in a given period (from-to)
def get_transactions(self, transaction_date: date):
def get_transactions_historic(self, transaction_date: date) -> list:
"""
Fetches historic transactions (only complete days (e.g. not today)) by date.
:param transaction_date: The date to look up.
:return: List of transactions on that date.
"""
ledger_date = transaction_date.strftime('%Y-%m-%d')

url = f"{self.api_endpoint}/report/v2/ledgers/{self.tokens['ledger_id']}/funds/dates/{ledger_date}"
Expand All @@ -112,6 +118,52 @@ def get_transactions(self, transaction_date: date):
response.raise_for_status()
return response.json()['items']

def get_transactions_latest_feed(self) -> list:
"""
Fetches transactions ahead of cursor. Used to fetch very recent transactions.
Moves the cursor as well.
:return: All transactions from the current cursor till it's emptied.
"""

transactions = []
cursor = self.tokens.get('cursor', "")

while True:
res = self.fetch_report_by_feed(cursor)
transactions.extend(res['items'])

try_later = res['tryLater'] == "true"

if try_later:
break

cursor = res['cursor']

# Note: Since MobilePay API doesn't return 'hasMore' like the docs says it does.
# We can just tell whether we're at the end by how many items are left.
if len(res['items']) == 0:
break

self.tokens['cursor'] = cursor
self.update_token_storage()
return transactions

def fetch_report_by_feed(self, cursor: str):
url = f"{self.api_endpoint}/report/v2/ledgers/{self.tokens['ledger_id']}/funds/feed"

params = {
'includeGDPRSensitiveData': "true",
'cursor': cursor,
}
headers = {
'authorization': "Bearer {}".format(self.tokens['access_token']),
}

response = requests.get(url, params=params, headers=headers)
response.raise_for_status()

return response.json()

# Client side check if the token has expired.
def refresh_expired_token(self):
self.read_token_storage()
Expand All @@ -136,12 +188,14 @@ def fetch_transactions(self) -> list:

transactions = []

transactions.extend(self.get_transactions_latest_feed())

for i in range(self.days_back):
past_date = date.today() - timedelta(days=i)
if past_date < self.manual_cutoff_date:
break

transactions.extend(self.get_transactions(past_date))
transactions.extend(self.get_transactions_historic(past_date))

return transactions
except HTTPError as e:
Expand Down Expand Up @@ -184,6 +238,12 @@ def import_mobilepay_payment(self, transaction):
if transaction['entryType'] != 'capture':
return

payment_datetime = parse_datetime(transaction['time'])

if payment_datetime.date() < self.manual_cutoff_date:
self.write_debug(f'Skipping transaction because it is before payment cutoff date {payment_datetime}')
return

trans_id = transaction['pspReference']

if MobilePayment.objects.filter(transaction_id=trans_id).exists():
Expand All @@ -199,8 +259,6 @@ def import_mobilepay_payment(self, transaction):

comment = strip_emoji(transaction['message'])

payment_datetime = parse_datetime(transaction['time'])

MobilePayment.objects.create(
amount=amount, # already in streg-ører
member=mobile_payment_exact_match_member(comment),
Expand Down

0 comments on commit 3bc41ba

Please sign in to comment.