Beispiel #1
0
 def test_pay_method_with_non_str_required_arguments_fails(self):
     with self.assertRaises(exceptions.InvalidArgumentError):
         payment_amount = json_builder.amount(currency=12457,
                                              value=12457)
         payment_metadata = json_builder.metadata(**{12457: 12457})
         payment_links = json_builder.links(callback_url=12457)
         json_builder.pay(12457,
                          payment_amount,
                          payment_metadata,
                          payment_links)
Beispiel #2
0
    def send_pay(self,
                 bearer_token,
                 callback_url,
                 destination,
                 value,
                 currency='KES',
                 **kwargs):
        """
        Creates an outgoing pay to a third party. The result of
        the pay is provided asynchronously and posted to the callback_url
        provided.
        Returns a request response object < class, 'requests.models.Response'>
        :param bearer_token: Access token to be used to make calls to
        the Kopo Kopo API
        :type bearer_token: str
        :param callback_url:
        :type callback_url: str
        :param destination: ID of the destination of funds.
        :type destination: str
        :param value: Value of money to be sent (child of amount JSON str)
        :type value: str
        :param currency: Currency of amount being transacted
        :type currency: str
        :param kwargs: Provision for optional metadata with maximum of 5
        key value pairs.
        :type kwargs: dict

        :return:requests.models.Response
        """
        # build send_pay url
        send_pay_url = self._build_url(SEND_PAY_PATH)

        # define headers
        headers = dict(self.headers)

        # check bearer token
        validation.validate_string_arguments(bearer_token)

        # add authorization to headers
        headers['Authorization'] = 'Bearer ' + bearer_token + ''

        # create amount json object
        pay_amount = json_builder.amount(currency=currency, value=value)

        # create metadata json object
        pay_metadata = json_builder.metadata(', '.join(
            ['{}={}'.format(k, v) for k, v in kwargs.items()]))

        # create links json object
        pay_links = json_builder.links(callback_url=callback_url)

        # create payment json object
        pay_json = json_builder.pay(destination, pay_amount, pay_metadata,
                                    pay_links)

        return self._make_requests(url=send_pay_url,
                                   method='POST',
                                   payload=pay_json,
                                   headers=headers)
 def test_successful_create_pay_request_to_bank_account(self):
     response = requests.post(
         headers=PayTestCase.header,
         json=json_builder.pay(
             "c533cb60-8501-440d-8150-7eaaff84616a", "bank_account",
             json_builder.amount('KES', 'python_sdk_value'), "test",
             json_builder.links(
                 "https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d"
             ),
             json_builder.metadata({
                 "cId": '8_675_309',
                 "notes": 'Salary payment May 2018'
             })),
         data=None,
         url=PayTestCase.pay_obj._build_url(pay.SEND_PAY_PATH))
     self.assertEqual(response.status_code, 201)
 def test_successful_create_pay_request_to_mobile_wallet(self):
     response = requests.post(
         headers=PayTestCase.header,
         json=json_builder.pay(
             "9764ef5f-fcd6-42c1-bbff-de280becc64b", "mobile_wallet",
             json_builder.amount('KES', 'python_sdk_value'), "test",
             json_builder.links(
                 "https://webhook.site/52fd1913-778e-4ee1-bdc4-74517abb758d"
             ),
             json_builder.metadata({
                 "cId": '8_675_309',
                 "notes": 'Salary payment May 2018'
             })),
         data=None,
         url=PayTestCase.pay_obj._build_url(pay.SEND_PAY_PATH))
     self.assertEqual(response.status_code, 201)
Beispiel #5
0
 def test_pay_method_with_all_required_arguments_succeeds(self):
     pay_json = json_builder.pay(payment_destination='sample_payment_destination',
                                 payment_amount='sample_payment_amount',
                                 payment_metadata='sample_payment_metadata',
                                 payment_links='sample_payment_links')
     self.assertIsNotNone(pay_json)
Beispiel #6
0
    def send_pay(self, kwargs):
        """
        Creates an outgoing pay to a third party. The result of
        the pay is provided asynchronously and posted to the callback_url
        provided.
        Returns a request response object < class, 'requests.models.Response'>
        :param kwargs: Provision for optional metadata with maximum of 5
        key value pairs.
        :type kwargs: dict

        :return:requests.models.Response
        """
        if 'access_token' not in kwargs:
            raise exceptions.InvalidArgumentError('Access Token not given.')

        if 'destination_reference' not in kwargs or \
                'destination_type' not in kwargs or \
                'callback_url' not in kwargs or \
                'description' not in kwargs or \
                'amount' not in kwargs:
            raise exceptions.InvalidArgumentError(
                'Invalid arguments for creating Outgoing Pay.')

        if 'currency' not in kwargs:
            currency = 'KES'

        if 'metadata' not in kwargs:
            pay_metadata = ''

        # iterate through kwargs
        if 'access_token' in kwargs:
            bearer_token = kwargs['access_token']
        if 'callback_url' in kwargs:
            callback_url = kwargs['callback_url']
        if 'description' in kwargs:
            description = kwargs['description']
        if 'currency' in kwargs:
            currency = 'KES'
        if 'metadata' in kwargs:
            pay_metadata = json_builder.metadata(kwargs['metadata'])

        # build send_pay url
        send_pay_url = self._build_url(SEND_PAY_PATH)

        # define headers
        headers = dict(self._headers)

        # check bearer token
        validation.validate_string_arguments(bearer_token)

        # add authorization to headers
        headers['Authorization'] = 'Bearer ' + bearer_token + ''

        # create amount json object
        pay_amount = json_builder.amount(currency=currency,
                                         value=kwargs['amount'])

        # create links json object
        pay_links = json_builder.links(callback_url=callback_url)

        # create payment json object
        pay_json = json_builder.pay(kwargs['destination_reference'],
                                    kwargs['destination_type'], pay_amount,
                                    description, pay_links, pay_metadata)

        return self._make_requests(url=send_pay_url,
                                   method='POST',
                                   payload=pay_json,
                                   headers=headers)