简体   繁体   English

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

[英]How to add coupons to stripe checkout

i am using stripe on my ruby on rails 5 website for my payment gateway plans.我在我的 ruby​​ on rails 5 网站上使用 Stripe 来实现我的支付网关计划。 I am using the api in the client-side just as it appears in this link .我在客户端使用 api,就像它出现在这个链接中一样 I need to add a coupon to my plan, I could create it in the stripe dashboard in the testmode like the plan but adding the id to the code doesn't redirect me to the stripe checkout page.我需要向我的计划添加优惠券,我可以像计划一样在测试模式中的条纹仪表板中创建它,但是将 id 添加到代码不会将我重定向到条纹结帐页面。 this is my javascript code:这是我的 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>

I've been trying to get it to appear on this page using subscription_data or subscriptionData but it still doesn't work, what could I be missing?我一直在尝试使用 subscription_data 或 subscriptionData 让它出现在这个页面上,但它仍然不起作用,我可能会遗漏什么?

<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',
        subscription_data: {
          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>

It's not possible to use coupons for subscriptions in client-side only Checkout.无法在仅限客户端的 Checkout 中使用优惠券进行订阅。 You'd have to create a Checkout Session on your server where you pass in your coupon ID: https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-subscription_data-coupon您必须在您的服务器上创建一个 Checkout Session,并在其中传递您的优惠券 ID: https : //stripe.com/docs/api/checkout/sessions/create#create_checkout_session-subscription_data-coupon

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM