/**
  * Log the PayPal recurring payment. 
  * 
  * The $data array paramter is a URL decoded version of the IPN post data.
  *   - Log the data in the pp_recurring_posts table
  *   - Update the account_subscriptions table with the new active_until date
  */
 public function log(array $ipnData)
 {
     $isLogged = false;
     $subscription = new Cart66AccountSubscription();
     if ($subscription->loadByPayPalBillingProfileId($ipnData['recurring_payment_id'])) {
         $data = array('account_id' => $subscription->accountId, 'recurring_payment_id' => $ipnData['recurring_payment_id'], 'mc_gross' => $ipnData['mc_gross'], 'txn_id' => $ipnData['txn_id'], 'product_name' => $ipnData['product_name'], 'first_name' => $ipnData['first_name'], 'last_name' => $ipnData['last_name'], 'payer_email' => $ipnData['payer_email'], 'ipn' => serialize($ipnData), 'next_payment_date' => $ipnData['next_payment_date'], 'time_created' => date('Y-m-d H:i:s', strtotime($ipnData['time_created'])));
         $this->setData($data);
         $id = $this->save();
         if ($id > 0) {
             $isLogged = true;
             Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] Recurring payment logged with ID: {$id}");
             $subscription->extendActiveUntil($ipnData['next_payment_date']);
         } else {
             Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] Failed to log recurring payment. " . print_r($data, true));
         }
     } else {
         Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] Unable to log recurring payment because the paypal billing profile id is unknown: " . $ipnData['recurring_payment_id']);
     }
     return $isLogged;
 }
 /**
  * Set active flag to zero and status to canceled.
  * The active_until date is not changed. Therefore, a canceled subscription
  * will remain active until the amount of time paid for has expired.
  * 
  * @param array $rawPost The IPN post data from PayPal
  * @return int or false The id of the subscription that was canceled or false if cancelation failed
  */
 public function cancelSubscription($rawPost)
 {
     $canceledId = false;
     $decodedPost = $this->_decodeRawPost($rawPost);
     $subId = $decodedPost['recurring_payment_id'];
     $sub = new Cart66AccountSubscription();
     Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] IPN request to cancel subscription {$subId}");
     if ($sub->loadByPayPalBillingProfileId($subId)) {
         $sub->active = 0;
         $sub->status = 'canceled';
         $sub->save();
         Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] Canceled subscription {$subId} via IPN request.");
         $canceledId = $sub->id;
     }
     return $canceledId;
 }
 public function cancelPayPalSubscription($attrs)
 {
     $link = '';
     if (Cart66Common::isLoggedIn()) {
         $account = new Cart66Account(Cart66Session::get('Cart66AccountId'));
         if ($account->isPayPalAccount()) {
             // Look for account cancelation request
             if (isset($_GET['cart66-task']) && $_GET['cart66-task'] == 'CancelRecurringPaymentsProfile') {
                 Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] Caught task: CancelPaymentsProfileStatus");
                 $sub = new Cart66AccountSubscription($account->getCurrentAccountSubscriptionId());
                 $profileId = $sub->paypalBillingProfileId;
                 $note = "Your subscription has been canceled per your request.";
                 $action = "Cancel";
                 $pp = new Cart66PayPalPro();
                 $pp->ManageRecurringPaymentsProfileStatus($profileId, $action, $note);
                 $url = str_replace('cart66-task=CancelRecurringPaymentsProfile', '', Cart66Common::getCurrentPageUrl());
                 $link = "We sent a cancelation request to PayPal. It may take a minute or two for the cancelation process to complete and for your account status to be changed.";
             } elseif ($subId = $account->getCurrentAccountSubscriptionId()) {
                 $sub = new Cart66AccountSubscription($subId);
                 if ($sub->status == 'active') {
                     $url = $sub->getSubscriptionManagementLink();
                     $text = isset($attrs['text']) ? $attrs['text'] : 'Cancel your subscription';
                     $link = "<a id='Cart66CancelPayPalSubscription' href=\"{$url}\">{$text}</a>";
                 } else {
                     $link = "Your account is {$sub->status} but will remain active until " . date(get_option('date_format'), strtotime($sub->activeUntil));
                 }
             }
         }
     }
     Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] Cancel paypal account link for logged in user: {$link}");
     return $link;
 }
