public function testConfigure() { ChargeBee_Environment::configure("test_site", "test_api_key"); $defEnvConfig = ChargeBee_Environment::defaultEnv(); $this->assertEqual("test_site", $defEnvConfig->getSite()); $this->assertEqual("test_api_key", $defEnvConfig->getApiKey()); $this->assertEqual("https://test_site.chargebee.com/api/v1", $defEnvConfig->getApiEndpoint()); }
public function reactivateAccount($inData) { ChargeBee_Environment::configure("rocketmailmerge", "Chargebee api token"); $data = array(); $card = array("number" => $inData['number'], "expiryMonth" => $inData['expiryMonth'], "expiryYear" => $inData['expiryYear'], "cvv" => $inData['cvv'], "billingZip" => $inData['billingZip']); try { $chargebee_result = ChargeBee_Subscription::update($inData['token'], array("planId" => "base_rate", "trialEnd" => 0, "card" => $card)); } catch (Exception $e) { header('Location: https://app.rocketmailmerge.com/account/reactivate.html?id=' . $inData['token'] . "&crd=0"); die; } //Reactivate their subscription $query = "UPDATE user_subscriptions set subscription_status = 'active'\r\n\t\t\t\t WHERE chargebee_id = ?"; $stmt = $this->sql->link->prepare($query); if (!$stmt) { die('Invalid query: ' . $this->sql->link->error); } else { $stmt->bind_param('s', $inData['token']); $resultFromExec = $stmt->execute(); } //Log them in $this->lookupUser(array("id" => $inData['token'])); $session = new iSession(); $session->mod_var("logged_in", 1); $session->mod_var("user_id", $this->outData['data']['user_id']); $session->update_session(); header('Location: https://app.rocketmailmerge.com/dashboard/?tut=1'); }
public function createNewSubscribedUser($inData) { $pass_hasher = new PasswordHash(8, FALSE); $time = $this->getCurrentDateTime(); $affectedRows = 0; $userID = ""; // //Begin account creation ///////////////////////////////////////// if (strlen($inData['password']) < 5) { $output['success'] = 0; $output['text'] = "Sorry, your password must be at least 5 characters."; $output['rex'] = "Bark bark woof woof!"; $output['return'] = 2; $this->outData = $output; $this->sendTechEmail("*****@*****.**", "Subscriber: Pass too short", "A user tried to register and account but their password was too short -----\n" . print_r($inData, 1)); header("location:https://" . $_SERVER['SERVER_NAME'] . "/account/register.html?r=0"); //Pass to short. die; return $output; } // //Begin account creation ///////////////////////////////////////// $username = $inData['email']; $email = $inData['email']; $password = @$pass_hasher->HashPassword($inData['password']); $new_user_guid = $this->create_guid(); $file_directory = $this->create_guid(); ChargeBee_Environment::configure("rocketmailmerge", "Chargebe api token"); //Build up card data $cardNumber = $inData['number']; $cardexpiryMonth = $inData['expiryMonth']; $cardexpiryYear = $inData['expiryYear']; $cardcvv = $inData['cvv']; $cardbillingZip = $inData['billingZip']; //Check that they supplied a valid email address if (filter_var($inData['email'], FILTER_VALIDATE_EMAIL)) { //Store the email in the session in case they're redirected back to thje register page. session_start(); $_SESSION['email'] = $inData['email']; $customer = array("first_name" => $inData['email']); $customer['email'] = $inData['email']; } else { $this->sendTechEmail("*****@*****.**", "Subscriber: Bogus Email", "A user tried to register and account but their email was bogus -----\n" . print_r($inData, 1)); header("location:https://" . $_SERVER['SERVER_NAME'] . "/account/register.html?r=2"); //Bogus email address die; } $card = array("number" => $cardNumber, "expiryMonth" => $cardexpiryMonth, "expiryYear" => $cardexpiryYear, "cvv" => $cardcvv, "billingZip" => $cardbillingZip); try { $chargebee_result = ChargeBee_Subscription::create(array("planId" => "base_rate", "id" => $new_user_guid, "customer" => $customer, "card" => $card, "list" => 1)); } catch (Exception $e) { $this->sendTechEmail("*****@*****.**", "Subscriber: Bad Credit Card", "A user tried to register and account but their credit card was rejected by Chargebee -----\n" . print_r($inData, 1)); header("location:https://" . $_SERVER['SERVER_NAME'] . "/account/register.html?r=3"); //Invalid Credit Card die; } $query = "INSERT INTO users (created_at, updated_at, username, password_hash, file_directory, email)\r\n\t\t\t\t VALUES (?, NOW(), ?, ?, ?, ?)"; $stmt = $this->sql->link->prepare($query); if (!$stmt) { die('Invalid query: ' . $this->sql->link->error); } else { $stmt->bind_param('sssss', $time, $username, $password, $file_directory, $email); $resultFromExec = $stmt->execute(); //Record the subscription management in the database for this user. if ($resultFromExec) { $affectedRows += $stmt->affected_rows; $userID = $stmt->insert_id; //////////////////////////////////////////////////////////////////////////////////////////////////////// // Create the user's example files //Create the background file link - Invoice mailmerge high. $dir1 = __DIR__ . "/../user_files/{$file_directory}/backgrounds/files/png/Invoice_backdrop-mailmerge-high/"; mkdir($dir1, 0700, true); symlink(__DIR__ . "/../newuser_examples/backgrounds/files/png/Invoice_backdrop-mailmerge-high/Invoice_backdrop-mailmerge-high.png", $dir1 . "Invoice_backdrop-mailmerge-high.png"); $dir2 = __DIR__ . "/../user_files/{$file_directory}/backgrounds/previews/png/Invoice_backdrop-mailmerge-high/"; mkdir($dir2, 0700, true); symlink(__DIR__ . "/../newuser_examples/backgrounds/previews/png/Invoice_backdrop-mailmerge-high/Invoice_backdrop-mailmerge-high.png", $dir2 . "Invoice_backdrop-mailmerge-high.png"); $dir3 = __DIR__ . "/../user_files/{$file_directory}/backgrounds/templates/png/Invoice_backdrop-mailmerge-high/"; mkdir($dir3, 0700, true); symlink(__DIR__ . "/../newuser_examples/backgrounds/templates/png/Invoice_backdrop-mailmerge-high/Invoice_backdrop-mailmerge-high.png", $dir3 . "Invoice_backdrop-mailmerge-high.png"); $dir4 = __DIR__ . "/../user_files/{$file_directory}/backgrounds/thumbnails/png/Invoice_backdrop-mailmerge-high/"; mkdir($dir4, 0700, true); symlink(__DIR__ . "/../newuser_examples/backgrounds/thumbnails/png/Invoice_backdrop-mailmerge-high/Invoice_backdrop-mailmerge-high.png", $dir4 . "Invoice_backdrop-mailmerge-high.png"); //Insert it into the database. $query = "INSERT INTO backgrounds (created_at, updated_at, user_id, name, data_path) \n\t\t\t\t\t\t VALUES (?, NOW(), ?, ?, ?)"; $stmt = $this->sql->link->prepare($query); $name = "Invoice_backdrop-mailmerge-high.png"; $dataPath = "/backgrounds/server/user_files/{$file_directory}/backgrounds/templates/png/Invoice_backdrop-mailmerge-high/"; $stmt->bind_param('siss', $time, $userID, $name, $dataPath); $stmt->execute(); $backgroundID1 = $stmt->insert_id; $query = "INSERT INTO backgrounds_pages (created_at, updated_at, background_id, background_pg_num, file_name) \n\t\t\t\t\t\t VALUES (?, NOW(), ?, '1', ?)"; $stmt = $this->sql->link->prepare($query); $name = "Invoice_backdrop-mailmerge-high.png"; $stmt->bind_param('sis', $time, $backgroundID1, $name); $stmt->execute(); $backgroundPGID1 = $stmt->insert_id; //Create the background file link - Example envelope. $dir1 = __DIR__ . "/../user_files/{$file_directory}/backgrounds/files/png/example-envelope/"; mkdir($dir1, 0700, true); symlink(__DIR__ . "/../newuser_examples/backgrounds/files/png/example-envelope/example-envelope.png", $dir1 . "example-envelope.png"); $dir2 = __DIR__ . "/../user_files/{$file_directory}/backgrounds/previews/png/example-envelope/"; mkdir($dir2, 0700, true); symlink(__DIR__ . "/../newuser_examples/backgrounds/previews/png/example-envelope/example-envelope.png", $dir2 . "example-envelope.png"); $dir3 = __DIR__ . "/../user_files/{$file_directory}/backgrounds/templates/png/example-envelope/"; mkdir($dir3, 0700, true); symlink(__DIR__ . "/../newuser_examples/backgrounds/templates/png/example-envelope/example-envelope.png", $dir3 . "example-envelope.png"); $dir4 = __DIR__ . "/../user_files/{$file_directory}/backgrounds/thumbnails/png/example-envelope/"; mkdir($dir4, 0700, true); symlink(__DIR__ . "/../newuser_examples/backgrounds/thumbnails/png/example-envelope/example-envelope.png", $dir4 . "example-envelope.png"); //Insert it into the database. $query = "INSERT INTO backgrounds (created_at, updated_at, user_id, name, data_path) \n\t\t\t\t\t\t VALUES (?, NOW(), ?, ?, ?)"; $stmt = $this->sql->link->prepare($query); $name = "example-envelope.png"; $dataPath = "/backgrounds/server/user_files/{$file_directory}/backgrounds/templates/png/example-envelope/"; $stmt->bind_param('siss', $time, $userID, $name, $dataPath); $stmt->execute(); $backgroundID2 = $stmt->insert_id; $query = "INSERT INTO backgrounds_pages (created_at, updated_at, background_id, background_pg_num, file_name) \n\t\t\t\t\t\t VALUES (?, NOW(), ?, '1', ?)"; $stmt = $this->sql->link->prepare($query); $name = "example-envelope.png"; $stmt->bind_param('sis', $time, $backgroundID2, $name); $stmt->execute(); $backgroundPGID2 = $stmt->insert_id; //Create the datasource file copy - Example data. $dir1 = __DIR__ . "/../user_files/{$file_directory}/datasources/Example - Customer Invoices 2013/"; mkdir($dir1, 0700, true); copy(__DIR__ . "/../newuser_examples/datasources/Example - Customer Invoices 2013/Example - Customer Invoices 2013", $dir1 . "Example - Customer Invoices 2013"); $query = "INSERT INTO datasources (created_at, updated_at, user_id, name, data_path, file_name, headers, `lines`) \n\t\t\t\t\t\t VALUES (?, NOW(), ?, ?, ?, ?, ?, 2)"; $stmt = $this->sql->link->prepare($query); $path = "/datasources/server/user_files/{$file_directory}/datasources/Example - Customer Invoices 2013/"; $name1 = "Example - Business Letter 2014"; $name2 = "Example - Business Letter 2014"; $headers = '["invoiceNum","name","address","suburb","state","postcode","billingPeriod","invoiceIssuedDate","clientNum","billingDetails","thankyouTxt","invoiceAmt","overdueTxt","overdueAmt","GrandTotal"]'; $stmt->bind_param('sissss', $time, $userID, $name1, $path, $name2, $headers); $stmt->execute(); $datasourceID = $stmt->insert_id; //Create a sample document for the new user. $query = "INSERT INTO documents (created_at, updated_at, user_id, name, datasource_id) \n\t\t\t\t\t\t VALUES (?, NOW(), ?, ?, ?)"; $stmt = $this->sql->link->prepare($query); $name = "Example - Business Letter"; $stmt->bind_param('sisi', $time, $userID, $name, $datasourceID); $stmt->execute(); $documentID = $stmt->insert_id; $query = "INSERT INTO documents_pages (created_at, updated_at, document_id, document_pg_num, background_id, background_pg_id, preset, width, height, selected_measurement_unit, variables ) \n\t\t\t\t\t\t VALUES (?, NOW(), ?, ?, ?, ?, ?, ?, ?, ?, ?), (?, NOW(), ?, ?, ?, ?, ?, ?, ?, ?, ?)"; $stmt = $this->sql->link->prepare($query); $variables1 = 'a:1:{i:0;a:12:{s:4:"name";s:8:"variable";s:1:"x";s:5:"127.5";s:1:"y";s:3:"294";s:5:"width";s:3:"381";s:6:"height";s:2:"87";s:4:"text";s:44:"<name> <address> <suburb> <state> <postcode>";s:11:"font_family";s:5:"Arial";s:9:"font_size";s:2:"16";s:10:"font_style";s:0:"";s:12:"font_padding";s:2:"10";s:10:"font_color";s:12:"rgb(0, 0, 0)";s:10:"font_align";s:1:"L";}}'; $variables2 = 'a:13:{i:0;a:12:{s:4:"name";s:8:"variable";s:1:"x";s:3:"700";s:1:"y";s:3:"700";s:5:"width";s:3:"200";s:6:"height";s:4:"39.2";s:4:"text";s:12:"<invoiceAmt>";s:11:"font_family";s:5:"Arial";s:9:"font_size";s:2:"16";s:10:"font_style";s:4:"bold";s:12:"font_padding";s:2:"10";s:10:"font_color";s:12:"rgb(0, 0, 0)";s:10:"font_align";s:1:"L";}i:1;a:12:{s:4:"name";s:8:"variable";s:1:"x";s:3:"536";s:1:"y";s:3:"750";s:5:"width";s:3:"161";s:6:"height";s:2:"36";s:4:"text";s:12:"<overdueTxt>";s:11:"font_family";s:5:"Arial";s:9:"font_size";s:2:"16";s:10:"font_style";s:4:"bold";s:12:"font_padding";s:2:"10";s:10:"font_color";s:12:"rgb(0, 0, 0)";s:10:"font_align";s:1:"R";}i:2;a:12:{s:4:"name";s:8:"variable";s:1:"x";s:3:"700";s:1:"y";s:3:"750";s:5:"width";s:3:"201";s:6:"height";s:2:"36";s:4:"text";s:12:"<overdueAmt>";s:11:"font_family";s:5:"Arial";s:9:"font_size";s:2:"16";s:10:"font_style";s:4:"bold";s:12:"font_padding";s:2:"10";s:10:"font_color";s:12:"rgb(0, 0, 0)";s:10:"font_align";s:1:"L";}i:3;a:12:{s:4:"name";s:8:"variable";s:1:"x";s:3:"201";s:1:"y";s:3:"427";s:5:"width";s:3:"229";s:6:"height";s:2:"32";s:4:"text";s:10:"30/11/2012";s:11:"font_family";s:5:"Arial";s:9:"font_size";s:2:"12";s:10:"font_style";s:4:"bold";s:12:"font_padding";s:2:"10";s:10:"font_color";s:12:"rgb(0, 0, 0)";s:10:"font_align";s:1:"L";}i:4;a:12:{s:4:"name";s:8:"variable";s:1:"x";s:3:"201";s:1:"y";s:3:"466";s:5:"width";s:3:"229";s:6:"height";s:2:"32";s:4:"text";s:15:"<billingPeriod>";s:11:"font_family";s:5:"Arial";s:9:"font_size";s:2:"12";s:10:"font_style";s:4:"bold";s:12:"font_padding";s:2:"10";s:10:"font_color";s:12:"rgb(0, 0, 0)";s:10:"font_align";s:1:"L";}i:5;a:12:{s:4:"name";s:8:"variable";s:1:"x";s:3:"201";s:1:"y";s:3:"506";s:5:"width";s:3:"230";s:6:"height";s:2:"32";s:4:"text";s:19:"<invoiceIssuedDate>";s:11:"font_family";s:5:"Arial";s:9:"font_size";s:2:"12";s:10:"font_style";s:4:"bold";s:12:"font_padding";s:2:"10";s:10:"font_color";s:12:"rgb(0, 0, 0)";s:10:"font_align";s:1:"L";}i:6;a:12:{s:4:"name";s:8:"variable";s:1:"x";s:3:"202";s:1:"y";s:3:"546";s:5:"width";s:3:"228";s:6:"height";s:2:"32";s:4:"text";s:11:"<clientNum>";s:11:"font_family";s:5:"Arial";s:9:"font_size";s:2:"12";s:10:"font_style";s:4:"bold";s:12:"font_padding";s:2:"10";s:10:"font_color";s:12:"rgb(0, 0, 0)";s:10:"font_align";s:1:"L";}i:7;a:12:{s:4:"name";s:8:"variable";s:1:"x";s:3:"201";s:1:"y";s:3:"586";s:5:"width";s:3:"228";s:6:"height";s:2:"32";s:4:"text";s:12:"<invoiceNum>";s:11:"font_family";s:5:"Arial";s:9:"font_size";s:2:"12";s:10:"font_style";s:4:"bold";s:12:"font_padding";s:2:"10";s:10:"font_color";s:12:"rgb(0, 0, 0)";s:10:"font_align";s:1:"L";}i:8;a:12:{s:4:"name";s:8:"variable";s:1:"x";s:3:"629";s:1:"y";s:3:"901";s:5:"width";s:3:"276";s:6:"height";s:2:"52";s:4:"text";s:12:"<GrandTotal>";s:11:"font_family";s:5:"Arial";s:9:"font_size";s:2:"32";s:10:"font_style";s:0:"";s:12:"font_padding";s:2:"10";s:10:"font_color";s:12:"rgb(0, 0, 0)";s:10:"font_align";s:1:"L";}i:9;a:12:{s:4:"name";s:8:"variable";s:1:"x";s:2:"46";s:1:"y";s:3:"181";s:5:"width";s:3:"438";s:6:"height";s:2:"49";s:4:"text";s:22:"INVOICE : <invoiceNum>";s:11:"font_family";s:5:"Arial";s:9:"font_size";s:2:"26";s:10:"font_style";s:0:"";s:12:"font_padding";s:2:"10";s:10:"font_color";s:14:"rgb(0, 3, 215)";s:10:"font_align";s:1:"L";}i:10;a:12:{s:4:"name";s:8:"variable";s:1:"x";s:3:"128";s:1:"y";s:3:"299";s:5:"width";s:3:"331";s:6:"height";s:18:"63.199999999999996";s:4:"text";s:50:"<name> <address> <suburb> <state> <postcode>";s:11:"font_family";s:5:"Arial";s:9:"font_size";s:2:"12";s:10:"font_style";s:0:"";s:12:"font_padding";s:2:"10";s:10:"font_color";s:12:"rgb(0, 0, 0)";s:10:"font_align";s:1:"L";}i:11;a:12:{s:4:"name";s:8:"variable";s:1:"x";s:3:"496";s:1:"y";s:3:"560";s:5:"width";s:3:"384";s:6:"height";s:4:"34.4";s:4:"text";s:13:"<thankyouTxt>";s:11:"font_family";s:5:"Arial";s:9:"font_size";s:2:"12";s:10:"font_style";s:0:"";s:12:"font_padding";s:2:"10";s:10:"font_color";s:12:"rgb(0, 0, 0)";s:10:"font_align";s:1:"R";}i:12;a:12:{s:4:"name";s:8:"variable";s:1:"x";s:3:"501";s:1:"y";s:3:"296";s:5:"width";s:3:"378";s:6:"height";s:18:"41.599999999999994";s:4:"text";s:16:"<billingDetails>";s:11:"font_family";s:5:"Arial";s:9:"font_size";s:2:"16";s:10:"font_style";s:0:"";s:12:"font_padding";s:2:"10";s:10:"font_color";s:12:"rgb(0, 0, 0)";s:10:"font_align";s:1:"R";}}'; $pgNum = 1; $pgNum2 = 2; $empty = ''; $pgSize = 'a4'; $zero = 0; $size1 = 220; $size4 = 110; $size2 = 210; $size3 = 297; $stmt->bind_param('siiiissssssiiiisssss', $time, $documentID, $pgNum, $backgroundID2, $backgroundPGID2, $empty, $size1, $size4, $zero, $variables1, $time, $documentID, $pgNum2, $backgroundID1, $backgroundPGID1, $pgSize, $size2, $size3, $zero, $variables2); $stmt->execute(); //////////////////////////////////////////////////////////////////////////////////////////////////////// // Create the user's welcome messages $welcomeMessage[0] = "If you need any support simply email our friendly team at <a href=\"mailto:support@rocketmailmerge.com\">support@rocketmailmerge.com</a>"; $welcomeMessage[1] = "Thanks for signing up to Rocket Mail Merge! This is a help notice, you can click on it to hide it."; $query = "INSERT INTO user_notices (created_at, updated_at, user_id, type, message) \n\t\t\t\t\t\t VALUES (?, NOW(), ?, 'msg_Info', ?), (?, NOW(), ?, 'msg_Info', ?)"; $stmt = $this->sql->link->prepare($query); $stmt->bind_param('sissis', $time, $userID, $welcomeMessage[0], $time, $userID, $welcomeMessage[1]); $resultFromExec = $stmt->execute(); $affectedRows += $stmt->affected_rows; //////////////////////////////////////////////////////////////////////////////////////////////////////// // Create their statistics data $query = "INSERT INTO user_statistics (created_at, updated_at, user_id) \n\t\t\t\t\t\t VALUES(?, NOW(), ?)"; $stmt = $this->sql->link->prepare($query); $stmt->bind_param('si', $time, $userID); $resultFromExec = $stmt->execute(); $affectedRows += $stmt->affected_rows; //////////////////////////////////////////////////////////////////////////////////////////////////////// // Create their subscription record. $query = "INSERT INTO user_subscriptions (created_at, updated_at, user_id, chargebee_id, subscription_plan_id, subscription_status ) \r\n\t\t\t\t\t\t VALUES(?, NOW(), ?, ?, 'base_rate', 'active')"; $stmt = $this->sql->link->prepare($query); $stmt->bind_param('sis', $time, $userID, $new_user_guid); $resultFromExec = $stmt->execute(); $affectedRows += $stmt->affected_rows; //We delegate our chargebee account creation as the curl process can take a few seconds to return the data //we don't want new user's to be delayed in account creation. if ($resultFromExec) { } } /* free result */ $stmt->free_result(); $stmt->close(); } if ($affectedRows > 0 && $userID != "") { $output['success'] = 1; $output['text'] = "Congratulations, your account has been created."; $output['return'] = 1; $output['data']['user_id'] = $userID; $output['parse_curl'] = 1; $session = new iSession(); $session->mod_var("logged_in", 1); $session->mod_var("user_id", $userID); $session->update_session(); //Great success - Redirect the user to their dashboard. header('Location: https://app.rocketmailmerge.com/dashboard/?tut=1'); die; } else { $output['success'] = 0; $output['text'] = "Sorry, that email is already registered to an account. Please choose another."; $output['rex'] = "Grrr"; $output['return'] = 1; $this->sendTechEmail("*****@*****.**", "Subscriber: Account already registered", "A user tried to register and account but their email was already registered -----\n" . print_r($inData, 1)); header("location:https://" . $_SERVER['SERVER_NAME'] . "/account/register.html?r=1"); //Account already taken. die; } $this->outData = $output; return $output; }
<?php Autoloader::namespaces(array('Chargebee' => Bundle::path('chargebee') . 'chargebee-php/lib/')); // include require_once __DIR__ . '/chargebee-php/lib/ChargeBee.php'; // load config $config = Config::get('chargebee'); ChargeBee_Environment::configure($config['site_name'], $config['site_api_key']);
<?php require_once dirname(__FILE__) . "/ErrorHandler.php"; /* * Adding ChargeBee php libraries */ require_once dirname(__FILE__) . "/lib/ChargeBee.php"; /* * Sets the environment for calling the Chargebee API. * You need to sign up at ChargeBee app to get this credential. * It is better if you fetch configuration from the environment * properties instead of hard coding it in code. */ ChargeBee_Environment::configure("<your-site>", "<your-api-key>"); function endsWith($origStr, $suffix) { return substr($origStr, -strlen($suffix)) === $suffix; }
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; } }
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; } } }
<?php require 'lib/ChargeBee.php'; //Code from apidocs ChargeBee_Environment::configure("rrcb-test", "jaGdadHeCQxfmFQG2sEgSrzHdyt23cwcd"); function testParams() { $result = ChargeBee_Subscription::retrieve('1rifg4iOsZUL4mBhF'); // print_r($result); //print_r($result->subscription()->currentTermEnasdd); //print_r($result->customer()); //print_r($result->customer()->cfDateOfBirth); //print_r($result->customer()->cfGender); print_r($result->subscription()); } testParams(); //$var = "cfDateOfBirth"; //print_r(substr($var,0,2));