条带结账.js - 将自定义参数传递给令牌回调
Stripe checkout.js - passing custom params to token callback
我正在遵循 Stripe Docs 中的此示例集成(略有修改,以便能够将点击处理程序添加到多个按钮:
<script src="https://checkout.stripe.com/checkout.js"></script>
<button id="customButton">Purchase</button>
<script>
var handler = StripeCheckout.configure({
key: 'pk_test_jPVRpCB1MLjWu2P71eTvXBZD',
image: '/square-image.png',
token: function(token) {
// Use the token to create the charge with a server-side script.
// You can access the token ID with `token.id`
}
});
$('.pay-deposit').click( function(e) {
// Open Checkout with further options
handler.open({
name: 'Demo Site',
description: '2 widgets ($20.00)',
amount: 2000
});
e.preventDefault();
});
在我的特殊情况下,我有几个按钮,例如:
<button class='pay-deposit' booking-id='3455'>Pay Deposit</button>
<button class='pay-deposit' booking-id='335'>Pay Deposit</button>
<button class='pay-deposit' booking-id='34'>Pay Deposit</button>
。显然,我想以某种方式将点击按钮的预订ID传递给令牌回调。找不到任何示例或解释涵盖这个看似简单的案例......任何帮助非常感谢。谢谢!
这有点晚了,但也许它会帮助其他人。 这是从 Rails 示例修改而来的:
# HTML file
<script src="https://checkout.stripe.com/checkout.js"></script>
<button class='pay-deposit' data-booking-id='3455'>Pay Deposit</button>
<button class='pay-deposit' data-booking-id='335'>Pay Deposit</button>
<button class='pay-deposit' data-booking-id='34'>Pay Deposit</button>
# JS file
$('.pay-deposit').on('click', function(event) {
event.preventDefault();
// Get booking information from database
var booking_id = $(this).data('booking-id');
$.getJSON("/bookings/"+booking_id, function(data) {
// Open Checkout with further options
handler = stripe_checkout(booking_id);
handler.open({
name: "My Bookings",
description: data["description"],
amount: data["amount"],
email: data["email"],
});
// Close Checkout on page navigation
$(window).on('popstate', function() {
handler.close();
});
});
});
function stripe_checkout(booking_id) {
var handler = StripeCheckout.configure({
key: 'pk_test_jPVRpCB1MLjWu2P71eTvXBZD',
token: function(token) {
// Send the charge through
$.post("/charges/create",
{ token: token.id, booking_id: booking_id }, function(data) {
if (data["status"] == "ok") {
window.location = "/some-url";
} else {
// Deal with error
alert(data["message"]);
}
});
}
});
return handler;
}
# Bookings controller
class BookingsController < ApplicationController
def show
@booking = Booking.find(params[:id])
attrs = @booking.attributes
attrs.merge!("email" => current_user.email)
respond_to do |format|
format.json { render json: attrs.to_json }
end
end
end
# Charges controller
class ChargesController < ApplicationController
def create
booking = Booking.find(params[:booking_id])
customer = Stripe::Customer.create(card: params[:token])
charge = Stripe::Charge.create(
customer: customer.id,
amount: booking.amount,
description: booking.description,
currency: 'usd'
)
if charge.paid
# Do some things for successful charge
respond_to do |format|
format.json { render json: {status: "ok"}.to_json }
end
else
respond_to do |format|
format.json { render json: {status: "fail", message: "Error with processing payment. Please contact support."}.to_json }
end
end
end
end
将令牌初始值设定项从"配置"移动到"打开"。
var handler = StripeCheckout.configure({
key: 'pk_test_jPVRpCB1MLjWu2P71eTvXBZD',
image: '/square-image.png'
});
$('.pay-deposit').click( function(e) {
var data = $(this).data('booking-id');
// Open Checkout with further options
handler.open({
name: 'Demo Site',
description: '2 widgets ($20.00)',
amount: 2000,
token: function(token) {
// here you go!
alert(data);
}
});
e.preventDefault();
});
并切换到:
<button class='pay-deposit' data-booking-id='3455'>Pay Deposit</button>
<button class='pay-deposit' data-booking-id='335'>Pay Deposit</button>
<button class='pay-deposit' data-booking-id='34'>Pay Deposit</button>
相关文章:
- 如何将自定义参数传递到FullCalendar事件提要
- 如何调用另一个js文件中的函数或触发一个自定义事件,该事件将参数作为Jquery中的数据对象传递
- extjs-使用传递的参数创建自定义函数
- 将角度工厂中的参数用于自定义标头
- 调用插件外部的自定义函数并传递参数
- 使用函数参数的自定义时间表
- 如何在jQuery PrintArea中传递自定义html标签
- 条带结账.js - 将自定义参数传递给令牌回调
- require.js-未定义的模块作为参数传递以定义回调
- JS用self作为参数创建自定义对象
- 是否可以在casper.base64encode()中传递自定义请求头?
- 调用带有不同参数的自定义对象实例
- 使用jQuery根据URL参数显示自定义内容的最佳方式是什么?
- 如何通过Web-Workers传递自定义类实例
- 在AngularJS中传递额外的参数给自定义排序函数
- jQuery+JavaScript:使用单个对象作为参数创建自定义ajax调用函数
- 如何创建使用前缀作为参数的自定义方法
- 通过在JavaScript中传递自定义参数来查询数据表
- ecmascript 5 -用固定参数的自定义顺序套用Javascript函数
- 如何在Backbone.js中传递自定义函数从路由器到视图到模板?