1

クレジットカードを使用するRailsアプリを作成していて、Stripeを使用してそれを実行しようとしています。課金するためにアプリからStripeにデータを渡す際に問題が発生します。それは私がこのトピックで助けを得ることを望んでいるものです。

まず、標準フォームがあります(テスト目的ですばやく送信するために、プレースホルダーの代わりに値を使用します)。フォームは名前と電子メールをDBに正常に入力し、顧客の「計画」は当面の間コントローラーにハードコードされます。

    <%= form_for @customer do |f| %>
      <div class="payment-errors"></div>
      <div class="name field">
        <%= f.label :name %>
        <%= f.text_field :name, :value => "Your name" %>
      </div>
      <div class="email field">
        <%= f.label :email %>
        <%= f.text_field :email, :value => "yourname@example.com" %>
      </div>
      <div class="cc_number field">
        <%= label_tag 'cc_number' %>
        <%= text_field_tag 'cc_number', nil, :value => "4242424242424242" %>
      </div>
      <div class="ccv field">
        <%= label_tag 'ccv' %>
        <%= text_field_tag 'ccv', nil, :value => "123" %>
      </div>
      <div class="cc_expiration field">
        <%= label_tag 'cc_month', "Expiration date" %>
        <%= text_field_tag 'cc_month', nil, :value => "12" %>
        <%= text_field_tag 'cc_year', nil, :value => "2012" %>
      </div>
      <div class="actions">
        <%= f.submit "Continue", :class => 'btn' %>
      </div>
    <% end %>

またsignups_view、上記のコードがある私の場所には、このJSがあり、ほとんどがStripeによって提供されています。

<script type="text/javascript">
  // this identifies your website in the createToken call below
  Stripe.setPublishableKey('<%= STRIPE['public'] %>');

  function stripeResponseHandler(status, response) {
      if (response.error) {
          // show the errors on the form
          $(".payment-errors").text(response.error.message);
          $("input[type=submit]").removeAttr("disabled");
      } else {
          var form$ = $("form");
          // 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='customer[stripe_token]' id='stripeToken' value='" + token + "'/>");
          // and submit
          $('.cc_number.field, .ccv.field, .cc_expiration.field').remove();
          form$.get(0).submit();
      }
  }

  $(document).ready(function() {
    $("form").submit(function(event) {
      // disable the submit button to prevent repeated clicks
      $('input[type=submit]').attr("disabled", "disabled");

      Stripe.createToken({
          number: $('#cc_number').val(),
          cvc: $('#ccv').val(),
          exp_month: $('#cc_month').val(),
          exp_year: $('#cc_year').val()
      }, stripeResponseHandler);

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

</script>

form$.append("<input type='hidden' name='customer[stripe_token]' id='stripeToken' value='" + token + "'/>");私のRubyアプリがに到達すると壊れるので、行に問題があるようcustomer[stripe_token]です。

Finally, in my `customers_controller`, I have:

  def create
    @customer = Customer.new(params[:customer])
    @customer.product = 

    if @customer.save
      save_order
      redirect_to @customer
    else
      render action: 'new'
    end

  def save_order
    Stripe.api_key = STRIPE['secret']
    charge = Stripe::Charge.create(
      :amount => 20,
      :currency => "usd",
      :card => @customer.stripe_token,
      :description => "Product 1"
    )
  end

フォームを送信するたびにelse、コントローラーの句にヒットします。十分なデバッグを行った後、グーグルでこれを取り除き、最初から再構築しても、まだ困惑しています。

どんな助けでも大歓迎です。

編集:追加しましたcustomer model

  attr_accessible :name, :email, :stripe_token, :product

  email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

  validates :email, :presence => true,
                    :format => { :with => email_regex },
                    :length => { :minimum => 6, :maximum => 60 },
                    :uniqueness => { :case_sensitive => false }

  validates :name, :length => {:minimum => 2, :maximum => 80 }
4

1 に答える 1

0

Customer モデルを見て、何が起こっているのかを理解するのに役立ちます。@customer.save が false を返す場合、バリデーターが失敗している可能性が高いことを意味します。

また、モデルにアクセス可能な属性として strip_token がありますか? そうしないと、あなたがやっているようにフォームからそれを割り当てることができません。トークンは1 回しか使用できないため、データベースに保存しないでください。

class Customer 
  attr_accessor :stripe_token # do you have this?
end 

もう 1 つ注意: 後で顧客の支払いを取得してアカウントをキャンセルできるように、Stripe ID フィールドを保存することをお勧めします。

于 2012-08-19T21:26:20.920 に答える