public static function getChangeSubscriptionXml($timeframe, $newPlanCode, $newQuantity, $newUnitAmount, $add_ons) { $doc = new DOMDocument("1.0"); $root = $doc->appendChild($doc->createElement("subscription")); $root->appendChild($doc->createElement("timeframe", $timeframe)); if ($newPlanCode != null) { $root->appendChild($doc->createElement("plan_code", $newPlanCode)); } if ($newQuantity != null) { $root->appendChild($doc->createElement("quantity", $newQuantity)); } if ($newUnitAmount != null) { $root->appendChild($doc->createElement("unit_amount", $newUnitAmount)); } if (isset($add_ons)) { $root->appendChild(RecurlySubscription::getAddOnsXml($add_ons, $doc)); } return $doc->saveXML(); }
RecurlyClient::SetAuth(RECURLY_API_USERNAME, RECURLY_API_PASSWORD, RECURLY_SUBDOMAIN, RECURLY_ENVIRONMENT); // Setting timezone for time() function. date_default_timezone_set('America/New_York'); // Replace with the user's unique ID in your system $account_id = '14'; if ($_SERVER['REQUEST_METHOD'] == 'POST') { //Recurly Account $account = new RecurlyAccount($account_id); $account->username = $_POST['account']['username']; $account->first_name = $_POST['account']['first_name']; $account->last_name = $_POST['account']['last_name']; $account->email = $_POST['account']['email']; //Wordpress Account $newWordpressUser = array('user_login' => $_POST['account']['username'], 'user_pass' => $_POST['account']['password'], 'user_nicename' => $_POST['account']['first_name'] . ' ' . $_POST['account']['last_name'], 'nickname' => $_POST['account']['first_name'] . ' ' . $_POST['account']['last_name'], 'first_name' => $_POST['account']['first_name'], 'last_name' => $_POST['account']['last_name'], 'display_name' => $_POST['account']['first_name'] . ' ' . $_POST['account']['last_name'], 'user_registered' => date("Y-m-d H:i:s"), 'user_email' => $_POST['account']['email']); //Recurly Subscription $subscription = new RecurlySubscription(); $subscription->plan_code = $_POST['plan_type']; $subscription->account = $account; $subscription->billing_info = new RecurlyBillingInfo($subscription->account->account_code); $billing_info = $subscription->billing_info; $billing_info->first_name = $account->first_name; $billing_info->last_name = $account->last_name; $billing_info->address1 = $_POST['billing_info']['address1']; $billing_info->address2 = $_POST['billing_info']['address2']; $billing_info->city = $_POST['billing_info']['city']; $billing_info->state = $_POST['billing_info']['state']; $billing_info->country = $_POST['billing_info']['country']; $billing_info->zip = $_POST['billing_info']['zip']; $billing_info->credit_card->number = $_POST['credit_card']['number']; $billing_info->credit_card->year = intval($_POST['credit_card']['year']); $billing_info->credit_card->month = intval($_POST['credit_card']['month']);
public function createRecurlySubscription2(RecurlyAccount $account, $planCode, $billingFirstName, $billingLastName, $billingAddr1, $billingAddr2 = "", $billingCity, $billingState, $billingZIP, $billingCountry = "US", $ccNumber, $ccExpMonth, $ccExpYear, $ccVerificationValue) { $billing_info = new RecurlyBillingInfo($account->account_code); $billing_info->first_name = $billingFirstName; $billing_info->last_name = $billingLastName; $billing_info->address1 = $billingAddr1; $billing_info->address2 = $billingAddr2; $billing_info->city = $billingCity; $billing_info->state = $billingState; $billing_info->country = $billingCountry; $billing_info->zip = $billingZIP; $billing_info->credit_card->year = intval($ccExpYear); $billing_info->credit_card->month = intval($ccExpMonth); if (trim($ccVerificationValue) != "") { $billing_info->credit_card->verification_value = $ccVerificationValue; } if (substr($ccNumber, 0, 5) != "*****") { $billing_info->credit_card->number = $ccNumber; } $billing_info->ip_address = $_SERVER['REMOTE_ADDR']; $account_info = $billing_info->update(); $account_info = null; $currentSubscription = RecurlySubscription::getSubscription($account->account_code); $currentPlanCode = ""; $currentPlanCost = 0; $newPlanCost = 0; if ($planCode != "" && $planCode != "daily") { $newPlan = RecurlyPlan::getPlan($planCode); $newPlanCost = $newPlan->unit_amount_in_cents; } if ($currentSubscription) { $currentPlanCode = $currentSubscription->plan_code; $currentPlan = RecurlyPlan::getPlan($currentPlanCode); $currentPlanCost = $currentSubscription->unit_amount_in_cents; } if ($planCode == "" || $planCode == "daily") { // if the plan is being moved from a monthly plan to daily, then we have to cancel subscription. if ($currentSubscription) { RecurlySubscription::cancelSubscription($account->account_code); } } else { // if the plan is being moved from one monthly plan to another, then we have to // upgrade or downgrade. if ($currentSubscription) { // get the current plan's amount and compare to the new one if ($newPlanCost > $currentPlanCost) { // upgrade RecurlySubscription::changeSubscription($account->account_code, 'now', $planCode, 1); } elseif ($newPlanCost < $currentPlanCost) { RecurlySubscription::changeSubscription($account->account_code, 'renewal', $planCode, 1); } } else { // no current subscription and we want a monthly, so just add a new one $subscription = new RecurlySubscription(); $subscription->plan_code = $planCode; $subscription->account = $account; $subscription->billing_info = $billing_info; $subscription->create(); } } return $account_info; }