$userBillingActionId = Adrlist_Billing::addBillingAction($planRow['userId'], $planRow['billingOfferId'], $billingActionId, 1, __FILE__ . ' ' . __LINE__);
                    $transactionAmount = explode(' ', $_REQUEST['transactionAmount']);
                    $transactionAmount = $transactionAmount[1];
                    //$transactionAmount = preg_split('/USD /', $_REQUEST['transactionAmount'], -1);
                    $debug->add('$transactionAmount: ' . $transactionAmount);
                    //See if the user has a record in userBilling.
                    if ($billingActionId == 4) {
                        //Payment was successful.
                        //If the user currently has a plan, issue a refund.
                        $calculateRefund = Adrlist_Billing::calculateRefund($planRow['userId']);
                        if (is_array($calculateRefund)) {
                            Adrlist_Billing::amazonRefundRequest($calculateRefund['userPlanArray']['userBillingActionId'], 'Pro-rated refund for ' . $calculateRefund['userPlanArray']['name'], $calculateRefund['refundAmount']);
                        } else {
                            $redirect = false;
                        }
                        if (Adrlist_Billing::getUserPlan($planRow['userId']) === false) {
                            //The user has no record in userBiling.
                            $userBillingStmt = $Dbc->prepare("INSERT INTO\n\tuserBilling\nSET\n\tuserId = ?,\n\tuserBillingActionId = ?,\n\tbillingOfferId = ?,\n\tdateAdded = ?");
                            $userBillingParams = array($planRow['userId'], $userBillingActionId, $planRow['billingOfferId'], DATETIME);
                        } else {
                            //The user has a record in userBilling. Update it with the new plan info.
                            $userBillingStmt = $Dbc->prepare("UPDATE\n\tuserBilling\nSET\n\tbillingOfferId = ?,\n\tdateAdded = ?\nWHERE\n\tuserId = ?\n");
                            $userBillingParams = array($planRow['billingOfferId'], DATETIME, $planRow['userId']);
                        }
                        $userBillingStmt->execute($userBillingParams);
                        $messageCenter->newMessage(1, $planRow['userId'], 'Thank you for your purchase!', 'Thank you for your purchase. Your account has been credited according to the plan you chose.<br>
<br>
<span class="bold textMedium">Plan Details:</span>
' . ucfirst($planRow['type']) . ': ' . $planRow['name'] . '<br>
Price: ' . $planRow['price'] . '<br>
Credits: ' . $planRow['credits'] . '<br>
Esempio n. 2
0
function reconcileLists($userId)
{
    /*
    Automatically lock lists beyond the account credit balance, starting with the list with the oldest modified date.
    
    Returns true if lists were locked.
    */
    global $debug, $message, $Dbc;
    try {
        $locked = false;
        $userBillingInfo = Adrlist_Billing::getUserPlan($_SESSION['userId']);
        $_SESSION['credits'] = $_SESSION['siteRoleId'] == 5 ? 9999 : $userBillingInfo['credits'];
        $activeLists = getActiveLists($_SESSION['userId']);
        $_SESSION['activeLists'] = count($activeLists);
        $creditBalance = $_SESSION['credits'] - $_SESSION['activeLists'];
        if ($creditBalance < 0) {
            $Dbc->beginTransaction();
            //Get a list of the user's currently unlocked lists.
            $unlockedListsStmt = $Dbc->prepare("SELECT\n\tlists.listId AS 'listId',\n\tlists.listName AS 'listName',\n\tlists.modified AS 'modified'\nFROM\n\tlists\nJOIN\n\tuserListSettings ON userListSettings.listId = lists.listId AND\n\tuserListSettings.userId = ? AND\n\tuserListSettings.listRoleId = 4\nWHERE\n\tlists.locked = 0\nORDER BY\n\tmodified ASC");
            $unlockedListsParams = array($_SESSION['userId']);
            $unlockedListsStmt->execute($unlockedListsParams);
            $preUnlockedLists = array();
            $listsToLock = abs($creditBalance);
            $lockListId = '';
            $x = 1;
            while ($row = $unlockedListsStmt->fetch(PDO::FETCH_ASSOC)) {
                $preUnlockedLists[] = $row;
                $lockListId .= empty($lockListId) ? $row['listId'] : ', ' . $row['listId'];
                if ($x = $listsToLock) {
                    //Only lock as many lists as we need to.
                    break;
                }
            }
            $debug->add('$listsToLock: ' . $listsToLock);
            $lockStmt = $Dbc->query("UPDATE\n\tlists\nSET\n\tlocked = 1\nWHERE\n\tlistId IN (" . $lockListId . ")");
            pdoError(__LINE__, $lockStmt, '');
            $lockStmt->execute();
            //Re-run the unlocked list query to check for differences.
            $unlockedListsStmt->execute($unlockedListsParams);
            $postUnlockedLists = array();
            while ($row = $unlockedListsStmt->fetch(PDO::FETCH_ASSOC)) {
                $postUnlockedLists[] = $row;
            }
            $debug->printArray($postUnlockedLists, '$postUnlockedLists');
            $difference = arrayRecursiveDiff($preUnlockedLists, $postUnlockedLists);
            $debug->printArray($difference, '$difference');
            $message .= 'As you have more active lists than credits, the following lists were locked ' . faqLink(29) . ':<br>';
            foreach ($difference as $key => $value) {
                $message .= $value['listName'] . '<br>';
            }
            $Dbc->commit();
            $locked = true;
            //$Dbc->rollback();
        }
    } catch (Adrlist_CustomException $e) {
    } catch (PDOException $e) {
        error(__LINE__, '', '<pre>' . $e . '</pre>');
    }
    return $locked;
}
Esempio n. 3
0
function buildBilling()
{
    //Build the billing info including credits and expiration date.
    global $debug, $message, $success, $Dbc, $returnThis;
    $output = '';
    try {
        if (empty($_SESSION['userId'])) {
            throw new Adrlist_CustomException('', '$_SESSION[\'userId\'] is empty.');
        }
        //Refresh credits.
        reconcileLists($_SESSION['userId']);
        $userPlan = Adrlist_Billing::getUserPlan($_SESSION['userId']);
        $purchasedPlan = false;
        $hiddenRows = array();
        $billingOutput = '';
        $foundRows = false;
        if (count($userPlan) > 1) {
            $nameLength = 0;
            $billingPlansArray = array();
            $billingPlansHiddenArray = array();
            foreach ($userPlan as $key => $value) {
                $debug->add('key: ' . $key);
                if (is_numeric($key) && $value['validCredit']) {
                    $foundRows = true;
                    if ($value['validCredit']) {
                        $purchasedPlan = $value['type'] == 'plan' ? true : $purchasedPlan;
                    }
                    $newDate = Adrlist_Time::addToDate($value['dateAdded'], $value['period'], $value['length']);
                    $newDate = Adrlist_Time::utcToLocal($newDate, false)->format('F j, Y');
                    if ($value['type'] == 'plan') {
                        $nextBillingDate = $newDate;
                        $expires = 'N/A';
                    } else {
                        $nextBillingDate = 'N/A';
                        $expires = $newDate;
                    }
                    $nameLength = strlen($value['name']) > $nameLength ? strlen($value['name']) * 0.8 : $nameLength;
                    $billingOutput .= '
<div class="columnParent">
	<div class="break">
		<div class="columnLeft" style="font-weight:none">
			Billing Plan:
		</div>
		<div class="columnRight">
			' . $value['name'] . '
		</div>
	</div>
	<div class="break">
		<div class="columnLeft">
			Next Billing Date:
		</div>
		<div class="columnRight">
			' . $nextBillingDate . '
		</div>
	</div>
	<div class="break">
		<div class="columnLeft">
			Credits:
		</div>
		<div class="columnRight">
			' . $value['credits'] . '
		</div>
	</div>
	<div class="break">
		<div class="columnLeft">
			Expires:
		</div>
		<div class="columnRight">
			' . $expires . '
		</div>
	</div>
</div>
<button class="ui-btn ui-mini ui-btn-icon-right ui-icon-carat-r ui-btn-inline ui-corner-all"  toggle="planTerms' . $value['userBillingId'] . '">Plan Terms</button>
<div class="hide" id="planTerms' . $value['userBillingId'] . '">
	<div class="textLeft" style="margin:1em"><br>
		' . nl2br($value['terms']) . '
	</div>
</div>';
                    /*Currently not used.
                    					$name = '<button class="ui-btn ui-mini ui-btn-icon-right ui-icon-carat-r ui-btn-inline ui-corner-all" toggle="billingPlanAction' . $value['userBillingId'] . '">' . $value['name'] . '</button>';
                    					$billingPlansArray[$value['userBillingId']] = array(
                    						$name,
                    						$nextBillingDate,
                    						$value['credits'],
                    						$expires
                    					);
                    					$billingPlanActions = '<div class="break">Plan Actions</div>
                    <button class="changePaymentCard ui-btn ui-btn-inline ui-corner-all ui-mini"><i class="fa fa-credit-card" ></i>Change Payment Card</button><button class="changePlan ui-btn ui-btn-inline ui-corner-all ui-mini"><i class="fa fa-credit-card" ></i>Change Plan</button>
                    
                    <button class="ui-btn ui-mini ui-btn-icon-right ui-icon-carat-r ui-btn-inline ui-corner-all"  toggle="planTerms' . $value['userBillingId'] . '">Plan Terms</button>
                    <div class="hide" id="planTerms' . $value['userBillingId'] . '">
                    	<div class="textLeft" style="margin:1em"><br>
                    		' . nl2br($value['terms']) . '
                    	</div>
                    </div>';
                    					$billingPlansHiddenArray[$value['userBillingId']] = array('billingPlanAction' . $value['userBillingId'],$billingPlanActions);*/
                }
            }
            /*if($foundRows){
            			$titleArray = array(
            				array('Name'),
            				array('Next Billing Date',1),
            				array('Credits',2),
            				array('Expires',3)
            			);
            			$buildBillingRows = new Adrlist_BuildRows('billingPlans',$titleArray,$billingPlansArray);
            			$buildBillingRows->addHiddenRows($billingPlansHiddenArray);
            			$billingOutput .= $buildBillingRows->output();
            		}*/
        }
        if (!$foundRows) {
            $billingOutput = '<div style="margin:1em">This account doesn\'t have any active plans or promotions.</div>';
        }
        $billingOutput .= $purchasedPlan ? '<button class="changePaymentCard ui-btn ui-btn-inline ui-corner-all ui-mini"><i class="fa fa-credit-card" ></i>Change Payment Card</button><button class="changePlan ui-btn ui-btn-inline ui-corner-all ui-mini"><i class="fa fa-credit-card" ></i>Change Plan</button>
' : '<button class="ui-btn ui-btn-icon-left ui-btn-inline ui-corner-all ui-mini ui-shadow" id="addCredits">Add Credits</button>';
        $output .= '<button class="buildBillingHistory ui-btn ui-btn-icon-left ui-btn-inline ui-corner-all ui-icon-clock ui-mini ui-shadow">Billing History</button><div class="myAccountTitle textCenter">
	Billing
</div>
<div class="textCenter">
' . $billingOutput . '
	<div class="columnParent">
		<div class="break">
			<div class="columnLeft" style="font-weight:none">
				Credits:
			</div>
			<div class="columnRight">
				' . $_SESSION['credits'] . '
			</div>
		</div>
		<div class="break">
			<div class="columnLeft">
				Active Lists:
			</div>
			<div class="columnRight">
				' . $_SESSION['activeLists'] . '
			</div>
		</div>
		<div class="hr2"></div>
		<div class="break">
			<div class="columnLeft">
				Credit Balance:
			</div>
			<div class="columnRight">
				' . ($_SESSION['credits'] - $_SESSION['activeLists']) . '
			</div>
		</div>
	</div>
</div>';
        if (MODE == 'buildBilling') {
            $success = true;
            $returnThis['buildBilling'] = $output;
        }
    } catch (Adrlist_CustomException $e) {
    } catch (PDOException $e) {
        error(__LINE__, '', '<pre>' . $e . '</pre>');
    }
    if (MODE == 'buildBilling') {
        returnData();
    } else {
        return $output;
    }
}