Skip to content

Instantly share code, notes, and snippets.

@miharekar
Last active October 23, 2023 13:10
Show Gist options
  • Save miharekar/71d0737d8f509d49ccf8001b877f9efa to your computer and use it in GitHub Desktop.
Save miharekar/71d0737d8f509d49ccf8001b877f9efa to your computer and use it in GitHub Desktop.
Stripe Webhook Upgrader
Since you can't update Stripe API version on a webhook, the only way to do it, is to disable one, and create a new one.
# frozen_string_literal: true
class StripeWebhookUpgrader
attr_reader :id, :api_version, :stripe_options, :events
def initialize(id, api_version = nil, stripe_options: {}, events: nil)
@id = id
@api_version = api_version
@stripe_options = stripe_options
@events = events
end
def upgrade(retrying: false)
return if existing.status == "disabled" || existing.api_version == api_version
new_webhook = Stripe::WebhookEndpoint.create(upgrade_params, stripe_options)
Stripe::WebhookEndpoint.update(new_webhook.id, { disabled: true }, stripe_options)
end
private
def upgrade_params
{
url: existing.url,
enabled_events: enabled_events,
description: "#{existing.description} (#{api_version})",
metadata: existing.metadata.to_h,
connect: existing.application.present?
}.tap do |params|
params[:api_version] = api_version if api_version.present?
end
end
def existing
@existing ||= Stripe::WebhookEndpoint.retrieve(id, stripe_options)
end
def enabled_events
events || existing.enabled_events
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment