コード例 #1
0
 function closePendingInvoice($invoiceObj)
 {
     $invoiceId = $invoiceObj->id;
     $subscriptionId = $invoiceObj->subscriptionId;
     $startDate = $invoiceObj->startDate;
     $endDate = $invoiceObj->endDate;
     $chargeInCents = MeterBilling::getUsageCharge($startDate, $endDate, $subscriptionId);
     $addChargeParam = array("amount" => $chargeInCents, "description" => "monthly usage");
     /*
      * Calling ChargeBee Add Charge Invoice API and add Charge to invoice 
      * based on the usage made by customer.
      */
     ChargeBee_Invoice::addCharge($invoiceId, $addChargeParam);
     $addonQuantity = MeterBilling::getQuantityUsed($startDate, $endDate, $subscriptionId);
     $addAddonCharge = array("addonId" => "wallpapers", "addonQuantity" => $addonQuantity);
     /* 
      * Calling the ChargeBee Add Addon Charge Invoice API and add the no of 
      * addons used by customers to the invoice.
      */
     ChargeBee_Invoice::addAddonCharge($invoiceId, $addAddonCharge);
     /*
      * Closing the invoice and Collecting the payment(if auto collection is on)
      * by calling the ChargeBee Collect Invoice API.
      */
     ChargeBee_Invoice::collect($invoiceId);
 }
