如何更新Stripe客户以取消订阅计划或添加订阅计划

时间:2019-01-28 21:13:31

标签: php stripe-payments

我这里有几件事。请耐心等待,我尝试详细介绍一下。我正在尝试向客户帐户页面添加取消和添加计划选项。 (1-5)有多种计划可供选择,他们可以选择任何一种或全部。

1.>我正在尝试为现有Stripe客户添加计划。我注意到,每次单击条带付款按钮,该条带都会创建一个新的客户ID,即使它们存在于数据库中也是如此。我希望他们能够单击“添加计划”(针对特定计划),然后将他们重定向到审阅页面,在其中可以单击“立即付款”条纹付款按钮,付款并在程序中更新现有的新计划条带化帐户。另外,理想情况下,我希望计划的续订日期与他们现有的其他每月计划(收费)日期相匹配。

2.>我正试图取消现有Stripe客户的计划。我希望他们能够单击“取消”(对于特定计划,有多个可用选项),被重定向到“取消评论”页面,然后他们可以单击“取消”按钮,然后...从那里我要更新Stripe中的现有客户取消该特定计划(使用cancel_at_period_end)。

我能够在注册时创建客户,费用,计划等。从那里,我将Stripe客户ID存储在我的数据库中。除了我的数据库中的Stripe ID外,我不知道如何检索所需的数据,也不知道如何使用它来完成上述步骤。

Stripe API很好,但是有些事情对我来说确实是广泛而令人困惑的(编程新手)。

任何帮助将不胜感激。如果您可以提供带有解释的示例,那就太好了!谢谢你。

到目前为止,这里有一些代码:

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

}

?>

我从下面的建议中更新了代码。我现在收到以下两个错误: 1.)Stripe注意:Stripe \ Customer实例的未定义​​属性:订阅 2.)PHP致命错误:未捕获错误:调用未定义的方法Stripe \ Customer :: plan()

我在下面更新了部分代码。我已经消除了上面的错误#1:

//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

1 个答案:

答案 0 :(得分:1)

$cu = Stripe_Customer::retrieve($customeridstripe);应该是$cu =\Stripe\Customer::retrieve($customeridstripe);

相关问题