Class: SolidusSubscriptions::Api::V1::SubscriptionsController

Inherits:
BaseController
  • Object
show all
Defined in:
app/controllers/solidus_subscriptions/api/v1/subscriptions_controller.rb

Instance Method Summary collapse

Methods inherited from BaseController

#subscription_guest_token

Instance Method Details

#cancelObject



49
50
51
52
53
54
55
56
57
# File 'app/controllers/solidus_subscriptions/api/v1/subscriptions_controller.rb', line 49

def cancel
  load_subscription

  if @subscription.cancel
    render json: @subscription.to_json
  else
    render json: @subscription.errors.to_json, status: :unprocessable_entity
  end
end

#createObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/solidus_subscriptions/api/v1/subscriptions_controller.rb', line 9

def create
  store = params[:store_id].nil? ? ::Spree::Store.default : ::Spree::Store.find(id: params[:store_id])
  attributes = create_subscription_params.merge(user: current_api_user, store: store)
  acceptable_payment_details = update_payment_attributes(attributes)

  if acceptable_payment_details
    subscription = SolidusSubscriptions::Subscription.new(attributes)

    if subscription.save
      render json: subscription.to_json(include: [:line_items, :shipping_address, :billing_address])
    else
      render json: subscription.errors.to_json, status: :unprocessable_entity
    end
  else
    error_message = I18n.t('solidus_subscriptions.subscription.invalid_payment_details')

    render json: { payment_source_type: [error_message] }.to_json, status: :unprocessable_entity
  end
end

#pauseObject



59
60
61
62
63
64
65
66
67
# File 'app/controllers/solidus_subscriptions/api/v1/subscriptions_controller.rb', line 59

def pause
  load_subscription

  if @subscription.pause(actionable_date: actionable_date_param)
    render json: @subscription.to_json
  else
    render json: @subscription.errors.to_json, status: :unprocessable_entity
  end
end

#resumeObject



69
70
71
72
73
74
75
76
77
# File 'app/controllers/solidus_subscriptions/api/v1/subscriptions_controller.rb', line 69

def resume
  load_subscription

  if @subscription.resume(actionable_date: actionable_date_param)
    render json: @subscription.to_json
  else
    render json: @subscription.errors.to_json, status: :unprocessable_entity
  end
end

#skipObject



39
40
41
42
43
44
45
46
47
# File 'app/controllers/solidus_subscriptions/api/v1/subscriptions_controller.rb', line 39

def skip
  load_subscription

  if @subscription.skip
    render json: @subscription.to_json
  else
    render json: @subscription.errors.to_json, status: :unprocessable_entity
  end
end

#updateObject



29
30
31
32
33
34
35
36
37
# File 'app/controllers/solidus_subscriptions/api/v1/subscriptions_controller.rb', line 29

def update
  load_subscription

  if @subscription.update(subscription_params)
    render json: @subscription.to_json(include: [:line_items, :shipping_address, :billing_address])
  else
    render json: @subscription.errors.to_json, status: :unprocessable_entity
  end
end