Example #4
0
 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;
 }
Example #5
0
 // =============================
 // = Start Spreedly Processing =
 // =============================
 if (Cart66Session::get('Cart66Cart')->hasSpreedlySubscriptions()) {
     $accountErrors = $account->validate();
     if (count($accountErrors) == 0) {
         $account->save();
         // Save account data locally which will create an account id and/or update local values
         Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] Account data validated and saved for account id: " . $account->id);
         try {
             $spreedlyCard = new SpreedlyCreditCard();
             $spreedlyCard->hydrateFromCheckout();
             $subscriptionId = Cart66Session::get('Cart66Cart')->getSpreedlySubscriptionId();
             $productId = Cart66Session::get('Cart66Cart')->getSpreedlyProductId();
             Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] About to create a new spreedly account subscription: Account ID: {$account->id} | Subscription ID: {$subscriptionId}");
             $accountSubscription = new Cart66AccountSubscription();
             $accountSubscription->createSpreedlySubscription($account->id, $subscriptionId, $productId, $spreedlyCard);
         } catch (SpreedlyException $e) {
             Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] Failed to checkout: " . $e->getCode() . ' ' . $e->getMessage());
             $errors['spreedly failed'] = $e->getMessage();
             $accountSubscription->refresh();
             if (empty($accountSubscription->subscriberToken)) {
                 Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] About to delete local account after spreedly failure: " . print_r($account->getData(), true));
                 $account->deleteMe();
             } else {
                 // Set the subscriber token in the session for repeat attempts to create the subscription
                 Cart66Session::set('Cart66SubscriberToken', $account->subscriberToken);
             }
             if (count($errors)) {
                 try {
                     Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] Unable to process order: " . print_r($errors, true));
 public function updateMembershipProductIds()
 {
     global $wpdb;
     $output = false;
     // Check for subscriptions lacking a product id
     $sql = 'SELECT id from ' . Cart66Common::getTableName('account_subscriptions') . " WHERE product_id='0' OR product_id IS NULL";
     $needyAccountSubscriptions = $wpdb->get_results($sql);
     Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] " . count($needyAccountSubscriptions) . " accounts found that need to have a product id updated.");
     if (count($needyAccountSubscriptions) > 0) {
         // accounts needing product id have been found
         foreach ($needyAccountSubscriptions as $accountId) {
             $account = new Cart66AccountSubscription($accountId->id);
             $accountProductId = $account->getProductId();
             if ($accountProductId && !is_array($accountProductId)) {
                 $account->updateProductId($accountProductId);
                 Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] Adding Product id: {$accountProductId} to account id: {$accountId->id} ");
             } elseif (is_array($accountProductId)) {
                 $latestProductId = $account->findLatestProductId($accountProductId);
                 Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] Multiple products found for account {$accountId->id}");
                 if ($latestProductId) {
                     Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] Latest membership product id found, id: {$latestProductId}");
                     $account->updateProductId($latestProductId);
                 }
             } else {
                 $output[] = "The subscription id:{$accountId->id} belonging to {$account->billing_first_name} {$account->billing_last_name} does not have a product ID associated with it. This will prevent notifications from being sent out. Please <a href='" . Cart66Common::replaceQueryString('page=cart66-accounts&accountId=' . $account->account_id) . "'>edit the account</a> and select a product ID.";
                 Cart66Common::log('[' . basename(__FILE__) . ' - line ' . __LINE__ . "] No products were found matching the feature level and subscription plan name of the account id: {$accountId->id}");
             }
         }
     }
     return $output;
 }
Example #7
0
 /**
  * Delete the account and all account subscriptions assoicated with this account
  */
 public function deleteMe()
 {
     if ($this->id > 0) {
         $sub = new Cart66AccountSubscription();
         $subs = $sub->getModels("where account_id={$this->id}");
         foreach ($subs as $s) {
             $s->deleteMe();
         }
         parent::deleteMe();
     }
 }