def parse(days_back=7): print(", ".join(["'%s'" % column for column in columns])) client = fiobank.FioBank(token=settings.FIO_TOKEN) gen = client.period( datetime.datetime.now() - datetime.timedelta(days=days_back), datetime.datetime.now(), ) for payment in gen: if payment['amount'] >= 0: variable_symbol = payment['variable_symbol'] recipient_message = payment['recipient_message'] if recipient_message: recipient_message_without_d = re.sub( "\D", "", recipient_message).lstrip("0") else: recipient_message_without_d = "" if variable_symbol: variable_symbol = variable_symbol.lstrip("0") else: variable_symbol = "" try: invoice = Invoice.objects.get( variable_symbol__in=(variable_symbol, recipient_message, recipient_message_without_d), total_amount__lte=payment['amount'] + 1, total_amount__gte=payment['amount'] - 1, ) if 'CZK' == payment['currency']: invoice.paid_date = payment['date'] invoice.save() else: print_missing_payment(payment) except Invoice.DoesNotExist: print_missing_payment(payment)
def parse(days_back=7): client = fiobank.FioBank(token=settings.FIO_TOKEN) gen = client.period( datetime.datetime.now() - datetime.timedelta(days=days_back), datetime.datetime.now(), ) for payment in gen: if payment['amount'] >= 0: variable_symbol = payment['variable_symbol'] recipient_message = payment['recipient_message'] if recipient_message: # Replace all non-letter characters and leading zeros from VS recipient_message_without_d = re.sub("\D", "", recipient_message).lstrip("0") # noqa else: recipient_message_without_d = "" if variable_symbol: variable_symbol = variable_symbol.lstrip("0") else: variable_symbol = "" try: order = Order.objects.get( variable_symbol__in=(variable_symbol, recipient_message, recipient_message_without_d), total_amount__lte=payment['amount'] + 1, total_amount__gte=payment['amount'] - 1, ) if 'CZK' == payment['currency']: order.paid_date = payment['date'] order.save() except Order.DoesNotExist: order = None new_payment, created = FioPayment.objects.get_or_create( ident=payment['transaction_id'], defaults={ 'message': payment['recipient_message'], 'symspc': payment['specific_symbol'], 'symvar': variable_symbol, 'currency': payment['currency'], 'amount': payment['amount'], 'received_at': payment['date'], 'user_identification': payment['user_identification'], 'bank': payment['bank_name'], 'symcon': payment['constant_symbol'], 'sender': payment['account_number'], 'status': 'paid', }, ) new_payment.order = order new_payment.save()
def fetch_payments(cls, from_date=None): client = fiobank.FioBank(token=settings.FIO_TOKEN) for transaction in client.last(from_date=from_date): matches = [] # Extract from message if transaction["recipient_message"]: matches.extend( PROFORMA_RE.findall(transaction["recipient_message"])) # Extract from variable symbol if transaction["variable_symbol"]: matches.extend( PROFORMA_RE.findall(transaction["variable_symbol"])) # Extract from sender reference if transaction.get("reference", None): matches.extend(PROFORMA_RE.findall(transaction["reference"])) # Extract from comment for manual pairing if transaction["comment"]: matches.extend(PROFORMA_RE.findall(transaction["comment"])) # Process all matches for proforma_id in matches: proforma_id = "P{}".format(proforma_id) try: related = Payment.objects.get(backend=cls.name, invoice=proforma_id, state=Payment.PENDING) backend = cls(related) proforma = backend.get_proforma() proforma.mark_paid( json.dumps(transaction, indent=2, cls=DjangoJSONEncoder)) backend.git_commit([proforma.paid_path], proforma) if floor(float( proforma.total_amount)) <= transaction["amount"]: print("Received payment for {}".format(proforma_id)) backend.payment.details["transaction"] = transaction backend.success() else: print("Underpaid {}: received={}, expected={}".format( proforma_id, transaction["amount"], proforma.total_amount, )) except Payment.DoesNotExist: print( "No matching payment for {} found".format(proforma_id))
def update(self): """ Perform update from source (Fio webpages). """ new_count=0 fd = datetime.date.today() - datetime.timedelta(days=150) fio = fiobank.FioBank(token='W2Sd4Us7ie5w2Jd0bvU0JQC8VfADE2u1jUHhZDikR1bjU6jVqT5csYXq48mijqNA') for payment in fio.last(from_date=fd.strftime('%Y-%m-%d')): try: pid = '' if 'comment' in payment: pid = payment['comment'] if 'account_name' in payment: pid = payment['account_name'] payment['identification'] = pid if not 'recipient_message' in payment: payment['recipient_message'] = '' symbols = ['variable_symbol', 'specific_symbol', 'constant_symbol'] for sym in symbols: if sym not in payment: payment[sym] = 0 print("looking up {0} {1} {2} {3}".format(payment['date'], payment['identification'], payment['recipient_message'], payment['amount'])) Payment.objects.get(date=payment['date'], identification=payment['identification'], message=payment['recipient_message'], amount=payment['amount']) except Payment.MultipleObjectsReturned: continue except Payment.DoesNotExist: new_count += 1 self.add_payment(payment) print("Update complete (%d new payments)" % new_count)