#emily24-checkout-description

1 messages ยท Page 1 of 1 (latest)

zealous portal
#

hey there, thanks for you patience and saving the code for the thread ๐Ÿ™‚

late tendon
#
         
        // Set API key 
        \Stripe\Stripe::setApiKey(STRIPE_API_KEY); 
         
        // Fetch the Checkout Session to display the JSON result on the success page 
        try { 
            $checkout_session = \Stripe\Checkout\Session::retrieve($session_id); 
        }catch(Exception $e) {  
            $api_error = $e->getMessage();  
        } 
         
        if(empty($api_error) && $checkout_session){ 
            // Retrieve the details of a PaymentIntent 
            try { 
                $intent = \Stripe\PaymentIntent::retrieve($checkout_session->payment_intent); 
            } catch (\Stripe\Exception\ApiErrorException $e) { 
                $api_error = $e->getMessage(); 
            } 
             
            // Retrieves the details of customer 
            try { 
                // Create the PaymentIntent 
                $customer = \Stripe\Customer::retrieve($checkout_session->customer); 
            } catch (\Stripe\Exception\ApiErrorException $e) { 
                $api_error = $e->getMessage(); 
            } 
             
            if(empty($api_error) && $intent){  
                // Check whether the charge is successful 
                if($intent->status == 'succeeded'){ 
                    // Customer details 
                    $name = $customer->name; 
                    $email = $customer->email; 
                     
                    // Transaction details  
                    $transactionID = $intent->id; 
                    $paidAmount = $intent->amount; 
                    $paidAmount = ($paidAmount/100); 
                    $paidCurrency = $intent->currency; 
                    $paymentStatus = $intent->status;
                    $paymentItemNumber = $intent->description;
                     ```
#
      {
        "id": "li_1Ja3W2HhaDPa8eVPudjB6QM3",
        "object": "item",
        "adjustable_quantity": null,
        "description": "237",```
#

I need to get description from line_items

#

$paymentItemNumber = $intent->description;

#

this is returning nothing at all

#
                    $paidAmount = $intent->amount; 
                    $paidAmount = ($paidAmount/100); 
                    $paidCurrency = $intent->currency; 
                    $paymentStatus = $intent->status;```
#

these work and return datasets

zealous portal
#

so, hang on

#

the line_items are on the session, not the payment intent

#

are you just trying to get them in the wrong place?

#

where you've got $intent->description; would be getting the description form the payment intent, no the session line items

#
$session = \Stripe\Checkout\Session::retrieve('cs_test_123', [
     'expand' => ['line_items']
    ]);
#

you could also use this request ot expand the customer and payment intent and the same time, too ๐Ÿ™‚

late tendon
#

let me try that and see what I break next ๐Ÿ˜„

zealous portal
#

keep in mind too that the line items are a list/array, so you'll need to traverse the index

#

item 0, item 1 ...etc

late tendon
#

this applies to retrieving metadata as well?

zealous portal
#

metadata should be included by default for each object

late tendon
#

All I want to do is pass an identifier so I know what the product id is after the checkout is complete

#
                'price_data' => [ 
                    'product_data' => [ 
                        'name' => $productName, 
                        'metadata' => [ 
                            'pro_id' => $productId
                        ] 
                    ], ```
#

seems it's a line_item too

mortal flume
#

Hello! The code snippet above looks like it's from creation of a Checkout Session where you're specifying new Product data (which will create a new Product in Stripe every time). Can you tell me more about your use case/what you're using Checkout to charge people for?

late tendon
#

yes

#

one moment

#

but the issue is all the price\item is static

#

basically they can goto the page and decide how much for a particular "item"

mortal flume
#

So you need dynamic amounts defined by the customer for your integration?

late tendon
#

yes i'm able to dynamically set the price the problem is I have no idea what item it is for

#

without returning a product identifier after checkout

mortal flume
#

I recommend you add the product identifier to the Checkout Session as metadata.

late tendon
#

it already is I see it in the stripe test data

#

but I need to take further action once the checkout is complete

mortal flume
#

Can you give me the ID of a Checkout Session with the metadata set so I can see what you're seeing? It's the ID that starts with cs_

late tendon
#

sure one moment

#

cs_test_a1B6JCzaPBGd5pMsciIfFN8eWSJcgYxUu0dgzJjC4vijyN9ntOBwXLQgDD

mortal flume
#

In Stripe a Product has one or more Prices, and you use Checkout to charge people for one or more of those Prices. You can create a Checkout Session with Product and Price data to create Products and Prices on the fly (for dynamic amounts), but the Checkout Session's line items will only show the Prices, not the Products they belong to.

Thinking about it more, if you're selling multiple Products/Prices in a single Checkout Session, you would probably want to add the Product ID you need as metadata on the Price, not the Product. That way you can access it directly on the Checkout Session with line_items expanded: https://stripe.com/docs/api/checkout/sessions/object#checkout_session_object-line_items-data-price-metadata

#

Yeah, in that Checkout Session you're setting the metadata on the Product, not the Price. If you change it so you're setting it on the Price I think it will work the way you want.

#

Wait a second... can you set metadata on the Price when creating a Checkout Session...

#

Oh, wow, you can't.

#

Hm, okay.

#

Going to flag that internally.

late tendon
#
      {
        "id": "li_1Ja3W2HhaDPa8eVPudjB6QM3",
        "object": "item",
        "adjustable_quantity": null,
        "description": "237",
        "discount_amounts": [
        ],```
#

seems I set it on the description successfully

#

"description": "237", <===

mortal flume
#

The description is visible to the Customer. If that's okay with you that will work too.

late tendon
#

sure it doesn't bother me at all

#

it's just a product id

#

long as they can't change it ๐Ÿ˜„

mortal flume
#

Nope, they can't change it! ๐Ÿ™‚

late tendon
#

that's one thing I wasn't sure about pre-purchase and wanted to ask

#

because it's using javascript it seems

mortal flume
#

What is?

late tendon
#

wasn't sure if they could manipulate the request

mortal flume
#

Your PHP code is creating the Checkout Session and setting the description, right?

late tendon
#

yes and handling the price to make sure they don't do anything undesirable

#

but I wasn't sure if it was just echo'd and handled by stripe js

#

where they could set any product id paired with that price

mortal flume
#

No, they can't modify the description on the Checkout Session object in Stripe. They could modify the Checkout page to display something different client-side, but that change wouldn't impact anything server side.

late tendon
#

phew good thanks

#

sorry for my newbie understanding of things

mortal flume
#

No worries at all, we're here to help!

late tendon
#

$paymentItemNumber = $intent->description;

#

didn't seem to work

#

which brings me full circle

mortal flume
#

$intent is the Payment Intent, right?

#

One sec, let me type up some code...

late tendon
#

i'll have to look at it closely who knows how secure it is I just want a test that works

mortal flume
#

Try this:

$checkout_session = \Stripe\Checkout\Session::retrieve([
    'id' => $session_id,
    'expand' => [
        'line_items'
    ],
]);

$first_description = $checkout_session->line_items->data[0]->description;
#

You need to retrieve the Checkout Session with line_items expanded and then traverse the object down to the description.

#

You would change data[0] to data[1] for the second description, data[2] for the third, etc.

#

Or you can loop over data to handle them all.

#

Does that make sense?

late tendon
#

yes let me see if that works thanks

#

are you guy the in the videos on youtube? ๐Ÿ˜„

#

your avatar looks familiar hah

mortal flume
#

No, I'm not. ๐Ÿ™‚

#

I know him though!

late tendon
#

hah my mistake

#

thanks I'll give this a shot ๐Ÿ˜„

#

phew that works thank you so much

mortal flume
#

Awesome!

late tendon
#

i know you're doing your job but a working code example geez

#

best support I've ever experienced hah

mortal flume
#

That's great to hear, thank you!

late tendon
#

thanks for helping a newbie and not talking down to me

#

night

mortal flume
#

Anytime! Have a great night!