我刚刚实现了条纹,整个事情似乎很容易/简单。我错过了什么吗?我只是在我的控制器中有一个new.html.erb视图和new以及创建动作。
我的观点:
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<script type="text/javascript">
Stripe.setPublishableKey("pk_test_uZ5i6q4hDYctglEMPYiqLUh5");
</script>
<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>
<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>
My Charges控制器:
def new
end
def create
# Amount in cents
@amount = 2000
# 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
答案 0 :(得分:0)
验证它是否正常工作的方法是检查您是否可以在Stripe帐户网站上查看交易或订阅。确保在收费或注册订阅时使用Stripe的测试卡之一。
它看起来如此简单的原因是因为这段代码隐藏了幕后发生的事情。在您发布的代码中,当您执行类似创建客户的操作时,它会在Ruby中创建对象,但它也会向Stripe API发送请求,并使客户也在其数据库中。创建订阅时会发生同样的事情;订阅对象是在Ruby中创建的,并且请求被发送到Stripe的API,该API也在其末尾创建订阅。