#.abishek

1 messages ยท Page 1 of 1 (latest)

mossy arrowBOT
#

Hello! We'll be with you shortly. Below are links to other discussions we've had with you in the past week in case you want to review that information. If your question is related to one of these previous discussions, please provide a comprehensive summary of the current state and what you need help with now. We help many users simultaneously, so a summary allows us to resolve your issue as soon as possible.

solar fern
#

๐Ÿ‘‹ happy to help

onyx iris
#

ok

#

let me try that

#

thank you.

solar fern
#

let me know if you need any more help

onyx iris
#

and if I wanted to get the payment method used for the Checkout Session, is that easy to do or do I have to dig into the payment_intent or invoice objects?

solar fern
#

yes unfortunately you'd have to dig in

onyx iris
#

ok, thank you

#

how do I set the expand for items in an array. For example, on the CheckoutSession's lineItems, if they are a subscription, I want to expand the subscription object

#

wud this work

  $expand = [
      'line_items.data.price',
      'line_items.data.subscription',
      'customer.invoice_settings.default_payment_method',
      'customer.default_source'
  ];
solar fern
#

yes it should

onyx iris
#

ok, thank you. let me test it out. I don't know if it is a good idea to expand all the required data in one go or if do another API query

solar fern
#

I'm not sure you need customer.default_source

#

this will soon be deprecated

#

it's just here because of backwards compatibility and we used to have some of our internal integrations that still rely on it

onyx iris
solar fern
#

yeah I see

onyx iris
#

i had to end up doing this

$stripePaymentMethod = $stripeTransaction->payment_method ?? 
                           $stripeTransaction->invoice->default_payment_method ?? 
                           $stripeTransaction->invoice->default_source ?? 
                           $stripeTransaction->customer->invoice_settings->default_payment_method ?? 
                           $stripeTransaction->customer->default_source;
#

because our app has some old subscriptions

#

so this is the only way to retrieve the payment method for those subscriptions

#

i am assuming that is the true order for the check

#

also, would you be able to answer specific question to PHP SDK?

solar fern
onyx iris
#

on the PHP SDK, the Checkout session has the object line_items. This returns a Collection Object. I wanted to see if there was a way to filter the line_items that are only of type Subscription

solar fern
#

you'd have to do it yourself

#

the SDK doesn't have that capability in it

onyx iris
#

ok, thank you

#

usually collection classes have a where or filter methods that can be used to do that stuff, was hoping something like that is there

solar fern
#

I'm not sure to be honest, you can always try and see if it works

onyx iris
#

is it just me?

solar fern
#

yes because this needs to be expanded otherwise it wouldn't show

onyx iris
#

ok

solar fern
#

this change was made to have faster responses when calling the checkout session

#

or when sending the webhook event

onyx iris
#

ok

#

after the Stripe Checkout is completed, I am processing some stuff on the checkout.session.complete, but I am also subscription to subscription.created webhook. On the subscription.created webhook, is there a property that indicates that this subscription was created using a checkout session id?

solar fern
#

you shouldn't use customer.subscription.created for this

#

instead what you need to listen to is the invoice.paid events

onyx iris
#

invoice.paid wouldn't trigger if the checkout session has items that aren't recurring

solar fern
#

it would for subscriptions

#

that's what you're looking for

#

so basically in checkout.session.completed if mode is subscription then you can ignore the event

#

and just wait for the invoice.paid event

onyx iris
#

ok, because our cart feature has the option to include both one-off items and recurring items together

solar fern
#

once you receive that, you check the billing_reason and if it's subscription_create then it means there's a new subscription

onyx iris
#

once they add stuff to the cart, then we redirect the user to Stripe Checkout

solar fern
#

and the one-off payments would be added to the 1st invoice

onyx iris
#

the one-off don't recur monthly, is that still ok

mossy arrowBOT
solar fern
#

yes as I said it will only be on the first invoice

onyx iris
#

ok

#

ok, just confirming again, invoice.paid won't trigger if the CheckoutSession has only one-off items

#

in that case, I have to use the checkout.session.completed right?

hazy star
#

hi! I'm taking over this thread.

onyx iris
#

thank you @hazy star

hazy star
#

