繁体   English   中英

Stripe - 当我用零计划更新订阅时,PaymentIntent 是 null

[英]Stripe - PaymentIntent is null when I updated subscription with plan zero

我正在使用条纹元素,当我创建付款意向时,如果有特定价格的计划,它对我有用

但是,当计划为零 $0 时,它无法识别 payment_intent 或 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 class="form-row">
    <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 = "/" ;
    }
  });
});   

我不知道它是否已经很好地解释了我,我想要的是创建一个每月 0 美元的订阅,就像我一直在做其他价格的计划一样

欢迎任何帮助,非常感谢您阅读我

来自Stripe PaymentIntent 文档

最低金额为 0.50 美元或等值的收费货币。

您可以通过免费试用创建订阅,但由于上述付款意图限制,我认为如果计费金额为 0 美元,它将不起作用。

对于通过免费试用创建或金额为 0 至 0.5 美元的订阅,该条带不会生成付款意向。 为了解决这个问题,您需要在创建免费试用订阅时设置“payment_behavior: default_incomplete”,否则第一张发票为 0 美元,那么您将在 pending_setup_intent 取回设置意向。 检查条纹文档 然后您获得 setupIntent_clientSecret 并将其发送给您的客户端以使用 stripe.confirmCardSetup() 完成支付流程。 条纹参考

这是该链接的答案,用于回答 stackoverflow 帖子

暂无
暂无

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

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