質問編集履歴

6

追記

2024/11/07 17:58

投稿

student
student

スコア83

test CHANGED
File without changes
test CHANGED
@@ -63,6 +63,9 @@
63
63
  ```
64
64
 
65
65
  ### 該当のソースコード
66
+ ```config/initialize/stripe.rb
67
+ Stripe.api_key = Rails.application.credentials.stripe_api_key
68
+ ```
66
69
 
67
70
  ```teams_controllers
68
71
  class TeamsController < ApplicationController

5

追記

2024/11/07 08:54

投稿

student
student

スコア83

test CHANGED
File without changes
test CHANGED
@@ -65,11 +65,49 @@
65
65
  ### 該当のソースコード
66
66
 
67
67
  ```teams_controllers
68
+ class TeamsController < ApplicationController
69
+ require "stripe"
70
+
71
+ def new
72
+ @user = current_user
73
+ @team = Team.find_by(user_id: @user.id)
74
+ end
75
+
76
+ def create
77
+ @team = Team.new(user_id: current_user.id)
78
+ customer = Stripe::Customer.create({
79
+ source: params[:stripeToken],
80
+ })
81
+ @team.subscription_type = params[:subscription_type]
82
+ if @team.subscription_type == 1
83
+ @team.plan_id = "price_*************************"
84
+ elsif @team.subscription_type == 2
85
+ @team.plan_id = "price_*************************"
86
+ end
87
+
88
+ subscription = Stripe::Subscription.create({
89
+ customer: customer.id,
90
+ items: [{price: @team.plan_id}],
91
+ })
92
+ @team.customer_id = customer.id
93
+ @team.stripe_subscription_id = subscription.id
94
+ @team.active_until = Time.zone.at(subscription.current_period_end)
95
+ if @team.save
96
+ @user = current_user
97
+ @user.subscription = true
98
+ @user.save
99
+ flash[:success] = "成功しました"
100
+ redirect_to root_url
101
+ else
102
+ render 'new'
103
+ end
104
+ end
105
+
68
- def destroy
106
+ def destroy
69
107
  @team = Team.find_by(user_id: current_user.id)
70
- deleting_stripe_subscription = Stripe::Subscription.retrieve(@team.stripe_subscription_id)
108
+ @stripe = Stripe::Subscription.retrieve(@team.stripe_subscription_id)
71
109
  if current_user.unsubscribe
72
- deleting_stripe_subscription.delete
110
+ @stripe.delete
73
111
  @user = current_user
74
112
  @user.subscription = false
75
113
  @uers.save
@@ -78,6 +116,9 @@
78
116
  else
79
117
  render 'new'
80
118
  end
119
+ end
120
+
121
+ end
81
122
  ```
82
123
 
83
124
  ### 試したこと

4

追記

2024/11/07 08:52

投稿

student
student

スコア83

test CHANGED
File without changes
test CHANGED
@@ -8,7 +8,58 @@
8
8
  ### 発生している問題・エラーメッセージ
9
9
 
