Module: Webhookdb::Replicator::StripeV1Mixin

Instance Method Summary collapse

Instance Method Details

#_fetch_backfill_page(pagination_token, **_kwargs) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/webhookdb/replicator/stripe_v1_mixin.rb', line 99

def _fetch_backfill_page(pagination_token, **_kwargs)
  url = self._mixin_backfill_url
  url += pagination_token if pagination_token.present?
  response = Webhookdb::Http.get(
    url,
    basic_auth: {username: self.service_integration.backfill_key},
    logger: self.logger,
    timeout: Webhookdb::Stripe.http_timeout,
  )
  data = response.parsed_response
  next_page_param = nil
  if data["has_more"]
    last_item_id = data["data"][-1]["id"]
    next_page_param = "?starting_after=" + last_item_id
  end
  return data["data"], next_page_param
end

#_mixin_backfill_urlObject

Raises:

  • (NotImplementedError)


12
13
14
# File 'lib/webhookdb/replicator/stripe_v1_mixin.rb', line 12

def _mixin_backfill_url
  raise NotImplementedError
end

#_mixin_event_type_namesObject

this array describes which event this webhook should subscribe to stripe.com/docs/api/events/types

Raises:

  • (NotImplementedError)


18
19
20
# File 'lib/webhookdb/replicator/stripe_v1_mixin.rb', line 18

def _mixin_event_type_names
  raise NotImplementedError
end

#_resource_and_event(request) ⇒ Object



6
7
8
9
10
# File 'lib/webhookdb/replicator/stripe_v1_mixin.rb', line 6

def _resource_and_event(request)
  body = request.body
  return body.fetch("data").fetch("object"), body if body.fetch("object") == "event"
  return body, nil
end

#_timestamp_column_nameObject



26
27
28
# File 'lib/webhookdb/replicator/stripe_v1_mixin.rb', line 26

def _timestamp_column_name
  return :updated
end

#_verify_backfill_401_err_msgObject



91
92
93
# File 'lib/webhookdb/replicator/stripe_v1_mixin.rb', line 91

def _verify_backfill_401_err_msg
  return "It looks like that API Key is invalid. Please reenter the API Key you just created:"
end

#_verify_backfill_403_err_msgObject



83
84
85
86
87
88
89
# File 'lib/webhookdb/replicator/stripe_v1_mixin.rb', line 83

def _verify_backfill_403_err_msg
  return "It looks like that API Key does not have permission to access #{self.resource_name_singular} Records. " \
         "Please check the permissions by going to the list of restricted keys and " \
         "hovering over the information icon in the entry for this key. " \
         "Once you've verified or corrected the permissions for this key, " \
         "please reenter the API Key you just created:"
end

#_verify_backfill_err_msgObject



95
96
97
# File 'lib/webhookdb/replicator/stripe_v1_mixin.rb', line 95

def _verify_backfill_err_msg
  return "An error occurred. Please reenter the API Key you just created:"
end

#_webhook_response(request) ⇒ Object



22
23
24
# File 'lib/webhookdb/replicator/stripe_v1_mixin.rb', line 22

def _webhook_response(request)
  return Webhookdb::Stripe.webhook_response(request, self.service_integration.webhook_secret)
end

#calculate_backfill_state_machineObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/webhookdb/replicator/stripe_v1_mixin.rb', line 58

def calculate_backfill_state_machine
  step = Webhookdb::Replicator::StateMachineStep.new
  unless self.service_integration.backfill_key.present?
    step.output = %(In order to backfill #{self.resource_name_plural}, we need an API key.
From your Stripe Dashboard, go to Developers -> API Keys -> Restricted Keys -> Create Restricted Key.
Create a key with Read access to #{self.restricted_key_resource_name}.
Submit, then copy the key when Stripe shows it to you:
    )
    return step.secret_prompt("Restricted Key").backfill_key(self.service_integration)
  end

  unless (result = self.verify_backfill_credentials).verified
    self.service_integration.replicator.clear_backfill_information
    step.output = result.message
    return step.secret_prompt("Restricted Key").backfill_key(self.service_integration)
  end

  step.output = %(Great! We are going to start backfilling your #{self.resource_name_plural}.
#{self._query_help_output}
    )
  return step.completed
end

#calculate_webhook_state_machineObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/webhookdb/replicator/stripe_v1_mixin.rb', line 30

def calculate_webhook_state_machine
  step = Webhookdb::Replicator::StateMachineStep.new
  unless self.service_integration.webhook_secret.present?
    step.output = %{You are about to start replicating #{self.resource_name_singular} info into WebhookDB.
We've made an endpoint available for #{self.resource_name_singular} webhooks:

#{self._webhook_endpoint}

From your Stripe Dashboard, go to Developers -> Webhooks -> Add Endpoint.
Use the URL above, and choose all of the following events:
#{self._mixin_event_type_names.join("\n  ")}
Then click Add Endpoint.

The page for the webhook will have a 'Signing Secret' section.
Reveal it, then copy the secret (it will start with `whsec_`).
    }
    return step.secret_prompt("secret").webhook_secret(self.service_integration)
  end

  step.output = %(Great! WebhookDB is now listening for #{self.resource_name_singular} webhooks.
#{self._query_help_output}
In order to backfill existing #{self.resource_name_plural}, run this from a shell:

#{self._backfill_command}
  )
  return step.completed
end

#restricted_key_resource_nameObject



81
# File 'lib/webhookdb/replicator/stripe_v1_mixin.rb', line 81

def restricted_key_resource_name = self.resource_name_plural.gsub(/^Stripe /, "")