示例#1
0
// Include required library files.
require_once '../includes/config.php';
require_once '../includes/paypal.class.php';
require_once '../includes/paypal.adaptive.class.php';
/*print_r($_SESSION);*/
// Create PayPal object.
$PayPalConfig = array('Sandbox' => $sandbox, 'DeveloperAccountEmail' => $developer_account_email, 'ApplicationID' => $application_id, 'DeviceID' => $device_id, 'IPAddress' => $_SERVER['REMOTE_ADDR'], 'APIUsername' => $api_username, 'APIPassword' => $api_password, 'APISignature' => $api_signature, 'APISubject' => $api_subject);
$PayPal = new PayPal_Adaptive($PayPalConfig);
// Prepare request arrays
$PreapprovalFields = array('CancelURL' => 'http://maestros-ites.com/testserver1/teecircle/paypal/cancel.php', 'CurrencyCode' => 'USD', 'DateOfMonth' => '', 'DayOfWeek' => '', 'EndingDate' => '', 'IPNNotificationURL' => '', 'MaxAmountPerPayment' => '', 'MaxNumberOfPayments' => '', 'MaxTotalAmountOfPaymentsPerPeriod' => '', 'MaxTotalAmountOfAllPayments' => $_SESSION['MaxTotalAmountOfAllPayments'], 'Memo' => '', 'PaymentPeriod' => '', 'PinType' => '', 'ReturnURL' => 'http://maestros-ites.com/testserver1/teecircle/paypal/return.php', 'SenderEmail' => '', 'StartingDate' => date('Y-m-d'), 'FeesPayer' => '', 'DisplayMaxTotalAmount' => 'true');
$ClientDetailsFields = array('CustomerID' => '', 'CustomerType' => '', 'GeoLocation' => '', 'Model' => '', 'PartnerName' => '');
$PayPalRequestData = array('PreapprovalFields' => $PreapprovalFields, 'ClientDetailsFields' => $ClientDetailsFields);
/*echo '<pre>';print_r($PreapprovalFields);die();*/
// Pass data into class for processing with PayPal and load the response array into $PayPalResult
$PayPalResult = $PayPal->Preapproval($PayPalRequestData);
// Write the contents of the response array to the screen for demo purposes.
/*echo '<pre />';
print_r($PayPalResult);*/
if ($PayPalResult['Ack'] == 'Success') {
    $croval = mysql_fetch_array(mysql_query("SELECT id FROM crowed_payment WHERE order_id=" . $_SESSION['order_id'] . " AND \r\r\n\t                                            \t\t\t\t\t\t\t\t prod_id='" . $_SESSION['prodids'] . "' AND \r\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t user_id='" . $_SESSION['user_id'] . "'"));
    if (isset($croval['id']) && !empty($croval['id'])) {
        mysql_query("UPDATE crowed_payment SET order_date='" . date('Y-m-d') . "',\r\r\n\t\t\t\t\t\t\t\t\t\t\t   order_amount='" . $_SESSION['MaxTotalAmountOfAllPayments'] . "',\r\r\n\t\t\t\t\t\t\t\t\t\t\t   preapprovalkey='" . $PayPalResult['PreapprovalKey'] . "',\r\r\n\t\t\t\t\t\t\t\t\t\t\t   correlationid='" . $PayPalResult['CorrelationID'] . "',\r\r\n\t\t\t\t\t\t\t\t\t\t\t   order_status='pending' WHERE id=" . $croval['id']);
    } else {
        mysql_query("INSERT INTO crowed_payment SET order_id=" . $_SESSION['order_id'] . ",\r\r\n\t                                            prod_id='" . $_SESSION['prodids'] . "',\r\r\n\t\t\t\t\t\t\t\t\t\t\t\torder_date='" . date('Y-m-d') . "',\r\r\n\t\t\t\t\t\t\t\t\t\t\t\torder_amount='" . $_SESSION['MaxTotalAmountOfAllPayments'] . "',\r\r\n\t\t\t\t\t\t\t\t\t\t\t\tpreapprovalkey='" . $PayPalResult['PreapprovalKey'] . "',\r\r\n\t\t\t\t\t\t\t\t\t\t\t\tcorrelationid='" . $PayPalResult['CorrelationID'] . "',\r\r\n\t\t\t\t\t\t\t\t\t\t\t\torder_status='pending',\r\r\n\t\t\t\t\t\t\t\t\t\t\t\tuser_id='" . $_SESSION['user_id'] . "'");
    }
    $crovall = mysql_fetch_array(mysql_query("SELECT id FROM crowed_payment WHERE order_id=" . $_SESSION['order_id'] . " AND \r\r\n\t                                            \t\t\t\t\t\t\t\t prod_id='" . $_SESSION['prodids'] . "' AND\r\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t preapprovalkey='" . $PayPalResult['PreapprovalKey'] . "' AND  \r\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t user_id='" . $_SESSION['user_id'] . "'"));
    $_SESSION['orid'] = $crovall['id'];
    header('location:' . $PayPalResult['RedirectURL']);
    die;
}
 function backerPreapprovePayment($array)
 {
     extract($array);
     // the following (other than user_amount name change) are a bit redundant
     // but here for a touch of security...
     $backerId = sanitize_string($array['backerId']);
     $user_amount = sanitize_string($array['amount']);
     $projectId = sanitize_string($array['projectId']);
     $rewardId = sanitize_string($array['rewardId']);
     $TrackingID = generate_password(5);
     $user = mysql_fetch_array($this->con->recordselect("SELECT * from `users` where `userId` = " . $_SESSION['userId']));
     //$backer_paypal = base64_decode($user['paypalUserAccount']);
     $backer_email = base64_decode($user['emailAddress']);
     $senderPhoneCountryCode = '';
     $senderPhone = '';
     $project_rcd = mysql_fetch_array($this->con->recordselect("SELECT * from `projects` where `projectId` = {$projectId} "));
     $creator = mysql_fetch_array($this->con->recordselect("SELECT * from `users` where `userId` = " . $project_rcd['userId']));
     $creator_paypal = base64_decode($creator['paypalUserAccount']);
     $project = mysql_fetch_array($this->con->recordselect("SELECT * from `projectbasics` where `projectId` = {$projectId} "));
     $Memo = "Support CrowdedRocket project: " . $project['projectTitle'];
     $CurrencyCode = 'USD';
     $DateOfMonth = '0';
     $DayOfWeek = 'NO_DAY_SPECIFIED';
     $StartingDate = date('Y-m-d', strtotime("+10 minutes", $project['projectEnd']));
     $EndingDate = date('Y-m-d', strtotime("+5 days", $project['projectEnd']));
     $IPNNotificationURL = '';
     $MaxAmountPerPayment = $user_amount;
     $MaxNumberOfPayments = 1;
     $MaxTotalAmountOfAllPaymentsPerPeriod = $user_amount;
     $MaxTotalAmountOfAllPayments = $user_amount;
     $PaymentPeriod = 'NO_PERIOD_SPECIFIED';
     $PinType = 'NOT_REQUIRED';
     $ReturnURL = SITE_URL . 'thankyou.php?projectId=' . $projectId . '&trackingId=' . $TrackingID;
     //$CancelURL =SITE_URL."browseproject/".$projectId."/".Slug($selectProject['projectTitle']).'/';
     $CancelURL = SITE_URL . 'precancel.php?projectId=' . $projectId . '&trackingId=' . $TrackingID;
     //$SenderEmail='admin@'.$_SERVER['SERVER_NAME'];
     $FeesPayer = 'PRIMARYRECEIVER';
     $DisplayMaxTotalAmount = true;
     $CustomerID = $backerId;
     $CustomerType = 'Backer';
     $GeoLocation = '';
     $Model = '1.0';
     $PartnerName = DISPLAYSITENAME;
     $commission = get_commission($projectId, $user_amount, '0', 'p');
     // Prepare request arrays
     $PreapprovalFields = array('CancelURL' => $CancelURL, 'CurrencyCode' => $CurrencyCode, 'DateOfMonth' => $DateOfMonth, 'DayOfWeek' => $DayOfWeek, 'EndingDate' => $EndingDate, 'IPNNotificationURL' => $IPNNotificationURL, 'MaxAmountPerPayment' => $MaxAmountPerPayment, 'MaxNumberOfPayments' => $MaxNumberOfPayments, 'MaxTotalAmountOfAllPaymentsPerPeriod' => $MaxTotalAmountOfAllPaymentsPerPeriod, 'MaxTotalAmountOfAllPayments' => $MaxTotalAmountOfAllPayments, 'Memo' => $Memo, 'PaymentPeriod' => $PaymentPeriod, 'PinType' => $PinType, 'ReturnURL' => $ReturnURL, 'SenderEmail' => $backer_email, 'StartingDate' => $StartingDate, 'FeesPayer' => $FeesPayer, 'DisplayMaxTotalAmount' => $DisplayMaxTotalAmount);
     $ClientDetailsFields = array('CustomerID' => $CustomerID, 'CustomerType' => $CustomerType, 'GeoLocation' => $GeoLocation, 'Model' => $Model, 'PartnerName' => $PartnerName);
     /*									 
     		$Receivers = array();
     		$Receiver = array(
     						'Amount' => $commission, 											
     						'Email' => $this->PayPalConfig["DeveloperAccountEmail"],  												
     						'InvoiceID' => '', 											
     						'PaymentType' => '', 										
     						'PaymentSubType' => '', 									
     						'Phone' => array('CountryCode' => '', 'PhoneNumber' => '', 'Extension' => ''), 
     						'Primary' => 'FALSE'												
     						);
     		array_push($Receivers,$Receiver);			
     		$Receiver = array(
     						'Amount' => $user_amount, 											
     						'Email' =>$creator_paypal, 												
     						'InvoiceID' => '', 											
     						'PaymentType' => '', 										
     						'PaymentSubType' => '', 									
     						'Phone' => array('CountryCode' => $senderPhoneCountryCode, 'PhoneNumber' => $senderPhone, 'Extension' => ''), 
     						'Primary' => 'TRUE'												
     						);
     		array_push($Receivers,$Receiver);	
     		
     		$PayPalRequestData = array(
     							 'PreapprovalFields' => $PreapprovalFields, 
     							 'ClientDetailsFields' => $ClientDetailsFields,
     							 'Receivers' => $Receivers
     							 );
     */
     $PayPalRequestData = array('PreapprovalFields' => $PreapprovalFields, 'ClientDetailsFields' => $ClientDetailsFields);
     // Pass data into class for processing with PayPal and load the response array into $PayPalResult
     $PayPal = new PayPal_Adaptive($this->PayPalConfig);
     wrtlog("backerPreapprovePayment requesting Preapproval using: " . print_r($PayPalRequestData, true));
     $PayPalResult = $PayPal->Preapproval($PayPalRequestData);
     wrtlog("backerPreapprovePayment after Preapproval - PayPalResult: " . print_r($PayPalResult, true));
     /* ex per paypal doc
     		Response
     		--------
     		responseEnvelope.ack=Success
     		&preapprovalKey=PA-5KY19448VE6821234 #Value of the preapproval key, for use in subsequent steps
     		
     		However, what we get back from paypal.adaptive.class.php is array(
     								   'Errors' => $Errors, 
     								   'Ack' => $Ack, 
     								   'Build' => $Build, 
     								   'CorrelationID' => $CorrelationID, 
     								   'Timestamp' => $Timestamp, 
     								   'PreapprovalKey' => $PreapprovalKey, 
     								   'RedirectURL' => $PreapprovalKey != '' ? $RedirectURL : '', 
     								   'XMLRequest' => $XMLRequest, 
     								   'XMLResponse' => $XMLResponse)
     		//// expect only the Ack, PreapprovalKey, RedirectURL, XMLRequest and XMLResponse to be filled..
     		*/
     if (isset($PayPalResult['Ack']) && strtolower($PayPalResult['Ack']) == 'success') {
         // jwg -- $PayPalResult contains both the current TrackingID and the new CorrelationID
         if (!isset($PayPalResult['PreapprovalKey'])) {
             $errors = "UNEXPECTED - no PreapprovalKey in paypal preapproval response.";
             wrtlog($errors);
             wrtlog("PayPalResult from Preapproval: " . print_r($PayPalResult, true));
             $_SESSION['msgType1'] = array('from' => 'user', 'type' => 'error', 'var' => "multiple", 'val' => $errors);
             return SITE_URL . "projectBacker/" . $projectId;
         } else {
             // Create a pre-approved projectbacking record including the preapproval key
             $final_arr = array();
             foreach ($PayPalRequestData as $K => $P) {
                 $final_arr['request.' . addslashes($K)] = addslashes(is_array($P) ? json_encode($P) : $P);
             }
             foreach ($PayPalResult 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='?'
             $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 . ", '" . $user_amount . "', " . time() . ",NULL,'" . $preapproval_detail_id . "','?','" . $PayPalResult['PreapprovalKey'] . "','" . $TrackingID . "','" . $commission . "')");
             wrtlog("backerPreapprovePayment redirecting to " . $PayPalResult['RedirectURL']);
             redirect($PayPalResult['RedirectURL']);
             // This is where user makes actual decision to pre-approve
             // control returns to either ReturnURL (thankyou.php) or CancelURL (precancel.php)
         }
     } else {
         //$PayPalErrorsSerialized = serialize($PayPalResult['Errors']);
         //$PayPalErrorsUnserialized = unserialize($PayPalErrorsSerialized);
         //print $PayPalErrorsSerialized;
         $error = array();
         foreach ($PayPalResult['Errors'] as $er) {
             $error[] = $er['Message'];
         }
         $errors = implode("<br/>", $error);
         $_SESSION['msgType1'] = array('from' => 'user', 'type' => 'error', 'var' => "multiple", 'val' => $errors);
         return SITE_URL . "projectBacker/" . $projectId;
     }
 }