コード例 #2
0
     */
    if (!checkIfRequestIsFromChargeBee()) {
        return;
    }
    $content = file_get_contents('php://input');
    /* 
     * Assigning the recieved content to ChargeBee Event object.
     */
    $event = ChargeBee_Event::deserialize($content);
    /*
     * Checking the event type as Invoice Created to add Charge for Meter Billing.
     */
    $eventType = $event->eventType;
    if ($eventType == "invoice_created") {
        $invoiceId = $event->content()->invoice()->id;
        $invoiceObj = ChargeBee_Invoice::retrieve($invoiceId)->invoice();
        if ($invoiceObj->status == "pending") {
            $meterBilling = new MeterBilling();
            $meterBilling->closePendingInvoice($invoiceObj);
            echo "Invoice has been closed successfully";
        } else {
            echo "Invoice is not in pending state";
        }
    }
}
/* Check if the request is from chargebee. 
 * You can secure the webhook either using
 *   - Basic Authentication
 *   - Or check for specific value in a parameter.
 * For demo purpose we are using the second option though 
 * basic auth is strongly preferred. Also store the key 
コード例 #3
0
ファイル: index.php プロジェクト: jonca44/pdf-mailmerge
 private function processChargeBeeData($inData)
 {
     //echo "Processing $inData";
     $inData = json_decode($inData, 1);
     $sqlData = array();
     $varData = array();
     if ($inData === null) {
         echo "Invalid Data provided.\n";
         return "Invalid data!";
     }
     echo "Processing : ";
     print_r($inData);
     foreach ($inData['content'] as $key => $value) {
         /*	"subscription": {
         			"id": "HwqE2xeNnwOeYF44",
         			"plan_id": "enterprise",
         			"plan_quantity": 1,
         			"status": "in_trial",
         			"trial_start": 1352158863,
         			"trial_end": 1353454863,
         			"created_at": 1352158863,
         			"due_invoices_count": 0,
         			"object": "subscription"
         		},
         		"customer": {
         			"id": "HwqE2xeNnwOeYF44",
         			"first_name": "Leon",
         			"last_name": "Test",
         			"email": "*****@*****.**",
         			"created_at": 1352158863,
         			"object": "customer",
         			"card_status": "valid"
         		},
         		"card": {
         			"customer_id": "HwqE2xeNnwOeYF44",
         			"status": "valid",
         			"gateway": "chargebee",
         			"first_name": "Leon",
         			"last_name": "Testington",
         			"iin": "411111",
         			"last4": "1111",
         			"card_type": "visa",
         			"expiry_month": 2,
         			"expiry_year": 2020,
         			"billing_addr1": "123 Fake Street",
         			"billing_city": "Fake suburb",
         			"billing_state": "QLD",
         			"billing_country": "AU",
         			"billing_zip": "1234",
         			"object": "card",
         			"masked_number": "411111******1111"
         		},
         		*/
         switch ($key) {
             case "customer":
                 $allowed = array("customer_first_name", "customer_last_name", "customer_email", "customer_created_at");
                 foreach ($value as $customerKey => $customerData) {
                     if (in_array("customer_" . $customerKey, $allowed)) {
                         $sqlData[] = "customer_" . $customerKey . " = ?";
                         $varData[] = $customerData;
                     }
                 }
                 //Flag the curl request as processed/deleted.
                 $query = "UPDATE user_subscriptions \n\t\t\t\t\t\t\t\t  SET " . implode(",", $sqlData) . "\t\n\t\t\t\t\t\t\t\t  WHERE chargebee_id = ?";
                 $stmt = $this->sql->link->prepare($query);
                 for ($c = 0; $c < sizeof($varData); $c++) {
                     $stmt->mbind_param('s', $varData[$c]);
                 }
                 //Bind the document id, document page num and user id.
                 $stmt->mbind_param('s', $value['id']);
                 $stmt->execute();
                 break;
             case "card":
                 $allowed = array("card_status", "card_first_name", "card_last_name", "card_card_type", "card_expiry_month", "card_expiry_year", "card_billing_addr1", "card_billing_addr2", "card_billing_city", "card_billing_state", "card_billing_country", "card_billing_zip", "card_masked_number");
                 foreach ($value as $customerKey => $customerData) {
                     if (in_array("card_" . $customerKey, $allowed)) {
                         $sqlData[] = "card_" . $customerKey . " = ?";
                         $varData[] = $customerData;
                     }
                 }
                 //Flag the curl request as processed/deleted.
                 $query = "UPDATE user_subscriptions \n\t\t\t\t\t\t\t\t  SET " . implode(",", $sqlData) . "\t\n\t\t\t\t\t\t\t\t  WHERE chargebee_id = ?";
                 $stmt = $this->sql->link->prepare($query);
                 for ($c = 0; $c < sizeof($varData); $c++) {
                     $stmt->mbind_param('s', $varData[$c]);
                 }
                 //Bind the document id, document page num and user id.
                 $stmt->mbind_param('s', $value['customer_id']);
                 $stmt->execute();
                 break;
             case "subscription":
                 $allowed = array("subscription_plan_id", "subscription_plan_quantity", "subscription_status", "subscription_trial_start", "subscription_trial_end", "subscription_created_at", "subscription_due_invoices_count", "subscription_activated_at", "subscription_current_term_start", "subscription_current_term_end");
                 foreach ($value as $customerKey => $customerData) {
                     if (in_array("subscription_" . $customerKey, $allowed)) {
                         $sqlData[] = "subscription_" . $customerKey . " = ?";
                         $varData[] = $customerData;
                     }
                 }
                 //Flag the curl request as processed/deleted.
                 $query = "UPDATE user_subscriptions \n\t\t\t\t\t\t\t\t  SET " . implode(",", $sqlData) . "\t\n\t\t\t\t\t\t\t\t  WHERE chargebee_id = ?";
                 $stmt = $this->sql->link->prepare($query);
                 for ($c = 0; $c < sizeof($varData); $c++) {
                     $stmt->mbind_param('s', $varData[$c]);
                 }
                 //Bind the document id, document page num and user id.
                 $stmt->mbind_param('s', $value['id']);
                 $stmt->execute();
                 break;
             default:
                 break;
         }
     }
     require '../php/chargebee/ChargeBee.php';
     ChargeBee_Environment::configure("rocketmailmerge", "1opwNtcdAXu19XWee9cdCFe6E3FfYcCvUF");
     switch ($inData['event_type']) {
         //Reactivate free trial accounts when the free trial runs out and is killed by chargebee
         case "subscription_canceled_no_card":
             $returnResults = array();
             $subscriptionID = $inData['content']['subscription']['id'];
             $subscriptionPlanID = $inData['content']['subscription']['plan_id'];
             $cardStatus = $inData['content']['customer']['card_status'];
             $trialEnd = $inData['content']['subscription']['trial_end'];
             $currentTermStart = $inData['content']['subscription']['current_term_start'];
             //The free trial has expired. Reactivate their account.
             if ($subscriptionPlanID == "free" && $cardStatus == "no_card") {
                 $result = ChargeBee_Subscription::reactivate($subscriptionID);
                 //Locate the user's page usage.
                 $query = "SELECT user_subscriptions.chargebee_id, user_subscriptions.user_id, user_statistics.billing_cycle_pages, subscription_plans.included_pages\n\t\t\t\t\t\t\t\tFROM user_subscriptions\n\t\t\t\t\t\t\t\tLEFT JOIN user_statistics ON user_subscriptions.user_id = user_statistics.user_id\n\t\t\t\t\t\t\t\tLEFT JOIN subscription_plans ON user_subscriptions.subscription_plan_id = subscription_plans.plan_name\n\t\t\t\t\t\t\t\tWHERE user_subscriptions.chargebee_id =  ?";
                 $stmt = $this->sql->link->prepare($query);
                 if (!$stmt) {
                     die('Invalid query: ' . $this->sql->link->error);
                 } else {
                     $stmt->bind_param('s', $subscriptionID);
                     $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;
                     }
                 }
                 $userID = $data['user_id'];
                 //Reset the user statistics back to 0.
                 $query = "UPDATE user_statistics SET billing_cycle_logins = 0, billing_cycle_documents = 0, billing_cycle_pages = 0\n\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('i', $userID);
                     $resultFromExec = $stmt->execute();
                 }
             }
             break;
         case "invoice_created":
             echo "Invoice Created!";
             $returnResults = array();
             $invoiceID = $inData['content']['invoice']['id'];
             $subscriptionID = $inData['content']['invoice']['subscription_id'];
             $result = ChargeBee_Subscription::retrieve($subscriptionID);
             $subscription = $result->subscription();
             $addonName = $subscription->__get('planId') . "_extra_pages";
             //build out of the name of their plan + '_extra_pages'
             //Locate the user's page usage.
             $query = "SELECT user_subscriptions.chargebee_id, user_subscriptions.user_id, user_statistics.billing_cycle_pages, subscription_plans.included_pages\n\t\t\t\t\t\t\tFROM user_subscriptions\n\t\t\t\t\t\t\tLEFT JOIN user_statistics ON user_subscriptions.user_id = user_statistics.user_id\n\t\t\t\t\t\t\tLEFT JOIN subscription_plans ON user_subscriptions.subscription_plan_id = subscription_plans.plan_name\n\t\t\t\t\t\t\tWHERE user_subscriptions.chargebee_id =  ?";
             $stmt = $this->sql->link->prepare($query);
             if (!$stmt) {
                 die('Invalid query: ' . $this->sql->link->error);
             } else {
                 $stmt->bind_param('s', $subscriptionID);
                 $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;
                 }
             }
             $userID = $data['user_id'];
             //Calculate how many pages over their included subscription pages they've used.
             $billingCyclePages = $data['billing_cycle_pages'];
             if (!is_numeric($data['billing_cycle_pages'])) {
                 $billingCyclePages = 0;
             }
             $billingCyclePages = $billingCyclePages - $data['included_pages'];
             //Add the extra pages used by the user to the invoice, if > 0
             if ($billingCyclePages > 0) {
                 $result = ChargeBee_Invoice::addAddonCharge($invoiceID, array("addon_id" => $addonName, "addon_quantity" => $billingCyclePages));
             }
             //Reset the user statistics back to 0.
             $query = "UPDATE user_statistics SET billing_cycle_logins = 0, billing_cycle_documents = 0, billing_cycle_pages = 0\n\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('i', $userID);
                 $resultFromExec = $stmt->execute();
             }
             //Close the invoice and charge the customer.
             $result = ChargeBee_Invoice::collect($invoiceID);
             //$invoice = $result->invoice();
             break;
         case "subscription_renewed":
             break;
             //Fired when a user reactivates their subscription. Clear the counters for paid accounts.
         //Fired when a user reactivates their subscription. Clear the counters for paid accounts.
         case "subscription_reactivated":
             $returnResults = array();
             $subscriptionID = $inData['content']['subscription']['id'];
             $subscriptionPlanID = $inData['content']['subscription']['plan_id'];
             //Locate the user's page usage.
             $query = "SELECT user_subscriptions.chargebee_id, user_subscriptions.user_id, user_statistics.billing_cycle_pages, subscription_plans.included_pages\n\t\t\t\t\t\t\tFROM user_subscriptions\n\t\t\t\t\t\t\tLEFT JOIN user_statistics ON user_subscriptions.user_id = user_statistics.user_id\n\t\t\t\t\t\t\tLEFT JOIN subscription_plans ON user_subscriptions.subscription_plan_id = subscription_plans.plan_name\n\t\t\t\t\t\t\tWHERE user_subscriptions.chargebee_id =  ?";
             $stmt = $this->sql->link->prepare($query);
             if (!$stmt) {
                 die('Invalid query: ' . $this->sql->link->error);
             } else {
                 $stmt->bind_param('s', $subscriptionID);
                 $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;
                 }
             }
             $userID = $data['user_id'];
             //Reset the usage for free accounts.
             if ($subscriptionPlanID != "free") {
                 //Reset the user statistics back to 0.
                 $query = "UPDATE user_statistics SET billing_cycle_logins = 0, billing_cycle_documents = 0, billing_cycle_pages = 0\n\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('i', $userID);
                     $resultFromExec = $stmt->execute();
                 }
             }
             break;
             //Terminate free trial accounts when the free trial runs out.
         //Terminate free trial accounts when the free trial runs out.
         case "subscription_activated":
             $returnResults = array();
             $subscriptionID = $inData['content']['subscription']['id'];
             $subscriptionPlanID = $inData['content']['subscription']['plan_id'];
             $cardStatus = $inData['content']['customer']['card_status'];
             $trialEnd = $inData['content']['subscription']['trial_end'];
             $currentTermStart = $inData['content']['subscription']['current_term_start'];
             //The free trial has expired. Cancel their account.
             //if($subscriptionPlanID == "free" && $cardStatus == "no_card" && ($trialEnd == $currentTermStart)) {
             //	$result = ChargeBee_Subscription::cancel($subscriptionID);
             //}
             break;
         default:
             break;
     }
 }
