如何将优惠券添加到条纹结帐

问题描述

我在Rails 5网站上的红宝石上使用条纹作为我的付款网关计划。我正在客户端使用api,就像在this link显示的一样。我需要在计划中添加优惠券,我可以像计划一样在测试模式下在条纹仪表板中创建优惠券,但是将ID添加代码中不会将我重定向到条纹结帐页面。这是我的JavaScript代码

    <script src="https://js.stripe.com/v3"></script>
    
    <script type="text/javascript">
    
      $(document).ready(function() {
    
        var stripe = Stripe('<%= Rails.configuration.stripe[:publishable_key] %>');
    
        $('.payment-action').on('click',function() {
    
          const data = $(this).data();
          const user_email = '<%= current_user ? current_user.email : ""%>'
          const user = '<%= current_user.id%>'
    
          stripe.redirecttocheckout({
            lineItems: [{
              // Define the product and price in the Dashboard first,and use the price
              // ID in your client-side code. You may also pass a SKU id into the `price`
              // field 
              price: data['plankey'],quantity: 1
            }],customerEmail: user_email,mode: 'subscription',subscriptionData: {
              coupon: 'WaS5wFHC'
            },successUrl: 'https://www.my_web.network/success_payment?session_id={CHECKOUT_SESSION_ID}&p_i='+data['plan']+'&us='+user,cancelUrl: 'https://www.my_web.network/update_plan'
          });
        });
    
      });
    
    
    </script>

我一直在尝试使用subscription_data或subscriptionData将其显示this page上,但仍然无法正常工作,我可能会丢失什么?

<script type="text/javascript">

  $(document).ready(function() {

    var stripe = Stripe('<%= Rails.configuration.stripe[:publishable_key] %>');

    $('.payment-action').on('click',function() {

      const data = $(this).data();
      const user_email = '<%= current_user ? current_user.email : ""%>'
      const user = '<%= current_user.id%>'

      stripe.redirecttocheckout({
        lineItems: [{
          // Define the product and price in the Dashboard first,and use the price
          // ID in your client-side code. You may also pass a SKU id into the `price`
          // field 
          price: data['plankey'],quantity: 1
        }],subscription_data: {
          coupon: 'WaS5wFHC'
        },cancelUrl: 'https://www.my_web.network/update_plan'
      });
    });

  });


</script>

解决方法

仅在客户端Checkout中无法将优惠券用于订阅。您必须在您的服务器上创建一个Checkout会话,并在其中传递优惠券ID:https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-subscription_data-coupon