Home > OS >  Stripe PHP Subscription Creation Not Working
Stripe PHP Subscription Creation Not Working

Time:11-05

Whenever I try to create a Stripe subscription in PHP with the 'id' parameter, it gives an error of invalid parameter, and when I remove the id parameter, it works. Does anyone know how to manually set the subscription id instead of getting an error like this?

What I tried:

$stripe->subscriptions->create([
    'id' => $id,
    'customer' => $id,
    'items' => [
        ['price' => 'price_'],
    ],
]);

What I got:

Fatal error: Uncaught Error sending request to Stripe: (Status 400) (Request req_no) Received unknown parameter: id Stripe\Exception\InvalidRequestException: Received unknown parameter: id in public_html/stripe-php-13.0.0/lib/Exception/ApiErrorException.php:38

CodePudding user response:

There really is no id key in the api. Perhaps you need to use metadata for your own keys, for example:


$stripe->subscriptions->create([
        'customer' => $customer->id,
        'metadata' => [
            'id' => $id,
        ],
        'items' => [[
            'price' => $price->id,
        ]],
    ]);

CodePudding user response:

Here's how you should create a subscription in Stripe using PHP:

$stripe->subscriptions->create([
    'customer' => $customer_id, 
    'items' => [
        ['price' => 'price_XXXXXXXXXXXXXX'], 
    ],
]);

Here's an example of how you might capture the subscription ID after creation:

$subscription = $stripe->subscriptions->create([
    'customer' => $customer_id,
    'items' => [
        ['price' => 'price_XXXXXXXXXXXXXX'],
    ],
]);

$subscription_id = $subscription->id; // This is the generated subscription ID
  • Related