Files
ubicloud/model/payment_method.rb
Enes Cakir 55ab63cc31 Fix the stripe_data helper for the payment method
It returns a `Stripe::StripeObject`, which doesn’t have all the methods
from a hash.

It fails in production with the following exception:

    NoMethodError: undefined method 'slice' for #<Stripe::StripeObject:0x00007f9c27e9b4a0> (NoMethodError)

`to_h` returns a hash, but the keys are symbols, not strings. Since we
use string keys in the codebase, it's better to convert them before
slicing.
2025-06-04 14:38:13 +03:00

42 lines
1.4 KiB
Ruby

# frozen_string_literal: true
require_relative "../model"
require "stripe"
class PaymentMethod < Sequel::Model
many_to_one :billing_info
plugin ResourceMethods
def stripe_data
if (Stripe.api_key = Config.stripe_secret_key)
@stripe_data ||= Stripe::PaymentMethod.retrieve(stripe_id)["card"].to_h.transform_keys!(&:to_s).slice(*%w[last4 brand exp_month exp_year])
end
end
def after_destroy
if (Stripe.api_key = Config.stripe_secret_key)
Stripe::PaymentMethod.detach(stripe_id)
end
super
end
end
# Table: payment_method
# Columns:
# id | uuid | PRIMARY KEY
# stripe_id | text | NOT NULL
# order | integer |
# billing_info_id | uuid |
# created_at | timestamp with time zone | NOT NULL DEFAULT now()
# card_fingerprint | text |
# fraud | boolean | NOT NULL DEFAULT false
# preauth_amount | integer |
# preauth_intent_id | text |
# Indexes:
# payment_method_pkey | PRIMARY KEY btree (id)
# payment_method_preauth_intent_id_key | UNIQUE btree (preauth_intent_id)
# payment_method_stripe_id_key | UNIQUE btree (stripe_id)
# Foreign key constraints:
# payment_method_billing_info_id_fkey | (billing_info_id) REFERENCES billing_info(id)