function subscriptionCancel() { $subscriptionId = getSubscriptionId(); $cancelStatus = $_POST['cancel_status']; $params = array(); if ($cancelStatus == "cancel_on_next_renewal") { $params['end_of_term'] = 'true'; } $result = ChargeBee_Subscription::cancel($subscriptionId, $params); header("Location: subscription"); }
public function requestPlanChange($inData) { $userID = $this->session->get_user_var('id'); $plans = array('free', 'contractor', 'professional', 'small_business', 'office', 'enterprise', 'cancel', 'reactivate', 'card_update'); if (in_array($inData['plan'], $plans)) { /************** * Locate user'S SUBSCRIPTION ID *****************************/ $row_tmb = array(); $data = array(); $query = "SELECT chargebee_id\n\t\t\t\t\t FROM user_subscriptions\n\t\t\t\t\t WHERE user_id = ?"; $stmt = $this->sql->link->prepare($query); if (!$stmt) { die('Invalid query: ' . $this->sql->link->error); } else { $stmt->bind_param('i', $userID); $resultFromExec = $stmt->execute(); $stmt->store_result(); stmt_bind_assoc($stmt, $returnResults); // loop through all result rows while ($stmt->fetch()) { foreach ($returnResults as $key => $value) { $row_tmb[$key] = $value; } $data = $row_tmb; } } try { ChargeBee_Environment::configure("rocketmailmerge", "1opwNtcdAXu19XWee9cdCFe6E3FfYcCvUF"); switch ($inData['plan']) { case "card_update": $result = ChargeBee_HostedPage::updateCard(array("customer" => array("id" => $data['chargebee_id']))); $hostedPage = $result->hostedPage(); $output['success'] = 1; $output['text'] = "Successfully fetched the card update page."; $output['url'] = $hostedPage->__get('url'); $output['return'] = 1; break; case "cancel": $result = ChargeBee_Subscription::cancel($data['chargebee_id'], array("end_of_term" => 'true')); $subscription_status = $result->subscription()->__get('status'); $query = "UPDATE user_subscriptions set subscription_status = ?\n\t\t\t\t\t\t\t\t WHERE user_id = ?"; $stmt = $this->sql->link->prepare($query); if (!$stmt) { die('Invalid query: ' . $this->sql->link->error); } else { $stmt->bind_param('si', $subscription_status, $userID); $resultFromExec = $stmt->execute(); } $output['success'] = 1; $output['text'] = "Successfully cancelled your subscription."; $output['return'] = 4; break; case "reactivate": $result = ChargeBee_Subscription::reactivate($data['chargebee_id']); $subscription_status = $result->subscription()->__get('status'); $query = "UPDATE user_subscriptions set subscription_status = ?\n\t\t\t\t\t\t\t\t WHERE user_id = ?"; $stmt = $this->sql->link->prepare($query); if (!$stmt) { die('Invalid query: ' . $this->sql->link->error); } else { $stmt->bind_param('si', $subscription_status, $userID); $resultFromExec = $stmt->execute(); } $output['success'] = 1; $output['text'] = "Successfully reactivated your subscription."; $output['return'] = 5; break; case "free": $this->fetchDashboardData(); if ($this->outData['totals']['document_count'] <= 3 && $this->outData['totals']['document_count'] <= 3 && $this->outData['totals']['document_count'] <= 3) { $result = ChargeBee_Subscription::update($data['chargebee_id'], array("planId" => "free")); $output['success'] = 1; $output['text'] = "Successfully updated your account to free."; $output['return'] = 2; } else { $output['success'] = 1; $output['text'] = "Your account does not meet the criteria for the free subscription."; $output['return'] = 6; } break; default: $result = ChargeBee_HostedPage::checkoutExisting(array("subscription" => array("id" => $data['chargebee_id'], "planId" => $inData['plan']))); $hostedPage = $result->hostedPage(); $output['success'] = 1; $output['text'] = "Successfully fetched the plan change page."; $output['url'] = $hostedPage->__get('url'); $output['return'] = 1; break; } $this->outData = $output; return $output; } catch (Exception $e) { $output['success'] = 0; $output['text'] = "Unable to fetch the plan change page."; $output['return'] = 3; $this->outData = $output; return $output; } } }