示例#1
0
    def _handlePlanStatusRequests(self,
                                  type,
                                  endpoint,
                                  isPostRequest=False,
                                  data=None):

        # Checks if it is a post request
        if isPostRequest:
            response = requests.post(endpoint,
                                     headers=self.headers,
                                     data=json.dumps(data))
        else:
            response = requests.get(endpoint, headers=self.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 PlanStatusError(type, {
                "error": True,
                "returnedData": responseJson
            })
示例#2
0
    def _handleTransferStatusRequests(self,
                                      endpoint,
                                      isPostRequest=False,
                                      data=None):
        # Request headers
        headers = {
            'content-type': 'application/json',
        }

        # 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 initiate(self, transferDetails):
        # Performing shallow copy of transferDetails to avoid public exposing payload with secret key
        transferDetails = copy.copy(transferDetails)

        # adding reference if not already included
        if not ("reference" in transferDetails):
            transferDetails.update(
                {"reference": generateTransactionReference()})
        transferDetails.update({"seckey": self._getSecretKey()})

        # These are the parameters required to initiate a transfer
        requiredParameters = ["amount", "currency", "beneficiary_name"]

        checkIfParametersAreComplete(requiredParameters, transferDetails)
        checkTransferParameters(requiredParameters, transferDetails)

        # Collating request headers
        headers = {
            'content-type': 'application/json',
        }

        endpoint = self._baseUrl + self._endpointMap["transfer"]["initiate"]
        response = requests.post(endpoint,
                                 headers=headers,
                                 data=json.dumps(transferDetails))
        return self._handleInitiateResponse(response, transferDetails)
示例#4
0
    def _handleCardStatusRequests(self,
                                  type,
                                  endpoint,
                                  isPostRequest=False,
                                  data=None):
        #check if resposnse is a post response
        if isPostRequest:
            response = requests.post(endpoint,
                                     headers=self.headers,
                                     data=json.dumps(data))
        else:
            response = requests.get(endpoint, headers=self.headers)

        #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 RecepientStatusError(type, {
                "error": True,
                "returnedData": responseJson
            })
示例#5
0
    def bulk(self, bulkDetails):

        bulkDetails = copy.copy(bulkDetails)
        # Collating request headers
        headers = {
            'content-type': 'application/json',
        }

        bulkDetails.update({"seckey": self._getSecretKey()})

        requiredParameters = ["title", "bulk_data"]

        checkIfParametersAreComplete(requiredParameters, bulkDetails)

        checkTransferParameters(requiredParameters, bulkDetails)

        endpoint = self._baseUrl + self._endpointMap["transfer"]["bulk"]
        # Collating request headers
        headers = {
            'content-type': 'application/json',
        }
        response = requests.post(endpoint,
                                 headers=headers,
                                 data=json.dumps(bulkDetails))
        return self._handleBulkResponse(response, bulkDetails)
示例#6
0
    def refund(self, flwRef):
        """ This is used to refund a transaction from any of Rave's component objects.\n 
             Parameters include:\n
            flwRef (string) -- This is the flutterwave reference returned from a successful call from any component. You can access this from action["flwRef"] returned from the charge call
        """
        payload = {
            "ref": flwRef,
            "seckey": self._getSecretKey(),
        }
        headers = {"Content-Type": "application/json"}
        endpoint = self._baseUrl + self._endpointMap["refund"]

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

        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)
示例#7
0
    def validate(self, flwRef, otp, endpoint=None):
        """ This is the base validate call.\n
             Parameters include:\n
            flwRef (string) -- This is the flutterwave reference returned from a successful charge call. You can access this from action["flwRef"] returned from the charge call\n
            otp (string) -- This is the otp sent to the user \n
        """

        if not endpoint:
            endpoint = self._baseUrl + self._endpointMap["account"]["validate"]

        # Collating request headers
        headers = {
            'content-type': 'application/json',
        }

        payload = {
            "PBFPubKey": self._getPublicKey(),
            "transactionreference": flwRef,
            "transaction_reference": flwRef,
            "otp": otp
        }

        response = requests.post(endpoint,
                                 headers=headers,
                                 data=json.dumps(payload))
        return self._handleValidateResponse(response, flwRef)
示例#8
0
    def create(self, vcardDetails):
        vcardDetails = copy.copy(vcardDetails)
        vcardDetails.update({"seckey": self._getSecretKey()})
        
        requiredParameters = ["currency", "amount", "billing_name", "billing_address", "billing_city", "billing_state", "billing_postal_code", "billing_country"]
        checkIfParametersAreComplete(requiredParameters, vcardDetails)

        endpoint = self._baseUrl + self._endpointMap["virtual_card"]["create"]
        response = requests.post(endpoint, headers=self.headers, data=json.dumps(vcardDetails))
        return self._handleCreateResponse(response, vcardDetails)
