Class: Mpp::Methods::Stripe::ChargeIntent

Inherits:
Object
  • Object
show all
Defined in:
lib/mpp/methods/stripe/charge_intent.rb

Overview

Server-side charge intent that verifies payment via Stripe PaymentIntents. Requires the ‘stripe` gem.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(secret_key:, api_base: Defaults::STRIPE_API_BASE) ⇒ ChargeIntent

Returns a new instance of ChargeIntent.



14
15
16
17
18
# File 'lib/mpp/methods/stripe/charge_intent.rb', line 14

def initialize(secret_key:, api_base: Defaults::STRIPE_API_BASE)
  @name = "charge"
  @secret_key = secret_key
  @api_base = api_base
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/mpp/methods/stripe/charge_intent.rb', line 12

def name
  @name
end

Instance Method Details

#verify(credential, request) ⇒ Object



20
21
22
23
24
25
26
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/mpp/methods/stripe/charge_intent.rb', line 20

def verify(credential, request)
  # Check challenge expiry
  challenge_expires = credential.challenge.expires
  if challenge_expires
    expires = Time.iso8601(challenge_expires.gsub("Z", "+00:00"))
    raise Mpp::VerificationError, "Request has expired" if expires < Time.now.utc
  end

  payload_data = credential.payload
  unless payload_data.is_a?(Hash) && payload_data.key?("spt")
    raise Mpp::VerificationError, "Invalid credential payload: missing spt"
  end

  spt = payload_data["spt"]
  external_id = payload_data["externalId"]

  # Build PaymentIntent params
  params = {
    amount: Integer(request["amount"]),
    currency: request["currency"],
    shared_payment_granted_token: spt,
    confirm: true,
    automatic_payment_methods: {
      enabled: true,
      allow_redirects: "never"
    }
  }

  # Include metadata from methodDetails if present
  method_details = request["methodDetails"]
  if method_details.is_a?(Hash) && method_details["metadata"].is_a?(Hash)
    params[:metadata] = method_details["metadata"].transform_values(&:to_s)
  end

  # Create PaymentIntent via Stripe SDK
  begin
    Kernel.require "stripe"
  rescue LoadError
    raise "stripe gem is required for Stripe charge verification. Install with: gem install stripe"
  end

  begin
    client = ::Stripe::StripeClient.new(@secret_key)
    result = client.v1.payment_intents.create(params)
  rescue => e
    raise Mpp::VerificationError, e.message
  end

  # https://docs.stripe.com/error-low-level#idempotency
  if result.respond_to?(:last_response) &&
      result.last_response&.headers&.[]("idempotent-replayed") == "true"
    raise Mpp::VerificationError, "Payment has already been processed."
  end

  pi_id = result.id
  status = result.status

  if status == "requires_action"
    raise Mpp::PaymentActionRequiredError.new(reason: "PaymentIntent #{pi_id} requires action")
  end

  unless status == "succeeded"
    raise Mpp::VerificationError, "PaymentIntent #{pi_id} has status: #{status}"
  end

  Mpp::Receipt.success(pi_id, method: "stripe", external_id: external_id)
end