how to create customer and charge subscription at same time?

570 Views Asked by At

I'm using stripe and had all my subscriptions working properly before. Just recently I am trying to move the same code to a different stripe account and I am receiving errors when trying to charge a subscription.

<?php 


require_once('../stripe-php-master/init.php');


// (switch to the live key later)
\Stripe\Stripe::setApiKey("sk_test_hlQNSE1fy1cGmsqDSbR9LfDF");

try
{
  $customer = \Stripe\Customer::create(array(
    'email' => $_POST['stripeEmail'],
    'source'  => $_POST['stripeToken'],
    'plan' => 'basic_annually'
  ));

  header('Location: ../../subscription-success.html');
  exit;
}
catch(Exception $e)
{
  header('Location:oops.html');
  error_log("unable to sign up customer:" . $_POST['stripeEmail'].
    ", error:" . $e->getMessage());
}

this is returning the following error

unable to sign up customer: '[email protected]', error:No such plan: basic_monthly; one exists with a name of basic_monthly, but its ID is 504102373103330.
2

There are 2 best solutions below

0
On BEST ANSWER

Plan objects have both an id and a name attribute. id is the plan's unique identifier while name is its display name.

When creating a customer or a subscription, the value of the plan parameter must be set to a valid plan ID, not to a plan's name.

In your case, the error message explicitly tells you that there is plan with "basic_monthly" as its name, but the plan's id is 504102373103330, so that's the value you'd need to pass in the plan parameter to create a subscription to that plan.

0
On

you must change secret and test api keys