Module: Payhub::NextAction

Defined in:
lib/payhub/next_action.rb

Defined Under Namespace

Classes: Lightbox, OtpRequired, QR, Redirect

Class Method Summary collapse

Class Method Details

.decode(raw) ⇒ Object

Raises:

  • (ArgumentError)


27
28
29
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
57
# File 'lib/payhub/next_action.rb', line 27

def decode(raw)
  return nil if raw.nil?
  raise ArgumentError, "next_action must be an object or nil" unless raw.is_a?(Hash)

  case raw["type"]
  when "otp_required"
    OtpRequired.new(
      psp_ref: raw["psp_ref"].to_s,
      masked_destination: raw["masked_destination"].to_s,
      expires_at: raw["expires_at"]
    )
  when "redirect"
    Redirect.new(
      url: raw["url"].to_s,
      method: (raw["method"] || "GET").to_s.upcase,
      fields: (raw["fields"] || {}).each_with_object({}) { |(k, v), h| h[k.to_s] = v.to_s },
      expires_at: raw["expires_at"]
    )
  when "qr"
    QR.new(
      reference: raw["reference"].to_s,
      qr_payload: raw["qr_payload"].to_s,
      expires_at: raw["expires_at"]
    )
  when "lightbox"
    params = (raw["params"] || {}).each_with_object({}) { |(k, v), h| h[k.to_s] = v.to_s }
    Lightbox.new(params: params, script_url: params["lightbox_js_url"])
  else
    raise ArgumentError, "unknown next_action.type: #{raw["type"].inspect}"
  end
end