Ejemplo n.º 1
0
 def get(self, clientId):
     # first check if card exists in consumer account
     try:
         cacheService = CacheService()
         ca = cacheService.getClientById(clientId)
         return ca
     except Client.DoesNotExist:
         raise Exception(SSException.CLIENT_NOT_PRESENT)
Ejemplo n.º 2
0
 def authenticate(self, request):
     token = request.META.get('HTTP_SSCLIENTTOKEN')
     cacheService = CacheService()
     if not token:
         raise exceptions.NotAuthenticated(SSException.AUTH_FAILED)
     try:
         client = cacheService.getClientByToken(token)
         return (client, None)
     except Client.DoesNotExist:
         raise exceptions.NotAuthenticated(SSException.AUTH_FAILED)
Ejemplo n.º 3
0
 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
Ejemplo n.º 4
0
    def recordTxn(self, cardNum, txType, amount, mccCode, merchantUuid,
                  merchantName, txnApprovalVO):
        currTimeMillis = SSUtil.getMillis()
        ssConst = SSConst()
        cacheService = CacheService()
        merchServ = MerchantService()
        merchant = None
        print "inside record txn " + merchantUuid
        try:
            consumerCard = cacheService.getCardByNum(cardNum)
            category = cacheService.getCategoryByMccCode(mccCode)
            approvalTypeJson = ssConst.getJson(ssConst.APPROVAL_TYPES)
            try:
                merchant = cacheService.getMerchantByUuid(str(merchantUuid))
            except:
                merchant = merchServ.createMerchant(merchantUuid, merchantName,
                                                    None, None, mccCode)
            # Record transaction
            txn = ConsumerTxn()
            txn.consumerId = consumerCard.consumerId
            txn.cardId = consumerCard.id
            txn.txType = txType
            txn.txDate = currTimeMillis
            txn.amtSpentSS = amount
            txn.category_id = category.id
            txn.merchant_id = merchant.id
            txn.reviewStatus = merchant.reviewStatus
            txn.ssApproval = txnApprovalVO.approval
            # decide over the status
            if (txnApprovalVO.approval == 'Approve'
                    or txnApprovalVO.approval == 'Warn'):
                txn.ssApprovalStatus = ssConst.APPROVAL_STATUSES[0][0]
            if (txnApprovalVO.approval == 'Block'):
                txn.ssApprovalStatus = ssConst.APPROVAL_STATUSES[1][0]
            if (txnApprovalVO.approval == 'AskMe'):
                txn.ssApprovalStatus = ssConst.APPROVAL_STATUSES[2][0]

            txn.created = currTimeMillis
            txn.updated = currTimeMillis
            txn.save()

            #Establish relation with consumer and merchant
            merchServ.addConsumerRelation(merchant.id, consumerCard.consumerId)
            searchServ = SearchService()
            txnSerializer = ConsumerTxnSerializer(txn, many=False)
            if (txnApprovalVO.approval == 'Approve'
                    or txnApprovalVO.approval == 'Warn'):
                searchServ.upload("txn", txn.id, txnSerializer.data)
            return txn

        except Exception as e:
            print "Exception while recording txn " + e.message
            # actually log the fact that it has gone wrong
            pass
Ejemplo n.º 5
0
 def registerDeviceGCMRegistrationId(self, registrationId, consumerId):
     millis = SSUtil.getMillis()
     cacheService = CacheService()
     try:
         consumerDevice = ConsumerDevice.objects.get(consumerId=consumerId)
         consumerDevice.deviceRegistrationId = registrationId
         consumerDevice.updated = millis
         consumerDevice.save()
         cacheService.setDevice(consumerDevice.id)
         return 1
     except:
         raise Exception(SSException.PROCESSING_FAILED)
Ejemplo n.º 6
0
 def authenticate(self, request):
     uuid = request.META.get('HTTP_SSMERCHANTTOKEN')
     cacheService = CacheService()
     if not uuid:
         raise exceptions.NotAuthenticated(SSException.AUTH_FAILED)
     try:
         merchant = cacheService.getMerchantByUuid(uuid)
         if merchant.installed == 1:
             return (merchant, None)
         else:
             raise exceptions.NotAuthenticated(SSException.AUTH_FAILED)
     except Merchant.DoesNotExist:
         raise exceptions.NotAuthenticated(SSException.AUTH_FAILED)
