kh67
kh67

Reputation: 81

How to update Stripe Customer to cancel subscription plan or add subscription plan

I have a few things going on here. Please be patient as I try to outline it in detail. I am trying to add cancel and add plan(s) options to the customer account page. There are multiple plans to choose from (1-5) and they can have any or all they choose.

1.> I am trying to add plan(s) for existing Stripe customer. I notice that every time the stripe payment button is clicked that stripe creates a new customer id, even if they exist in the database. I want them to be able to click "add plan" (for a specific plan), then they are redirected to review page where they can click the "pay now" Stripe payment button, pay and the program updates the new plan in their existing Stripe account. ALSO, I would ideally like the plan renewal date to match their existing monthly renewal (charge) date with the other existing plans they have.

2.>I am trying to cancel plan(s) for existing Stripe customer. I want them to be able to click cancel (for a specific plan, there are multiple available), be redirected to cancel review page where they can then click the cancel button and... from there I want to update the existing customer in Stripe to cancel that specific plan ( with cancel_at_period_end).

I am able to create customer, charge, plans etc. on signup. From there I have stored the Stripe Customer ID in my database. Outside of the Stripe ID in my database I have no clue how to retrieve the data needed, nor how to use it to complete these steps, discussed above.

Stripe API is very good but some things are just really broad and confusing to me (someone new to programming).

Any help would be appreciated. If you could provide examples with explanations that would be FABULOUS! Thank you, in advance.

Here is some of the code, so far:

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

$stripe = [
  "secret_key"      => "sk_test_secret",
  "publishable_key" => "pk_test_publish",
];

\Stripe\Stripe::setApiKey($stripe['secret_key']);


 $token  = $_POST['stripeToken'];
 $email  = $_POST['stripeEmail'];


$mplan = 'plan_m'; //I pulled this from stripe dashboard

//cancel m category------------------------------------

    if(isset($mcancel)){
        echo "<p>You are about to cancel your Subscription.</p>" . "<p>Press CANCEL button, below, to complete, or return to your <a href=http://example.com/Account-Details>Account Details page.</a></p>";
?>
<form id="mcancelnow" name="mcancelnow" action=""<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>>

    <input type="submit" value="CANCEL Subscription NOW" id="mcancelnow" name="mcancelnow" /><br /><br />

<?php

}
    if(isset($mcancelnow)){

$sqlstripem = "SELECT * FROM idaccount WHERE ID = '".$_SESSION['session']."'";
$resultstripem = mysqli_query($dbcon, $sqlstripem) or die("Error");

while($row = mysqli_fetch_assoc($resultstripem)){
     echo "<br />" . "Stripe Customer ID:  " . $row['stripecustid'] . "!";
$customeridstripe = $row['stripecustid'];
echo $customeridstripe; // is returning expected result if I take the isset condition out.
}

// Retrieve the request's body and parse it as JSON:

$input = @file_get_contents('php://input');
$event_json = json_decode($input);

// Do something with $event_json

http_response_code(200); // PHP 5.4 or greater

//update customer to cancel m plan on STRIPE

 $cu = \Stripe\Customer::retrieve($customeridstripe); 
 $subscription = $cu->subscription; 
 $plan = $cu->plan($mplan);
 $plan->active = false;
 $cu->save();

echo "subscription:  " . $subscription . "plan:  " . $plan; //nothing returned for subscription or plan
echo "token:  " . $token; //nothing returned for token

}

?>

I updated code from suggestion, below. I now receive these two errors: 1.)Stripe Notice: Undefined property of Stripe\Customer instance: subscription 2.)PHP Fatal error: Uncaught Error: Call to undefined method Stripe\Customer::plan()

I have updated portion of code, below. I have eliminated error #1, above:

//update customer to cancel m plan on STRIPE

 $cu = \Stripe\Customer::retrieve($customeridstripe);
 echo $cu . "<br />"; //returns expected result

 $subid = $cu->subscriptions->data[0]->id;
 echo "subscription id:  " . $subid . "<br />"; //returns expected result

 $subid2 = \Stripe\Subscription::retrieve($subid);
 echo "subid2:  " . $subid2 . "<br />";  //returns expected result

 $plan = $subid2->id('plan_idhere');
 echo "plan id:  " . $plan . "<br />";  //nothing returned CODE HALTS HERE

 $planresult = $plan->active = false;
 echo "plan result:  " . $planresult . "<br />"; //nothing returned

 $cu->save();

  echo "sub id:  " . $subid;  //nothing returned

Upvotes: 2

Views: 1252

Answers (1)

kristina
kristina

Reputation: 182

$cu = Stripe_Customer::retrieve($customeridstripe); should be $cu =\Stripe\Customer::retrieve($customeridstripe);

Upvotes: 1

Related Questions