所以我一直在尝试让Stripe在我的应用程序中启动并运行,目前问题是没有为事务生成支付令牌。当我尝试处理测试付款时,我收到此错误:Must provide source or customer.
在控制台内部我收到此错误:Uncaught ReferenceError: Stripe is not defined
我认为问题是<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
文件没有被读取,因为它应该是我的令牌。我已经有一段时间了,已经阅读了我可以在网上获得的所有内容,但还没有找到适合我的解决方案。它可能很小,任何帮助都非常值得赞赏。
这是我的控制器:
require "stripe"
class ChargesController < ApplicationController
def new
@project = Project.find(params[:project_id])
end
def create
binding.pry
@project = Project.find(params[:project_id])
Stripe.api_key = "sk_test_JlKC4V7nmCQ0sE4iNAVyoAxA"
#Get the credit card details submitted by the form
token = params[:stripeToken]
# Amount in cents, this is being read and recorded in stripe dashboard
amount = (params[:amount].to_f * 100).to_i
charge = Stripe::Charge.create(
:amount => amount,
:source => token,
:description => 'Rails Stripe customer',
:currency => 'usd'
)
rescue Stripe::CardError => e
flash[:error] = e.message
#save what we need for our server as a new payment
binding.pry
@payment = Payment.create({
user_id: current_user.id,
project_id: @project.id,
amount: @amount,
comments: params[:comments]
})
#make a function in payments model that will convert the cents back into dollars
@project.addMoney(@amount) #add it to project
@payment.save
end
private
def charges_params
params.require(:payment).permit(:comments, :user_id, :project_id, :amount)
end
end
我没有创建客户,因为Stripe支持通过电子邮件明确告诉我,如果我只处理一次性付款,我就不需要了。这是付款表单,其中包含头部的Javascript(根据Stripe的示例):
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js">
// The required Stripe lib
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<script>
Stripe.setPublishableKey('<%= ENV['SECRET_KEY'] %>');
function stripeResponseHandler(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 {
// response contains id and card, which contains additional card details
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();
}
}
$('#payment-form').submit(function(event) {
var $form = $(this);
alert('you clicked submit');
console.log('this function was hit');
// Disable the submit button to prevent repeated clicks
$form.find('submit').prop('disabled', true);
Stripe.card.createToken($form, stripeResponseHandler);
// Prevent the form from submitting with the default action
return false;
});
</script>
</head>
<!-- form -->
<div class="container">
<div class="row Row one">
<div class="col-sm-12 col-md-10">
<h1>Make your contribution</h1>
<%= form_for @project, url: project_charges_path, :html => {:id => "payment-form"}, method: 'post' do |f| %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= f.hidden_field :@project_id, :value => @project.id %>
<div class= "field">
<%= label_tag :card_number, "Credit Card Number" %><br>
<%= text_field_tag :card_number, nil, name: nil, class: 'form-control', :required => true %><br>
</div>
<div class= "field">
<%= label_tag :card_code, "Security Code (cvc)" %><br>
<%= text_field_tag :card_code, nil, name: nil, class: 'form-control', :required => true %><br>
</div>
<div class= "field">
<%= label_tag :card_month, "Expiration" %>
<%= select_month nil, {add_month_numbers: true}, {name: nil, id: "card_month"} %>
<%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, id: "card_year"}%>
</div>
<div class= "field">
<%= label_tag :amount, "Amount" %><br>
<%= text_field_tag :amount, nil, name: nil, class: 'form-control', :required => true %>
</div>
<div class= "field">
<%= label_tag :comments, "Add a comment?" %><br>
<%= text_area_tag :comments, nil, name: nil, class: 'form-control', :required => true %>
</div>
<div class= "actions">
<%= f.submit 'Submit', :class => 'contribution-submit' %>
</div>
<div id="stripe_error">
<noscript>JavaScript is not enabled and is required for this form. First enable it in your web browser settings.</noscript>
</div>
<% end %>
</div>
</div>
<!-- <div class="row"></div> -->
</div>
我觉得奇怪的另一件事可能值得一提,如果它表明问题可能是什么 - 这个视图文件夹中的文件(表单所在的位置)没有拾取任何提供的网站css application.html.erb。这就是为什么我必须手动将它包含在表单的html.erb的头部,以及包含js.stripe.com
文件。 css现在正在渲染,但是这个文件似乎没有做任何事情。
答案 0 :(得分:1)
不确定它到底是做了什么,但又回去再次复制Stripes代码,它现在有效。我的表单现在看起来像这样:
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>Contribution Form</title>
<!-- The required Stripe lib -->
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<!-- jQuery is used only for this example; it isn't required to use Stripe -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
// This identifies your website in the createToken call below
Stripe.setPublishableKey('<%= 'pk_test_KfCg1YmVXwBYyEdPEWnfibF8'%>');
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(e) {
var $form = $(this);
// Disable the submit button to prevent repeated clicks
$form.find('button').prop('disabled', true);
Stripe.card.createToken({
number: $('.card-number').val(),
cvc: $('.card-cvc').val(),
exp_month: $('.card-expiry-month').val(),
exp_year: $('.card-expiry-year').val()}, stripeResponseHandler);
// Prevent the form from submitting with the default action
return false;
});
});
</script>
</head>
<body>
<!-- form -->
<div class="container">
<div class="row Row one">
<div class="col-sm-12 col-md-10">
<h1>Make your contribution</h1>
<%= form_for @project, url: project_charges_path, :html => {:id => "payment-form"}, method: 'post' do |f| %>
<%= f.hidden_field :user_id, :value => current_user.id %>
<%= f.hidden_field :@project_id, :value => @project.id %>
<div class= "field">
<%= label_tag :card_number, "Credit Card Number" %><br>
<%= text_field_tag :card_number, nil, name: nil, class: ' card-number form-control', :required => true %><br>
</div>
<div class= "field">
<%= label_tag :card_code, "Security Code (cvc)" %><br>
<%= text_field_tag :card_code, nil, name: nil, class: 'card-cvc form-control', :required => true %><br>
</div>
<div class= "field">
<%= label_tag :card_month, "Expiration" %>
<%= select_month nil, {add_month_numbers: true}, {name: nil, class: "card-expiry-month"} %>
<%= select_year nil, {start_year: Date.today.year, end_year: Date.today.year+15}, {name: nil, class: "card-expiry-year"}%>
</div>
<div class= "field">
<%= label_tag :amount, "Amount" %><br>
<%= text_field_tag :amount %>
</div>
<div class= "field">
<%= label_tag :comments, "Add a comment?" %><br>
<%= text_area_tag :comments %>
</div>
<div class= "actions">
<%= f.submit 'Submit', :class => 'contribution-submit' %>
</div>
<div id="stripe_error">
<noscript>JavaScript is not enabled and is required for this form. First enable it in your web browser settings.</noscript>
</div>
<% end %>
</div>
</div>
<!-- <div class="row"></div> -->
</div>
</body>
</html>