user3507560
user3507560

Reputation: 353

Stripe Payment 'client_reference_id'

I'm intergrating with the Stripe Payment Gateway - the latest version of the API.

It's all working apart from the 'client_reference_id' does not get passed to either the webhook or the transaction (Does not appear in the log) - I need this to update the order in the database.

My code is:

$session = \Stripe\Checkout\Session::create([
            'billing_address_collection' => 'required',
            'payment_method_types' => ['card'],
            'line_items' => [[
                'amount' => 1011,
                'currency' => 'GBP',
                'name' => 'Purchase',
                'description' => 'test',
                'quantity' => 1,
            ]],
            'client_reference_id' => 'TEST1111223',
            'mode' => 'payment',
            'success_url' => 'https://example.com/success',
            'cancel_url' => 'https://example.com/cancel'
        ]);

According to the documentation this should be passed: https://stripe.com/docs/api/checkout/sessions/create

Am I doing something wrong?

Someone else asked the same question but was not answered: The "client_reference_id" argument is not passed

Upvotes: 21

Views: 19219

Answers (3)

Senthil
Senthil

Reputation: 634

It would make sense for the customer.created event to have the client_reference_id. However that is not the case! It does show up in the checkout.session.completed Event.

Below is the object detail

Stripe\Event Object
(
    [id] => evt_1KCO7....dffL4wc0
    [object] => event
    [api_version] => 2020-08-27
    [created] => 1640868753
    [data] => Stripe\StripeObject Object
        (
            [object] => Stripe\Checkout\Session Object
                (
                    [id] => cs_test_a1A....NhSTmvD
                    [object] => checkout.session
                    [after_expiration] => 
                    [allow_promotion_codes] => 
                    [amount_subtotal] => 15000
                    [amount_total] => 15000
                    [automatic_tax] => Stripe\StripeObject Object
                        (
                            [enabled] => 
                            [status] => 
                        )

                    [billing_address_collection] => 
                    [cancel_url] => http://example.org/cancel
                    [client_reference_id] => 3181234
                    [consent] => 
                    [consent_collection] => 
                    [currency] => usd
                    [customer] => cus_Ks8...bT
                    [customer_details] => Stripe\StripeObject Object
                        (
                            [email] => [email protected]
                            [phone] => 
                            [tax_exempt] => none
                            [tax_ids] => Array
                                (
                                )

                        )

                    [customer_email] => [email protected]
                    [expires_at] => 1640955127
                    [livemode] => 
                    [locale] => 
                    [metadata] => Stripe\StripeObject Object
                        (
                        )

                    [mode] => payment
                    [payment_intent] => pi_3K...1p2sJ
                    [payment_method_options] => Array
                        (
                        )

                    [payment_method_types] => Array
                        (
                            [0] => card
                        )

                    [payment_status] => paid
                    [phone_number_collection] => Stripe\StripeObject Object
                        (
                            [enabled] => 
                        )

                    [recovered_from] => 
                    [setup_intent] => 
                    [shipping] => 
                    [shipping_address_collection] => 
                    [shipping_options] => Array
                        (
                        )

                    [shipping_rate] => 
                    [status] => complete
                    [submit_type] => 
                    [subscription] => 
                    [success_url] => http://exmaple.org/success
                    [total_details] => Stripe\StripeObject Object
                        (
                            [amount_discount] => 0
                            [amount_shipping] => 0
                            [amount_tax] => 0
                        )

                    [url] => 
                )

        )

    [livemode] => 
    [pending_webhooks] => 2
    [request] => Stripe\StripeObject Object
        (
            [id] => 
            [idempotency_key] => 
        )

    [type] => checkout.session.completed
)

In the customer.created you do get the customer email if you passed it during checkout. You can also use that to connect it back to your customer without the need for the client_reference_id. I check in both events and update the database with stripe customer id at the first successful opportunity.

Upvotes: 9

Christopher Rox
Christopher Rox

Reputation: 1

I did some digging and I think I know what's going on. Your client reference is alright, but you're likely not listening to both webhooks (If you are using stripe connect for example, you will get events on both hooks which can be deceiving).

I would use a truly unique ID for that client reference so that down to road you don't have mixed content. This v4 function works wonderously.

public static function v4() 
    {
        return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',

        // 32 bits for "time_low"
        mt_rand(0, 0xffff), mt_rand(0, 0xffff),

        // 16 bits for "time_mid"
        mt_rand(0, 0xffff),

        // 16 bits for "time_hi_and_version",
        // four most significant bits holds version number 4
        mt_rand(0, 0x0fff) | 0x4000,

        // 16 bits, 8 bits for "clk_seq_hi_res",
        // 8 bits for "clk_seq_low",
        // two most significant bits holds zero and one for variant DCE1.1
        mt_rand(0, 0x3fff) | 0x8000,

        // 48 bits for "node"
        mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
        );
    }

Upvotes: -6

Satya Arya
Satya Arya

Reputation: 113

A unique string to reference the Checkout session. This can be a customer ID, a cart ID, or similar. It is included in the checkout.session.completed webhook and can be used to fulfill the purchase.

source https://stripe.com/docs/js/checkout/redirect_to_checkout

Upvotes: 1

Related Questions