function redirectHandler()
{
    $id = $_GET['id'];
    $result = ChargeBee_HostedPage::retrieve($id);
    $hostedPage = $result->hostedPage();
    if ($hostedPage->state != "succeeded") {
        header("HTTP/1.0 400 Error");
        include $_SERVER["DOCUMENT_ROOT"] . "/error_pages/400.html";
    } else {
        header("Location: thankyou.php?subscription_id=" . URLencode($hostedPage->content()->subscription()->id));
    }
}
function redirectHandler()
{
    $hostedPageId = $_GET['id'];
    /* Requesting ChargeBee server about the Hosted page state and 
     * getting the details of the created subscription.
     */
    $result = ChargeBee_HostedPage::retrieve($hostedPageId);
    $hostedPage = $result->hostedPage();
    /*
     * Checking the state of the hosted page. If the state is not "succeeded",
     * then cusotmer checkout is considered as failed.
     */
    if ($hostedPage->state != "succeeded") {
        header("HTTP/1.0 400 Error");
        include $_SERVER["DOCUMENT_ROOT"] . "/error_pages/400.html";
        return;
    }
    $subscriptionId = $hostedPage->content()->subscription()->id;
    addShippingAddress($subscriptionId, $result);
    header("Location: thankyou?subscription_id=" . URLencode($subscriptionId));
}
function redirectFromChargeBee()
{
    /* The request will have hosted page id and state of the customer card
     * update status.
     */
    if ("succeeded" == $_GET['state']) {
        /* Request the ChargeBee server about the Update Card Hosted Page state 
         * and provides details about the customer.
         */
        $result = ChargeBee_HostedPage::retrieve($_GET['id']);
        $hostedPage = $result->hostedPage();
        if ($hostedPage->state != "succeeded") {
            header("HTTP/1.0 400 Error");
            include $_SERVER["DOCUMENT_ROOT"] . "/error_pages/400.html";
            return;
        }
        $customerId = $hostedPage->content()->customer()->id;
        $queryParameters = "customer_id=" . urlencode($customerId) . "&updated=" . urlencode("true");
        header("Location: profile?" . $queryParameters);
    } else {
        header("HTTP/1.0 400 Error");
        include $_SERVER["DOCUMENT_ROOT"] . "/error_pages/400.html";
    }
}
/*
 * Adding ChargeBee php libraries and configuration files.
 */
require_once dirname(__FILE__) . "/Config.php";
/* This php file is configured as redirect url for the hosted page in ChargeBee 
 * app. Hosted page Id and state of the hosted page will be sent along the request
 */
$hostedPageId = $_GET['id'];
$status = $_GET['state'];
try {
    if ($status == "succeeded") {
        /* 
         * Retrieving the hosted page and getting the details
         * of the subscription created through hosted page.
         */
        $result = ChargeBee_HostedPage::retrieve($hostedPageId);
        $hostedPage = $result->hostedPage();
        if ($hostedPage->state != "succeeded") {
            header("HTTP/1.0 400 Error");
            include $_SERVER["DOCUMENT_ROOT"] . "/error_pages/400.html";
            return;
        }
        /* 
         * Forwarding the user to thank you page.
         */
        $content = $hostedPage->content();
        $queryParameters = "name=" . $content->customer()->firstName . "&planId=" . $content->subscription()->planId;
        header("Location:thankyou.html?" . $queryParameters);
    } else {
        /* 
         * If the state is not success then displaying the 
function redirectHandler()
{
    $id = $_GET['id'];
    $result = ChargeBee_HostedPage::retrieve($id);
    if ($result->hostedPage()->state == "succeeded") {
        header("Location: /ssp-php/subscription");
    } else {
        customError400();
    }
}
Example #6
0
 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;
         }
     }
 }