简体   繁体   English

Stripe checkout.js - 将自定义参数传递给令牌回调

[英]Stripe checkout.js - passing custom params to token callback

I'm following this example integration from Stripe Docs (slightly modified in order to be able to add click handlers to more than one button: 我正在关注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();
  });

In my particular case I have a few buttons like: 在我的特定情况下,我有几个按钮,如:

<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>

... and obviously I'd like to pass a booking-id of clicked button somehow to token callback. ...显然我想以某种方式传递点击按钮的预订ID以进行令牌回调。 Couldn't find any example or explanation covering this seemingly simple case... any help much appreciated. 找不到任何关于这个看似简单案例的例子或解释......任何帮助都非常感激。 thanks! 谢谢!

This is a little bit late, but maybe it will help someone else. 这有点晚了,但也许它会帮助别人。 This is modified from a Rails example: 这是从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

Move your token initializer from configure to open. 将令牌初始化程序从configure移至open。

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();
  });

And switch to: 并切换到:

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Stripe Checkout.js 令牌回调会在付款处理之前阻止吗? - Does Stripe Checkout.js token callback block until payment processed? 条纹自定义结帐-将JS变量传递给金额 - Stripe Custom Checkout - Passing JS variable into amount 如何设计类似于paypal或stripe的自己的checkout.js - How to design own checkout.js similar to paypal or stripe Stripe Checkout https://checkout.stripe.com/checkout.js 和 Elements https://js.stripe.com/v3/ 之间的区别 - Difference between Stripe Checkout https://checkout.stripe.com/checkout.js and Elements https://js.stripe.com/v3/ 如何从条纹 checkout.js 迁移到条纹元素? - how can I migrate from stripe checkout.js to stripe elements? 条纹支付集成-将.js [checkout.js]移出循环的方法是什么? - Stripe payment integration - What is the way to move .js [checkout.js] outside of loop? Javascript回调和自定义条带检出处理程序 - Javascript Callback and Custom Stripe Checkout Handler 万事达支付网关 checkout.js 方法未触发完整回调 function - MasterCard payment gateway checkout.js method not triggering complete Callback function WooCommerce自定义checkout.js-订单以账单地址作为送货地址 - WooCommerce custom checkout.js - Order being placed with billing address as shipping address PayPal checkout.js-获取付款ID和付款人ID - PayPal checkout.js - Get paymentID & payerID
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM