def processTxn(request): data = json.loads(request.body) print data try: validate(data) except Exception as e: return JSONResponse(SSUtil.err(e.message), status=status.HTTP_412_PRECONDITION_FAILED) tvServ = TxnValidatorService() txnApprovalVO = tvServ.getApproval(data['cardNum'], data['amount'], data['merchantUuid'], data['merchantName'], data['txType'], data['mccCode']) delayed = True millisStart = SSUtil.getMillis() if delayed: asyncRecordTransaction.delay(data['cardNum'], data['amount'], data['merchantUuid'], data['merchantName'], data['txType'], data['mccCode'], txnApprovalVO, request.user) else: asyncRecordTransaction(data['cardNum'], data['amount'], data['merchantUuid'], data['merchantName'], data['txType'], data['mccCode'], txnApprovalVO, request.user) millisEnd = SSUtil.getMillis() timeTaken = millisEnd - millisStart res={ "flag":txnApprovalVO.immediateApproval, "msg":txnApprovalVO.remarks, "time":timeTaken } return JSONResponse(SSUtil.success(res), status=status.HTTP_200_OK)
def registerCard(request): consumer = request.user data = json.loads(request.body) print data # TODO: Need to do it nicely if 'cardNum' not in data or data['cardNum'] is None: return JSONResponse(SSUtil.err(SSException.CARD_NUM_MISSING), status=status.HTTP_412_PRECONDITION_FAILED) elif 'phoneNum' not in data or data['phoneNum'] is None: return JSONResponse(SSUtil.err(SSException.PHONE_NUM_MISSING), status=status.HTTP_412_PRECONDITION_FAILED) elif 'activationCode' not in data or data['activationCode'] is None: return JSONResponse(SSUtil.err(SSException.ACTIVATION_CODE_MISSING), status=status.HTTP_412_PRECONDITION_FAILED) else: cs = CardService() try: cc = cs.registerCard(consumer.id, data['cardNum'], data['activationCode'], data['phoneNum']) serializer = ConsumerCardSerializer(cc, many=False) return JSONResponse(SSUtil.success(serializer.data), status=status.HTTP_201_CREATED) except Exception as ex: return JSONResponse(SSUtil.err(ex.args[0]), status=status.HTTP_400_BAD_REQUEST)
def addOffer(request): ms = MerchantService() title = None description = None endDate = None code = None codeType = "text" categoryId = None imgUrl = None if not 'title' in request.POST or request.POST['title'] is None: return JSONResponse(SSUtil.err("Title is required"), status=status.HTTP_412_PRECONDITION_FAILED) else: title = request.POST['title'] if 'description' in request.POST and not request.POST['description'] is None: description = request.POST['description'] if 'endDate' in request.POST and not request.POST['endDate'] is None: endDate = request.POST['endDate'] if 'code' in request.POST and not request.POST['code'] is None: code = request.POST['code'] if 'codeType' in request.POST and not request.POST['codeType'] is None: codeType = request.POST['codeType'] if 'imgUrl' in request.FILES and not request.FILES['imgUrl'] is None: print "imgurl is not none" imgUrl = request.FILES['imgUrl'] # try: ser = MerchantOfferSerializer(ms.addOffer(request.user.id,title,description, code,codeType,endDate,categoryId, imgUrl)) return JSONResponse(SSUtil.success(ser.data), status=status.HTTP_200_OK)
def merchant(request, format=None): if request.method == 'GET': try: ser = MerchantSerializer(request.user) return JSONResponse(SSUtil.success(ser.data), status=status.HTTP_200_OK) except Exception as e: return JSONResponse(SSUtil.err(e.message), status=status.HTTP_409_CONFLICT) elif request.method == 'PUT': try: data = json.loads(request.body) name = None description = None businessHours = None if 'name' in data and not data['name'] is None: name = data['name'] if 'description' in data and not data['description'] is None: description = data['description'] if 'businessHours' in data and not data['businessHours'] is None: businessHours = data['businessHours'] ms = MerchantService() merchant = ms.updateMerchant(request.user.id, name, description, businessHours) ser = MerchantSerializer(merchant) return JSONResponse(SSUtil.success(ser.data), status=status.HTTP_200_OK) except Exception as e: return JSONResponse(SSUtil.err(e.message), status=status.HTTP_409_CONFLICT)
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 toggleCardLockStatus(request, cardId): try: cs = ConsumerService() s = cs.toggleCardLockStatus(cardId, request.user.id) return JSONResponse(SSUtil.success(s), status=status.HTTP_200_OK) except: return JSONResponse(SSUtil.err(SSException.PROCESSING_FAILED), status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def getOffer(request, offerId): print "Here" ms = MerchantService(); try: serializer = MerchantOfferSerializer(ms.getOffer(offerId), many=False) return JSONResponse(SSUtil.success(serializer.data), status=status.HTTP_200_OK) except: return JSONResponse(SSUtil.err(SSException.INVALID_INPUT), status=status.HTTP_400_BAD_REQUEST)
def unlockMerchantStatus(request, merchantId): try: cs = ConsumerService() s = cs.unlockMerchantStatus(merchantId, request.user.id) return JSONResponse(SSUtil.success(s), status=status.HTTP_200_OK) except: return JSONResponse(SSUtil.err(SSException.PROCESSING_FAILED), status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def getReviewDtl(request, reviewId): try: ms = MerchantService(); res = ms.getReviewDtl(reviewId) return JSONResponse(SSUtil.success(res), status=status.HTTP_200_OK) except: return JSONResponse(SSUtil.err("Invalid data"), status=status.HTTP_400_BAD_REQUEST)
def getReviewTemplate(request, merchantId): try: ms = MerchantService(); serializer = ReviewTemplateSerializer(ms.getReviewTemplate(merchantId), many=False) return JSONResponse(SSUtil.success(serializer.data), status=status.HTTP_200_OK) except: return JSONResponse(SSUtil.err(SSException.INVALID_INPUT), status=status.HTTP_400_BAD_REQUEST)
def getPrefs(request, cardId): try: cs = ConsumerService() prefJson = cs.getPrefsJson(request.user.id, cardId) return JSONResponse(SSUtil.success(prefJson), status=status.HTTP_200_OK) except: return JSONResponse(SSUtil.err(SSException.PROCESSING_FAILED), status=status.HTTP_400_BAD_REQUEST)
def reviews(request, merchantId): try: ms = MerchantService(); ssConst = SSConst() data = ms.getReviewSummary(merchantId, ssConst.getStartTime("all_time")) return JSONResponse(SSUtil.success(data), status=status.HTTP_200_OK) except: return JSONResponse(SSUtil.err("No reviews available"), status=status.HTTP_400_BAD_REQUEST)
def searchTxns(request, cardId): searchServ = SearchService() if request.GET["q"] is None or request.GET["q"] == '': return JSONResponse(SSUtil.success(Array()), status=status.HTTP_200_OK) else: start = request.GET.get('start', 0) limit = request.GET.get('start', 10) res = searchServ.searchTxns(cardId, request.GET["q"], start, limit) return JSONResponse(SSUtil.success(res), status=status.HTTP_200_OK)
def getTxnAggSummary(request, format=None): if 'duration' not in request.GET or request.GET["duration"] is None or request.GET["duration"] == '': return JSONResponse(SSUtil.err("Duration is required"), status=status.HTTP_412_PRECONDITION_FAILED) try: ms = MerchantService(); data = ms.getTxnAggSummary(request.user.id, request.GET['duration']) return JSONResponse(SSUtil.success(data), status=status.HTTP_200_OK) except Exception as e: return JSONResponse(SSUtil.err(e.message), status=status.HTTP_409_CONFLICT)
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 getTxnMiscDetails(request, cardId, txnId): tServ = TxnService() try: txs = tServ.getTxnMiscDetails(request.user.id, cardId, txnId) if not txs is None: return Response(SSUtil.success(txs), status=status.HTTP_200_OK) else: return Response(SSUtil.err(SSException.INVALID_INPUT), status=status.HTTP_200_OK) except: return JSONResponse(SSUtil.err(SSException.INVALID_INPUT), status=status.HTTP_400_BAD_REQUEST)
def getTxn(request, cardId, txnId): tServ = TxnService() try: txn = tServ.getTxn(request.user.id, cardId, txnId) txs = ConsumerTxnSerializer(txn, many=False) if not txn is None: return Response(SSUtil.success(txs.data), status=status.HTTP_200_OK) else: return Response(SSUtil.err(SSException.INVALID_INPUT), status=status.HTTP_200_OK) except: return JSONResponse(SSUtil.err(SSException.INVALID_INPUT), status=status.HTTP_400_BAD_REQUEST)
def register(request, format=None): token = request.META.get('HTTP_SSTOKEN') try: if token is None: return JSONResponse(SSUtil.err(SSException.DEVICE_TOKEN_MISSING), status=status.HTTP_400_BAD_REQUEST) else: cs = ConsumerService() id = cs.registerDevice(token) return JSONResponse(SSUtil.success(id), status=status.HTTP_201_CREATED) except: return JSONResponse(SSUtil.err(SSException.DEVICE_TOKEN_EXISTS), status=status.HTTP_409_CONFLICT)
def markOfferStatus(request, offerId): ms = MerchantService(); st = None if not 'status' in request.DATA or request.DATA['status'] is None: return JSONResponse(SSUtil.err("Status is required"), status=status.HTTP_412_PRECONDITION_FAILED) else: st = request.DATA['status'] try: ser = MerchantOfferSerializer(ms.markOfferStatus(offerId,st)) return JSONResponse(SSUtil.success(ser.data), status=status.HTTP_200_OK) except: return JSONResponse(SSUtil.err(SSException.PROCESSING_FAILED), status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def saveLocation(request): data = json.loads(request.body) if not 'lat' in data or data['lat'] is None: return JSONResponse(SSUtil.err("Latitude is required"), status=status.HTTP_412_PRECONDITION_FAILED) if not 'lng' in data or data['lng'] is None: return JSONResponse(SSUtil.err("Longitude is required"), status=status.HTTP_412_PRECONDITION_FAILED) try: cs = ConsumerService() s = cs.saveLocation(request.user.id, data['lat'], data['lng']) return JSONResponse(SSUtil.success(s), status=status.HTTP_200_OK) except: return JSONResponse(SSUtil.err(SSException.PROCESSING_FAILED), status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def register(request, format=None): data = json.loads(request.body) if not 'uuid' in data or data['uuid'] is None: return JSONResponse(SSUtil.err("Merchant Id is required"), status=status.HTTP_412_PRECONDITION_FAILED) if not 'accessCode' in data or data['accessCode'] is None: return JSONResponse(SSUtil.err("Access Code is required"), status=status.HTTP_412_PRECONDITION_FAILED) try: ms = MerchantService(); merchant = ms.registerMerchant(data['uuid'], data['accessCode']) ser = MerchantSerializer(merchant) return JSONResponse(SSUtil.success(ser.data), status=status.HTTP_201_CREATED) except Exception as e: return JSONResponse(SSUtil.err(e.message), status=status.HTTP_409_CONFLICT)
def getOnDemandConsumerOffersByMerchant(self, consumerId, merchantId): ssConst = SSConst() txnService = TxnService() currTimeMillis = SSUtil.getMillis() merchantOffers = MerchantOffer.objects.filter( status=ssConst.OFFER_STATUSES[0][0], merchant_id=merchantId) miscDetails = txnService.getConsumerMerchantMiscDetails( consumerId, merchantId) for offer in merchantOffers: targettings = MerchantOfferTargetting.objects\ .all()\ .filter(offer_id=offer.id, targetType = ssConst.OFFER_TARGET_TYPES[4][0]) for targetting in targettings: if (int(miscDetails["totalOnMerchant"]) >= int( targetting.minTotalSpend) or int(miscDetails["visitsOnMerchant"]) >= int( targetting.minVisits)): self.addConsumerOfferIfRequired(consumerId, offer, offer.merchant_id) consumerOffers = ConsumerOffer.objects \ .filter(Q(status=ssConst.CONSUMER_OFFER_STATUSES[0][0]) | Q(status=ssConst.CONSUMER_OFFER_STATUSES[1][0]), consumerId=consumerId, merchantId = merchantId , endDate__gt=currTimeMillis ).order_by("-updated") return consumerOffers
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
def getMerchantOffers(request): ms = MerchantService(); st = None if 'status' in request.GET and not request.GET['status'] is None and request.GET['status'] != '': st = request.GET['status'] serializer = MerchantOfferSerializer(ms.getOffers(request.user.id, st), many=True) return JSONResponse(SSUtil.success(serializer.data), status=status.HTTP_200_OK)
def deleteLimitPrefs(request, cardId): data = json.loads(request.body) if not 'categoryKey' in data or data['categoryKey'] is None: return JSONResponse(SSUtil.err("Category key is required"), status=status.HTTP_412_PRECONDITION_FAILED) if not 'periodKey' in data or data['periodKey'] is None: return JSONResponse(SSUtil.err("Period key is required"), status=status.HTTP_412_PRECONDITION_FAILED) try: cs = ConsumerService() s = cs.deletePref(request.user.id, cardId, data['categoryKey'], data['periodKey']) return JSONResponse(SSUtil.success(s), status=status.HTTP_200_OK) except: return JSONResponse(SSUtil.err(SSException.PROCESSING_FAILED), status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def blockedMerchants(request): ms = MerchantService() serializer = ConsumerMerchantSerializer(ms.getBlockedMerchants( request.user.id), many=True) return JSONResponse(SSUtil.success(serializer.data), status=status.HTTP_200_OK)
def saveDeviceRegistrationId(request): data = json.loads(request.body) try: if data['registrationId'] is None: raise Exception() except Exception as e: return JSONResponse(SSUtil.err(e.message), status=status.HTTP_412_PRECONDITION_FAILED) try: cs = ConsumerService() s = cs.registerDeviceGCMRegistrationId(data['registrationId'], request.user.id) return JSONResponse(SSUtil.success(s), status=status.HTTP_200_OK) except: return JSONResponse(SSUtil.err(SSException.PROCESSING_FAILED), status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def addTag(self, txnId, tag): #first check if consumer has that tag consumerTag = None millis = SSUtil.getMillis() txn = ConsumerTxn.objects.get(id=txnId) try: consumerTag = ConsumerTag.objects.get(consumerId=txn.consumerId, tag=tag) except ConsumerTag.DoesNotExist: consumerTag = ConsumerTag() consumerTag.consumerId = txn.consumerId consumerTag.tag = tag consumerTag.created = millis consumerTag.updated = millis consumerTag.save() ## Now make mapping in txnTag try: txnTag = TxnTag.objects.get(cardId=txn.cardId, consumerTag_id=consumerTag.id, consumerTxn_id=txnId) except TxnTag.DoesNotExist: txnTag = TxnTag() txnTag.cardId = txn.cardId txnTag.consumerTag_id = consumerTag.id txnTag.consumerTxn_id = txnId txnTag.created = millis txnTag.updated = millis txnTag.save() txn = ConsumerTxn.objects.get(id=txnId) searchServ = SearchService() txnSerializer = ConsumerTxnSerializer(txn, many=False) searchServ.upload("txn", txn.id, txnSerializer.data) return consumerTag
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