実現したいこと
Stripeでのサブスク機能を作成しているのですが、サブスク登録は出来るのですが、削除がエラーによってStripeにある顧客情報を削除できないのでこのエラーを解決したいです。
参考サイト
RailsアプリにStripeを用いたサブスクリプション決済を導入する(詳しく解説)
発生している問題・エラーメッセージ
undefined method `delete' for #<Stripe::Subscription:0x17188 id=sub_******************> JSON: { "id": "sub_******************", "object": "subscription", "application": null, "application_fee_percent": null, "automatic_tax": {"enabled":false,"liability":null}, "billing_cycle_anchor": ******************, "billing_cycle_anchor_config": null, "billing_thresholds": null, "cancel_at": null, "cancel_at_period_end": false, "canceled_at": null, "cancellation_details": {"comment":null,"feedback":null,"reason":null}, "collection_method": "charge_automatically", "created": ******************, "currency": "jpy", "current_period_end": ******************, "current_period_start": ******************, "customer": "cus_******************", "days_until_due": null, "default_payment_method": null, "default_source": null, "default_tax_rates": [ ], "description": null, "discount": null, "discounts": [ ], "ended_at": null, "invoice_settings": {"account_tax_ids":null,"issuer":{"type":"self"}}, "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_******************"}, "latest_invoice": "in_******************", "livemode": false, "metadata": {}, "next_pending_invoice_item_invoice": null, "on_behalf_of": null, "pause_collection": null, "payment_settings": {"payment_method_options":null,"payment_method_types":null,"save_default_payment_method":"off"}, "pending_invoice_item_interval": null, "pending_setup_intent": null, "pending_update": null, "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"}, "quantity": 1, "schedule": null, "start_date": 1730967712, "status": "active", "test_clock": null, "transfer_data": null, "trial_end": null, "trial_settings": {"end_behavior":{"missing_payment_method":"create_invoice"}}, "trial_start": null }
該当のソースコード
config/initialize/stripe.rb
1Stripe.api_key = Rails.application.credentials.stripe_api_key
teams_controllers
1class TeamsController < ApplicationController 2 require "stripe" 3 4 def new 5 @user = current_user 6 @team = Team.find_by(user_id: @user.id) 7 end 8 9 def create 10 @team = Team.new(user_id: current_user.id) 11 customer = Stripe::Customer.create({ 12 source: params[:stripeToken], 13 }) 14 @team.subscription_type = params[:subscription_type] 15 if @team.subscription_type == 1 16 @team.plan_id = "price_*************************" 17 elsif @team.subscription_type == 2 18 @team.plan_id = "price_*************************" 19 end 20 21 subscription = Stripe::Subscription.create({ 22 customer: customer.id, 23 items: [{price: @team.plan_id}], 24 }) 25 @team.customer_id = customer.id 26 @team.stripe_subscription_id = subscription.id 27 @team.active_until = Time.zone.at(subscription.current_period_end) 28 if @team.save 29 @user = current_user 30 @user.subscription = true 31 @user.save 32 flash[:success] = "成功しました" 33 redirect_to root_url 34 else 35 render 'new' 36 end 37 end 38 39 def destroy 40 @team = Team.find_by(user_id: current_user.id) 41 @stripe = Stripe::Subscription.retrieve(@team.stripe_subscription_id) 42 if current_user.unsubscribe 43 @stripe.delete 44 @user = current_user 45 @user.subscription = false 46 @uers.save 47 flash[:notice] = "解約に成功しました" 48 redirect_to root_url 49 else 50 render 'new' 51 end 52 end 53 54end
試したこと
if文を削除して削除機能を試みたのですが、ダメでした。
エラー文には顧客情報が記載されていたので、恐らくdeleting_stripe_subscription = Stripe::Subscription.retrieve(@team.stripe_subscription_id)の箇所はしっかりと機能しているのかなと思われます。
destroyでも同様のエラーでした。
補足情報(FW/ツールのバージョンなど)
ruby 3.2.2
rails 6
あなたの回答
tips
プレビュー