def getPrefsJson(self, consumerId, cardId): consumerCard = None cacheService = CacheService() ssConst = SSConst() periodKeysJson = ssConst.getJson(ssConst.PERIOD_KEYS) txnTypesJson = ssConst.getJson(ssConst.TXN_TYPES) approvalTypeJson = ssConst.getJson(ssConst.APPROVAL_TYPES) try: consumerCard = ConsumerCard.objects.get(id=cardId) except ConsumerCard.DoesNotExist: raise Exception(SSException.CARD_NOT_PRESENT) prefsJson = { "txTypes": { "Online": 1, "International": 1 }, "limits": { "Daily": { "userLimit": consumerCard.limit, "action": "Block", "maxLimit": consumerCard.limit }, "Monthly": { "userLimit": consumerCard.limit, "action": "Block", "maxLimit": consumerCard.limit }, }, "customLimits": [] } if SSUtil.isIdinList(consumerCard.blockedTxTypes, "Online"): prefsJson["txTypes"]["Online"] = 0 if SSUtil.isIdinList(consumerCard.blockedTxTypes, "International"): prefsJson["txTypes"]["International"] = 0 prefs = cacheService.getConsumerPrefs(consumerId) if prefs: for pref in prefs: if str(pref.cardId) == str(cardId): if pref.categoryKey == 'Any': prefsJson["limits"][ pref.periodKey]["userLimit"] = pref.limit prefsJson["limits"][ pref.periodKey]["action"] = pref.ssApproval else: customLimit = {} if not customLimit.get(pref.periodKey): customLimit[pref.periodKey] = {} customLimit[pref.periodKey]["userLimit"] = pref.limit customLimit[pref.periodKey]["action"] = pref.ssApproval customLimit[ pref.periodKey]["categoryKey"] = pref.categoryKey customLimit[ pref.periodKey]["maxLimit"] = consumerCard.limit prefsJson["customLimits"].append(customLimit) return prefsJson
def manageTags(self, txnId, selected, newOne): millis = SSUtil.getMillis() txn = ConsumerTxn.objects.get(id=txnId) if not selected is None: txnTags = TxnTag.objects.all().filter(consumerTxn_id=txnId) for txnTag in txnTags: if not SSUtil.isIdinList(selected, txnTag.consumerTag_id): self.removeTag(txnId, txnTag.consumerTag_id) else: selected = SSUtil.removeIdFromList(selected, txnTag.consumerTag_id) # Now check what remains, and tag to them if not selected is None and selected != '': lst = selected.split(",") for tagId in lst: txnTag = TxnTag() txnTag.cardId = txn.cardId txnTag.consumerTag_id = tagId txnTag.consumerTxn_id = txnId txnTag.created = millis txnTag.updated = millis txnTag.save() else: #Remove all txnTags = TxnTag.objects.filter(consumerTxn_id=txnId) for txnTag in txnTags: self.removeTag(txnId, txnTag.consumerTag_id) #Create new one if sent if not newOne is None and newOne != '': consumerTag = self.addTag(txnId, newOne)
def toggleCardLockStatus(self, cardId, consumerId): millis = SSUtil.getMillis() cacheService = CacheService() statusFlag = 0 try: consumer = Consumer.objects.get(id=consumerId) consumerCard = ConsumerCard.objects.get(id=cardId) if SSUtil.isIdinList(consumer.blockedCards, cardId): consumer.blockedCards = SSUtil.removeIdFromList( consumer.blockedCards, cardId) statusFlag = 1 consumerCard.status = 1 else: consumer.blockedCards = SSUtil.addIdToList( consumer.blockedCards, cardId) statusFlag = 0 consumerCard.status = 0 consumer.updated = millis consumerCard.updated = millis consumer.save() cacheService.setConsumer(consumer.id) consumerCard.save() cacheService.setCard(consumerCard.id) return statusFlag except: raise Exception(SSException.PROCESSING_FAILED)
def unlockTxTypeStatus(self, cardId, txType): millis = SSUtil.getMillis() statusFlag = 1 cacheService = CacheService() try: consumerCard = ConsumerCard.objects.get(id=cardId) if SSUtil.isIdinList(consumerCard.blockedTxTypes,txType): consumerCard.blockedTxTypes = SSUtil.removeIdFromList(consumerCard.blockedTxTypes,txType) statusFlag = 1 consumerCard.updated = millis consumerCard.save() cacheService.setCard(cardId) return statusFlag except: raise Exception(SSException.PROCESSING_FAILED)
def lockMerchantStatus(self, merchantId, consumerId): millis = SSUtil.getMillis() statusFlag = 0 cacheService = CacheService() try: consumer = Consumer.objects.get(id=consumerId) if not SSUtil.isIdinList(consumer.blockedMerchants, merchantId): consumer.blockedMerchants = SSUtil.addIdToList( consumer.blockedMerchants, merchantId) statusFlag = 0 consumer.updated = millis consumer.save() cacheService.setConsumer(consumer.id) merchServ = MerchantService() merchServ.toggleStatus(merchantId, consumerId, statusFlag) return statusFlag except: raise Exception(SSException.PROCESSING_FAILED)
def getApproval(self, cardNum, amount, merchantUuid, merchantName, txType, mccCode): ssConst = SSConst() consumerCard = None consumer = None category = None prefs = None aggs = None merchant = None millis = SSUtil.getMillis() cacheService = CacheService() cardService = CardService() approvalTypeJson = ssConst.getJson(ssConst.APPROVAL_TYPES) print "1" # Check if card exists try: consumerCard = cacheService.getCardByNum(cardNum) except Exception as e: return TxnValidationVO(False, False, 'Block', SSException.INVALID_CARD, False) print "1.5 " + str(consumerCard.blockedTxTypes) if SSUtil.isIdinList(consumerCard.blockedTxTypes, txType): return TxnValidationVO(False, True, 'Block', txType + " transactions are disabled.", True) print "2" try: merchant = cacheService.getMerchantByUuid(merchantUuid) except Exception as e: pass print "3" # Check if card is blocked if consumerCard.status == 0: return TxnValidationVO(False, True, 'Block', "Your card is blocked.", True) print "4" # By any chance the category is not in our records, go back, # Also need this for previous txn try: category = cacheService.getCategoryByMccCode(mccCode) except TxnCategory.DoesNotExist: return TxnValidationVO(False, False, 'Block', "Unknown category.", False) print "5" # if it was tried in last 10 minutes and user approved it - go ahead try: if not merchant is None: maxAllowedCreatedTime = millis - ssConst.ALLOWED_MILLIS_FOR_RETRY qs = ConsumerTxn.objects.all().filter( consumerId=consumerCard.consumerId, cardId=consumerCard.id, txType__endswith=txType, category_id=category.id, merchant_id=merchant.id, amtSpentSS=amount, created__gt=maxAllowedCreatedTime).order_by('-created')[:1] consumerTxn = qs[0] if not consumerTxn is None and consumerTxn.ssApprovalStatus == ssConst.APPROVAL_STATUSES[ 3][0]: return TxnValidationVO( True, True, 'Approve', "We approved a transaction on your card for " + ssConst.CURRENCY_SYMBOL + str(amount) + ".", True) except Exception as e: print "Error while matching transaction " + e.message pass print "6" # No point in moving further if the consumer doesn't exist, but don't record it for now try: consumer = cacheService.getConsumerById(consumerCard.consumerId) except Consumer.DoesNotExist: return TxnValidationVO(False, False, 'Block', SSException.INVALID_USER, False) print "7" # Check if merchant is blocked print "blocked merchants are " + consumer.blockedMerchants if not merchant is None: if SSUtil.isIdinList(consumer.blockedMerchants, merchant.id): return TxnValidationVO( False, True, 'Block', "You have blocked all transactions from Merchant \"" + merchant.name + "\"", True) print "8" # Now check the aggregates and preferences prefs = cacheService.getConsumerPrefs(consumer.id) if not prefs: cardService.createDefaultPrefsAndAggs(consumerCard) print "9" aggs = cacheService.getConsumerAggs(consumer.id) if not aggs: #TODO: If user has no aggregates then no point in holding it - we just tried creating return TxnValidationVO( True, True, 'Block', "Internal server error - No aggregates available for the user. ", False) for pref in prefs: #Ignore the preferences which are of no use for this transaction if pref.categoryKey != 'Any' and pref.categoryKey != category.name: continue if pref.txType != 'Any' and pref.txType != txType: continue if not merchant is None and pref.merchantId != -1 and pref.merchantId != merchant.id: continue if pref.cardId != -1 and pref.cardId != consumerCard.id: continue # Compare matching aggs for the rest of the preferences for agg in aggs: if pref.periodKey == agg.periodKey and pref.categoryKey == agg.categoryKey and pref.txType == agg.txType and agg.merchantId == pref.merchantId and agg.cardId == pref.cardId: if pref.limit < (agg.amtSpentSS + Decimal(amount)): if pref.ssApproval == 'Block' or pref.ssApproval == 'AskMe': msgToUser = "******" + pref.periodKey.lower( ) + " spend limit of " + ssConst.CURRENCY_SYMBOL + str( pref.limit) if pref.categoryKey != 'Any': msgToUser = msgToUser + " for " + pref.categoryKey msgToUser = msgToUser + "." return TxnValidationVO(False, True, pref.ssApproval, msgToUser, True) print "10" return TxnValidationVO( True, True, 'Approve', "We approved a " + ssConst.CURRENCY_SYMBOL + str(amount) + " charge from " + merchantName + ".", True)