javascript - 为什么条带客户令牌没有保存在我的数据库中?
问题描述
我目前正在学习提升技能教程“基本 Web 开发人员课程”(目前在第 162 课上),我尝试将 Stripe 与付费会员联系起来。本教程正在使用旧版本的 gems 和库等,这就是我告诉你的原因。
问题是在 Stripe 给我发回一个客户令牌之后。它不会存储在我的数据库中,也不会登录会员资格。相反,我的提交按钮将保持禁用状态并显示字符串“处理中”。
开发控制台显示:“未捕获的类型错误:无法读取未定义的属性‘提交’”消息。
我需要改变什么?有没有。与我的 save_with_subscription 方法有关吗?
attr_accessor :stripe_card_token
def save_with_subscription
if valid?
customer = Stripe::Customer.create(description: email, plan: plan_id, card: stripe_card_token)
self.stripe_customer_token = customer.id
save!
end
它的js文件:
/* global $, Stripe */
//Document ready.
$(document).on('turbolinks:load', function(){
var theForm = $('#pro_form');
var submitBtn = $('#form-signup-btn');
//Set Stripe public key.
Stripe.setPublishableKey( $('meta[name="stripe-key"]').attr('content') );
//When user clicks form submit btn,
submitBtn.click(function(event){
//prevent default submission behavior.
event.preventDefault();
submitBtn.val("Processing").prop('disabled', true);
//Collect the credit card fields.
var ccNum = $('#card_number').val(),
cvcNum = $('#card_code').val(),
expMonth = $('#card_month').val(),
expYear = $('#card_year').val();
//Use Stripe JS library to check for card errors.
var error = false;
//Validate card number.
if(!Stripe.card.validateCardNumber(ccNum)) {
error = true;
alert('The credit card number appears to be invalid');
}
//Validate CVC number.
if(!Stripe.card.validateCVC(cvcNum)) {
error = true;
alert('The CVC number appears to be invalid');
}
//Validate expiration date.
if(!Stripe.card.validateExpiry(expMonth, expYear)) {
error = true;
alert('The expiration date appears to be invalid');
}
if (error) {
//If there are card errors, don't send to Stripe.
submitBtn.prop('disabled', false).val("Sign Up");
} else {
//Send the card info to Stripe.
Stripe.createToken({
number: ccNum,
cvc: cvcNum,
exp_month: expMonth,
exp_year: expYear
}, stripeResponseHandler);
}
return false;
});
//Stripe will return a card token.
function stripeResponseHandler(status, response) {
//Get the token from the response.
var token = response.id;
//Inject the card token in a hidden field.
theForm.append( $('<input type="hidden" name="user[stripe_card_token]">').val(token) );
//Submit form to our Rails app.
theForm.get(0).submit();
}
});
我的所有代码都可以在https://github.com/JakkSwords/upskill_saas_tutorial/tree/user_memberships找到
解决方案
推荐阅读
- mysql - root@% 如何授予用户选择 information_schema.Tables 中的所有条目
- python - 如何将字符串数组的 numpy 数组转换为嵌套的 numpy 数组?
- typescript - Next.js 12 并删除 .babelrc,现在我收到 Typescript/Jsx 错误
- reactjs - 盖茨比阴影问题
- php - 如何通过分组来计算价值
- flutter - 如何显示选择的值?
- latex - 在 Latex 中添加图片时忽略边距并换行
- python - 如何将从 image_dataset_from_directory 获得的数据集拆分为数据和标签?
- vue.js - 为什么从另一个文件导入的 SCSS 变量在 Vue 3 中不起作用?
- autodesk-forge - 如何检查模型是否超出查看器容器?