function doPreapproval($array)
 {
     // $array is $_POST from paypalInfo.tpl.php form submission
     // extract yields: $amount, $rewardId, $projectId, $backerId
     // $backerId has been verified == $_SESSION['userId'] by our caller
     extract($array);
     $TrackingID = generate_password(5);
     $user = mysql_fetch_array($this->con->recordselect("SELECT * from `users` where `userId` = {$backerId} "));
     $senderEmail = base64_decode($user['paypalUserAccount']);
     if (empty($senderEmail)) {
         $senderEmail = base64_decode($user['emailAddress']);
     }
     $project_rcd = mysql_fetch_array($this->con->recordselect("SELECT * from `projects` where `projectId` = {$projectId} "));
     $project = mysql_fetch_array($this->con->recordselect("SELECT * from `projectbasics` where `projectId` = {$projectId} "));
     $memo = "Support CrowdedRocket project: " . $project['projectTitle'];
     $returnUrl = SITE_URL . 'modules/projectBacker/thankyou.php?projectId=' . $projectId . '&trackingId=' . $TrackingID;
     $cancelUrl = SITE_URL . 'modules/projectBacker/precancel.php?projectId=' . $projectId . '&trackingId=' . $TrackingID;
     $currencyCode = 'USD';
     $startingDate = date('Y-m-d', strtotime("+10 minutes", $project['projectEnd']));
     $endingDate = date('Y-m-d', strtotime("+5 days", $project['projectEnd']));
     // copied from samples PreapprovalReceipt.php
     $requestEnvelope = new RequestEnvelope("en_US");
     $preapprovalRequest = new PreapprovalRequest($requestEnvelope, $cancelUrl, $currencyCode, $returnUrl, $startingDate);
     $preapprovalRequest->endingDate = $endingDate;
     $preapprovalRequest->maxAmountPerPayment = $amount;
     $preapprovalRequest->maxNumberOfPayments = 1;
     $preapprovalRequest->maxNumberOfPaymentsPerPeriod = 1;
     $preapprovalRequest->maxTotalAmountOfAllPayments = $amount;
     $preapprovalRequest->memo = $memo;
     $preapprovalRequest->senderEmail = $senderEmail;
     $preapprovalRequest->feesPayer = 'PRIMARYRECEIVER';
     $preapprovalRequest->displayMaxTotalAmount = 'TRUE';
     $service = new AdaptivePaymentsService(Configuration::getAcctAndConfig());
     try {
         $response = $service->Preapproval($preapprovalRequest);
         //wrtlog("DEBUG payment.class.php service Preapproval response: ".print_r($response,true));
         $ack = strtoupper($response->responseEnvelope->ack);
         if ($ack != "SUCCESS") {
             wrtlog("WARNING payment.class.php Preapproval failed. response=" . print_r($resonse, true));
             $redirectUrl = isset($response->redirectUrl) ? $response->redirectUrl : $cancelUrl;
         } else {
             $token = $response->preapprovalKey;
             $redirectUrl = isset($response->redirectUrl) ? $response->redirectUrl : $returnUrl;
             // Create a pre-approved projectbacking record including the preapproval key
             $final_arr = array();
             foreach ($preapprovalRequest as $K => $P) {
                 $final_arr['request.' . addslashes($K)] = addslashes(is_array($P) ? json_encode($P) : $P);
             }
             $response_array = dismount($response);
             foreach ($response_array as $K => $P) {
                 $final_arr[addslashes($K)] = addslashes(is_array($P) ? json_encode($P) : $P);
             }
             $this->con->insert("insert into preapproval_detail (detail) values ('" . json_encode($final_arr) . "')");
             $preapproval_detail_id = mysql_insert_id();
             // create initial backing record with payment_status='?'
             $commission = get_commission($projectId, $amount);
             $this->con->insert("INSERT INTO `projectbacking` " . "(rewardId, projectId, userId, pledgeAmount, backingTime, paypalId,preapproval_detail_id,payment_status,preapproval_key,tracking_id,pledgeCommision) " . "VALUES('" . $rewardId . "','" . $projectId . "', " . $backerId . ", '" . $amount . "', " . time() . ",NULL,'" . $preapproval_detail_id . "','?','" . $token . "','" . $TrackingID . "','" . $commission . "')");
             redirect($redirectUrl);
             // control returns to either returnUrl (thankyou.php) or cancelUrl (precancel.php)
         }
     } catch (Exception $ex) {
         return $ex;
         // return error to caller
     }
 }
 * (Optional) Whether to display the maximum total amount of this preapproval. It is one of the following values:
   TRUE – Display the amount
    FALSE – Do not display the amount (default)