10
10
  ```
11
- undefined method `destroy' for #<Stripe::Subscription
11
+ undefined method `delete' for #<Stripe::Subscription:0x17188 id=sub_******************> JSON: {
12
+ "id": "sub_******************",
13
+ "object": "subscription",
14
+ "application": null,
15
+ "application_fee_percent": null,
16
+ "automatic_tax": {"enabled":false,"liability":null},
17
+ "billing_cycle_anchor": ******************,
18
+ "billing_cycle_anchor_config": null,
19
+ "billing_thresholds": null,
20
+ "cancel_at": null,
21
+ "cancel_at_period_end": false,
22
+ "canceled_at": null,
23
+ "cancellation_details": {"comment":null,"feedback":null,"reason":null},
24
+ "collection_method": "charge_automatically",
25
+ "created": ******************,
26
+ "currency": "jpy",
27
+ "current_period_end": ******************,
28
+ "current_period_start": ******************,
29
+ "customer": "cus_******************",
30
+ "days_until_due": null,
31
+ "default_payment_method": null,
32
+ "default_source": null,
33
+ "default_tax_rates": [
34
+ ],
35
+ "description": null,
36
+ "discount": null,
37
+ "discounts": [
38
+ ],
39
+ "ended_at": null,
40
+ "invoice_settings": {"account_tax_ids":null,"issuer":{"type":"self"}},
41
+ "items": {"object":"list","data":[{"id":"si_******************","object":"subscription_item","billing_thresholds":null,"created":******************,"discounts":[],"metadata":{},"plan":{"id":"price_******************","object":"plan","active":true,"aggregate_usage":null,"amount":800,"amount_decimal":"800","billing_scheme":"per_unit","created":1730648018,"currency":"jpy","interval":"month","interval_count":1,"livemode":false,"metadata":{},"meter":null,"nickname":null,"product":"prod_R9ObC80yEpP4t4","tiers_mode":null,"transform_usage":null,"trial_period_days":null,"usage_type":"licensed"},"price":{"id":"price_******************","object":"price","active":true,"billing_scheme":"per_unit","created":1730648018,"currency":"jpy","custom_unit_amount":null,"livemode":false,"lookup_key":null,"metadata":{},"nickname":null,"product":"prod_R9ObC80yEpP4t4","recurring":{"aggregate_usage":null,"interval":"month","interval_count":1,"meter":null,"trial_period_days":null,"usage_type":"licensed"},"tax_behavior":"unspecified","tiers_mode":null,"transform_quantity":null,"type":"recurring","unit_amount":800,"unit_amount_decimal":"800"},"quantity":1,"subscription":"sub_******************","tax_rates":[]}],"has_more":false,"total_count":1,"url":"/v1/subscription_items?subscription=sub_******************"},
42
+ "latest_invoice": "in_******************",
43
+ "livemode": false,
44
+ "metadata": {},
45
+ "next_pending_invoice_item_invoice": null,
46
+ "on_behalf_of": null,
47
+ "pause_collection": null,
48
+ "payment_settings": {"payment_method_options":null,"payment_method_types":null,"save_default_payment_method":"off"},
49
+ "pending_invoice_item_interval": null,
50
+ "pending_setup_intent": null,
51
+ "pending_update": null,
52
+ "plan": {"id":"price_******************","object":"plan","active":true,"aggregate_usage":null,"amount":800,"amount_decimal":"800","billing_scheme":"per_unit","created":1730648018,"currency":"jpy","interval":"month","interval_count":1,"livemode":false,"metadata":{},"meter":null,"nickname":null,"product":"prod_R9ObC80yEpP4t4","tiers_mode":null,"transform_usage":null,"trial_period_days":null,"usage_type":"licensed"},
53
+ "quantity": 1,
54
+ "schedule": null,
55
+ "start_date": 1730967712,
56
+ "status": "active",
57
+ "test_clock": null,
58
+ "transfer_data": null,
59
+ "trial_end": null,
60
+ "trial_settings": {"end_behavior":{"missing_payment_method":"create_invoice"}},
61
+ "trial_start": null
62
+ }
12
63
  ```
13
64
 
14
65
  ### 該当のソースコード

3

追記

2024/11/07 07:58

投稿

student
student

スコア83

test CHANGED
File without changes
test CHANGED
@@ -8,7 +8,7 @@
8
8
  ### 発生している問題・エラーメッセージ
9
9
 
10
10
  ```
11
- undefined method `delete'
11
+ undefined method `destroy' for #<Stripe::Subscription
12
12
  ```
13
13
 
14
14
  ### 該当のソースコード

2

追記

2024/11/05 21:29

投稿

student
student

スコア83

test CHANGED
File without changes
test CHANGED
@@ -32,6 +32,7 @@
32
32
  ### 試したこと
33
33
  if文を削除して削除機能を試みたのですが、ダメでした。
34
34
  エラー文には顧客情報が記載されていたので、恐らくdeleting_stripe_subscription = Stripe::Subscription.retrieve(@team.stripe_subscription_id)の箇所はしっかりと機能しているのかなと思われます。
35
+ destroyでも同様のエラーでした。
35
36
 
36
37
  ### 補足情報(FW/ツールのバージョンなど)
37
38
  ruby 3.2.2

1

編集

2024/11/05 21:16

投稿

student
student

スコア83

test CHANGED
File without changes
test CHANGED
@@ -1,5 +1,9 @@
1
1
  ### 実現したいこと
2
2
  Stripeでのサブスク機能を作成しているのですが、サブスク登録は出来るのですが、削除がエラーによってStripeにある顧客情報を削除できないのでこのエラーを解決したいです。
3
+
4
+
5
+ 参考サイト
6
+ [RailsアプリにStripeを用いたサブスクリプション決済を導入する(詳しく解説)](https://qiita.com/nR9h3kLy/items/e5937f33ae8a19b46324)
3
7
 
4
8
  ### 発生している問題・エラーメッセージ
5
9