class Payola::PaymentGateways::StripePaymentGateway

Attributes

amount[RW]
api_key[RW]
customer_id[RW]
interval[RW]
interval_count[RW]
payment_token[RW]
plan_id[RW]
plan_name[RW]
subscription_id[RW]

Public Class Methods

apply_subscription(**args) click to toggle source
# File lib/payola/payment_gateways/stripe_payment_gateway.rb, line 41
def self.apply_subscription(**args)
  new(**args).apply_subscription
end
new(**args) click to toggle source
# File lib/payola/payment_gateways/stripe_payment_gateway.rb, line 17
def initialize(**args)
  args.each do |name, value|
    public_send("#{name}=", value)
  end

  Stripe.api_key       = api_key || Payola.config.payment_gateway_api_key

  combined_id          = subscription_id || ''
  self.customer_id     = combined_id[/\A(cus_[A-Za-z0-9]{14})\:/, 1] || 'no_customer'
  self.subscription_id = combined_id[/\:(sub_[A-Za-z0-9]{14})\z/, 1] || 'no_subscription'
end

Public Instance Methods

apply_subscription() click to toggle source
# File lib/payola/payment_gateways/stripe_payment_gateway.rb, line 29
def apply_subscription
  update_subscription_plan
  update_subscription_payment_token

  {
    status:                   subscription.status,
    plan:                     subscription.plan[:id],
    subscription_id:          "#{subscription.customer}:#{subscription.id}",
    last_four_of_credit_card: last_four_of_credit_card,
  }
end

Private Instance Methods

customer() click to toggle source
# File lib/payola/payment_gateways/stripe_payment_gateway.rb, line 78
def customer
  Stripe::Customer.retrieve customer_id
rescue Stripe::InvalidRequestError => e
  raise Payola::Errors::PaymentGatewayRequestError.wrap(e) unless e.message.match 'No such customer'

  begin
    Stripe::Customer.create.tap do |customer|
      self.customer_id = customer.id
    end
  rescue StandardError => e
    raise Payola::Errors::PaymentGatewayRequestError.wrap(e)
  end
end
customer_default_card() click to toggle source
# File lib/payola/payment_gateways/stripe_payment_gateway.rb, line 66
def customer_default_card
  customer.cards.retrieve(customer.default_card)
end
last_four_of_credit_card() click to toggle source
# File lib/payola/payment_gateways/stripe_payment_gateway.rb, line 62
def last_four_of_credit_card
  customer_default_card.last4 unless plan.amount.zero?
end
plan() click to toggle source
# File lib/payola/payment_gateways/stripe_payment_gateway.rb, line 92
def plan
  Stripe::Plan.retrieve plan_id
rescue Stripe::InvalidRequestError => e
  raise Payola::Errors::PaymentGatewayRequestError.wrap(e) unless e.message.match 'No such plan'

  begin
    Stripe::Plan.create id:             plan_id,
                        amount:         amount.cents,
                        currency:       amount.currency,
                        interval:       interval,
                        interval_count: interval_count,
                        name:           plan_name
  rescue StandardError => e
    raise Payola::Errors::PaymentGatewayRequestError.wrap(e)
  end
end
subscription() click to toggle source
# File lib/payola/payment_gateways/stripe_payment_gateway.rb, line 70
def subscription
  @subscription ||= customer.subscriptions.retrieve(subscription_id)
rescue Stripe::InvalidRequestError => e
  raise Payola::Errors::PaymentGatewayRequestError.wrap(e) unless e.message.match 'does not have a subscription'

  @subscription ||= customer.subscriptions.create(plan: plan.id, card: payment_token)
end
update_subscription_payment_token() click to toggle source
# File lib/payola/payment_gateways/stripe_payment_gateway.rb, line 56
def update_subscription_payment_token
  subscription.save(card: payment_token)
rescue Stripe::InvalidRequestError => e
  raise Payola::Errors::PaymentGatewayRequestError.wrap(e) unless e.message.match 'cannot use a Stripe token more than once'
end
update_subscription_plan() click to toggle source
# File lib/payola/payment_gateways/stripe_payment_gateway.rb, line 47
def update_subscription_plan
  if subscription.plan != plan.id
    subscription.plan = plan.id
    subscription.save
  end
rescue Stripe::InvalidRequestError => e
  raise Payola::Errors::PaymentGatewayRequestError.wrap(e)
end