コード例 #1
0
    def _preliminaryResponseChecks(self, response, TypeOfErrorToRaise,
                                   reference):
        # Check if we can obtain a json
        try:
            responseJson = response.json()
        except:
            raise ServerError({
                "error": True,
                "reference": reference,
                "errMsg": response
            })

        # Check if the response contains data parameter
        if not responseJson.get("data", None):
            raise TypeOfErrorToRaise({
                "error":
                True,
                "reference":
                reference,
                "errMsg":
                responseJson.get("message", "Server is down")
            })

        # Check if it is returning a 200
        if not response.ok:
            errMsg = responseJson["data"].get("message", None)
            raise TypeOfErrorToRaise({"error": True, "errMsg": errMsg})

        return responseJson
コード例 #2
0
    def _handleTransferStatusRequests(self,
                                      endpoint,
                                      isPostRequest=False,
                                      data=None):
        # Request headers
        headers = {
            'content-type': 'application/json',
            'authorization': 'Bearer ' + self._getSecretKey(),
        }

        # Checks if it is a post request
        if isPostRequest:
            response = requests.post(endpoint,
                                     headers=headers,
                                     data=json.dumps(data))
        else:
            response = requests.get(endpoint, headers=headers)

        # Checks if it can be parsed to json
        try:
            responseJson = response.json()
        except:
            raise ServerError({"error": True, "errMsg": response.text})

        # Checks if it returns a 2xx code
        if response.ok:
            return {"error": False, "returnedData": responseJson}
        else:
            raise TransferFetchError({
                "error": True,
                "returnedData": responseJson
            })
コード例 #3
0
    def _preliminaryResponseChecks(self, response, TypeOfErrorToRaise, tx_ref=None, flw_ref=None):
        preliminary_error_response = copy.deepcopy(response_object)
        preliminary_error_response = Payment.deleteUnnecessaryKeys(preliminary_error_response, "transactionComplete", "chargecode", "vbvmessage", "vbvcode", "acctmessage", "currency")

        # Check if we can obtain a json
        try:
            responseJson = response.json()
        except:
            raise ServerError({"error": True, "tx_ref": tx_ref, "flw_ref": flw_ref, "errMsg": response})


        # Check if the response contains data parameter
        if responseJson.get("data", None):
            if tx_ref:
                flw_ref = responseJson["data"].get("flw_ref", None)
            if flw_ref:
                tx_ref = responseJson["data"].get("tx_ref", None)
        else:
            raise TypeOfErrorToRaise({"error": True, "tx_ref": tx_ref, "flw_ref": flw_ref, "errMsg": responseJson.get("message", "Server is down")})
        
        # Check if it is returning a 200
        if not response.ok:
            errMsg = responseJson["data"].get("message", None)
            raise TypeOfErrorToRaise({"error": True, "tx_ref": tx_ref, "flw_ref": flw_ref, "errMsg": errMsg})
        
        return {"json": responseJson, "flw_ref": flw_ref, "tx_ref": tx_ref}
コード例 #4
0
    def _preliminaryResponseChecks(self, response, TypeOfErrorToRaise, name):
        #check if we can get json
        try:
            responseJson = response.json()
        except:
            raise ServerError({
                "error": True,
                "name": name,
                "errMsg": response
            })

        #check for data parameter in response
        if not responseJson.get("data", None):
            raise TypeOfErrorToRaise({
                "error":
                True,
                "name":
                name,
                "errMsg":
                responseJson.get("message", "Server is down")
            })

        #check for 200 response
        if not response.ok:
            errMsg = response["data"].get("message", None)
            raise TypeOfErrorToRaise({"error": True, "errMsg": errMsg})

        return responseJson
コード例 #5
0
    def _handleCardStatusRequests(self, type, endpoint, method, data=None):
        
        #check if response is a post response
        if method == 'GET':
            if data == None:
                response = requests.get(endpoint, headers=self.headers)
            else:
                response = requests.get(endpoint, headers=self.headers, data=json.dumps(data))
        elif method == 'POST':
            response = requests.post(endpoint, headers=self.headers, data=json.dumps(data))
        elif method == 'PUT':
            response = requests.put(endpoint, headers=self.headers)
        elif method == 'PATCH':
            response = requests.patch(endpoint, headers=self.headers, data=json.dumps(data))
        else:
            response = requests.delete(endpoint, headers=self.headers, data=json.dumps(data))
        
        #check if it can be parsed to JSON
        try:
            responseJson = response.json()
        except:
            raise ServerError({"error": True, "errMsg": response.text})

        if response.ok:
            return {"error": False, "returnedData": responseJson}
        else:
            raise CardStatusError(type, {"error": True, "returnedData": responseJson })
コード例 #6
0
    def _handleTransferStatusRequests(self, endpoint, method, data=None):
        # Request headers
        headers = {
            'content-type': 'application/json',
            'authorization': 'Bearer ' + self._getSecretKey(),
        }

        #check if response is a post response
        if method == 'GET':
            if data == None:
                response = requests.get(endpoint, headers=headers)
            else:
                response = requests.get(endpoint,
                                        headers=headers,
                                        data=json.dumps(data))
        elif method == 'POST':
            response = requests.post(endpoint, headers=headers)
        elif method == 'PUT':
            response = requests.put(endpoint, headers=headers)
        elif method == 'PATCH':
            response = requests.patch(endpoint,
                                      headers=headers,
                                      data=json.dumps(data))
        else:
            response = requests.delete(endpoint,
                                       headers=headers,
                                       data=json.dumps(data))

        # Checks if it can be parsed to json
        try:
            responseJson = response.json()
        except:
            raise ServerError({"error": True, "errMsg": response.text})

        # Checks if it returns a 2xx code
        if response.ok:
            return {"error": False, "returnedData": responseJson}
        else:
            raise TransferFetchError({
                "error": True,
                "returnedData": responseJson
            })
コード例 #7
0
    def refund(self, details):
        """ This is used to refund a transaction from any of Rave's component objects.\n 
             Parameters include:\n
            flw_ref (string) -- This is the flutterwave reference returned from a successful call from any component. You can access this from action["flw_ref"] returned from the charge call
        """
        
        headers = {
            "Content-Type":"application/json",
            'authorization' : 'Bearer ' + self._getSecretKey(),
        }
        endpoint = self._baseUrl + self._endpointMap["refund"]

        response = requests.post(endpoint, headers = headers, data=json.dumps(details))

        try:
            responseJson = response.json()
        except ValueError:
            raise ServerError(response)
        
        if responseJson.get("status", None) == "error":
            raise RefundError(responseJson.get("message", None))
        elif responseJson.get("status", None) == "success":
            return True, responseJson.get("data", None)