def card_balance(id): card_dets = paydnadb.cards.find_one({'_id': id}) resp = xml_client.balance(card_dets.get('profile_id'), card_dets.get('_id'), get_trans_id()) if resp.get('resultCode') == 1: paydnadb.cards.update_one({'_id': id}, {'$set': {'balance': resp['balanceAmount']}}) return make_response({ "balance": resp['balanceAmount']}, 200) else: return make_response({'error': 'Error getting status for the card'})
def get_profilebalance(profilename): print(profilename) profileID = name_to_pid(profilename) print(profileID) if profileID != '': resp = xml_client.profile_balance(profileID, get_trans_id()) # return { 'profile': resp.get('cardNumber'), 'expiryDate': resp.get('expiryDate').value, 'balance': resp.get('balanceAmount'), 'transactionID': resp.get('clientTransactionID') } if resp.get('resultCode') == 1: paydnadb.profiles.update_one({'name': profilename}, {'$set': {'balance': resp.get('balanceAmount')}}) return { 'profile': profilename, 'balance': resp.get('balanceAmount') } else: return { 'result': resp.get('resultText')}
def get_balance(): profilename = request.args.get('profilename') profile = { k:v for (k,v) in [profile.items() for profile in PROFILES if profile['name'] == profilename][0] } card = request.args.get('card') pID = profile.get('profileID') logger.info(f'Card balance for {profilename} ({pID}), {card}') resp = xml_client.balance(pID, card, get_trans_id()) if resp.get('resultCode') == 1: logger.info(f"Balance response: card: {resp.get('cardNumber')}, 'expiryDate': {resp.get('expiryDate').value}, 'balance': {resp.get('balanceAmount')}, 'transactionID': {resp.get('clientTransactionID')}") return { 'card': resp.get('cardNumber'), 'expiryDate': resp.get('expiryDate').value, 'balance': resp.get('balanceAmount'), 'transactionID': resp.get('clientTransactionID') } else: return resp
def card_status(id): skipped_fields =['clientTransactionID', 'serverTransactionID', 'resultCode', 'resultText'] card_dets = paydnadb.cards.find_one({'_id': id}) resp = xml_client.status(card_dets.get('profile_id'), card_dets.get('_id'), get_trans_id()) status = {} for k, v in resp.items(): if k not in skipped_fields: status[k] = get_bool(v) if status != {}: paydnadb.cards.update_one({'_id': id}, {'$set': {'status': status}}) return make_response({ "status": status}, 200) else: return make_response({'error': 'Error getting status for the card'})
def deduct_card(id): print(request.args) if request.method == 'POST': card_dets = paydnadb.cards.find_one({'_id': id}) current_bal = card_dets.get('balance') if card_dets.get('balance') else 0 deduct_value = int(request.args.get('deductValue')) print(current_bal - deduct_value) if ((current_bal - deduct_value) >= 0): resp = xml_client.deduct_card_load_profile(card_dets.get('profile_id'), card_dets.get('_id'), deduct_value, get_trans_id()) print(resp) if resp.get('resultCode') == 1: paydnadb.cards.update_one({'_id': id}, {'$set': {'balance': current_bal - deduct_value}}) return { "action": f'card deducted with R{(deduct_value / 100.0):.2f}' } else: return { "deductError": resp.get('resultText')} else: return { "deductError": 'Insufficient funds on card', 'cardBalance': current_bal } else: return make_response({'Error': 'method not allowed'}, 401)
def get_month_load_todate(card): month_balance = 0 today = datetime.now() start_date = datetime(today.year, today.month, 1) profile_id = paydnadb.cards.find_one({'_id': card}).get('profile_id') resp = xml_client.statement_by_date_range(profile_id, card, to_xml_date(start_date), to_xml_date(today), get_trans_id()) for statement_line in resp.get('statement'): if statement_line.get('transactionType') == 1: month_balance += statement_line.get('transactionAmount') return month_balance