Home > Blockchain >  Stripe - PaymentIntent is null when I updated subscription with plan zero
Stripe - PaymentIntent is null when I updated subscription with plan zero

Time:12-15

I'm using stripe element and when I create a payment intention it works for me if there is a plan with a certain price

But when the plan is zero $0 it does not recognize payment_intent or the client_secret

# Here I created an object of type subscription incompletely (@subscription)
# In such a way that the user can enter their credit card and with the help of 
# @subscriptions confirm if everything is fine in checkout.html.erb

def checkout
    @subscription = Stripe::Subscription.create(
      customer: current_user.stripe_customer_id, # stripe customer_id for suscription 
      items: [{
        price: params[:price] # attached price of suscription plans
      }],
      payment_behavior: 'default_incomplete', # PaymentIntent with status=incomplete
      expand: ['latest_invoice.payment_intent'] # return the payment_intent data
    )
end


# checkout.html.erb
<form id="payment-form">
  <div >
    <label for="card-element">
      <!-- Debit/credit card -->
    </label>

    <div id="card-element">
      <!-- a Stripe Element will be inserted here -->
    </div>

    <!-- Used to display Elements errors -->
    <div id="card-errors" role="alert"></div>
  </div>

  <button id="submit">Submit Payment</button>
</form>

<script>
   // Initialize stripe elements
   var stripe = Stripe("<%= ENV['STRIPE_PUBLISHABLE_KEY'] %>");
   var elements = stripe.elements();
   var cardElement = elements.create('card');
   cardElement.mount('#card-element');

   var formElement = document.getElementById('payment-form');

   formElement.addEventListener('submit', function(event) {
     event.preventDefault();

  # here I create the payment intention but when the plan has a subscription of $ 0 
  # it 
  # does not recognize me neither the field client_secret nor the payment_intent

  stripe.confirmCardPayment("<%= 
     @subscription.latest_invoice.payment_intent.client_secret %>", {
    payment_method: { card: cardElement }
     }).then(function(result) {
    if (result.error) {
      console.log(result.error.message);
      window.location.reload();
    } else {
      window.location.href = "/" ;
    }
  });
});   

I do not know if it has explained me well, what I would like is to create a monthly subscription with $ 0 as I have been doing with plans of other prices

Any help is welcome, thank you very much for reading me

CodePudding user response:

From the Stripe PaymentIntent docs

The minimum amount is $0.50 US or equivalent in charge currency.

You can create subscriptions with free trials but I don't think it will work if the billed amount is $0, due to the payment intent restriction mentioned above.

  • Related