Ejemplo n.º 7
0
 def authenticate(self, request):
     cacheService = CacheService()
     deviceToken = request.META.get('HTTP_SSTOKEN')
     if not deviceToken:
         raise exceptions.NotAuthenticated(SSException.AUTH_FAILED)
     try:
         cd = cacheService.getConsumerDeviceByToken(deviceToken)
         c = cacheService.getConsumerById(cd.consumerId)
         return (c, None)
     except ConsumerDevice.DoesNotExist:
         cs = ConsumerService()
         cid = cs.registerDevice(deviceToken)
         c = Consumer.objects.get(id=cid)
         return (c, None)
Ejemplo n.º 8
0
    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)
Ejemplo n.º 9
0
    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)
Ejemplo n.º 10
0
    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)
Ejemplo n.º 11
0
    def deletePref(self, consumerId, cardId, categoryKey, periodKey):
        pref = None
        millis = SSUtil.getMillis()
        cacheService = CacheService()
        txnService = TxnService()
        try:
            pref = ConsumerPrefs.objects.get(consumerId=consumerId,
                                             cardId=cardId,
                                             periodKey=periodKey,
                                             categoryKey=categoryKey)
            pref.delete()
        except ConsumerPrefs.DoesNotExist:
            pass

        try:
            agg = ConsumerAgg.objects.get(consumerId=consumerId,
                                          cardId=cardId,
                                          periodKey=periodKey,
                                          categoryKey=categoryKey)
            agg.delete()
        except ConsumerAgg.DoesNotExist:
            pass
        cacheService.setConsumerPrefs(consumerId)
        txnService.recalculateAgg(consumerId, None, None)
        cacheService.setConsumerAggs(consumerId)
        return True
Ejemplo n.º 12
0
 def savePref(self, consumerId, cardId, categoryKey, periodKey, action,
              userLimit):
     pref = None
     millis = SSUtil.getMillis()
     cacheService = CacheService()
     txnService = TxnService()
     try:
         if categoryKey is None:
             pref = ConsumerPrefs.objects.get(consumerId=consumerId,
                                              cardId=cardId,
                                              periodKey=periodKey,
                                              categoryKey='Any')
         else:
             pref = ConsumerPrefs.objects.get(consumerId=consumerId,
                                              cardId=cardId,
                                              periodKey=periodKey,
                                              categoryKey=categoryKey)
     except ConsumerPrefs.DoesNotExist:
         pref = ConsumerPrefs()
         pref.consumerId = consumerId
         pref.cardId = cardId
         pref.periodKey = periodKey
         pref.categoryKey = categoryKey
         pref.created = millis
         pref.txType = "Any"
         pref.merchantId = -1
     pref.ssApproval = action
     pref.limit = userLimit
     pref.updated = millis
     pref.save()
     cacheService.setConsumerPrefs(consumerId)
     txnService.recalculateAgg(consumerId, None, None)
     cacheService.setConsumerAggs(consumerId)
     return pref
Ejemplo n.º 13
0
    def createDefaultPrefsAndAggs(self, card):
        agg = None
        pref = None
        defPeriodKeys = {"Daily","Monthly"}
        defAction = "Approve"
        millis = SSUtil.getMillis()
        cacheService = CacheService()
        for periodKey in defPeriodKeys:
            try:
                pref = ConsumerPrefs.objects.get(consumerId=card.consumerId,
                                    cardId = card.id, periodKey=periodKey,
                                    categoryKey='Any')
            except:
                pref = ConsumerPrefs()
                pref.consumerId = card.consumerId
                pref.cardId = card.id
                pref.periodKey = periodKey
                pref.categoryKey = 'Any'
                pref.created = millis
                pref.txType = "Any"
                pref.merchantId = -1
                pref.ssApproval = defAction
            pref.limit = card.limit
            pref.updated = millis
            pref.save()

            try:
                agg = ConsumerAgg.objects.get(consumerId=card.consumerId,
                                    cardId = card.id, periodKey=periodKey,
                                    categoryKey='Any')
            except:
                agg = ConsumerAgg()
                agg.consumerId = card.consumerId
                agg.cardId = card.id
                agg.periodKey = periodKey
                agg.categoryKey = 'Any'
                agg.created = millis
                agg.txType = "Any"
                agg.merchantId = -1
                agg.ssApproval = defAction
            agg.amtSpentSS = 0.00
            agg.updated = millis
            agg.save()
        cacheService.setConsumerPrefs(card.consumerId)
        cacheService.setConsumerAggs(card.consumerId)
