条纹创建两个订阅

时间:2016-11-12 17:51:21

标签: ruby-on-rails stripe-payments

我刚刚使用Stripe将功能模型转换为创建charge以创建subscription,由于某种原因,它现在创建了两个订阅而不是一个订阅。我的new视图中的代码自工作以来没有改变,所以问题不在这里(在我看来),但是因为this SO post我想要的js有问题无论如何要表现出来:

<%= form_tag charges_path do %>
  <article>
    <% if flash[:error].present? %>
      <div id="error_explanation">
        <p><%= flash[:error] %></p>
      </div>
    <% end %>
    <label class="amount">
      <span>Amount: $7.99/month</span>
    </label>
  </article>

  <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
          data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
          data-description="Generator Subscription"
          data-amount="799"
          data-locale="auto"></script>
<% end %>

这是我的控制者,我相信问题必定存在:

class ChargesController < ApplicationController
  def new
    unless current_user
      flash[:error] = "Step one is to create an account!"
      redirect_to new_user_registration_path
    end

    if current_user.access_generator 
      flash[:notice] = "Silly rabbit, you already have access to the generator!"
      redirect_to controller: 'generators', action: 'new'
    end
  end

  def create
    customer = Stripe::Customer.create(
      :email => params[:stripeEmail],
      :source  => params[:stripeToken],
      :plan => "generator_access"
    )

    subscription = Stripe::Subscription.create(
      :customer => customer.id,
      :plan => "generator_access"
    )

    if subscription
      current_user.update_attributes(access_generator: true)
      current_user.update_attributes(stripe_customer_id: subscription.customer)
      current_user.update_attributes(stripe_sub_id_generator: subscription.id)
      flash[:notice] = "You have been granted almighty powers of workout generation! Go forth and sweat!"
      redirect_to controller: 'generators', action: 'new'
    end

  rescue Stripe::CardError => e
    flash[:error] = e.message
    redirect_to new_charge_path
  end

  def cancel_subscription
    @user = current_user
    customer = Stripe::Customer.retrieve(@user.stripe_customer_id)
    subscription = Stripe::Subscription.retrieve(@user.stripe_sub_id_generator)

    if customer.cancel_subscription(params[:customer_id])
      @user.update_attributes(stripe_customer_id: nil, access_generator: false, stripe_sub_id_generator: nil)
      flash[:notice] = "Your subscription has been cancelled."
      redirect_to user_path(@user)
    else
      flash[:error] = "There was an error canceling your subscription. Please notify us."
      redirect_to user_path(@user)
    end
  end

end

cancel_subscription方法工作正常(一旦我通过条带信息中心手动删除重复订阅),所以它必须是“创建”中的内容。方法。我还检查了我的控制台,并且正确地更新了User属性的信息,以匹配正在创建的两个重复订阅中的第二个。

有人能看出为什么这段代码会产生两个订阅吗?

2 个答案:

答案 0 :(得分:0)

使用计划创建客户时,会自动创建相应的订阅。您不需要手动创建它。

答案 1 :(得分:0)

对于 Stripe V3,这是创建和检查订阅以避免同一客户的多个值的方法 - 在下面的代码中,我为客户创建了一个试用订阅。

   ## method to create/fetch customer
   def create_or_retrieve_customer(user)
    customer = retrieve_stripe_customer(user)

    if customer.nil? or customer.deleted?
      customer = Stripe::Customer.create({
        email: user.email,
        description: "Created on #{Time.zone.now.to_date}",
        name: user.username,
      })
    end
    customer
  end

   ## get the customer  
   customer = create_or_retrieve_customer(self)
    ##setup the subscription
    @subscription = Stripe::Subscription.create({
      customer: customer.id,
      items: [
        {
          price: "prod_aksfdklajfl",
        },
      ],
      trial_end: 30.days.from_now.to_date.to_time.to_i,
      cancel_at_period_end: true,
      billing_cycle_anchor: 30.days.from_now.to_date.to_time.to_i
    })

获得订阅 ID 后,将其存储在用户表中的数据库中,然后在需要时对其进行验证。