#chrisdaviesweb

1 messages · Page 1 of 1 (latest)

strange timberBOT
idle heron
#

It may actually getting passed properly, but there are a lot of objects involved in a Checkout Session so it may not be getting passed to the object that you are expecting. Can you show me your code for setting this metadata? And where are you checking for the metadata?

brazen pendant
#

Thank you.

#

Here is my code:

#

$checkout_session = \Stripe\Checkout\Session::create([
'payment_method_types' => ['card'],
'line_items' => [[
'price_data' => [
'product' => 'prod_MysjVZMMHVMwsn',
'currency' => 'gbp',
'unit_amount' => '2000',
'tax_behavior' => 'exclusive',
],
'adjustable_quantity' => [
'enabled' => false
],
'quantity' => 1,
]],
'metadata' => [
'entry_id' => $_POST['prod_entry_id']
],
'automatic_tax' => [
'enabled' => true,
],
'mode' => 'payment',
'allow_promotion_codes' => false,
'billing_address_collection' => 'required',
'success_url' => $YOUR_DOMAIN . 'success',
'cancel_url' => $YOUR_DOMAIN . 'cancelled'
]);

idle heron
#

Gotcha, and are you checking for that metadata on the Checkout Session itself or on the PaymentIntent after the session completes?

brazen pendant
#

I'm looking at the 'Metadata' in the payment.

#

I'm then checking '$event->data->object->metadata->entry_id' in the callback

#

Like so:

#

\Stripe\Stripe::setApiKey('xxx');

    function print_log($val) {
        return file_put_contents('php://stderr', print_r($val, TRUE));
    }
    
    $endpoint_secret = 'xxx';
    
    $payload = @file_get_contents('php://input');
    $sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
    $event = null;
    
    try {
        $event = \Stripe\Webhook::constructEvent(
            $payload, $sig_header, $endpoint_secret
        );
    } catch(\UnexpectedValueException $e) {
        http_response_code(400);
        exit();
    } catch(\Stripe\Exception\SignatureVerificationException $e) {
        http_response_code(400);
        exit();
    }

    switch ($event->type) {
        case 'checkout.session.completed':
            $entry_id = $event->data->object->metadata->entry_id;
            if ( $entry_id )
            {
                $this->update_status($event->data->object->metadata->entry_id);
            }
            else 
            {
                $this->log_error('entry_id missing');
            }
            break;
        default:
            $this->log_error('Received unknown event type ' . $event->type);
    }

    http_response_code(200);
idle heron
#

Gotcha, that is actually a separate object and the metadata isn't automatically copied from the session to the payment. If you set this metadata in payment_intent_data.metadata , that should set the metadata where you are expecting once the session has completed https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-payment_intent_data-metadata

#

Oh interesting. checkout.session.completed should be the Checkout Session itself so your code should work. Do you have the ID of an event (evt_123) where you set this metadata but didn't see it on the event?

brazen pendant
#

Yes, let me check for it for you

#

Would the PI be any use? pi_3MEwWvFXUxyD6BGS1McgTZKe

#

Oh interesting. Looking in checkout.session.completed its showing the metadata

idle heron
#

Yeah, so you can set the metadata on the session itself if you want it in checkout.session.completed or in that payment_intent_data hash if you want it in the payment_intent.succeeded event

brazen pendant
#

Is it not possible to set it on the payment?

idle heron
#

payment_intent_data.metadata should do that. Have you tried that and it still isn't showing up?

brazen pendant
#

Sorry, I'm a little confused.

#

I'm checking 'checkout.session.completed'

#

Is this the wrong place to check?

idle heron
#

That is a perfectly valid place to check. Just so we align on terminology, is the object in "checkout.session.completed" what you are calling "the payment"?

#

Looking in checkout.session.completed its showing the metadata
Also it sounded like this metadata was showing up. Is it not anymore?

brazen pendant
#

Are you able to take a look at payment 'pi_3MExaPFXUxyD6BGS24KgBr49' as an example?

#

Previously, I have set the metadata as I have in the original code I posted. This then shows under 'Metadata' in the dashboard payment area. I was then able to retrieve it.

#

Now however, this is blank, but if I check events and logs section, in 'checkout.session.completed', I can see the metadata correctly.

#

I just can't get it in my webhook

#

Heres an example where in the webhook code:

#

switch ($event->type) {
case 'checkout.session.completed':
$entry_id = $event->data->object->metadata->entry_id;
if ( $entry_id )
{
$this->update_status($entry_id);
}
else
{
$this->log_error('entry_id missing');
}
break;
default:
$this->log_error('Received unknown event type ' . $event->type);
}

idle heron
#

So pi_3MExaPFXUxyD6BGS24KgBr49 was created when checkout session cs_test_a1FbOwsaGGKcFVLohDLDI9i4poDathx9II9CXkWU9oRcvnWDiYpc5ThYm6 was completed. When you created that checkout session, you passed entry_id in as metadata at the top level of your API call. That set the metatada on the checkout session itselt (cs_test_a1FbO...) but did not set the metadata on the payment intent. So, the metadata is on the checkout.session.created event but not on the payment_intent.succeeded. The dashboard shows metadata on the payment intent, so it shows nothing because there is no metadata on pi_3MExaPFXUxyD6BGS24KgBr49 itself.

Checkout Session creation request: https://dashboard.stripe.com/test/logs/req_JLdlTxv40rlRZc
checkout.session.completed event with metadata https://dashboard.stripe.com/test/events/evt_1MExavFXUxyD6BGS1HECC7RD
payment_intent.succeeded event with no metadata

#

You can set this metadata in both places if you want it to show up on both objects, just keep in mind that the data isn't synced. So if you update it in one place it wouldn't get updated on the other

brazen pendant
#

Thank you

#

I'm seeing that in webhooks "Connection to remote host denied"

#

I think this may actually be the issue.

#

Stripe CLI is showing as 200 returned...