#sw-svl_code

1 messages ¡ Page 1 of 1 (latest)

thorny roverBOT
#

👋 Welcome to your new thread!

⏲️ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.

⏱️ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always start a new thread if you have another question.

🔗 This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1384483660300026027

📝 Have more to share? Add more details, code, screenshots, videos, etc. below.

junior breach
#

The relevant code:

price = create_price(donation[:reference], monetised_amount, interval, interval_count)
customer = create_customer(donation)
subscription = create_subscription(price, customer, "#{donation[:forename]} #{donation[:surname]}", donation[:email])
invoice = ::Stripe::Invoice.retrieve(subscription.latest_invoice.id, { api_key: private_api_key })
{
  payment_intent_client_secret: invoice.confirmation_secret.client_secret,
  subscription_id: subscription.id
}
#

and:

      def create_price(reference, amount, interval, interval_count)
        ::Stripe::Price.create(
          {
            currency: amount.currency,
            unit_amount: amount.cents,
            recurring: {
              interval: interval,
              interval_count: interval_count
            },
            billing_scheme: 'per_unit',
            product_data: { name: "Recurring Donation - Reference: #{reference}" }
          },
          { api_key: private_api_key }
        )
      end

      def create_customer(donation)
        ::Stripe::Customer.create(
          {
            email: donation[:email],
            name: "#{donation[:forename]} #{donation[:surname]}",
            address: {
              city: donation[:city],
              country: donation[:country],
              line1: donation[:street],
              postal_code: donation[:zip],
              state: donation[:state]
            }
          },
          { api_key: private_api_key }
        )
      end

      def create_subscription(price, customer, full_name, email)
        ::Stripe::Subscription.create(
          {
            customer: customer.id,
            items: [{ price: price.id }],
            payment_behavior: 'default_incomplete',
            payment_settings: { save_default_payment_method: 'on_subscription' },
            collection_method: 'charge_automatically',
            expand: ['latest_invoice'],
            metadata: { name: full_name, email: email }
          },
          { api_key: private_api_key }
        )
      end
tepid epoch
junior breach
#

As I said the confirmation_secret returned from the invoice is nil

tepid epoch
#

ahhh sorry i missed that. can you share an example subscription ID? e.g. sub_***

junior breach
#

From the stripe dashboard or just from how I'm creating it in the code I shared above?

tepid epoch
junior breach
#

Sure, one second.

And this will be from 'test mode' if thats fine

#

Here: sub_1Rax8HFpgOJXHv3IoQQ2SunF

tepid epoch
#

yep test mode is perfect! looking now

#

it looks like you're just expanding on latest_invoice, what happens if you expand on latest_invoice.confirmation_secret?

#

it's a little confusing but that property only shows if you expand it

thorny roverBOT
junior breach
#

Thanks I'll give that a try

#

I'm still getting nil returned, the sub ID: sub_1RaxCjFpgOJXHv3IqDAFk0Ty

vale parrot
#

hi! I'm taking over this thread. having a look

junior breach
#

`NoMethodError - undefined method 'client_secret' for nil:

          payment_intent_client_secret: invoice.confirmation_secret.client_secret,`
vale parrot
#

I think it's because it's not invoice but latest_invoice you should use.

#

on the subscription object it:s latest_invoice.confirmation_secret.client_secret

junior breach
#

So I should change from 'retrieving' it to just chaing from the subscription?

currently:

        invoice = ::Stripe::Invoice.retrieve(subscription.latest_invoice.id, { api_key: private_api_key })
vale parrot
#

you don't need to retrive the invoice at all, the subscription directly conntains the information you need since you are using expand.

junior breach
#

Ah, thanks the docs were showing otherwise in places.

#

Just put through a test and it went through fine, thanks for your help!