yes that sounds correct. but I recommend testing all of this in test mode to make sure it works as expected.

onyx iris
#

will customer.subscription.created only be triggered if the payment for the subscription is completed?

#

and what about the scenario of a subscription has a trial? which webhook would you use if using Stripe Checkout?

hazy star
#

if you are using Checkout Session, I would always listen to checkout.session.completed

onyx iris
#

ok

#

any idea if PHP SDK has a method to convert the payload received on webhook to convert to its specific object. For example, on the checkout.session.completed, convert the payload to the Checkout\Session object?

hazy star
#

Well the checkout.session.completed does contain a Checkout Session payload.

onyx iris
#

right, but want to convert the json object on the request to Checkout Session object.

#

not sure if I am saying this right.

hazy star
#

Well you would do something like $checkout = $event->data->object, and then in the $checkout variable you have a Checkout Session object and can access all its properties

onyx iris
#

ok, thank you

hazy star
#

happy to help ๐Ÿ™‚

onyx iris
hazy star
#

sure, what's the question?

onyx iris
#

this doesn't trigger if a subscription is paused and never unpaused?

#

will it always stay in this state or will it get deleted at certain point?

hazy star
#

correct, in this case the Subscription is not deleted, so customer.subscription.deleted won't trigger

#

will it always stay in this state
yes, unless you make an API request to change this

onyx iris
#

ok, in this case, I need to listen to the subscription.updated webhook. correct?

hazy star
#

to know when the Subscription is paused? yes.

onyx iris
#

ok

hazy star
#

customer.subscription.updated

onyx iris
#

yes, thank you

#

do checkout session every expire or get deleted?

onyx iris
#

but not deleted right? even if I try to retrieve an expired checkout session, I would still get the object right?

hazy star
#

correct, but users wouldn't be able to access the url

onyx iris
#

ok and this applies to any object in Stripe right? for example, if a subscription ends, the customer.subscription.deleted is called, but I can still retrieve that object using the subscription id, right. Its like a soft delete

hazy star
#

correct

mossy arrowBOT
onyx iris
#

or will $checkoutSession->payment_intent->payment_method suffice?

royal willow
#

Why you want to use the mode payment with checkout Session and Subscription ?

#

what is the exact use case ?

onyx iris
#

we are storing the payment method used for the session

#

our cart has the option to have both one-off items and subscription on the same checkout flow

#

so if the mode for the checkout session is PAYMENT I need to get the payment method from the payment_intent object. If the mode is SUBSCRIPTION, I need to use the invoice object from the checkout session

royal willow
#

our cart has the option to have both one-off items and subscription on the same checkout flow
The you need to use the mode SUBSCRIPTION and not PAYMENT as you have at least one recurring item

onyx iris
#

there are chances that the cart can just have one-off items as well

royal willow
#

then you use the mode payment

onyx iris
#

right now I am doing this

        if ($checkoutSession->mode === Session::MODE_PAYMENT) {
            $paymentMethod = $checkoutSession->payment_intent->payment_method ?? 
                                $checkoutSession->customer->invoice_settings->default_payment_method ?? 
                                      $checkoutSession->customer->default_source;
        } else {
            $paymentMethod = $checkoutSession->invoice->default_payment_method ?? 
                                $checkoutSession->invoice->default_source ?? 
                                    $checkoutSession->customer->invoice_settings->default_payment_method ?? 
                                          $checkoutSession->customer->default_source;
        }
royal willow
#

you make a condition check before creating the Checkout Session

onyx iris
#

this is part of the webhook

#

sorry if I did not mention this

#

this is after the checkout is completed and I need to retrieve the payment method used for the session

#

i am using the checkout.session.completed webhook

royal willow
#

Yes that's good

#

but you can't use a PaymentMethod create from a Chekcout with mode payment for future usage

onyx iris
royal willow
#

if you want to collect the payment for future usage you need to use setup mode

onyx iris
#

i don't think I will use the setup mode

#

just the payment and subscription based on what is on the cart

royal willow
royal willow
#

but not in the case of Payment

onyx iris
#

to put it clearly, if the checkout is complete and the checkout session mode is PAYMENT are there chances of checkoutsession->payment_intent->payment_method to be null ?

