def post(cls): """ This method is used when merchant scans a Qr code of customer and send a pull payment request. :return: msg according to status of transaction """ _id = get_jwt_identity() payload = request.get_json() merchant = MerchantModel.find_merchant_by_id(_id) if merchant is None: return {"msg": INTERNAL_SERVER_ERROR}, 500 # Setting essential fields of Payload for pull funds transfer call payload["acquirerCountryCode"] = merchant.acquirerCountryCode payload["acquiringBin"] = merchant.acquiringBin payload["businessApplicationId"] = merchant.businessApplicationId payload["cardAcceptor"] = { "address": { "country": merchant.country, "state": merchant.state, "zipCode": merchant.zipCode }, "idCode": merchant.idCode, "name": merchant.name, "terminalId": merchant.terminalId } # Generates a unique system trace audit number. systemsTraceAuditNumber = str(uuid.uuid4().int >> 32)[0:6] while systemsTraceAuditNumber in SystemsTraceAuditNumber: systemsTraceAuditNumber = str(uuid.uuid4().int >> 32)[0:6] SystemsTraceAuditNumber.add(systemsTraceAuditNumber) payload["systemsTraceAuditNumber"] = systemsTraceAuditNumber payload["localTransactionDateTime"] = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S") payload["retrievalReferenceNumber"] = RetrievalNo.No() + str(systemsTraceAuditNumber) payload["senderPrimaryAccountNumber"] = "4895142232120006" # customer mobile number present in the qr scanned by merchant mobile_number = "" # customer wallet name wallet_name = "" # FLag tells whether we get mobile number from customer details or not. flag = False # status code is for transaction shows whether the transaction was successful or not status_code = False if "mobile_number" in payload: # mobile_number is present in payload flag = True # Setting payload for call to AuthApi for Confirmation of amount present in wallet of sender payloadAuthApi = {} mobile_number = payload["mobile_number"] wallet_name = payload["wallet_name"] payloadAuthApi["mobile_number"] = payload["mobile_number"] payloadAuthApi["wallet_name"] = payload["wallet_name"] del (payload["mobile_number"]) del (payload["wallet_name"]) payloadAuthApi["merchant_name"] = merchant.name payloadAuthApi["amount"] = payload["amount"] payloadAuthApi["systemsTraceAuditNumber"] = systemsTraceAuditNumber # call to authApi for confirmation of amount entered by customer. r = VisaNet.AmountConfirmation(payloadAuthApi) if r.status_code != 200: # Updating History for transaction failure. history = HistoryModel(amount=payload["amount"], transaction_id=systemsTraceAuditNumber, transaction_time=payload["localTransactionDateTime"], merchant_mobile_number=merchant.mobile_number, customer_mobile_number=mobile_number, customer_wallet_name=wallet_name, merchant_name=merchant.name, status=status_code ) history.save_to_db() return {'msg': PAYMENT_CANNOT_BE_COMPLETED}, 400 # deleting nonessential fields of payload if "wallet_name" in payload: del(payload["wallet_name"]) # Sending pull payments request to helper function response = FundsTransfer.merchant_pull_payments_post_response(payload) if response.status_code != 200: if flag: payloadAuthApi = { 'mobile_number': mobile_number, 'pan': payload["senderPrimaryAccountNumber"], 'systemsTraceAuditNumber': systemsTraceAuditNumber, 'code': response.status_code } # Sending request for rollback of payment denoted by code of payload r = VisaNet.TransactionConfirmation(payloadAuthApi) # setting history for payment failure history = HistoryModel(amount=payload["amount"], transaction_id=systemsTraceAuditNumber, transaction_time=payload["localTransactionDateTime"], merchant_mobile_number=merchant.mobile_number, customer_mobile_number=mobile_number, customer_wallet_name=wallet_name, merchant_name=merchant.name, status=status_code ) # saving history in the database history.save_to_db() return {"msg": INTERNAL_SERVER_ERROR}, 500 # payment approved by visa pull funds transfer api if response.status_code == 200: if flag: payloadAuthApi = { 'mobile_number': mobile_number, 'pan': payload["senderPrimaryAccountNumber"], 'systemsTraceAuditNumber': systemsTraceAuditNumber, 'code': response.status_code } # Sending confirmation of transaction to Auth api denoted by code of transaction r = VisaNet.TransactionConfirmation(payloadAuthApi) status_code = True # setting history for payment success history = HistoryModel(amount=payload["amount"], transaction_id=systemsTraceAuditNumber, transaction_time=payload["localTransactionDateTime"], merchant_mobile_number=merchant.mobile_number, customer_mobile_number=mobile_number, customer_wallet_name=wallet_name, merchant_name=merchant.name, status=status_code ) # Saving history in the database. history.save_to_db() return response
def put(cls): """ Completes the payment via VISA NET using mVisa API. Also supports functionality of rolling back the payment. """ payload = request.get_json() mobile_number = payload["mobile_number"] wallet_name = payload["wallet_name"] del (payload["mobile_number"]) del (payload["wallet_name"]) try: virtual_card = VirtualCardModel.find_by_mobile_number( mobile_number) except: return {"msg": INTERNAL_SERVER_ERROR}, 500 if not virtual_card: return {'msg': CARD_NOT_GENERATED}, 400 wallet_response = wallet.get_amount(mobile_number, float(payload['amount'])) pan = cipher.decrypt(virtual_card.pan) if wallet_response is None: return {'msg': INTERNAL_SERVER_ERROR}, 500 if wallet_response.status_code == 404: return Decryption.decrypt(wallet_response.json()), 401 systems_trace_audit_number = str(uuid.uuid4().int >> 32)[0:6] last_transaction_time = datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S") payload['senderAccountNumber'] = pan payload['systemsTraceAuditNumber'] = systems_trace_audit_number payload['retrievalReferenceNumber'] = str(datetime.utcnow().strftime("%y%d%H")) + \ systems_trace_audit_number payload['localTransactionDateTime'] = last_transaction_time visa_response = MVisa.merchant_push_payments_post_payload(payload) print(pan, visa_response.status_code, visa_response.json()) if visa_response is None: wallet_response = wallet.send_amount(mobile_number, float(payload['amount'])) return {"msg": ROLL_BACK}, 500 visa_response_status = visa_response.status_code visa_response = visa_response.json() if visa_response_status != 200: wallet_response = wallet.send_amount(mobile_number, float(payload['amount'])) return {"msg": ROLL_BACK}, 500 virtual_card.last_transaction_time = last_transaction_time history = HistoryModel(payload['amount'], last_transaction_time, mobile_number, systems_trace_audit_number, payload["cardAcceptor"]["name"], wallet_name, "Success") virtual_card.count += 1 try: virtual_card.save_to_db() history.save_to_db() except: return {"msg": DATABASE_ERROR}, 500 return {'msg': visa_response}, 200
def post(cls): _id = get_jwt_identity() payload = request.get_json() merchant = MerchantModel.find_merchant_by_id(_id) if merchant is None: return {"msg": INTERNAL_SERVER_ERROR}, 500 payload["acquirerCountryCode"] = merchant.acquirerCountryCode payload["acquiringBin"] = merchant.acquiringBin payload["businessApplicationId"] = merchant.businessApplicationId payload["cardAcceptor"] = { "address": { "country": merchant.country, "state": merchant.state, "zipCode": merchant.zipCode }, "idCode": merchant.idCode, "name": merchant.name, "terminalId": merchant.terminalId } systemsTraceAuditNumber = str(uuid.uuid4().int >> 32)[0:6] while systemsTraceAuditNumber in SystemsTraceAuditNumber: systemsTraceAuditNumber = str(uuid.uuid4().int >> 32)[0:6] SystemsTraceAuditNumber.add(systemsTraceAuditNumber) payload["systemsTraceAuditNumber"] = systemsTraceAuditNumber payload["localTransactionDateTime"] = datetime.utcnow().strftime( "%Y-%m-%dT%H:%M:%S") payload["retrievalReferenceNumber"] = RetrievalNo.No() + str( systemsTraceAuditNumber) payload["senderPrimaryAccountNumber"] = "4895142232120006" # print(payload) mobile_number = "" wallet_name = "" flag = False status_code = False if "mobile_number" in payload: flag = True payloadAuthApi = {} mobile_number = payload["mobile_number"] wallet_name = payload["wallet_name"] payloadAuthApi["mobile_number"] = payload["mobile_number"] payloadAuthApi["wallet_name"] = payload["wallet_name"] del (payload["mobile_number"]) del (payload["wallet_name"]) payloadAuthApi["merchant_name"] = merchant.name payloadAuthApi["amount"] = payload["amount"] payloadAuthApi["systemsTraceAuditNumber"] = systemsTraceAuditNumber r = VisaNet.AmountConfirmation(payloadAuthApi) print(r) print(r.json()) if r.status_code != 200: history = HistoryModel( amount=payload["amount"], transaction_id=systemsTraceAuditNumber, transaction_time=payload["localTransactionDateTime"], merchant_mobile_number=merchant.mobile_number, customer_mobile_number=mobile_number, customer_wallet_name=wallet_name, merchant_name=merchant.name, status=status_code) history.save_to_db() return {'msg': PAYMENT_CANNOT_BE_COMPLETED}, 400 if "wallet_name" in payload: del (payload["wallet_name"]) response = FundsTransfer.merchant_pull_payments_post_response(payload) if response.status_code != 200: if flag: payloadAuthApi = { 'mobile_number': mobile_number, 'pan': payload["senderPrimaryAccountNumber"], 'systemsTraceAuditNumber': systemsTraceAuditNumber, 'code': response.status_code } r = VisaNet.TransactionConfirmation(payloadAuthApi) history = HistoryModel( amount=payload["amount"], transaction_id=systemsTraceAuditNumber, transaction_time=payload["localTransactionDateTime"], merchant_mobile_number=merchant.mobile_number, customer_mobile_number=mobile_number, customer_wallet_name=wallet_name, merchant_name=merchant.name, status=status_code) history.save_to_db() return {"msg": INTERNAL_SERVER_ERROR}, 500 if response.status_code == 200: if flag: payloadAuthApi = { 'mobile_number': mobile_number, 'pan': payload["senderPrimaryAccountNumber"], 'systemsTraceAuditNumber': systemsTraceAuditNumber, 'code': response.status_code } r = VisaNet.TransactionConfirmation(payloadAuthApi) status_code = True history = HistoryModel( amount=payload["amount"], transaction_id=systemsTraceAuditNumber, transaction_time=payload["localTransactionDateTime"], merchant_mobile_number=merchant.mobile_number, customer_mobile_number=mobile_number, customer_wallet_name=wallet_name, merchant_name=merchant.name, status=status_code) history.save_to_db() # response = FundsTransfer.merchant_push_payments_post_response() return response