Ejemplo n.º 14
0
    def recalculateAgg(self, consumerId, recentAmount, recentTxn):
        currTimeMillis = SSUtil.getMillis()
        ssConst = SSConst()
        cacheService = CacheService()
        prefs = cacheService.getConsumerPrefs(consumerId)
        name_map = {'total': 'total', 'pk': 'id'}
        periodKeysJson = ssConst.getJson(ssConst.PERIOD_KEYS)
        approvalTypeJson = ssConst.getJson(ssConst.APPROVAL_TYPES)
        if prefs:
            for pref in prefs:
                consumerAgg = ConsumerAgg()

                querySt = "select sum(amtSpentSS) as total, 1 as id from ss_consumer_txn where consumerId =" + str(
                    consumerId)
                querySt += " and ssApproval='" + ssConst.APPROVAL_TYPES[0][
                    0] + "'"
                if pref.periodKey != 'Any':
                    print "period key is " + pref.periodKey
                    print " value is " + periodKeysJson[pref.periodKey]
                    querySt += " and  FROM_UNIXTIME(created/1000) >= DATE_SUB(FROM_UNIXTIME(" + str(
                        currTimeMillis / 1000
                    ) + "), INTERVAL " + periodKeysJson[pref.periodKey] + ")"
                if pref.categoryKey != 'Any':
                    category = TxnCategory.objects.get(name=pref.categoryKey)
                    querySt += " and  category_id = " + str(category.id)
                if pref.txType != 'Any':
                    querySt += " and  txType = '" + pref.txType + "'"
                if pref.cardId != -1:
                    querySt += " and  cardId = " + str(pref.cardId)
                if pref.merchantId != -1:
                    querySt += " and  merchant_id = " + str(pref.merchantId)
                print querySt
                res = ConsumerTxn.objects.raw(querySt, translations=name_map)
                total = 0
                for x in res:
                    total = x.total
                if total == None:
                    total = 0
                print total
                # check if aggregate exists for this pref
                print str(consumerId) + str(
                    pref.cardId
                ) + pref.periodKey + " " + pref.categoryKey + " " + pref.txType + " " + str(
                    pref.merchantId)
                try:
                    consumerAgg = ConsumerAgg.objects.get(
                        consumerId=consumerId,
                        cardId=pref.cardId,
                        periodKey=pref.periodKey,
                        categoryKey=pref.categoryKey,
                        txType=pref.txType,
                        merchantId=pref.merchantId)
                    print "Got consumer agg " + str(total)
                except ConsumerAgg.DoesNotExist:
                    consumerAgg.periodKey = pref.periodKey
                    consumerAgg.categoryKey = pref.categoryKey
                    consumerAgg.txType = pref.txType
                    consumerAgg.cardId = pref.cardId
                    consumerAgg.merchantId = pref.merchantId
                    consumerAgg.consumerId = pref.consumerId
                    consumerAgg.created = currTimeMillis
                consumerAgg.amtSpentSS = total
                consumerAgg.updated = currTimeMillis
                consumerAgg.save()

        cacheService.setConsumerAggs(consumerId)
        #fix balance now
        if not recentTxn is None:
            if recentTxn.ssApproval == ssConst.APPROVAL_TYPES[0][0]:
                card = ConsumerCard.objects.get(id=recentTxn.cardId)
                card.amtSpentSS = card.amtSpentSS + recentAmount
                card.currOS = card.currOS + recentAmount
                card.avaialbleLimit = card.limit - card.currOS
                card.updated = currTimeMillis
                card.save()
                cacheService.setCard(card.id)
Ejemplo n.º 15
0
    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)