royal willow
#

No I don't think so.

onyx iris
#

do I still need to check for the customer.invoice_settings if the mode is PAYMENT, like I have done above

mossy arrowBOT
royal willow
#

Why you want to check that field if the checkout session is already completed ?

onyx iris
#

which field are you referring to on the last message?

royal willow
#

customer.invoice_settings

muted mortar
#

This thread has been ongoing for almost ~3 hours at this point. What specifically are you trying to achieve?

onyx iris
#

there are multiple questions in this thread which have already been solved, but the last one was in relation to getting the payment method from the checkout session

muted mortar
#

It'd be set on either the payment_intent or the subscription depending on the mode of the session

#

Checkout doesn't automatically set customer[invoice_settings][default_payment_method]

onyx iris
#

just to give you an overview, our app has a custom cart functionality, that supports adding both recurring and one-off items. We set the mode for the CheckoutSession when we create it based on the items on the Cart. So if it has atleast one recurring product, then we set the mode to SUBSCRIPTION else PAYMENT. We never set the mode to FUTURE. Once the checkout is completed, we use the checkout.session.completed webhook to process the checkout and convert our cart to an Order. In this process, we also want to store what payment method was used by the customer for processing the checkout

#

When doing this, we check if the mode is PAYMENT or SUBSCRIPTION

#

if the mode is PAYMENT we use the session->payment_intent->payment_method object

#

but if the mode is SUBSCRIPTION we use a logic to get the payment method

muted mortar
#

And for mode: 'subscription' you can use subscription[default_payment_method]

onyx iris
#

because session->invoice->default_payment_method and session->invoice->default_source returns null at times

#

which is why we added this functionality

#
if ($checkoutSession->mode === Session::MODE_PAYMENT) {
    $stripePaymentMethod = $checkoutSession->payment_intent->payment_method ?? 
                              $checkoutSession->customer->invoice_settings->default_payment_method ?? 
                                  $checkoutSession->customer->default_source;
} else {
    $stripePaymentMethod = $checkoutSession->invoice->default_payment_method ?? 
                                $checkoutSession->invoice->default_source ?? 
                                    $checkoutSession->customer->invoice_settings->default_payment_method ?? 
                                          $checkoutSession->customer->default_source;
}

if ($stripePaymentMethod instanceof PaymentMethod) {

    $userPaymentMethod = $user->saveUsingStripePaymentMethod($stripePaymentMethod);
} else {

    $userPaymentMethod = $user->saveUsingStripeCard($stripePaymentMethod);
}

return $userPaymentMethod;
muted mortar
#

Seems overly elaborate when you just need to check the field I stated

onyx iris
#

its elaborate, because we have some subscriptions on our end where that field is null

#

some old subscriptions return the card object

muted mortar
#

Sure, but that won't ever happen on new subscriptions created by Checkout

#

So if is this a webhook handler listening for checkout.session.completed events the the old subscriptions are irrelevant no?

onyx iris
#

the old subscriptions also used Stripe Checkout, so this was an additional check

#

so you are saying subscription[default_payment_method] will always have a value on checkout.session.completed

#

I don't need all these checks anymore

muted mortar
#

Assuming payment details are provided on the session payment page, yes

onyx iris
#

this should be sufficient?

if ($checkoutSession->mode === Session::MODE_PAYMENT) {
      $stripePaymentMethod = $checkoutSession->payment_intent->payment_method;
  } else {
      $stripePaymentMethod = $checkoutSession->invoice->default_payment_method;
  }

  return $user->saveUsingStripePaymentMethod($stripePaymentMethod);
onyx iris
muted mortar
onyx iris
#

ah ok, we don't run that risk right now,

muted mortar
onyx iris
#

as I mentioned before, I am using the checkout.session.completed webhook, so I have the checkoutSession on the payload

#

oh, there is a checkoutsession.subscription

muted mortar
onyx iris
#

damn, i did not see that

#

thank you

muted mortar
#

That'll only be set in mode: 'subscription' sessions, and you can expand it to get default_payment_method which Checkout will always set

onyx iris
#

yes, that is part of the mode check, thank you. this is quite simple now