自定义条带表单不在Rails 4中生成令牌

时间:2013-12-22 17:24:09

标签: ruby-on-rails ruby-on-rails-4 stripe-payments

我正在尝试使用Bootstrap 3将条带放入我在Rails 4中构建的Web应用程序中。我已经关注了Stripe网站上的文档(https://stripe.com/docs/tutorials/forms),但我遇到了创建令牌的问题。 我的Stripe日志显示没有创建令牌,因此我可能是我的JS /表单导致问题。

我的表格: _new.html.erb

<form action="/charges" method="POST" id="payment-form">
    <span class="payment-errors"></span>
      <div class="form-group left-inner-addon ">
        <i class="glyphicon glyphicon-user"></i>
        <input name="sender_name" type="text" class="form-control" placeholder="Sender's Name"/>
      </div>

      <div class="form-group left-inner-addon">
        <i class="glyphicon glyphicon-send"></i>
        <input name="recipient_email" type="email" class="form-control" placeholder="Recipient's Email"/>
      </div>

      <div class="form-group left-inner-addon">
        <i class="glyphicon glyphicon-pencil"></i>
        <input name="recipient_msg" type="text" class="form-control" placeholder="Message to Recipient"/>
      </div>

      <div class="form-group left-inner-addon ">
          <i class="glyphicon glyphicon-credit-card"></i>
          <input type="text" class="form-control" placeholder="Credit Card Number" data-stripe="number"/>
      </div>

      <div class="form-group">
        <div class="row">
          <div class="col-xs-4 left-inner-addon ">
            <i class="glyphicon glyphicon-calendar"></i>
            <input type="text" class="form-control" placeholder="MM  " data-stripe="exp-month"/>
          </div>
          <div class="col-xs-5 left-inner-addon ">
            <i class="glyphicon glyphicon-calendar"></i>
            <input type="text" class="form-control" placeholder="YYYY" data-stripe="exp-year"/>
          </div>
        </div>
      </div>

      <div class="form-group">
        <div class="row">
          <div class="col-xs-5 left-inner-addon ">
            <i class="glyphicon glyphicon-ok-circle"></i>
            <input type="text" class="form-control" placeholder="CVC" data-stripe="cvc"/>
          </div>
        </div>
      </div>

  <button type="submit" class="btn btn-primary">Purchase</button>
</form>

与页面上呈现的条带相关联的JS / Jquery包含在我的_stripe.html.erb部分中,该部分被调用到my _head.html.erb partial中。 的 _stripe.html.erb

<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<script type="text/javascript">
    // This identifies your website in the createToken call below
    Stripe.setPublishableKey('pk_test_gGPOa1BlZvxdjnlnvJayZMzQ');

    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 re-submit
        $form.get(0).submit();
      }
    };

    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;
        });
        });

  </script>

我的费用控制器使用文档中的代码 的 charges_controller.rb

class ChargesController < ApplicationController

  def new
  end

def create
        # Set your secret key: remember to change this to your live secret key in production
        # See your keys here https://manage.stripe.com/account
        Stripe.api_key = "SECRET_KEY"

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

        # Create a Customer
        customer = Stripe::Customer.create(
          :card => token,
          :description => "payinguser@example.com"
        )
        puts customer

        # Charge the Customer instead of the card
        Stripe::Charge.create(
            :amount => 1000, # in cents
            :currency => "cad",
            :customer => customer.id
        )

        # Save the customer ID in your database so you can use it later
        save_stripe_customer_id(user, customer.id)

        # Later...
        customer_id = get_stripe_customer_id(user)

        Stripe::Charge.create(
          :amount   => 1500, # $15.00 this time
          :currency => "cad",
          :customer => customer_id
        )
end

private

def charges_params
    params.require(charges).permit(:sender_msg, :recipient_email, :recipient_msg, :stripeToken)
end

end 

当我将客户对象打印到控制台时,在我点击应用程序中的提交按钮后,它会打印出来:

{"id":"cus_3AahFG5MiJMbc9","object":"customer","created":1387731342,"livemode":false,"description":"payinguser@example.com","email":null,"delinquent":false,"metadata":{},"subscription":null,"discount":null,"account_balance":0,"cards":{"object":"list","count":0,"url":"/v1/customers/cus_3AahFG5MiJMbc9/cards","data":[]},"default_card":null}

条纹状态&amp;响应 在我的条带日志中,我看到一个402 POST到v1 / charge和一个200 POST到v1 / customers

402回复机构:

error:
message: "Cannot charge a customer that has no active card"
type: "card_error"
param: "card"
code: "missing"

200响应机构:

object: "customer"
created: 1387668402
id: cus_3AJmiyuxLK9rTw
livemode: false
description: "payinguser@example.com"
email: null
delinquent: false
metadata:
subscription: null
discount: null
account_balance: 0
cards:
object: "list"
count: 0
url: "/v1/customers/cus_3AJmiyuxLK9rTw/cards"
data:
default_card: null

我试图找出为什么我的代码没有创建令牌。 谢谢,

1 个答案:

答案 0 :(得分:0)

您似乎正在获得令牌,但其关键不在于您的预期。

来自条纹文档:

{
    id : "tok_u5dg20Gra" // String of token identifier
}