我在条带和磁道集成期间遇到此错误,我无法弄清楚原因。
我的收费控制器:
class ChargesController < ApplicationController
before_action :check_if_user_already_subscribed, only: [:new]
def new
end
def create
# Amount in cents
@amount = 100
# Create the charge on Stripe's servers - this will charge the user's card
begin
# Get the credit card details submitted by the form
customer = Stripe::Customer.create(
:email => params[:email],
:source => params[:stripeToken]
)
Stripe::Charge.create(
:amount => @amount,
:currency => 'usd',
:customer => customer.id,
:description => 'Example charge custom form'
)
current_user.subscribed = true
current_user.stripe_id = customer.id
current_user.expiry_date = Date.today + 90.days
current_user.save
flash[:success] = "Thank you for subscribing. Your account has been unlocked."
redirect_to root_path
rescue Stripe::CardError => e
flash[:danger] = e.message
redirect_to root_path
end
end
private
def check_if_user_already_subscribed
if current_user.subscribed
flash[:danger] = "You have already subscribed. Please wait until your subscription runs out to resubscribe."
redirect_to root_path
end
end
end
我的初始化程序stripe.rb:
Rails.configuration.stripe = {
:publishable_key => ENV['PUBLISHABLE_KEY'],
:secret_key => ENV['SECRET_KEY']
}
Stripe.api_key = Rails.configuration.stripe[:secret_key]
我正在使用figaro,所以configuration.yml(我已经替换了实际的键值):
PUBLISHABLE_KEY: "sk_test_key"
SECRET_KEY: "pk_test_key"
我的观点:
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<script type="text/javascript">
Stripe.setPublishableKey("pk_test_N9K3OPekS1Wi5zyyWtgcVLEe");
</script>
<div id= 'charges-new-promo'>
<div class = 'container-fluid'>
<div class = 'index-header'>
<h2 class ='text-center charge-title'>Upgrade Now.</h2>
<p class = ' text-center charge-sub-title'>Instantly unlock the entire site for 90 days.</p>
<p class = ' text center charge-stripe-title'><%= image_tag("stripe.png", :class => "stripe-img" ) %></p>
</div>
</div>
</div>
<div class = 'container'>
<div class = 'row'>
<div class = 'col-md-5 col-sm-12'>
<%= form_tag charges_path, id: 'payment-form' do %>
<div class = 'charge-form'>
<span class="payment-errors"></span>
<div class="form-group">
<label>
<input value="<%= current_user.email if current_user %>" type="hidden" data-stripe="email" >
</label>
</div>
<div class="form-group">
<label>
<span>Full name</span><span class = 'small-text'> (e.g. John Smith)</span>
<input type="text" size="5" data-stripe="name" class = 'form-field'>
</label>
</div>
<div class="form-group">
<label>
<span>Country</span><span class = 'small-text'> (e.g. United States)</span>
<input type="text" size="10" data-stripe="address_country" class = 'form-field'>
</label>
</div>
<div class="form-group">
<label>
<span>Address</span>
<input type="text" size="10" data-stripe="address_line1" class = 'form-field'>
</label>
</div>
<div class="form-group">
<label>
<span>Postal Code / Zip Code</span>
<input type="text" size="4" data-stripe="address_zip" class = 'form-field'>
</label>
</div>
<div class="form-group">
<label>
<span>Card Number</span>
<input type="text" size="20" data-stripe="number" class = 'form-field'>
</label>
</div>
<div class="form-group">
<label>
<span>Expiration (MM/YY)</span>
<input type="text" size="2" data-stripe="exp_month" class ='form-field-expiry'>
<span> / </span>
<input type="text" size="2" data-stripe="exp_year" class ='form-field-expiry'>
</label>
</div>
<div class="form-group">
<label>
<span>CVC</span>
<input type="text" size="3" data-stripe="cvc" class = 'form-field'>
</label>
</div>
<input type="submit" class="c-btn-submit-charge" value="Submit Payment">
</div>
<% end %>
</div>
<div class ='col-md-4 col-sm-12'>
<div class = 'payment-box'>
<p> Information about your order:</p>
<ul>
<li>You are subscribing to the full membership.</li>
<li>This subscription lasts for 3 months (90 days).</li>
<li>Do not subscribe again if you are already subscribed and your expiry date has not been passed.</li>
<li>Subscribing grants you access to every test and practice material on this website.</li>
<li>At the moment, this consists of 19 online DAT reading comprehension tests along with answer explanations.</li>
<li>Any additional premium material added (such as more tests or more DAT sections) after you subscribe will be automatically available to you with
no extra charge.</li>
</ul>
<div class = 'inner-box'>
<p class = 'text-center'> Total cost: $20 </p>
</div>
<div class ='inner-box-payment'>
</div>
</div>
</div>
</div>
</div>
<script>
$(function() {
var $form = $('#payment-form');
$form.submit(function(event) {
// Disable the submit button to prevent repeated clicks:
$form.find('.submit').prop('disabled', true);
// Request a token from Stripe:
Stripe.card.createToken($form, stripeResponseHandler);
// Prevent the form from being submitted:
return false;
});
});
function stripeResponseHandler(status, response) {
// Grab the form:
var $form = $('#payment-form');
if (response.error) { // Problem!
// Show the errors on the form:
$form.find('.payment-errors').text(response.error.message);
$form.find('.submit').prop('disabled', false); // Re-enable submission
} else { // Token was created!
// Get the token ID:
var token = response.id;
// Insert the token ID into the form so it gets submitted to the server:
$form.append($('<input type="hidden" name="stripeToken">').val(token));
// Submit the form:
$form.get(0).submit();
}
};
</script>
就是这样。我现在完全迷失了。出了什么问题?
答案 0 :(得分:1)
我正在使用figaro,所以configuration.yml(我已经取代了实际的密钥 值):
PUBLISHABLE_KEY:“sk_test_key”
SECRET_KEY:“pk_test_key”
您错误地设置了键值。
当时:
PUBLISHABLE_KEY: "sk_test_key"
SECRET_KEY: "pk_test_key"
必须:
PUBLISHABLE_KEY: "pk_test_key"
SECRET_KEY: "sk_test_key"
P.s“sk_test_key” - sk - &gt; SECRET_KEY
答案 1 :(得分:1)
您更改了键值吗?
看起来像this的答案应该可以解决您的问题。
因此,在您的yml中,您应该:
PUBLISHABLE_KEY: "pk_test_key"
SECRET_KEY: "sk_test_key"
Pk_key是可发布的密钥,而sk_key是秘密密钥。