2017-07-02 9 views
0

私はRailsを初めて使い、現在は私の最初のウェブサイトで作業しています。 これは私の問題です
サービスのサブスクリプションを作成するたびに、「Plan already exists」というStripe :: InvalidRequestErrorが表示されます。私はそれがストライププランIDで何かをしなければならないことを理解しました。Rails Stripe "Plan already exists"

私がしたいのは、ユーザーが購読をクリックすると、同じIDを持つプランがすでに存在するかどうかを確認する必要があります。同じIDを持つプランが存在しない場合は、プランを作成する必要があります。それが存在する場合は、プランを作成して顧客にプランを登録するべきではありません。

は、ここに私が試したものです:

class PaymentsController < ApplicationController 
    before_action :set_order 

    def create 
    @user = current_user 

    unless Stripe::Plan.id == @order.service.title 
    plan = Stripe::Plan.create(
     :name => @order.service.title, 
     :id => @order.service.title, 
     :interval => "month", 
     :currency => @order.amount.currency, 
     :amount => @order.amount_pennies, 
    ) 
    end 

あなたは私がちょうどストライプのIDを使用することができますが、どうやらこれがうまくいかないと考えていることがわかります上。

customer = Stripe::Customer.create(
    source: params[:stripeToken], 
    email: params[:stripeEmail], 
    ) 

    # Storing the customer.id in the customer_id field of user 
    @user.customer_id = customer.id 

    Stripe::Subscription.create(
    :customer => @user.customer_id, 
    :plan => @order.service.title, 
    ) 

    @order.update(payment: plan.to_json, state: 'paid') 
    redirect_to order_path(@order) 

    rescue Stripe::CardError => e 
     flash[:error] = e.message 
     redirect_to new_order_payment_path(@order) 
    end 

    private 

    def set_order 
     @order = Order.where(state: 'pending').find(params[:order_id]) 
    end 
    end 
+0

です。 'Stripe :: Plan.id'を検査してその値を印刷します。 – Pavan

+0

それは私に伝えます! #の未定義メソッド 'id '。だから私はそれを印刷しようとすると価値がない。最初にIDを作成したときにIDをStringの「テスト」に割り当てました。 –

+0

ユーザが購読ボタンをクリックしたときに生成されるパラメータを投稿できますか? – Pavan

答えて

0

あなたが計画の存在をチェックされている方法は、を書きではありません。 Stripe::Plan.idが機能しないので、このunless Stripe::Plan.id == @order.service.titleは常に失敗します。あなたはにコードを書く

What I want to do is, when the User clicks subscribe, it should check if the plan with the same id already exists. If the Plan with the same ID doesn't exist, it should create the Plan. If it does exist, it should not create the Plan and just subscribe the customer to the plan

@plan = Stripe::Plan.retrieve(@order.service.title) 
unless @plan 
    plan = Stripe::Plan.create(
    :name => @order.service.title, 
    :id => @order.service.title, 
    :interval => "month", 
    :currency => @order.amount.currency, 
    :amount => @order.amount_pennies, 
) 
end 

retrieveメソッドを使用して、計画
を取得し、下記好きに使用する必要があり、上記の方法のelse一部にその計画
とサブスクリプションを作成します。だから最終的な方法は

+1

ありがとうございました!これは完全に動作します –

関連する問題