示例#9
0
    def create(self, accountDetails):
        # Performing shallow copy of planDetails to avoid public exposing payload with secret key
        accountDetails = copy.copy(accountDetails)
        accountDetails.update({"seckey": self._getSecretKey()})
        requiredParameters = ["account_bank", "account_number", "business_name", "business_email", "business_contact", "business_contact_mobile", "business_mobile", "split_type", "split_value"]
        checkIfParametersAreComplete(requiredParameters, accountDetails)

        endpoint = self._baseUrl + self._endpointMap["subaccount"]["create"]
        response = requests.post(endpoint, headers=self.headers, data=json.dumps(accountDetails))
        return self._handleCreateResponse(response, accountDetails)
示例#10
0
    def create(self, details):
        # Performing shallow copy of planDetails to avoid public exposing payload with secret key
        details = copy.copy(details)
        details.update({"seckey": self._getSecretKey()})

        requiredParameters = ["service", "service_method", "service_version", "service_channel"]
        checkIfParametersAreComplete(requiredParameters, details)

        endpoint = self._baseUrl + self._endpointMap["bills"]["create"]
        response = requests.post(endpoint, headers=self.headers, data=json.dumps(details))
        return self._handleCreateResponse(response, details)
示例#11
0
    def create(self, details):
        # Performing shallow copy of planDetails to avoid public exposing payload with secret key
        details = copy.copy(details)
        details.update({"SECKEY": self._getSecretKey()})

        requiredParameters = ["numberofunits", "currency", "amount", "email", "txRef", "country"]
        checkIfParametersAreComplete(requiredParameters, details)

        endpoint = self._baseUrl + self._endpointMap["ebills"]["create"]
        response = requests.post(endpoint, headers=self.headers, data=json.dumps(details))
        return self._handleCreateResponse(response, details)
示例#12
0
    def create(self, details):
        details = copy.copy(details)
        details.update({"seckey": self._getSecretKey()})

        requiredParameters = ["account_number", "account_bank"]
        checkIfParametersAreComplete(requiredParameters, details)

        endpoint = self._baseUrl + self._endpointMap["recepient"]["create"]
        response = requests.post(endpoint,
                                 headers=self.headers,
                                 data=json.dumps(details))
        return self._handleCreateResponse(response, details)
示例#13
0
    def create(self, accountDetails):
        accountDetails = copy.copy(accountDetails)
        accountDetails.update({"seckey": self._getSecretKey()})

        requiredParameters = ["email", "narration"]
        checkIfParametersAreComplete(requiredParameters, accountDetails)

        endpoint = self._baseUrl + self._endpointMap["virtual_account"][
            "create"]
        response = requests.post(endpoint,
                                 headers=self.headers,
                                 data=json.dumps(accountDetails))
        return self._handleCreateResponse(response, accountDetails)
示例#14
0
    def create(self, planDetails):
        # Performing shallow copy of planDetails to avoid public exposing payload with secret key
        planDetails = copy.copy(planDetails)
        planDetails.update({"seckey": self._getSecretKey()})

        requiredParameters = ["amount", "name", "interval"]
        checkIfParametersAreComplete(requiredParameters, planDetails)

        endpoint = self._baseUrl + self._endpointMap["payment_plan"]["create"]
        response = requests.post(endpoint,
                                 headers=self.headers,
                                 data=json.dumps(planDetails))
        return self._handleCreateResponse(response, planDetails)
示例#15
0
 def capture(self, flwRef):
     """ This is called to complete the transaction.\n
          Parameters include:
         flwRef (string) -- This is the flutterwave reference you receive from action["flwRef"]
     """
     payload = {
         "SECKEY": self._getSecretKey(),
         "flwRef": flwRef
     }
     headers ={
         "Content-Type":"application/json"
     }
     endpoint = self._baseUrl + self._endpointMap["preauth"]["capture"]
     response = requests.post(endpoint, headers=headers, data=json.dumps(payload))
     return self._handleCaptureResponse(response, '')
示例#16
0
 def void(self, flwRef):
     """ This is called to void a transaction.\n 
          Parameters include:\n
         flwRef (string) -- This is the flutterwave reference you receive from action["flwRef"]\n
     """
     payload = {
         "SECKEY": self._getSecretKey(),
         "ref": flwRef,
         "action":"void"
     }
     headers ={
         "Content-Type":"application/json"
     }
     endpoint = self._baseUrl + self._endpointMap["preauth"]["refundorvoid"]
     response = requests.post(endpoint, headers=headers, data=json.dumps(payload))
     return self._handleRefundorVoidResponse(response, endpoint)
