Class: Spree::StripeSubscriptionsController

Inherits:
StoreController
  • Object
show all
Defined in:
app/controllers/spree/stripe_subscriptions_controller.rb

Instance Method Summary collapse

Instance Method Details

#change_payment_detailsObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/controllers/spree/stripe_subscriptions_controller.rb', line 42

def change_payment_details
  checkout_session = Stripe::Checkout::Session.create(
    payment_method_types: ['card'],
    mode: 'setup',
    customer: @stripe_customer.id,
    setup_intent_data: {
      metadata: {
        subscription_id: @subscription.stripe_subscription_id
      }
    },
    success_url: "#{update_payment_details_stripe_plan_stripe_subscription_url(@plan, @subscription)}?session_id={CHECKOUT_SESSION_ID}",
    cancel_url: stripe_plans_url
  )
  redirect_to checkout_session.url
rescue StandardError => e
  Rails.logger.error e.message
  flash[:error] = e.message
  redirect_to stripe_plans_path
end

#createObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/controllers/spree/stripe_subscriptions_controller.rb', line 11

def create
  checkout_session = Stripe::Checkout::Session.create(
    mode: 'subscription',
    customer: @stripe_customer.id,
    customer_update: { address: 'auto', name: 'auto' },
    client_reference_id: spree_current_user.id,
    line_items: [
      {
        'price': @stripe_plan.id,
        'quantity': 1
      }
    ],
    automatic_tax: { enabled: @configuration.automatic_tax },
    tax_id_collection: { enabled: @configuration.tax_id_collection },
    billing_address_collection: @configuration.billing_address_collection,
    success_url: stripe_plans_url,
    cancel_url: stripe_plans_url
  )
  redirect_to checkout_session.url
rescue StandardError => e
  Rails.logger.error e.message
  flash[:error] = e.message
  redirect_to stripe_plans_path
end

#destroyObject



126
127
128
129
130
# File 'app/controllers/spree/stripe_subscriptions_controller.rb', line 126

def destroy
  @subscription.unsubscribe
  flash[:success] = I18n.t('spree_stripe_subscriptions.messages.success.successfully_unsubscribed')
  redirect_to stripe_plans_path
end

#downgradeObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/controllers/spree/stripe_subscriptions_controller.rb', line 90

def downgrade
  active_subscription = spree_current_user.stripe_subscriptions.active.find(params[:id])
  destination_plan = Spree::StripePlan.active.find(params[:stripe_plan_id])

  subscription_schedule = active_subscription.stripe_subscription_schedule

  if subscription_schedule.present?
    Stripe::SubscriptionSchedule.update(
      subscription_schedule.id,
      {
        phases: [
          {
            items: [
              { price: active_subscription.plan.stripe_plan_id, quantity: 1 }
            ],
            start_date: active_subscription.current_period_start.to_i,
            end_date: active_subscription.current_period_end.to_i
          },
          {
            items: [
              { price: destination_plan.stripe_plan_id, quantity: 1 }
            ],
            start_date: active_subscription.current_period_end.to_i,
            # end_date: active_subscription.current_period_end.to_i + 1.month.to_i
          }
        ]
      }
    )
    flash[:success] = I18n.t('spree_stripe_subscriptions.messages.success.successfully_downgraded')
    redirect_to stripe_plans_path
  else
    flash[:alert] = I18n.t('spree_stripe_subscriptions.messages.errors.cannot_process_request')
    redirect_to stripe_plans_path
  end
end

#updateObject



36
37
38
39
40
# File 'app/controllers/spree/stripe_subscriptions_controller.rb', line 36

def update
  @subscription.cancel_renewal
  flash[:success] = I18n.t('spree_stripe_subscriptions.messages.success.successfully_canceled_renewal')
  redirect_to stripe_plans_path
end

#update_payment_detailsObject



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
87
88
# File 'app/controllers/spree/stripe_subscriptions_controller.rb', line 62

def update_payment_details
  return unless (session_id = params[:session_id])

  checkout_session = Stripe::Checkout::Session.retrieve(session_id)
  setup_intent = Stripe::SetupIntent.retrieve(checkout_session.setup_intent)

  # Set invoice_settings.default_payment_method on the Customer
  Stripe::Customer.update(
    @stripe_customer.id,
    { invoice_settings: { default_payment_method: setup_intent.payment_method } }
  )

  # Set default_payment_method on the Subscription
  Stripe::Subscription.update(
    @subscription.stripe_subscription_id,
    {
      default_payment_method: setup_intent.payment_method
    }
  )

  flash[:success] = I18n.t('spree_stripe_subscriptions.messages.success.successfully_updated')
  redirect_to stripe_plans_path
rescue StandardError => e
  Rails.logger.error e.message
  flash[:error] = e.message
  redirect_to stripe_plans_path
end