Exemple #1
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
Exemple #2
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
Exemple #3
0
def asyncRecordTransaction(cardNum, amount, merchantUuid, merchantName, txType,
                           mccCode, txnApprovalVO, client):
    ssConst = SSConst()
    txnServ = TxnService()
    offerServ = OffersService()
    print "transaction recorded " + txnApprovalVO.to_JSON()
    approvalTypeJson = ssConst.getJson(ssConst.APPROVAL_TYPES)
    txn = txnServ.recordTxn(cardNum, txType, Decimal(amount), int(mccCode),
                            merchantUuid, merchantName, txnApprovalVO)
    merchant = Merchant.objects.get(pk=txn.merchant_id)

    print txnApprovalVO.approval + " ---- " + str(txn.cardId)

    if txnApprovalVO.sendNotif:
        screenMessage = ''
        try:
            notificationType = ssConst.DEVICE_NOTIFICATION_TYPES["Info"]
            msg = ""
            if txnApprovalVO.approval == 'Warn' or txnApprovalVO.approval == 'Approve':
                msg = txnApprovalVO.remarks
                if merchant.reviewStatus == 'RR':
                    notificationType = ssConst.DEVICE_NOTIFICATION_TYPES[
                        "ReviewRequired"]
            elif txnApprovalVO.approval == 'Block':
                # msg = "We denied a transaction on your card for Rs. "+str(amount) +"."
                msg = txnApprovalVO.remarks
                notificationType = ssConst.DEVICE_NOTIFICATION_TYPES["Block"]
            else:
                msg = "Approval required for " + ssConst.CURRENCY_SYMBOL + str(
                    amount) + " charge from " + merchant.name + "."
                notificationType = ssConst.DEVICE_NOTIFICATION_TYPES[
                    "ApprovalRequired"]
                screenMessage = txnApprovalVO.remarks
            consumerServ = ConsumerService()
            consumerDevice = consumerServ.getConsumerDevice(txn.consumerId)
            android = Android()
            android.sendTxnNotification(consumerDevice.deviceRegistrationId,
                                        msg, "Alert from " + client.name,
                                        notificationType, txn.id, txn.cardId,
                                        txnApprovalVO.approval, screenMessage)
        except Exception as e:
            print "Exception while sending communication " + e.message + " " + ssConst.APPROVAL_TYPES[
                0][0]

    # Aggregate Txn if it was approved
    txnServ.recalculateAgg(txn.consumerId, Decimal(amount), txn)

    # Process post_swipe_offer
    if txnApprovalVO.approval == 'Warn' or txnApprovalVO.approval == 'Approve':
        time.sleep(20)
        offerServ.processOfferNotification(txn.id,
                                           ssConst.OFFER_TARGET_TYPES[1][0],
                                           consumerDevice.deviceRegistrationId)
Exemple #4
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)
Exemple #5
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)