示例#17
0
    def verify(self, txRef, endpoint=None):
        """ This is used to check the status of a transaction.\n
             Parameters include:\n
            txRef (string) -- This is the transaction reference that you passed to your charge call. If you didn't define a reference, you can access the auto-generated one from payload["txRef"] or action["txRef"] from the charge call\n
        """
        if not endpoint:
            endpoint = self._baseUrl + self._endpointMap["verify"]

        # Collating request headers
        headers = {
            'content-type': 'application/json',
        }

        # Payload for the request headers
        payload = {"txref": txRef, "SECKEY": self._getSecretKey()}
        response = requests.post(endpoint,
                                 headers=headers,
                                 data=json.dumps(payload))
        return self._handleVerifyResponse(response, txRef)
示例#18
0
    def refund(self, flwRef, amount=None):
        """ This is called to refund the transaction.\n
             Parameters include:\n
            flwRef (string) -- This is the flutterwave reference you receive from action["flwRef"]\n
            amount (Number) -- (optional) This is called if you want a partial refund
        """
        payload = {
            "SECKEY": self._getSecretKey(),
            "ref": flwRef,
            "action":"refund"
        }
        if amount:
            payload["amount"] = amount

        headers ={
            "Content-Type":"application/json"
        }
        endpoint = self._baseUrl + self._endpointMap["preauth"]["refundorvoid"]
        response = requests.post(endpoint, headers=headers, data=json.dumps(payload))
        return self._handleRefundorVoidResponse(response, endpoint)
示例#19
0
    def _handleVerifyStatusRequests(self,
                                    endpoint,
                                    isPostRequest=False,
                                    data=None):
        self.headers = {'content-type': 'application/json'}
        #check if resposnse is a post response
        if isPostRequest:
            response = requests.post(endpoint,
                                     headers=self.headers,
                                     data=json.dumps(data))
        else:
            response = requests.get(endpoint, headers=self.headers)

        #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 BVNFetchError({"error": True, "returnedData": responseJson})
示例#20
0
    def charge(self,
               paymentDetails,
               requiredParameters,
               endpoint,
               shouldReturnRequest=False,
               isMpesa=False):
        """ This is the base charge call. It is usually overridden by implementing classes.\n
             Parameters include:\n
            paymentDetails (dict) -- These are the parameters passed to the function for processing\n
            requiredParameters (list) -- These are the parameters required for the specific call\n
            hasFailed (boolean) -- This is a flag to determine if the attempt had previously failed due to a timeout\n
            shouldReturnRequest -- This determines whether a request is passed to _handleResponses\n
        """
        # Checking for required components
        try:
            checkIfParametersAreComplete(requiredParameters, paymentDetails)
        except:
            raise

        # Performing shallow copy of payment details to prevent tampering with original
        paymentDetails = copy.copy(paymentDetails)

        # Adding PBFPubKey param to paymentDetails
        paymentDetails.update({"PBFPubKey": self._getPublicKey()})

        # Collating request headers
        headers = {
            'content-type': 'application/json',
        }
        if "token" in paymentDetails:
            paymentDetails.update({"SECKEY": self._getSecretKey()})
            # print(json.dumps(paymentDetails))
            response = requests.post(endpoint,
                                     headers=headers,
                                     data=json.dumps(paymentDetails))
        else:
            # Encrypting payment details (_encrypt is inherited from RaveEncryption)
            encryptedPaymentDetails = self._encrypt(json.dumps(paymentDetails))

            # Collating the payload for the request
            payload = {
                "PBFPubKey": paymentDetails["PBFPubKey"],
                "client": encryptedPaymentDetails,
                "alg": "3DES-24"
            }
            response = requests.post(endpoint,
                                     headers=headers,
                                     data=json.dumps(payload))

        if shouldReturnRequest:
            if isMpesa:
                return self._handleChargeResponse(response,
                                                  paymentDetails["txRef"],
                                                  paymentDetails, True)
            return self._handleChargeResponse(response,
                                              paymentDetails["txRef"],
                                              paymentDetails)
        else:
            if isMpesa:
                return self._handleChargeResponse(response,
                                                  paymentDetails["txRef"],
                                                  paymentDetails, True)
            return self._handleChargeResponse(response,
                                              paymentDetails["txRef"])