def offerTargetting(request, offerId, targettingId): ms = MerchantService(); if request.method == 'GET': try: serializer = MerchantOfferTargettingSerializer(ms.getOfferTargetting(targettingId), 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) elif request.method == 'DELETE': try: flag = ms.deleteOfferTargetting(targettingId) if flag: return JSONResponse(SSUtil.success(flag), status=status.HTTP_200_OK) else: return JSONResponse(SSUtil.err(flag), status=status.HTTP_200_OK) except: return JSONResponse(SSUtil.err(SSException.INVALID_INPUT), status=status.HTTP_400_BAD_REQUEST) else: data = json.loads(request.body) targetType = None minVisits = 0 minTotalSpend = 0 if 'targetType' in data and not data['targetType'] is None: targetType = data['targetType'] if 'minVisits' in data and not data['minVisits'] is None: minVisits = data['minVisits'] if 'minTotalSpend' in data and not data['minTotalSpend'] is None: minTotalSpend = data['minTotalSpend'] try: ser = MerchantOfferTargettingSerializer(ms.updateOfferTargetting(targettingId, targetType, minVisits, minTotalSpend)) 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 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 reviewTemplate(request): if request.method == 'GET': try: ms = MerchantService(); serializer = ReviewTemplateSerializer(ms.getReviewTemplate(request.user.id), 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) if request.method == 'PUT': data = json.loads(request.body) if not 'criteria1' in data or data['criteria1'] is None: return JSONResponse(SSUtil.err("Criteria1 is required"), status=status.HTTP_412_PRECONDITION_FAILED) if not 'criteria2' in data or data['criteria2'] is None: return JSONResponse(SSUtil.err("Criteria2 is required"), status=status.HTTP_412_PRECONDITION_FAILED) if not 'criteria3' in data or data['criteria3'] is None: return JSONResponse(SSUtil.err("Criteria3 is required"), status=status.HTTP_412_PRECONDITION_FAILED) try: ms = MerchantService(); res = ReviewTemplateSerializer(ms.saveReviewTemplate(request.user.id, data['criteria1'], data['criteria2'], data['criteria3']),many=False) return JSONResponse(SSUtil.success(res.data), status=status.HTTP_200_OK) except: return JSONResponse(SSUtil.err(SSException.INVALID_INPUT), status=status.HTTP_400_BAD_REQUEST)
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 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 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 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 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 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 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 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 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 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 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 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 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 unlockTxTypeStatus(request, cardId): data = json.loads(request.body) try: if data['txType'] is None: raise Exception() except Exception as e: return JSONResponse(SSUtil.err(e.message), status=status.HTTP_412_PRECONDITION_FAILED) try: cs = CardService() if data['txType'] == 'Intenational': data['txType'] = 'International' s = cs.unlockTxTypeStatus(cardId, data['txType']) 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 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 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 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 addTag(request, txnId): data = json.loads(request.body) try: if data['tag'] is None: raise Exception() except Exception as e: return JSONResponse(SSUtil.err("Tag is missing"), status=status.HTTP_412_PRECONDITION_FAILED) print "tag is " + str(data['tag']) try: ts = TxnService() s = ts.addTag(txnId, data['tag']) ser = ConsumerTagSerializer(s, many=False) 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 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 saveReviewResponse(request, reviewId): data = json.loads(request.body) if not 'merchantComment' in data or data['merchantComment'] is None: return JSONResponse(SSUtil.err("Merchant comment is required"), status=status.HTTP_412_PRECONDITION_FAILED) offerId = None if 'offerId' in data and not data['offerId'] is None: offerId = data['offerId'] try: ms = MerchantService(); res = ms.saveReviewResponse(reviewId, data['merchantComment'], offerId) if res is None: return JSONResponse(SSUtil.err(False), status=status.HTTP_200_OK) else: ser = TxnReviewSerializer(res, many=False) return JSONResponse(SSUtil.success(ser.data), status=status.HTTP_200_OK) except: return JSONResponse(SSUtil.err("Invalid data"), status=status.HTTP_400_BAD_REQUEST)
def saveApproval(request, txnId): ssConst = SSConst() millis = SSUtil.getMillis() data = json.loads(request.body) try: if data['approval'] is None: raise Exception() except Exception as e: return JSONResponse(SSUtil.err(e.message), status=status.HTTP_412_PRECONDITION_FAILED) try: txn = ConsumerTxn.objects.get(pk=txnId) if data['approval'] == str(1): txn.ssApprovalStatus = ssConst.APPROVAL_STATUSES[3][0] else: txn.ssApprovalStatus = ssConst.APPROVAL_STATUSES[4][0] txn.updated = millis txn.save() return Response(SSUtil.success(1), status=status.HTTP_200_OK) except: return JSONResponse(SSUtil.err(SSException.INVALID_INPUT), status=status.HTTP_400_BAD_REQUEST)
def offer(request, offerId): ms = MerchantService(); if request.method == 'GET': 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) else: title = None description = None endDate = None code = None codeType = "text" categoryId = None if not 'title' in request.DATA or request.DATA['title'] is None: return JSONResponse(SSUtil.err("Title is required"), status=status.HTTP_412_PRECONDITION_FAILED) else: title = request.DATA['title'] if 'description' in request.DATA and not request.DATA['description'] is None: description = request.DATA['description'] if 'endDate' in request.DATA and not request.DATA['endDate'] is None: endDate = request.DATA['endDate'] if 'code' in request.DATA and not request.DATA['code'] is None: code = request.DATA['code'] if 'codeType' in request.DATA and not request.DATA['codeType'] is None: codeType = request.DATA['codeType'] if 'categoryId' in request.DATA and not request.DATA['categoryId'] is None: categoryId = request.DATA['categoryId'] try: ser = MerchantOfferSerializer(ms.updateOffer(offerId,title,description, code,codeType,endDate)) 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 card(request, pk): if request.method == 'GET': cs = CardService() ts = TxnService() cls = ClientService() try: card = cs.getCards(request.user.id, pk) txns = ts.getTxns(request.user.id, pk, 0, 20, None, None) ccs = ConsumerCardSerializer(card, many=False) txs = ConsumerTxnSerializer(txns, many=True) cl = cls.get(card.clientId) client = ClientSerializer(cl, many=False) return Response(SSUtil.success({ "card": ccs.data, "txns": txs.data, "client": client.data }), status=status.HTTP_200_OK) except: return JSONResponse(SSUtil.err(SSException.INVALID_INPUT), status=status.HTTP_400_BAD_REQUEST) else: return JSONResponse(SSUtil.err(SSException.METHOD_NOT_APPLICABLE), status=status.HTTP_400_BAD_REQUEST)
def saveLimitPrefs(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) if not 'userLimit' in data or data['userLimit'] is None: return JSONResponse(SSUtil.err("User specified limit is required"), status=status.HTTP_412_PRECONDITION_FAILED) if not 'action' in data or data['action'] is None: return JSONResponse(SSUtil.err("action is required"), status=status.HTTP_412_PRECONDITION_FAILED) try: cs = ConsumerService() s = cs.savePref(request.user.id, cardId, data['categoryKey'], data['periodKey'], data['action'], data['userLimit']) ser = ConsumerPrefsSerializer(s, many=False) 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 getTxnTaggedSummary(request, cardId): tServ = TxnService() try: try: start = int(request.GET['start']) except: start = 0 try: limit = int(request.GET['limit']) except: limit = 100 summary = tServ.getTxnTaggedSummary(request.user.id, cardId, start, limit) sumSer = TxnTagSummarySerializer(summary, many=True) return Response(SSUtil.success(sumSer.data), status=status.HTTP_200_OK) except: return JSONResponse(SSUtil.err(SSException.INVALID_INPUT), status=status.HTTP_400_BAD_REQUEST)