Exemplo n.º 1
0
function firstStep()
{
    validateParameters($_POST);
    $planId = "basic";
    $passThrough = array("address" => $_POST['addr'], "extended_addr" => $_POST['extended_addr'], "city" => $_POST['city'], "state" => $_POST['state'], "zip_code" => $_POST['zip_code']);
    try {
        /*
         * Calling ChargeBee Checkout new Hosted Page API to checkout a new subscription
         * by passing plan id the customer would like to subscribe and also passing customer 
         * first name, last name, email and phone details. The resposne returned by ChargeBee
         * has hosted page url and the customer will be redirected to that url.
         * 
         * Note: Parameter embed(Boolean.TRUE) can be shown in iframe
         *       whereas parameter embed(Boolean.FALSE) can be shown as seperate page.
         * Note : Here customer object received from client side is sent directly 
         *        to ChargeBee.It is possible as the html form's input names are 
         *        in the format customer[<attribute name>] eg: customer[first_name] 
         *        and hence the $_POST["customer"] returns an associative array of the attributes.              
         */
        $hostUrl = getHostUrl();
        $result = Chargebee_HostedPage::CheckoutNew(array("subscription" => array("planId" => $planId), "customer" => $_POST['customer'], "embed" => "false", "passThruContent" => json_encode($passThrough), "redirectUrl" => $hostUrl . "/checkout_two_step/redirect_handler", "cancelUrl" => $hostUrl . "/checkout_two_step/signup.html"));
        $redirectUrl = $result->hostedPage()->url;
        $jsonResponse = array("forward" => $redirectUrl);
        print json_encode($jsonResponse, true);
    } catch (ChargeBee_InvalidRequestException $e) {
        handleInvalidRequestErrors($e, "subscription[plan_id]");
    } catch (Exception $e) {
        handleGeneralErrors($e);
    }
}
function callingIframeCheckoutPage()
{
    header('Content-Type: application/json');
    validateParameters($_POST);
    $planId = "basic";
    try {
        $result = ChargeBee_HostedPage::CheckoutNew(array("subscription" => array("planId" => $planId), "customer" => $_POST['customer'], "embed" => "true", "iframeMessaging" => "true"));
        /*
         * Sending hosted page url and hosted page id as response.
         */
        $response = array("url" => $result->hostedPage()->url, "hosted_page_id" => $result->hostedPage()->id, "site_name" => ChargeBee_Environment::defaultEnv()->getSite());
        print json_encode($response);
    } catch (ChargeBee_InvalidRequestException $e) {
        handleInvalidRequestErrors($e, "subscription[plan_id]");
    } catch (Exception $e) {
        handleGeneralErrors($e);
    }
}
    try {
        $result = createSubscription();
        addShippingAddress($result->subscription(), $result->customer());
        $jsonResp = array();
        /*
         * Forwarding to success page after successful create subscription in ChargeBee.
         */
        $queryParameters = "name=" . urlencode($result->customer()->firstName) . "&planId=" . urlencode($result->subscription()->planId);
        $jsonResp["forward"] = "thankyou.html";
        echo json_encode($jsonResp, true);
    } catch (ChargeBee_PaymentException $e) {
        handleTempTokenErrors($e);
    } catch (ChargeBee_InvalidRequestException $e) {
        handleInvalidRequestErrors($e, "plan_id");
    } catch (Exception $e) {
        handleGeneralErrors($e);
    }
}
/* Creates the subscription in ChargeBee using the checkout details and 
 * stripe temporary token provided by stripe.
 */
function createSubscription()
{
    /*
     * Constructing a parameter array for create subscription api. 
     * It will have account information, the temporary token got from Stripe and
     * plan details.
     * For demo purpose a plan with id 'annual' is hard coded.
     * Other params are obtained from request object.
     * Note : Here customer object received from client side is sent directly 
     *        to ChargeBee.It is possible as the html form's input names are 
function subscriptionReactivate()
{
    header('Content-Type: application/json');
    try {
        $subscriptionId = getSubscriptionId();
        $result = ChargeBee_Subscription::reactivate($subscriptionId);
        $jsonResponse = array("forward" => "/ssp-php/subscription");
        print json_encode($jsonResponse, true);
    } catch (ChargeBee_PaymentException $e) {
        handleChargeAttemptFailureErrors($e);
    } catch (ChargeBee_InvalidRequestException $e) {
        handleInvalidErrors($e);
    } catch (Exception $e) {
        handleGeneralErrors($e);
    }
}