예제 #1
0
    def create_subscription(self, bearer_token, event_type, webhook_endpoint,
                            webhook_secret):
        """
        Creates a subscription to a webhook service.
        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 event_type:Type of subscription event. Should be one of:
        buygoods_transaction_received, buygoods_transaction_reversed,
        settlement_transfer_completed, customer_created
        :type event_type:  str
        :param webhook_endpoint: HTTP end point to send the webhook.
        :type webhook_endpoint: str
        :param webhook_secret: Secret used to encrypt the request payload using HMAC.
        :type webhook_secret: str
        :return: requests.models.Response
        """
        # event types
        event_types_to_check = [
            'b2b_transaction_received', 'buygoods_transaction_received',
            'buygoods_transaction_reversed',
            'merchant_to_merchant_transaction_received',
            'settlement_transfer_completed', 'customer_created'
        ]
        # build subscription url
        subscription_url = self._build_url(WEBHOOK_SUBSCRIPTION_PATH)

        # define headers
        headers = dict(self.headers)

        # validate string arguments
        validation.validate_string_arguments(bearer_token, event_type,
                                             webhook_endpoint, webhook_secret)

        headers['Authorization'] = 'Bearer ' + bearer_token + ''

        if not any(check in event_type for check in event_types_to_check):
            raise exceptions.InvalidArgumentError(
                'Event type not recognized by k2-connect')

        # validate webhook endpoint
        validation.validate_url(webhook_endpoint)

        # define subscription payload
        subscription_payload = json_builder.webhook_subscription(
            event_type=event_type,
            webhook_endpoint=webhook_endpoint,
            webhook_secret=webhook_secret)

        return self._make_requests(headers=headers,
                                   method='POST',
                                   url=subscription_url,
                                   payload=subscription_payload)
예제 #2
0
 def test_create_b2b_webhook_subscription_request_succeeds(self):
     response = requests.post(
         headers=WebhooksTestCase.header,
         json=json_builder.webhook_subscription(
             "b2b_transaction_received",
             "https://webhook.site/dcbdce14-dd4f-4493-be2c-ad3526354fa8",
             'till', '112233'),
         data=None,
         url=WebhooksTestCase.webhook_obj._build_url(
             webhooks.WEBHOOK_SUBSCRIPTION_PATH),
     )
     self.assertEqual(response.status_code, 201)
예제 #3
0
 def test_create_settlement_transfer_webhook_subscription_request_succeeds(
         self):
     response = requests.post(
         headers=WebhooksTestCase.header,
         json=json_builder.webhook_subscription(
             "settlement_transfer_completed",
             "https://webhook.site/dcbdce14-dd4f-4493-be2c-ad3526354fa8",
             'company'),
         data=None,
         url=WebhooksTestCase.webhook_obj._build_url(
             webhooks.WEBHOOK_SUBSCRIPTION_PATH),
     )
     self.assertEqual(response.status_code, 201)
예제 #4
0
 def test_webhook_subscription_method_with_non_str_required_arguments_fails(self):
     with self.assertRaises(exceptions.InvalidArgumentError):
         json_builder.webhook_subscription(event_type=12457,
                                           webhook_endpoint=12457,
                                           webhook_secret=12457)
예제 #5
0
 def test_webhook_subscription_method_with_all_required_arguments_succeeds(self):
     webhook_subscription_json = json_builder.webhook_subscription(event_type='sample_event_type',
                                                                   webhook_endpoint='sample_webhook_endpoint',
                                                                   webhook_secret='sample_webhook_secret')
     self.assertIsNotNone(webhook_subscription_json)
예제 #6
0
    def create_subscription(self, kwargs):
        """
        Creates a subscription to a webhook service.
        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
        :param kwargs: The values constitute all user input.
        :type kwargs: dict
        :return: requests.models.Response
        """
        if 'access_token' not in kwargs:
            raise exceptions.InvalidArgumentError('Access Token not given.')
        if 'event_type' not in kwargs:
            raise exceptions.InvalidArgumentError('Event Type not given.')

        if 'webhook_endpoint' not in kwargs or \
                'scope' not in kwargs:
            raise exceptions.InvalidArgumentError(
                'Invalid arguments for creating a Webhook Subscription.')

        if 'access_token' in kwargs:
            bearer_token = kwargs['access_token']
        if 'event_type' in kwargs:
            event_type = kwargs['event_type']
        if 'webhook_endpoint' in kwargs:
            webhook_endpoint = kwargs['webhook_endpoint']
        if 'scope' in kwargs:
            scope = kwargs['scope']
        if 'scope_reference' in kwargs:
            scope_reference = kwargs['scope_reference']

        # event types
        event_types_to_check = [
            'b2b_transaction_received', 'buygoods_transaction_received',
            'buygoods_transaction_reversed', 'm2m_transaction_received',
            'settlement_transfer_completed', 'customer_created'
        ]
        till_scope_event_types = [
            'b2b_transaction_received', 'buygoods_transaction_received',
            'buygoods_transaction_reversed'
        ]

        # build subscription url
        subscription_url = self._build_url(WEBHOOK_SUBSCRIPTION_PATH)

        # define headers
        headers = dict(self._headers)

        # validate string arguments
        validation.validate_string_arguments(bearer_token, event_type,
                                             webhook_endpoint, scope)

        if 'scope_reference' in kwargs:
            validation.validate_string_arguments(scope_reference)

        headers['Authorization'] = 'Bearer ' + bearer_token + ''

        if not any(check in event_type for check in event_types_to_check):
            raise exceptions.InvalidArgumentError(
                'Event type not recognized by k2-connect')

        if any(check in event_type for check in till_scope_event_types):
            if scope != 'till':
                raise exceptions.InvalidArgumentError(
                    'Invalid scope for given event type.')
            if 'scope_reference' not in kwargs or kwargs[
                    'scope_reference'] is None:
                raise exceptions.InvalidArgumentError(
                    'Scope reference not given.')

        if not any(check in event_type for check in till_scope_event_types):
            scope_reference = None
            if scope != 'company':
                raise exceptions.InvalidArgumentError(
                    'Invalid scope for given event type.')
            if 'scope_reference' in kwargs:
                raise exceptions.InvalidArgumentError(
                    'Invalid scope reference for given event type.')

        # validate webhook endpoint
        validation.validate_url(webhook_endpoint)

        # define subscription payload
        subscription_payload = json_builder.webhook_subscription(
            event_type=event_type,
            webhook_endpoint=webhook_endpoint,
            scope=scope,
            scope_reference=scope_reference)

        return self._make_requests(headers=headers,
                                   method='POST',
                                   url=subscription_url,
                                   payload=subscription_payload)