*/
if ($_POST['displayMaxTotalAmount'] != null) {
    $preapprovalRequest->displayMaxTotalAmount = $_POST['displayMaxTotalAmount'];
}
/*
* 	 ## Creating service wrapper object
Creating service wrapper object to make API call and loading
Configuration::getAcctAndConfig() returns array that contains credential and config parameters
*/
$service = new AdaptivePaymentsService(Configuration::getAcctAndConfig());
try {
    /* wrap API method calls on the service object with a try catch */
    $response = $service->Preapproval($preapprovalRequest);
} catch (Exception $ex) {
    require_once 'Common/Error.php';
    exit;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>PayPal Adaptive Payments - Preapproval</title>
<link href="Common/sdk.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="Common/sdk_functions.js"></script>
</head>

<body>
	<div id="wrapper">
 public function preapprove()
 {
     $logger = new PPLoggingManager('Preapproval');
     // ##PreapprovalRequest
     // The code for the language in which errors are returned, which must be
     // en_US.
     $requestEnvelope = new RequestEnvelope("en_US");
     // `PreapprovalRequest` takes mandatory params:
     //
     // * `RequestEnvelope` - Information common to each API operation, such
     // as the language in which an error message is returned.
     // * `Cancel URL` - URL to redirect the sender's browser to after
     // canceling the preapproval
     // * `Currency Code` - The code for the currency in which the payment is
     // made; you can specify only one currency, regardless of the number of
     // receivers
     // * `Return URL` - URL to redirect the sender's browser to after the
     // sender has logged into PayPal and confirmed the preapproval
     // * `Starting Date` - First date for which the preapproval is valid. It
     // cannot be before today's date or after the ending date.
     $preapprovalRequest = new PreapprovalRequest($requestEnvelope, "http://localhost/cancel", "USD", "http://localhost/return", "2013-12-18");
     // The URL to which you want all IPN messages for this preapproval to be
     // sent.
     // This URL supersedes the IPN notification URL in your profile
     $preapprovalRequest->ipnNotificationUrl = "http://localhost/ipn";
     // ## Creating service wrapper object
     // Creating service wrapper object to make API call and loading
     // configuration file for your credentials and endpoint
     $service = new AdaptivePaymentsService();
     try {
         // ## Making API call
         // Invoke the appropriate method corresponding to API in service
         // wrapper object
         $response = $service->Preapproval($preapprovalRequest);
     } catch (Exception $ex) {
         $logger->error("Error Message : " . $ex->getMessage());
     }
     // ## Accessing response parameters
     // You can access the response parameters in
     // response object as shown below
     // ### Success values
     if ($response->responseEnvelope->ack == "Success") {
         // The status of the payment. Possible values are:
         //
         // * CREATED - The payment request was received; funds will be
         // transferred once the payment is approved
         // * COMPLETED - The payment was successful
         // * INCOMPLETE - Some transfers succeeded and some failed for a
         // parallel payment or, for a delayed chained payment, secondary
         // receivers have not been paid
         // * ERROR - The payment failed and all attempted transfers failed
         // or all completed transfers were successfully reversed
         // * REVERSALERROR - One or more transfers failed when attempting
         // to reverse a payment
         // * PROCESSING - The payment is in progress
         // * PENDING - The payment is awaiting processing
         //$logger->log("Payment Status : ".$response);
         var_dump($response);
     } else {
         $logger->error("API Error Message : " . $response->error[0]->message);
     }
     return $response;
 }