Skip to content
Advertisement

Stripe – adding new customers to an existing plan?

I have a simple form that registers users. The form also uses Stripe to process payments. Stripe’s API is super clean but for some reason I can’t seem to figure this out. Please tell me if i’m doing something wrong here.

Right now I’m in a test account with Stripe, but every time I register a user in stripe it keeps creating a new plan. I know in my code below I have “StripePlan::create” that’s because I don’t know how to add a new customer to an existing plan. Is this normal the way I’m doing this currently?

In the screenshot below you will see that it will create a new plan for that customer and assign them to it. Is it possible to just keep adding new customers to an existing plan?

enter image description here

// Include configuration file  
require_once 'stripe/config.php'; 

// Include Stripe PHP library 
require_once 'vendor/autoload.php'; 

$payment_id = $statusMsg = $api_error = ''; 
$ordStatus = 'error'; 

// Check whether stripe token is not empty 
if(!empty($_POST['stripeToken'])){ 
    
    // Retrieve stripe token, card and user info from the submitted form data 
    $token  = $_POST['stripeToken']; 
    
    $users_email = $_POST['email']; 
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $full_name = $first_name.' '.$last_name;
    

    // Set API key 
    StripeStripe::setApiKey(STRIPE_API_KEY); 
    
    // Add customer to stripe 
    $customer = StripeCustomer::create(array( 
        'email' => $users_email, 
        'name' => $full_name,
        'source'  => $token
        )); 

    // Create a plan 
    try { 
        $plan = StripePlan::create([
            'amount' => 3000,
            'currency' => 'usd',
            'interval' => 'month',
            // 'product' => ['name' => 'Standard Plan'],
            'product' => 'prod_ktItFVvqgr4edf',
        ]);
    }catch(Exception $e) { 
        $api_error = $e->getMessage(); 
    } 
    
    if(empty($api_error) && $plan){ 
        // Creates a new subscription 
        try { 
            $subscription = StripeSubscription::create(array( 
                "customer" => $customer->id, 
                "items" => array( 
                    array( 
                        "plan" => $plan->id, 
                    ), 
                ), 
            )); 
        }catch(Exception $e) { 
            $api_error = $e->getMessage(); 
        } 
        
        if(empty($api_error) && $subscription){ 
            // Retrieve subscription data 
            $subsData = $subscription->jsonSerialize(); 
    
            // Check whether the subscription activation is successful 
            if($subsData['status'] == 'active'){ 
                // Subscription info 
                $subscrID = $subsData['id']; 
                $custID = $subsData['customer']; 
                $planID = $subsData['plan']['id']; 
                $created = date("Y-m-d H:i:s", $subsData['created']);
                $status = $subsData['status']; 
                
                // Insert into database
                
                // Insert into database


            }else{ 
                $statusMsg = "Subscription activation failed!"; 
            } 
        }else{ 
            $statusMsg = "Subscription creation failed! ".$api_error; 
        } 
    }else{ 
        $statusMsg = "Plan creation failed! ".$api_error; 
    } 
}else{ 
    $statusMsg = "Error on form submission, please try again."; 
} 

Advertisement

Answer

To create a subscription with an existing plan, simply pass in that older plan’s ID when creating the subscription instead of creating a new plan for each customer. In your // Create a plan block, either retrieve an existing plan and use that to create the subscription or create a new plan if needed. Or better yet, store the plan ID’s in your own database for easier retrieval.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement