if ($_REQUEST['transactionStatus'] == 'PENDING') { $billingActionId = 3; } elseif ($_REQUEST['transactionStatus'] == 'SUCCESS') { $billingActionId = 4; } //Add a payment billing action. $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);
function changePlan() { //The user has an active plan and wants to change to another plan. global $debug, $message, $success, $Dbc, $returnThis; $output = ''; try { //See if the user currently has an active plan or promotion. The userId is a unique field, so only one billing record can exist per user. $calculateRefund = Adrlist_Billing::calculateRefund($_SESSION['userId']); if (!is_array($calculateRefund)) { throw new Adrlist_CustomException('', '$calculateRefund returned false.'); } $debug->printArray($calculateRefund, '$calculateRefund'); $calculateRefund['refundAmount'] = '$' . $calculateRefund['refundAmount']; $beginningDate = Adrlist_Time::utcToLocal($calculateRefund['userPlanArray']['dateAdded'], false)->format('M j, Y'); //Return a DateTime object. $nextBillingDate = Adrlist_Time::addToDate($calculateRefund['userPlanArray']['dateAdded'], $calculateRefund['userPlanArray']['period'], $calculateRefund['userPlanArray']['length']); $nextBillingDate = Adrlist_Time::utcToLocal($nextBillingDate, false)->format('M j, Y'); //Return a DateTime object. $output .= '<div class="textMedium textLeft" style="margin:2em;line-height:1.5em"> Please select a new plan. You current plan is <span class="bold">' . $calculateRefund['userPlanArray']['name'] . '</span>.<br> You will directed to the payment processor to authorize a new payment and will receive a credit in the form of a pro-rated refund for the remainder of the current billing period. <br> <br> ' . $calculateRefund['daysRemaining'] . ' of ' . $calculateRefund['billingPeriodDays'] . ' days remaining in this billing period (' . $beginningDate . ' to ' . $nextBillingDate . ').<br> Refund Amount: ' . $calculateRefund['refundAmount'] . ' </div>' . buildBillingOffers(); $returnThis['billingOfferId'] = $calculateRefund['userPlanArray']['billingOfferId']; $returnThis['output'] = $output; $success = true; //Work to be done here. } catch (Adrlist_CustomException $e) { } catch (PDOException $e) { error(__LINE__, '', '<pre>' . $e . '</pre>'); } if (MODE == 'changePlan') { returnData(); } }