public static function accountsPage() { $data = array(); if (CART66_PRO) { $data['plan'] = new Cart66AccountSubscription(); $data['activeUntil'] = ''; $account = new Cart66Account(); if (isset($_REQUEST['cart66-action']) && $_REQUEST['cart66-action'] == 'delete_account') { // Look for delete request if (isset($_REQUEST['accountId']) && is_numeric($_REQUEST['accountId'])) { $account = new Cart66Account($_REQUEST['accountId']); $account->deleteMe(); $account->clear(); } } elseif (isset($_REQUEST['accountId']) && is_numeric($_REQUEST['accountId'])) { if (isset($_REQUEST['opt_out'])) { $account = new Cart66Account(); $account->load($_REQUEST['accountId']); $data = array('opt_out' => $_REQUEST['opt_out']); $account->setData($data); $account->save(); $account->clear(); } // Look in query string for account id $account = new Cart66Account(); $account->load($_REQUEST['accountId']); $id = $account->getCurrentAccountSubscriptionId(true); $data['plan'] = new Cart66AccountSubscription($id); // Return even if plan is expired if (date('Y', strtotime($data['plan']->activeUntil)) <= 1970) { $data['activeUntil'] = ''; } else { $data['activeUntil'] = date('m/d/Y', strtotime($data['plan']->activeUntil)); } } if ($_SERVER['REQUEST_METHOD'] == 'POST' && Cart66Common::postVal('cart66-action') == 'save account') { $acctData = $_POST['account']; // Format or unset password if (empty($acctData['password'])) { unset($acctData['password']); } else { $acctData['password'] = md5($acctData['password']); } // Strip HTML tags on notes field $acctData['notes'] = strip_tags($acctData['notes'], '<a><strong><em>'); $planData = $_POST['plan']; $planData['active_until'] = date('Y-m-d 00:00:00', strtotime($planData['active_until'])); // Updating an existing account if ($acctData['id'] > 0) { $account = new Cart66Account($acctData['id']); $account->setData($acctData); $account_errors = $account->validate(); $sub = new Cart66AccountSubscription($planData['id']); if ($planData['product_id'] != 'spreedly_subscription') { $sub->setData($planData); $subscription_product = new Cart66Product($sub->product_id); $sub->subscription_plan_name = $subscription_product->name; $sub->feature_level = $subscription_product->feature_level; $sub->subscriber_token = ''; } else { unset($planData['product_id']); $sub->setData($planData); } $subscription_errors = $sub->validate(); $errors = array_merge($account_errors, $subscription_errors); if (count($errors) == 0) { $account->save(); $sub->save(); $account->clear(); $sub->clear(); } else { $data['errors'] = $errors; $data['plan'] = $sub; $data['activeUntil'] = date('m/d/Y', strtotime($sub->activeUntil)); } } else { // Creating a new account $account = new Cart66Account(); $account->setData($acctData); $account_errors = $account->validate(); if (count($account_errors) == 0) { $sub = new Cart66AccountSubscription(); $sub->setData($planData); $subscription_errors = $sub->validate(); if (count($subscription_errors) == 0) { $account->save(); $sub->billingFirstName = $account->firstName; $sub->billingLastName = $account->lastName; $sub->billingInterval = 'Manual'; $sub->account_id = $account->id; $subscription_product = new Cart66Product($sub->product_id); $sub->subscription_plan_name = $subscription_product->name; $sub->feature_level = $subscription_product->feature_level; $sub->save(); $account->clear(); $data['just_saved'] = true; } else { $data['errors'] = $subscription_errors; } } else { $data['errors'] = $account_errors; } } } $data['url'] = Cart66Common::replaceQueryString('page=cart66-accounts'); $data['account'] = $account; } $view = Cart66Common::getView('admin/accounts.php', $data); echo $view; }
public function attachMembershipProduct($product, $firstName = null, $lastName = null) { if ($this->id > 0 && $product->isMembershipProduct()) { Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] Attaching a membership product to account: {$this->id}"); $firstName = isset($firstName) ? $firstName : $this->firstName; $lastName = isset($lastName) ? $lastName : $this->lastName; $data = array('account_id' => $this->id, 'billing_first_name' => $firstName, 'billing_last_name' => $lastName, 'subscription_plan_name' => $product->name, 'feature_level' => $product->featureLevel, 'status' => 'active', 'active' => 1, 'product_id' => $product->id); $duration = '+ ' . $product->billingInterval . ' ' . $product->billingIntervalUnit; if ($product->lifetimeMembership == 1) { $data['lifetime'] = 1; } else { $data['active_until'] = date('Y-m-d H:i:s', strtotime($duration)); } // Look for extension or upgrade if ($subscription = $this->getCurrentAccountSubscription()) { if ($subscription->featureLevel == $product->featureLevel && $product->lifetimeMembership != 1) { // Extend active_until date to prevent overlapping duration intervals $data['active_until'] = date('Y-m-d H:i:s', strtotime($subscription->activeUntil . $duration, Cart66Common::localTs())); } // Expire current subscription $subscription->status = 'canceled'; $subscription->active = 0; $subscription->activeUntil = date('Y-m-d 00:00:00', Cart66Common::localTs()); $subscription->save(); } $subscription = new Cart66AccountSubscription(); $subscription->setData($data); $subscription->save(); } }