Andrew Newby
Andrew Newby

Reputation: 5197

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

Upvotes: 1

Views: 3034

Answers (2)

Brian
Brian

Reputation: 29

Answering @RylanSchaeffer and @Justin here because I don't have enough reputation to comment on the accepted answer. Although you can't pass a dictionary directly, you can assign individual keys. Working in Dart my checkout helper function looks like this:

     SendToCheckout(
        {required String localUserId, // from my app, not Stripe
        required String localProductId, // from my app, not Stripe
        }) async {
    
      var headers = {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Authorization': 'BEARER_TOKEN_HERE'
      };
    
      var request = http.Request(
          'POST', Uri.parse('https://api.stripe.com//v1/checkout/sessions'));
    
      request.bodyFields = {
        'cancel_url': 'https://[YOUR_DOMAIN].com',
        'success_url': 'https://[YOUR_DOMAIN].com',
    
        // OTHER STRIPE PARAMETERS 
    
           
        // ASSIGNING METADATA FIELDS HERE:
        'subscription_data[metadata][my_cust_Id]': localUserId,
        'subscription_data[metadata][my_product_Id]': localProductId
      };
      request.headers.addAll(headers);
    
      http.StreamedResponse streamedResponse = await request.send();
      var response = await http.Response.fromStream(streamedResponse);
      final Map parsed = await json.decode(response.body);
    
          if (response.statusCode == 200) {
            print(parsed)
          } else {}
        }

Later you can search for subscriptions based on metadata fields with something like this:

       var request = http.Request(
              'GET',
              Uri.parse(
                  'https://api.stripe.com//v1/subscriptions/search? 
                   query=metadata["my_cust_id"]:"localId012345"'));

Upvotes: 1

taintedzodiac
taintedzodiac

Reputation: 2908

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

Upvotes: 8

Related Questions