简体   繁体   中英

STRIPE : This customer has no attached payment source or default payment method. RUBY ON RAILS

I've got some issues, i'm trying to implement subscription with stripe > it works when there is for exemple 3 items in my order > it create a subscription for the 3 items. The problem is that if the customer wants to stop sub only for ONE element, i dont know how to handle this...

So i was wondering to create a subscription for each element, this is my code

    customer = Stripe::Customer.create
    @order.line_items.each do |line_item|
      product = Stripe::Product.create(
        {
          name: line_item.product.name,
          metadata: {
            product_id: line_item.product.id,
            line_item_id: line_item.id
          }
        }
      )

      price = Stripe::Price.create(
        {
          product: product.id,
          unit_amount: line_item.product.price_cents,
          currency: 'eur',
          recurring: {
            interval: 'month'
          }
        }
      )

      Stripe::Subscription.create({
        customer: customer.id,
        items: [
          {price: price.id, quantity: line_item.quantity}
        ]
      })

but i got this error This customer has no attached payment source or default payment method. and i dont know how to attach it, even with documentation..

any help please? thank you

As said in comments ; To fix the error in the title:

  • You need to first have the "payment method", if you haven't already, create one:
    • Maybe using Stripe.js and it's elements API.
    • Which nowadays has "payment" element, allowing users to choose their payment-method.
    • And supports Setup-Intent's "client_secret" (beside Payment-Intent's), submittable using confirmSetup(...) method.

Then (using Stripe API):

  • Attach said "payment method" to the Customer:

    • (Optionally) set it as their default for invoices (with invoice_settings.default_payment_method ).
  • And, while creating the subscription, pass the customer (that which you atteched said "payment method" to).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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