Rails Stripe Connect 令牌问题
Rails Stripe Connect Token Issue
似乎没有正确创建令牌,或者它从未传递给 stripe,我不知道为什么。我已经删除了 turbolinks,因为我在 SO 上看到了它的问题。我已经遵循了所有可能的指南。之前我已经连接了 stripe payments 没问题,经过几天的尝试我无法解决这个问题。请帮忙!
对于我的客户来说,这是一种简单的付款方式,因此只需一次性收费。到目前为止甚至还没有设置客户。
我遇到的错误:您必须提供来源或客户 ID
条带的错误响应:
error:
type: "invalid_request_error"
message: "You must supply either a source or a customer id"
new.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('<%= @user.publishable_key %>');
// ...
</script>
<%= javascript_include_tag 'payment' %>
<%= form_for @payment, html: {id: "payment-form"} do |f| %>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<%= f.text_field :amount, autofocus: true, placeholder: "Amount", class: "form-control" %>
</div>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input class="form-control" data-stripe="number" maxlength="20" placeholder="Credit Card Number" type="text"></input>
</div>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input class="form-control" data-stripe="cvc" size="10" placeholder="Security Number" type="text"></input>
</div>
<div class="input-group">
<center><p>Expiration Date:</p></center>
<ul class="dates">
<li><%= select_month(Date.today, {add_month_numbers: true}, class: 'form-control', data: {stripe: "exp-month"}, style: "height: 46px;") %></li>
<li><%= select_year(Date.today.year, {start_year: Date.today.year, end_year: Date.today.year + 4}, class: 'form-control', data: {stripe: "exp-year"}, style: "height: 46px;") %></li>
</ul>
</div>
<div class="row">
<div class="col-xs-12">
<%= f.submit "Submit Payment", class: "btn btn-success col-xs-12" %>
</div>
</div>
<% end %>
支付控制器:
def new
@company = Company.find(params[:id])
@user = @company.users.first
@payment = Payment.new
end
def create
@result = StripeWrapper::Charge.create(source: params[:stripeToken])
if @result.present?
@payment = Payment.create(payment_params)
flash[:success] = "Thank you for your payment"
redirect_to thank_you_path
else
flash[:danger] = @result.error_message
render :new
end
end
private
def payment_params
params.require(:payment).permit(:user_id, :company_id, :reference_id)
end
Stripe_wrapper.rb
module StripeWrapper
class Charge
attr_reader :error_message, :response
def initialize(options={})
@response = options[:response]
@error_message = options[:error_message]
end
def self.create(options={})
begin
response = Stripe::Charge.create({
amount: 1000,
currency: 'usd',
source: options[:source],
description: "test charge",
application_fee: 123
}, {stripe_account: 'client_account_id'}
)
new(response: response)
rescue Stripe::CardError => e
new(error_message: e.message)
end
end
def successful?
response.present?
end
end
class Customer
attr_reader :response, :error_message
def initialize(options={})
@response = options[:response]
@error_message = options[:error_message]
end
def self.create(options={})
begin
response = Stripe::Customer.create({
card: options[:card],
email: options[:user_email],
description: "Service Pay"
},
{stripe_account: 'connect_client_id'}
)
new(response: response)
rescue Stripe::CardError => e
new(error_message: e.message)
end
end
def successful?
response.present?
end
def customer_token
response.id
end
end
end
payment.js
jQuery(function($) {
$('#payment-form').submit(function(event) {
var $form = $(this);
$form.find('button').prop('disabled', true);
Stripe.card.createToken($form, stripeResponseHandler);
return false;
});
});
var stripeResponseHandler = function(status, response) {
var $form = $('#payment-form');
if (response.error) {
$form.find('.payment-errors').text(response.error.message);
$form.find('button').prop('disabled', false);
} else {
var token = response.id;
$form.append($('<input type="hidden" name="stripeToken" />').val(token));
$form.get(0).submit();
}
};
你包括了Stripe.js吗?在 new.html.erb
的顶部添加
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<script type="text/javascript">
Stripe.setPublishableKey("#{ENV['STRIPE_PUBLISHABLE_KEY']}");
</script>
<%= javascript_include_tag 'payment' %>
ENV['STRIPE_PUBLISHABLE_KEY'] 应设置在 application.yml 或类似位置。您还需要在调用 StripeWrapper
之前将其放入创建控制器中
Stripe.api_key = ENV['STRIPE_SECRET_KEY']
跳上 stripe freenode 频道并获得一些帮助后,我们解决了问题,我想将结果留在这里供遇到此问题的其他人使用。
原来是 morris.js 图表库的 javascript 错误导致 javscript 的其余部分(即 payment.js 文件)未被执行,并且因此没有创建令牌。
morris.js错误是因为调用了jquery来显示一个图表(来自资产管道),但是没有div图表的容器。我通过简单地放置一个 div 和正确的 id 来解决这个问题,但是没有数据元素,所以没有显示数据和样式 display:none,所以没有向用户显示任何内容。
似乎没有正确创建令牌,或者它从未传递给 stripe,我不知道为什么。我已经删除了 turbolinks,因为我在 SO 上看到了它的问题。我已经遵循了所有可能的指南。之前我已经连接了 stripe payments 没问题,经过几天的尝试我无法解决这个问题。请帮忙!
对于我的客户来说,这是一种简单的付款方式,因此只需一次性收费。到目前为止甚至还没有设置客户。
我遇到的错误:您必须提供来源或客户 ID
条带的错误响应:
error:
type: "invalid_request_error"
message: "You must supply either a source or a customer id"
new.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('<%= @user.publishable_key %>');
// ...
</script>
<%= javascript_include_tag 'payment' %>
<%= form_for @payment, html: {id: "payment-form"} do |f| %>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<%= f.text_field :amount, autofocus: true, placeholder: "Amount", class: "form-control" %>
</div>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input class="form-control" data-stripe="number" maxlength="20" placeholder="Credit Card Number" type="text"></input>
</div>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input class="form-control" data-stripe="cvc" size="10" placeholder="Security Number" type="text"></input>
</div>
<div class="input-group">
<center><p>Expiration Date:</p></center>
<ul class="dates">
<li><%= select_month(Date.today, {add_month_numbers: true}, class: 'form-control', data: {stripe: "exp-month"}, style: "height: 46px;") %></li>
<li><%= select_year(Date.today.year, {start_year: Date.today.year, end_year: Date.today.year + 4}, class: 'form-control', data: {stripe: "exp-year"}, style: "height: 46px;") %></li>
</ul>
</div>
<div class="row">
<div class="col-xs-12">
<%= f.submit "Submit Payment", class: "btn btn-success col-xs-12" %>
</div>
</div>
<% end %>
支付控制器:
def new
@company = Company.find(params[:id])
@user = @company.users.first
@payment = Payment.new
end
def create
@result = StripeWrapper::Charge.create(source: params[:stripeToken])
if @result.present?
@payment = Payment.create(payment_params)
flash[:success] = "Thank you for your payment"
redirect_to thank_you_path
else
flash[:danger] = @result.error_message
render :new
end
end
private
def payment_params
params.require(:payment).permit(:user_id, :company_id, :reference_id)
end
Stripe_wrapper.rb
module StripeWrapper
class Charge
attr_reader :error_message, :response
def initialize(options={})
@response = options[:response]
@error_message = options[:error_message]
end
def self.create(options={})
begin
response = Stripe::Charge.create({
amount: 1000,
currency: 'usd',
source: options[:source],
description: "test charge",
application_fee: 123
}, {stripe_account: 'client_account_id'}
)
new(response: response)
rescue Stripe::CardError => e
new(error_message: e.message)
end
end
def successful?
response.present?
end
end
class Customer
attr_reader :response, :error_message
def initialize(options={})
@response = options[:response]
@error_message = options[:error_message]
end
def self.create(options={})
begin
response = Stripe::Customer.create({
card: options[:card],
email: options[:user_email],
description: "Service Pay"
},
{stripe_account: 'connect_client_id'}
)
new(response: response)
rescue Stripe::CardError => e
new(error_message: e.message)
end
end
def successful?
response.present?
end
def customer_token
response.id
end
end
end
payment.js
jQuery(function($) {
$('#payment-form').submit(function(event) {
var $form = $(this);
$form.find('button').prop('disabled', true);
Stripe.card.createToken($form, stripeResponseHandler);
return false;
});
});
var stripeResponseHandler = function(status, response) {
var $form = $('#payment-form');
if (response.error) {
$form.find('.payment-errors').text(response.error.message);
$form.find('button').prop('disabled', false);
} else {
var token = response.id;
$form.append($('<input type="hidden" name="stripeToken" />').val(token));
$form.get(0).submit();
}
};
你包括了Stripe.js吗?在 new.html.erb
的顶部添加<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<script type="text/javascript">
Stripe.setPublishableKey("#{ENV['STRIPE_PUBLISHABLE_KEY']}");
</script>
<%= javascript_include_tag 'payment' %>
ENV['STRIPE_PUBLISHABLE_KEY'] 应设置在 application.yml 或类似位置。您还需要在调用 StripeWrapper
之前将其放入创建控制器中Stripe.api_key = ENV['STRIPE_SECRET_KEY']
跳上 stripe freenode 频道并获得一些帮助后,我们解决了问题,我想将结果留在这里供遇到此问题的其他人使用。
原来是 morris.js 图表库的 javascript 错误导致 javscript 的其余部分(即 payment.js 文件)未被执行,并且因此没有创建令牌。
morris.js错误是因为调用了jquery来显示一个图表(来自资产管道),但是没有div图表的容器。我通过简单地放置一个 div 和正确的 id 来解决这个问题,但是没有数据元素,所以没有显示数据和样式 display:none,所以没有向用户显示任何内容。