簡體   English   中英

如何創建數量可變的Stripe訂閱?

[英]How to create a Stripe subscription with a variable quantity?

現在,我成功地創建了具有硬編碼數量的Stripe訂閱:

customer = stripe.Customer.create(email=request.form['stripeEmail'], source=request.form['stripeToken'])

# For the moment, change the quantity parameter for each customer
subscription = stripe.Subscription.create(
    customer = customer.id,
    items = [
        {
            'plan': 'plan_*************',
            'quantity': 7,
        },
    ],
)

想法是從前端獲得quantity價值。 我已經有了一個選擇器,可以通過編程方式設置數量值,實際上我正在使用它在Stripe Checkout中打印可變數量:

<script>
  var handler = StripeCheckout.configure({
    key: "{{pub_key}}",
    image: "https://stripe.com/img/documentation/checkout/marketplace.png",
    locale: "auto",
    zipCode: true,
    token: function(token) {
      // You can access the token ID with `token.id`.
      // Get the token ID to your server-side code for use.
    }
  });

  document.getElementById("customButton").addEventListener("click", function(e) {
    var quantity = document.getElementsByName("selectedQuantity")[0];
    var text = quantity.options[quantity.selectedIndex].text;
    // Open Checkout with further options:
    handler.open({
      name: "Company, Inc.",
      description: text + " Subscriptions",
      amount: 900 * text
    });
    e.preventDefault();
  });

  // Close Checkout on page navigation:
  window.addEventListener("popstate", function() {
    handler.close();
  });
</script>

注意,我從后端獲取公鑰,但是我不知道如何從后端的前端檢索數量值。

我怎么做?

編輯:

當我不使用自定義Checkout集成時,我有一個可以執行此操作的表單:

<form action="{{ url_for('pay') }}" method="POST">

但是這里沒有表格,所以我不確定下一步應該是什么。

您如何將令牌提交到后端服務器(即,您如何實現token功能)? 完成此操作后,您可以以相同的方式提交數量。

一種方法是使用表單,將Stripe令牌添加到該表單中的隱藏輸入中,然后使用Javascript提交。

這是一個例子: https : //jsfiddle.net/53u96kvw/

或者,如果您根本沒有表單,則可以發出具有相同信息的AJAX請求: https : //jsfiddle.net/kcp12o3z/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM