簡體   English   中英

在我的Rails應用程序中使用Stripe充值卡時出現問題

[英]Problems charging a card with Stripe in my Rails app

我正在構建一個使用信用卡的Rails應用,並且試圖使用Stripe來做到這一點。 我在將數據從我的應用程序傳遞到Stripe進行充電時遇到一些問題。 這就是我希望就此主題獲得幫助的方法。

首先,我有一個標准表單(使用值代替占位符,以便快速提交以進行測試)。 表單成功地輸入了名稱和電子郵件到數據庫中,並且暫時在控制器中對客戶的“計划”進行了硬編碼:

    <%= 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 + "'/>");行似乎有問題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 }

這將有助於查看您的客戶模型以了解發生的情況。 如果@ customer.save返回false,則意味着驗證程序可能失敗。

另外,您是否將stripe_token作為模型上的可訪問屬性? 否則,您將無法像執行操作一樣從表單中分配它。 需要注意的是令牌應該存儲在數據庫中,因為它只能使用一次。

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

還有一點要注意:您可能需要存儲一個Stripe ID字段,以便您可以檢索客戶付款並在以后取消他們的帳戶。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM