1

i am using stripe on my ruby on rails 5 website for my payment gateway plans. I am using the api in the client-side just as it appears in this link. 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. this is my javascript code:

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

<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>
jeff
  • 367
  • 4
  • 19

1 Answers1

0

It's not possible to use coupons for subscriptions in client-side only 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

Paul Asjes
  • 5,361
  • 1
  • 18
  • 20