Sha256: 0668edd0da428d16694660e7d312b96ec36994fb883d64c1c053f0324ea8b982

Contents?: true

Size: 680 Bytes

Versions: 7

Compression:

Stored size: 680 Bytes

Contents

# frozen_string_literal: true

module SolidusStripe
  class Customer < ApplicationRecord
    belongs_to :payment_method

    # Source is supposed to be a user or an order and needs to respond to #email
    belongs_to :source, polymorphic: true

    def self.retrieve_or_create_stripe_customer_id(payment_method:, order:)
      instance = find_or_initialize_by(payment_method: payment_method, source: order.user || order)

      instance.stripe_id ||
        instance.create_stripe_customer.tap { instance.update!(stripe_id: _1.id) }.id
    end

    def create_stripe_customer
      payment_method.gateway.request { Stripe::Customer.create(email: source.email) }
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
solidus_stripe-5.0.2 app/models/solidus_stripe/customer.rb
solidus_stripe-5.0.1 app/models/solidus_stripe/customer.rb
solidus_stripe-5.0.0 app/models/solidus_stripe/customer.rb
solidus_stripe-5.0.0.rc.3 app/models/solidus_stripe/customer.rb
solidus_stripe-5.0.0.rc.2 app/models/solidus_stripe/customer.rb
solidus_stripe-5.0.0.rc.1 app/models/solidus_stripe/customer.rb
solidus_stripe-5.0.0.alpha.1 app/models/solidus_stripe/customer.rb