Class: SolidusStripe::Webhook::Event

Inherits:
Object
  • Object
show all
Includes:
Omnes::Event
Defined in:
lib/solidus_stripe/webhook/event.rb

Overview

Omnes event wrapping a Stripe event for a given payment method.

All unknown methods are delegated to the wrapped Stripe event.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stripe_event:, payment_method:) ⇒ Event

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Event.



60
61
62
63
64
# File 'lib/solidus_stripe/webhook/event.rb', line 60

def initialize(stripe_event:, payment_method:)
  @stripe_event = stripe_event
  @payment_method = payment_method
  @omnes_event_name = :"#{PREFIX}#{stripe_event.type}"
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name) ⇒ Object (private)



82
83
84
# File 'lib/solidus_stripe/webhook/event.rb', line 82

def method_missing(method_name, ...)
  @stripe_event.send(method_name, ...)
end

Instance Attribute Details

#omnes_event_nameObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



54
55
56
# File 'lib/solidus_stripe/webhook/event.rb', line 54

def omnes_event_name
  @omnes_event_name
end

#payment_methodObject (readonly)



57
58
59
# File 'lib/solidus_stripe/webhook/event.rb', line 57

def payment_method
  @payment_method
end

Class Method Details

.from_request(payload:, signature_header:, slug:, tolerance: default_tolerance) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/solidus_stripe/webhook/event.rb', line 27

def from_request(payload:, signature_header:, slug:, tolerance: default_tolerance)
  payment_method = SolidusStripe::PaymentMethod.with_slug(slug).first!
  stripe_event = Stripe::Webhook.construct_event(
    payload,
    signature_header,
    payment_method.preferred_webhook_endpoint_signing_secret,
    tolerance: tolerance
  )
  new(stripe_event: stripe_event, payment_method: payment_method)
rescue ActiveRecord::RecordNotFound, Stripe::SignatureVerificationError, JSON::ParserError
  nil
end

.register(user_events:, bus:, core_events: CORE_EVENTS) ⇒ Object



40
41
42
43
44
# File 'lib/solidus_stripe/webhook/event.rb', line 40

def register(user_events:, bus:, core_events: CORE_EVENTS)
  (core_events + user_events).each do |event|
    bus.register(:"stripe.#{event}")
  end
end

Instance Method Details

#payloadHash<String, Object>

Serializable representation of the event.

Ready to be consumed by async Omnes adapters, like Omnes::Subscriber::Adapter::ActiveJob or Omnes::Subscriber::Adapter::Sidekiq.

Returns:

  • (Hash<String, Object>)


73
74
75
76
77
78
# File 'lib/solidus_stripe/webhook/event.rb', line 73

def payload
  {
    "stripe_event" => @stripe_event.as_json,
    "payment_method_id" => @payment_method.id
  }
end