如何使用Meteor js重定向到Stripe成功后的成功页面

How to redirect to a success page After Stripe success with Meteor js

本文关键字:成功 Stripe 何使用 Meteor js 重定向      更新时间:2023-09-26

我正在用流星、进行条纹支付测试

我正在从我的服务器成功地获得付款

我用流星法,

但我需要

  1. 向用户显示付款处理的可视化信息,然后在付款成功时显示
  2. 返回一个成功的支付对象)到客户端(我可以这样做,然后用各种信息呈现一个成功模板

我怎么能同时做到这两件事呢?

提交事件

Template.step4.events({
  'submit #insertpaymentInfo':function(e){
      e.preventDefault();
      Stripe.setPublishableKey('PUBLISHABLEKEY');
      var $form = $('#insertpaymentInfo');
      $form.find('button').prop('disabled', true);
      Stripe.card.createToken($form, callbackStripe);
  }
});

令牌接收后从条带服务器的回调

function callbackStripe(status, response){
  var $form = $('#insertpaymentInfo');
  console.log("hi 1")
  if (response.error) {
    $form.find('.payment-errors').text(response.error.message);
    $form.find('button').prop('disabled', false);
  } else {
    var token = response.id;
    //TODO build an array of relevant data that needs to be sent to the server
    //Token on its own only sent for testing
    Meteor.call('processPayment',token);
  }
}

这是我的流星方法服务器端

 Meteor.methods({
    'processPayment':function(stripeToken){
         console.log("stripe token = "+ stripeToken);
         check(stripeToken,String);
         console.log("payment and order can be processed and created");
         var Stripe = StripeAPI('TESTKEY');
      Stripe.charges.create({
       amount: 1000, // amount in cents, again
       currency: "usd",
       card: stripeToken,
       description: "payinguser@example.com"
      }, function (err, res) {
        console.log(err, res);
     });
 }
});

您可以使用一个反应变量来显示不同的子模板,在同一个父模板中处理所有这些模板。

Template.step4.created = function(){
  this.state = new ReactiveVar("editing");
}
<template name="step4">
  {{#if state "editing"}}
    {{> payment_form}}
  {{/if}}
  {{#if state "processing"}}
    {{> processing}}
  {{/if}}
  {{#if state "success"}}
    {{> success}}
  {{/if}}
</template>
Template.step4.helpers({
  state: function(param){
    return Template.instance().state.get() === param;
  }
});
Template.step4.events({
  'submit #insertpaymentInfo':function(e, template){
    e.preventDefault();
    Stripe.setPublishableKey('PUBLISHABLEKEY');
    var $form = $('#insertpaymentInfo');
    $form.find('button').prop('disabled', true);
    template.state.set("processing");
    Stripe.card.createToken($form, function(err, res){
      if (error){
        template.state.set("editing");
        // error handle as you did above
      } else {
        template.state.set("success");
        // show info via response object
      }
    });
  }
});