blob: 0ff53ba15c9f0ee8f3eba0d9a388012594978ea6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
# frozen_string_literal: true
module SolidusSubscriptions
module Api
module V1
class SubscriptionsController < BaseController
protect_from_forgery unless: -> { request.format.json? }
def create
store = params[:store_id].nil? ? ::Spree::Store.default : ::Spree::Store.find(id: params[:store_id])
attributes = subscription_params.merge(user: current_api_user, store: store)
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
end
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
def skip
load_subscription
if @subscription.skip
render json: @subscription.to_json
else
render json: @subscription.errors.to_json, status: :unprocessable_entity
end
end
def cancel
load_subscription
if @subscription.cancel
render json: @subscription.to_json
else
render json: @subscription.errors.to_json, status: :unprocessable_entity
end
end
private
def load_subscription
@subscription = SolidusSubscriptions::Subscription.find(params[:id])
authorize! action_name.to_sym, @subscription, subscription_guest_token
end
def subscription_params
params.require(:subscription).permit(SolidusSubscriptions.configuration.subscription_attributes | [
line_items_attributes: line_item_attributes,
])
end
def line_item_attributes
SolidusSubscriptions.configuration.subscription_line_item_attributes - [:subscribable_id] + [:id]
end
end
end
end
end
|