[英]How to integrate Stripe “Pay with Card” in backbonejs
I am trying to integrate Stripe "Pay with Card" checkout into backbone Node environment. 我正在尝试将Stripe“Pay with Card”结账集成到骨干节点环境中。 On the server side, I am using Stripe Node code - that part works good. 在服务器端,我使用条带节点代码 - 该部分工作正常。 However, on the client side, I am unable to capture the event. 但是,在客户端,我无法捕获该事件。
I would like to capture the submit event from the Stripe popup to call "paymentcharge" method in the view. 我想从Stripe弹出窗口捕获提交事件,在视图中调用“paymentcharge”方法。
Here is my code: 这是我的代码:
<!-- Stripe Payments Form Template -->
<form id="stripepaymentform" class="paymentformclass">
<script
src="https://checkout.stripe.com/v2/checkout.js" class="stripe-button"
data-key="pk_test_xxxxxxxxxxxxx"
data-amount="0299"
data-name="MyDemo"
data-description="charge for something"
data-image="assets\ico\icon-72.png">
</script>
</form>
Backbone View Class 骨干视图类
myprog.PaymentPanelView = Backbone.View.extend({
initialize: function () {
this.render();
},
render: function () {
$(this.el).html(this.template());
return this;
},
events : {
"submit" : "paymentcharge"
},
paymentcharge : function( event) {
this.model.set({stripeToken: stripeToken});
}
});
Backbone Model Class 骨干模型类
var PaymentChargeModel = Backbone.Model.extend({
url: function(){
return '/api/paymentcharge';
},
defaults: {
}
})
Setup/Call the View from header menu event 设置/从标题菜单事件调用视图
if (!this.paymentPanelView) {
this.paymentPanelView = new PaymentPanelView({model: new PaymentChargeModel()});
}
$('#content').html(this.paymentPanelView.el);
this.paymentPanelView.delegateEvents();
this.selectMenuItem('payment-menu');
I think the problem has to do with your View's el
and the event you are listening for. 我认为这个问题与你的View el
和你正在听的事件有关。
You never explicitly define your View's el
, which means it gets initialized to a detached <div>
element. 您从未明确定义View的el
,这意味着它被初始化为分离的<div>
元素。 You then use your template to fill that <div>
with the form element from the template. 然后,使用模板使用模板中的表单元素填充<div>
。 Even though your <div>
is detached, you get to see the content, because you add the content of you el
to #content
using jquery. 即使你<div>
被分离,你能看到的内容,因为你加你的内容el
以#content
使用jQuery。
I think the problem is that you are listening for a submit
event on the <div>
in your el
, not the contained <form>
. 我认为问题在于您正在监听el
<div>
的submit
事件,而不是包含<form>
。 Try changing your events hash to this: 尝试将您的事件哈希值更改为:
events: {
'submit form#stripepaymentform': 'paymentcharge'
}
Basically, listen for events on the contained element like in jquery's .on
. 基本上,在jquery的.on
包含元素的.on
。 You can also go right to a button click, something like this: 您也可以直接点击按钮,如下所示:
'click #mysubmitbutton': 'paymentcharge'
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.