Search code examples
stripe-payments

Stripe webhook is not returning all my metadata


Using PHP, when I generate my Stripe session I'm passing in 10 different key:value pairs to the metadata parameter, like so:

  $checkout_session = \Stripe\Checkout\Session::create([
    'payment_method_types' => ['card'],
    'client_reference_id' => $accountnumber,
    'mode' => 'payment',
    'line_items' => [[
      'price_data' => [
        'currency' => $currencycode,
        'product_data' => [
          'name' => 'Invoice Total',
        ],
        'unit_amount' => $invoicetotal,
      ],
      'quantity' => 1,
    ]],
    'payment_intent_data' => [
      'application_fee_amount' => $timesavrfee,
      'receipt_email' => $payeremail
    ],
    'success_url' => $successurl.'?session_id={CHECKOUT_SESSION_ID}&cid='.$companyid,
    'cancel_url' => $cancelurl.'?session_id={CHECKOUT_SESSION_ID}&cid='.$companyid,
    'metadata' => ['familyid'=>$familyid,
                   'transactionreference'=>$transactionreference,
                   'companyid'=>$companyid,
                   'paymenttype'=>$paymenttype,
                   'payer'=>$primarypayer,
                   'invoicedate'=>$invoicedate,
                   'feecomment'=>$feecomment,
                   'feeamount'=>$feeamount,
                   'processingfeeschargeitem'=>$processingfeeschargeitem,
                   'passonprocessingfees'=>$passonprocessingfees]
  ],['stripe_account' => $accountid]);

And I've confirmed that I have values for all of these by logging the values I'm passing in, however the webhook that I receive from Stripe for the "checkout.session.completed" event is only returning to me six out of these ten values. And I don't mean that it's returning blanks for the other four, I mean those other four don't even exist in the return. I've confirmed this by looking at the webhook history in my Stripe dashboard. The metadata section of the return looks like this:

"metadata": {
    "payer": "M",
    "companyid": "2142",
    "invoicedate": "2024-01-26 00:00:00",
    "paymenttype": "ONL",
    "transactionreference": "1157",
    "familyid": "29764010"
  },

As you can see, I'm missing the following four values:

  • feecomment
  • feeamount
  • processingfeeschargeitem
  • passonprocessingfees

According to the Stripe documentation, I can pass in up to 50 keys, each up to 40 characters in length, so I know I'm not breaking those limitations. Nor are any of my values anywhere near 500 characters long.

You can specify up to 50 keys, with key names up to 40 characters long and values up to 500 characters long.

So why am I not receiving those other four values, when the first six are working perfectly fine?


Solution

  • Stripe doesn't filter metadata in Events so if you have set those metadata values then they will be in the Event. This likely means that you think you are setting those values but they are empty/null and being ignored. You should be able to look at the API Request log in the Dashboard that created that Checkout Session and confirm the metadata you sent is not what you expect.

    You can also quickly confirm this works as expected by forcing say 20 metadata key/value pairs on creation and see those on the resulting Events. Here's some basic code to do this:

    $metadata = [];
    for($i = 0; $i < 20; $i++) {
      $key = "key $i";
      $value = "value $i";
      $metadata[$key] = $value;
    }
    
    $checkoutSession = $stripe->checkout->sessions->create([
      'allow_promotion_codes' => true,
      'success_url' => 'https://example.com/success?return=true',
      'line_items' => [
        [
          'price' => 'price_123',
          'quantity' => 1,
        ],
      ],
      'metadata' => $metadata,
      'mode' => 'payment',
    ]);
    

    If you complete the Checkout Session created with this code you are immediately going to see the metadata in the Event have all 20 keys from key 0 to key 19 as expected.