Class: Yookassa::Webhook::Notification

Inherits:
Object
  • Object
show all
Defined in:
lib/yookassa/webhook/notification.rb

Overview

Parses incoming webhook notification payloads from YooKassa.

Automatically wraps the notification object into the appropriate entity class (Payment, Refund, Payout, or Deal) based on the event type.

Examples:

Parsing a webhook in a Rails controller

notification = Yookassa::Webhook::Notification.parse(request.body.read)
notification.event     # => "payment.succeeded"
notification.object    # => #<Yookassa::Entities::Payment ...>
notification.object.id # => "2a5b8f3c-..."

Constant Summary collapse

ENTITY_MAP =

Maps event type prefixes to entity classes.

{
  "payment" => Entities::Payment,
  "refund" => Entities::Refund,
  "payout" => Entities::Payout,
  "deal" => Entities::Deal
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event:, type:, object:) ⇒ Notification



38
39
40
41
42
# File 'lib/yookassa/webhook/notification.rb', line 38

def initialize(event:, type:, object:)
  @event = event
  @type = type
  @object = object
end

Instance Attribute Details

#eventString (readonly)



19
20
21
# File 'lib/yookassa/webhook/notification.rb', line 19

def event
  @event
end

#objectEntities::Base (readonly)



25
26
27
# File 'lib/yookassa/webhook/notification.rb', line 25

def object
  @object
end

#typeString (readonly)



22
23
24
# File 'lib/yookassa/webhook/notification.rb', line 22

def type
  @type
end

Class Method Details

.parse(json_or_hash) ⇒ Notification

Parses a raw webhook payload (JSON string or Hash) into a Yookassa::Webhook::Notification.

Raises:

  • (JSON::ParserError)

    if string input is not valid JSON



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/yookassa/webhook/notification.rb', line 49

def self.parse(json_or_hash)
  data = json_or_hash.is_a?(String) ? JSON.parse(json_or_hash) : json_or_hash
  event = data["event"]
  entity_type = event.to_s.split(".").first
  entity_class = ENTITY_MAP[entity_type] || Entities::Base

  new(
    event: event,
    type: data["type"],
    object: entity_class.new(data["object"] || {})
  )
end