stripe-payments

Store metadata in a subscription with Stripe


I'm trying to figure out how to store metadata directly into a Stripe CC payments subscription. I'm using the PHP SDK and have:

  $test = $stripe->checkout->sessions->create([
      'customer_email' => $_GET["who"],
      'success_url' => $success_url,
      'payment_method_types' => ['card'],
      'cancel_url' => "https://www.example.com",
      'line_items' => [
        [
          'price' => $price_plan_id,
          'quantity' => 1,
        ],
      ],
      'payment_intent_data' => [
        'metadata' => [
            'who' => $_GET["who"],
            'total' => $_GET["total"],
            'period' => $_GET["period"],
            'description' => $_GET["description"],
            'district' => $_GET["district"],
            'what' => $_GET["what"],
            'ip' => $_SERVER["REMOTE_ADDR"]
        ]
      ],
      'mode' => $mode 'subscription',
    ]);

This gives me an error:

You can not pass payment_intent_data in subscription mode.

I've tried just doing:

  $test = $stripe->checkout->sessions->create([
      'customer_email' => $_GET["who"],
      'success_url' => $success_url,
      'payment_method_types' => ['card'],
      'cancel_url' => "https://www.example.com",
      'line_items' => [
        [
          'price' => $price_plan_id,
          'quantity' => 1,
        ],
      ],
      'metadata' => [
          'who' => $_GET["who"],
          'total' => $_GET["total"],
          'period' => $_GET["period"],
          'description' => $_GET["description"],
          'district' => $_GET["district"],
          'what' => $_GET["what"],
          'ip' => $_SERVER["REMOTE_ADDR"]
      ],
      'mode' => $mode 'subscription',
    ]);

And while it kind of works, its not assigned to the subscription (when you view it metadata is empty)

How do I go passing this along? I want to keep this data stored in the subscription element (not just the payment)

Thanks


Solution

  • You want to pass the metadata on to the resulting Subscription object by setting it in subscription_data.metadata: https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-subscription_data-metadata