添加新卡并删除不工作的卡Stripe

时间:2014-01-25 00:23:21

标签: ruby-on-rails stripe-payments

我有一个有效的订阅模式,可以让我成功地让人们注册为付费会员。我正在尝试编写一个“编辑页面”,允许我执行以下操作

  • 添加新的信用卡
  • 删除卡(取消订阅)
  • 将付款计划更改为另一个

目前我的页面存在于/ subscribe / edit,其代码为

<%= form_tag do %>
  <span class="payment-errors"></span>


  <div class="form-row">
    <label>
      <span>Card Number</span>
      <input type="text" size="20" data-stripe="number"/>
    </label>
  </div>

  <div class="form-row">
    <label>
      <span>CVC</span>
      <input type="text" size="4" data-stripe="cvc"/>
    </label>
  </div>

  <div class="form-row">
    <label>
      <span>Expiration (MM/YYYY)</span>
      <input type="text" size="2" data-stripe="exp-month"/>
    </label>
    <span> / </span>
    <input type="text" size="4" data-stripe="exp-year"/>
  </div>

  <%= button_to "Add a New Card", :method => "updateSubscription" %>

<!--  <%= select_tag "type", options_for_select([['Subscriber' ,'Subscriber'], ['Sustainer', 'Sustainer']]) %>

  <%= button_to "Change Subscription", :method => "changeSubscription" %> -->

  <%= link_to "Delete Card and Subscription", :method => "deleteCard" , :confirm => "You sure?" %>
<% end %>

<script type="text/javascript" src="https://js.stripe.com/v2/"></script>

<script type="text/javascript">

  <!-- CURRENTLY USING TEST KEY -->
  Stripe.setPublishableKey("TESTKEY");

  jQuery(function($) {
    $('#payment-form').submit(function(event) {
      var $form = $(this);

      // Disable the submit button to prevent repeated clicks
      $form.find('button').prop('disabled', true);

      Stripe.card.createToken($form, stripeResponseHandler);

      // Prevent the form from submitting with the default action
      return false;
    });
  });

  var stripeResponseHandler = function(status, response) {
    var $form = $('#payment-form');

    if (response.error) {
      // Show the errors on the form
     $form.find('.payment-errors').text(response.error.message);
     $form.find('button').prop('disabled', false);
    } else {
     // token contains id, last4, and card type
     var token = response.id;
     // Insert the token into the form so it gets submitted to the server
     $form.append($('<input type="hidden" name="stripeToken" />').val(token));
     // and submit
     $form.get(0).submit();
    }
};  
</script>

这是我的routes.rb

  get 'subscribe' => 'subscribe#stepone'
  get 'subscribe/sliding-scale' => 'subscribe#steptwo'
  get 'subscribe/sliding-scale/subscriber' => 'subscribe#subscriber'
  get 'subscribe/sliding-scale/supporter' => 'subscribe#supporter'
  get 'subscribe/sliding-scale/sustainer' => 'subscribe#sustainer'
  post 'subscribe/sliding-scale/:type' => 'subscribe#createSubscription'
  get 'subscribe/edit' => 'subscribe#edit', :as => :edit_subscription
  post 'subscribe/edit'
  post 'subscribe/edit/deleteCard' 
  post 'subscribe/edit/updateSubscription'
  post 'subscribe/edit/changeSubscription'

最后这是我的订阅控制器

class SubscribeController < ApplicationController

  def stepone
  end

  def steptwo
  end

  def subscriber
    @type = "subscriber"
  end

  def supporter
    @type = "supporter"
  end

  def sustainer
    @type ="sustainer"
  end

  def createSubscription
    # Set your secret key: remember to change this to your live secret key in production
    # See your keys here https://manage.stripe.com/account

    # Get the credit card details submitted by the form
    token = params[:stripeToken]

    # Create a Customer
    customer = Stripe::Customer.create(
     :card => token,
     :plan => params[:type],
     :email => current_user[:email]
    )

    current_user[:stripeCustomerId] = customer[:id]
    # sets role of user, we should have a check for successful signu
    current_user.add_role params[:type] 
    current_user.save()

    UserMailer.subscription_confirmation(current_user).deliver

    redirect_to root_url, :notice => "Thank you for subscribing!"


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

  def edit
    @user = current_user
  end

  def updateSubscription
    # Set your secret key: remember to change this to your live secret key in production
    # See your keys here https://manage.stripe.com/account

    # Get the credit card details submitted by the form
    token = params[:stripeToken]

    # Create A New Card
    customer = Stripe::Customer.retrieve(current_user.stripeCustomerId)
    customer.cards.create(stripeToken)


    redirect_to root_url, :notice => "Your Card Has Been Updated!"


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

  def changeSubscription
    customer = Stripe::Customer.retrieve(@user.stripeCustomerId)
    @user.update_subscription(:plan => :type, :prorate => true)
    current_role = @user.roles.first.name
    @user.remove_role current_role
    current_user.add_role params[:type]
  end

  def deleteCard
    customer = Stripe::Customer.retrieve(@user.stripeCustomerId)
    customer.cards.retrieve(customer.default_card).delete()
    current_role = @user.roles.first.name
    @user.remove_role current_role
  end

  def currentCard
    customer = Stripe::Customer.retrieve(@user.stripeCustomerId)
    card = customer.cards.retrieve(customer.default_card)
  end

end

目前,如果我点击“删除卡片”按钮,我会在网址栏http://localhost:3000/subscribe/edit?confirm=You+sure%3F&method=deleteCard中获得以下内容而页面没有任何变化,如果我提交新的卡片信息则相同。

让我知道我做错了什么以及如何纠正我可怕的方式:)

1 个答案:

答案 0 :(得分:0)

link_tobutton_to中的方法选项并不代表 controller 方法。

单击时,必须one of five HTTP verbs(获取,发布,放置,修补或删除)才能用于请求。

<%= link_to "Delete", @obj, :method => :delete , :confirm => "Sure?" %>  

查看form helpers的Rails指南,获取更深入的指导。

此外,从不在表单中使用button_to,因为button_to本身就是一种表单。 HTML规范禁止嵌套表单,因为它会导致意外问题,包括小火灾。要提交表单,请使用submit_tag