Javascript回调和自定义条带检出处理程序

时间:2018-07-28 13:38:37

标签: javascript callback stripe-payments

大部分工作并将令牌和价格传递到我的后端进行了一些更改,因为每个答案仍然无法从createBookingSuccessful回调中获取booking_id并将令牌与我的令牌一起传递到后端。对于javascript来说还很新,简单的条纹结帐起初还不错,但是现在我需要使用自定义方法从第三方预订回调中传递预订ID。

function initalizeWidget(duration, title, price, contractor) {
   var widget = new TimekitBooking();
   widget.init({
   app_key: 'live_widget_key_zr3c9idDjH',
   resources: [
      '1b6097f9-4806-3dec48c8'
    ],

    callbacks: {
      createBookingSuccessful: function(response)  {
        if (response.data) {
          // Update the booking)id here.
           var booking_id = response.data.id;
           console.log(booking_id);
           handler = StripeCheckout.configure(stripeCheckoutConfig);
           handler.open({
           name: contractor,
           description: title,
           zipCode: true,
           amount: price
         });
         // ...
      }
    }
  },   
 });
}


var stripeCheckoutConfig = {
  image:  'https://stripe.com/img/documentation/checkout/marketplace.png',
  locale: 'auto',
  key: 'pk_test_O9AlqrUIlJTH2a5V0e',

  token: function(token) {
    // Get the booking_id;
  var booking_id = this.booking;
   // Send the charge through
  $.post("/subscription/web/payment-method/",
    { token: token.id, price: {{ task_price_cents }}, booking_id:   booking_id}, function(data) {
  if (data["status"] == "ok") {
    window.location = "/some-url/";
  } else {
    // Deal with error
    alert(data["message"]);
  }
});
}
};

// Simply pass the config.
var handler = StripeCheckout.configure(stripeCheckoutConfig);

1 个答案:

答案 0 :(得分:0)

您可以使用data对象存储有关app的所有信息。

/**
 * Your application's data (everything related to your booking 
 * system on the client-side)
 * It can be accessed by the Objects/Functions in the same scope.
 */
var appData = {
  booking_id: null,
  handler: null
};

/**
 * Widget init
 */
var widget = new TimekitBooking();
widget.init({
  // ...
  callbacks: {
    createBookingSuccessful: function(response)  {
      if (response.data) {
        // Update the booking id here. 
        appData.booking_id = response.data.id;
        // ...
      }
    }
  }
};

// Pass the config.
appData.handler = StripeCheckout.configure({
  // ...
  token: function(token) {
    // Get the booking_id;
    var booking_id = appData.booking_id;
    // Send the charge through
    $.post(
      "/subscription/web/payment-method/",
      { token: token.id, price: {{ task_price_cents }}, booking_id: appData.booking_id },
      // ...
    );
  }
});

var handler = appData.handler;