<?php

//require_once "../boot-strap.php";
require_once "../source/PayPal/Service.php";
$service = new WPAM_PayPal_Service("https://api-3t.sandbox.paypal.com/nvp", "castor_1294111788_biz_api1.gmail.com", "1294111809", "AsaT6blQoEKAqTh8yzrIjhorxcKDAOtY0N2oc-MOoTj-YCr7wxRiRVtJ");
$request = new WPAM_PayPal_MassPayRequest(WPAM_PayPal_MassPayRequest::RECEIVERTYPE_EMAIL_ADDRESS, 'USD', 'Affiliate payout');
$request->addRecipient('*****@*****.**', '20.23', 1337);
$request->addRecipient('*****@*****.**', '19.99', 1338);
$response = $service->doMassPay($request);
print_r($response);
 private function processSubmitToPaypalRequest($request)
 {
     $options = new WPAM_Options();
     $db = new WPAM_Data_DataAccess();
     $aff_db = $db->getAffiliateRepository();
     $tr_db = $db->getTransactionRepository();
     $paypalService = new WPAM_PayPal_Service($options->getPaypalAPIEndPointURL(), $options->getPaypalAPIUser(), $options->getPaypalAPIPassword(), $options->getPaypalAPISignature());
     $massPayRequest = new WPAM_PayPal_MassPayRequest(WPAM_PayPal_MassPayRequest::RECEIVERTYPE_EMAIL_ADDRESS, __('Affiliate Payment', 'affiliates-manager'));
     $transactions = array();
     $amount = '0.00';
     $fee = '0.00';
     $totalAmount = '0.00';
     foreach ($request['affiliates'] as $affiliateArray) {
         $affiliateModel = $aff_db->load($affiliateArray['id']);
         if ($affiliateModel === NULL) {
             throw new Exception(__('Affiliate not found', 'affiliates-manager'));
         }
         if ($affiliateModel->paymentMethod !== 'paypal') {
             throw new Exception(__('Payment method for affiliate is not PayPal', 'affiliates-manager'));
         }
         $paymentTransaction = new WPAM_Data_Models_TransactionModel();
         $paymentTransaction->affiliateId = $affiliateModel->affiliateId;
         $paymentTransaction->amount = bcmul($affiliateArray['amount'], -1, 2);
         $paymentTransaction->dateCreated = time();
         $paymentTransaction->dateModified = time();
         $paymentTransaction->description = __('Payout via PayPal Mass Pay', 'affiliates-manager');
         $paymentTransaction->status = WPAM_Data_Models_TransactionModel::STATUS_PENDING;
         $paymentTransaction->type = WPAM_Data_Models_TransactionModel::TYPE_PAYOUT;
         $transactionId = $tr_db->insert($paymentTransaction);
         $transactions[] = array('transactionId' => $transactionId);
         $massPayRequest->addRecipient($affiliateModel->paypalEmail, $affiliateArray['amount'], $transactionId);
         $amount = bcadd($amount, $affiliateArray['amount'], 2);
         if ($amount * 0.02 > 1.0) {
             $fee += 1.0;
         } else {
             $fee = bcadd($fee, bcmul($affiliateArray['amount'], '0.02', 2), 2);
         }
     }
     $totalAmount = $amount + $fee;
     $ppResponse = $paypalService->doMassPay($massPayRequest);
     $ppLogModel = new WPAM_Data_Models_PaypalLogModel();
     $ppLogModel->ack = $ppResponse->getAck();
     $ppLogModel->build = $ppResponse->getBuild();
     $ppLogModel->correlationId = $ppResponse->getCorrelationId();
     $ppLogModel->dateOccurred = time();
     $ppLogModel->errors = $ppResponse->getErrors();
     $ppLogModel->responseTimestamp = $ppResponse->getTimestamp();
     $ppLogModel->version = $ppResponse->getVersion();
     $ppLogModel->rawResponse = $ppResponse->getRawResponse();
     $ppLogModel->status = $ppResponse->IsFailure() ? 'failed' : 'pending';
     $ppLogModel->amount = $amount;
     $ppLogModel->totalAmount = $totalAmount;
     $ppLogModel->fee = $fee;
     $ppLogId = $db->getPaypalLogRepository()->insert($ppLogModel);
     if ($ppResponse->IsFailure()) {
         foreach ($transactions as $transaction) {
             $tr_db->delete(array('transactionId' => $transaction['transactionId']));
         }
         return new WPAM_Pages_TemplateResponse('admin/paypalpayments/masspay_failed', array('response' => $ppResponse));
     }
     foreach ($transactions as $transaction) {
         $dbtr = $tr_db->load($transaction['transactionId']);
         $dbtr->referenceId = $ppResponse->getCorrelationId();
         $tr_db->update($dbtr);
     }
     $response = new WPAM_Pages_TemplateResponse('admin/paypalpayments/masspay_submitted', array('response' => $ppResponse, 'ppLogId' => $ppLogId));
     return $response;
 }