コード例 #4
0
<?php

require_once './header.php';
?>

<?php 
$listResult = ChargeBee_Invoice::invoicesForSubscription($subscriptionId, array("limit" => 20));
?>


<div id="cb-wrapper-ssp">
    <ul class="nav nav-tabs">
        <li><a href="subscription">Subscription</a></li>
        <li class="active"><a href="invoice_list">Invoices</a></li>
        <li class="pull-right">
            <?php 
include "./Logout.php";
?>
        </li>
    </ul>  
    <div id="cb-main-content" class="clearfix">
        <?php 
if ($listResult->count() == 0) {
    ?>
            <div class="text-center h4 cb-empty-space">
                No invoice found
            </div>
        <?php 
} else {
    ?>
        <h3 class="unstyle">Your Invoices</h3>       
コード例 #5
0
function invoiceAsPdf()
{
    $invoiceId = $_GET['invoice_id'];
    $invoice = ChargeBee_Invoice::retrieve($invoiceId)->invoice();
    if ($invoice->subscriptionId != getSubscriptionId()) {
        header("HTTP/1.0 400 Error");
        include $_SERVER["DOCUMENT_ROOT"] . "/error_pages/400.html";
        return;
    }
    $result = ChargeBee_Invoice::pdf($invoiceId);
    header("Location: " . $result->download()->downloadUrl);
}