• 11

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 187


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 187
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

How to send money to paypal using php

I have stored the customers paypal email address in database and I want to send them money using that email address. I am using PHP.

Anyone please suggest how to do that.

      • 1
    • seems strange that you're sending money to customers instead of collecting the money. i want to be on your list.
      • 1
    • Lets start with a list of what all you have tried? Did you google atleast? It is better if you can give a particular problem where you are stuck. Thanks
      • 1
    • @pinaki: Google is not an answer nor helpful on SO (most searches are leading back to the question anyway). But I agree with you that the question is too broad, and he should come back with a specific problem.
    • @Bobby: If I google for "php paypal example", I get a link for phpclasses for some sample code (link 3 on page 1). I didnt check out the validity of the code but I was refering to any such research done by the OP. That atleast allows us to narrow down the question.

I was looking for the same issue, here's what is working for me. Tested in 'sandbox' mode and using NVP (instead of SOAP). Your server must support CURL, in order to verify it use:

echo 'curl extension/module loaded/installed: ';
echo ( !extension_loaded('curl')) ? 'no' : 'yes';
echo "<br />\n";
phpinfo(INFO_MODULES); // just to be sure

If is not loaded or installed ask to your hostmaster or get it here, otherwise go ahead:

// code modified from source: https://cms.paypal.com/cms_content/US/en_US/files/developer/nvp_MassPay_php.txt
// documentation: https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/howto_api_masspay
// sample code: https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/library_code

// eMail subject to receivers
$vEmailSubject = 'PayPal payment';

/** MassPay NVP example.
 *  Pay one or more recipients. 

// For testing environment: use 'sandbox' option. Otherwise, use 'live'.
// Go to www.x.com (PayPal Integration center) for more information.
$environment = 'sandbox'; // or 'beta-sandbox' or 'live'.

 * Send HTTP POST Request
 * @param string The API method name
 * @param string The POST Message fields in &name=value pair format
 * @return array Parsed HTTP Response body
function PPHttpPost($methodName_, $nvpStr_)
 global $environment;

 // Set up your API credentials, PayPal end point, and API version.
 // How to obtain API credentials:
 // https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_api_NVPAPIBasics#id084E30I30RO
 $API_UserName = urlencode('my_api_username');
 $API_Password = urlencode('my_api_password');
 $API_Signature = urlencode('my_api_signature');
 $API_Endpoint = "https://api-3t.paypal.com/nvp";
 if("sandbox" === $environment || "beta-sandbox" === $environment)
  $API_Endpoint = "https://api-3t.$environment.paypal.com/nvp";
 $version = urlencode('51.0');

 // Set the curl parameters.
 $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, $API_Endpoint);
 curl_setopt($ch, CURLOPT_VERBOSE, 1);

 // Turn off the server and peer verification (TrustManager Concept).

 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($ch, CURLOPT_POST, 1);

 // Set the API operation, version, and API signature in the request.
 $nvpreq = "METHOD=$methodName_&VERSION=$version&PWD=$API_Password&USER=$API_UserName&SIGNATURE=$API_Signature$nvpStr_";

 // Set the request as a POST FIELD for curl.
 curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq);

 // Get response from the server.
 $httpResponse = curl_exec($ch);

 if( !$httpResponse)
  exit("$methodName_ failed: " . curl_error($ch) . '(' . curl_errno($ch) .')');

 // Extract the response details.
 $httpResponseAr = explode("&", $httpResponse);

 $httpParsedResponseAr = array();
 foreach ($httpResponseAr as $i => $value)
  $tmpAr = explode("=", $value);
  if(sizeof($tmpAr) > 1)
   $httpParsedResponseAr[$tmpAr[0]] = $tmpAr[1];

 if((0 == sizeof($httpParsedResponseAr)) || !array_key_exists('ACK', $httpParsedResponseAr))
  exit("Invalid HTTP Response for POST request($nvpreq) to $API_Endpoint.");

 return $httpParsedResponseAr;

// Set request-specific fields.
$emailSubject = urlencode($vEmailSubject);
$receiverType = urlencode('EmailAddress');
$currency = urlencode('USD'); // or other currency ('GBP', 'EUR', 'JPY', 'CAD', 'AUD')

// Receivers
// Use '0' for a single receiver. In order to add new ones: (0, 1, 2, 3...)
// Here you can modify to obtain array data from database.
$receivers = array(
  0 => array(
    'receiverEmail' => "user1@paypal.com", 
    'amount' => "20.00",
    'uniqueID' => "id_001", // 13 chars max
    'note' => " payment of commissions"), // I recommend use of space at beginning of string.
  1 => array(
    'receiverEmail' => "user2@paypal.com",
    'amount' => "162.38",
    'uniqueID' => "A47-92w", // 13 chars max, available in 'My Account/Overview/Transaction details' when the transaction is made 
    'note' => " payoff of what I owed you"  // space again at beginning.
$receiversLenght = count($receivers);

// Add request-specific fields to the request string.

$receiversArray = array();

for($i = 0; $i < $receiversLenght; $i++)
 $receiversArray[$i] = $receivers[$i];

foreach($receiversArray as $i => $receiverData)
 $receiverEmail = urlencode($receiverData['receiverEmail']);
 $amount = urlencode($receiverData['amount']);
 $uniqueID = urlencode($receiverData['uniqueID']);
 $note = urlencode($receiverData['note']);
 $nvpStr .= "&L_EMAIL$i=$receiverEmail&L_Amt$i=$amount&L_UNIQUEID$i=$uniqueID&L_NOTE$i=$note";

// Execute the API operation; see the PPHttpPost function above.
$httpParsedResponseAr = PPHttpPost('MassPay', $nvpStr);

if("SUCCESS" == strtoupper($httpParsedResponseAr["ACK"]) || "SUCCESSWITHWARNING" == strtoupper($httpParsedResponseAr["ACK"]))
 exit('MassPay Completed Successfully: ' . print_r($httpParsedResponseAr, true));
 exit('MassPay failed: ' . print_r($httpParsedResponseAr, true));


Good luck!

  • 13
Reply Report
    • I have done a successful masspay, but it doesn't send an email.. is it supposed to? am I doing something wrong? - paypal dev couldn't be more frustrating
    • I tested only in sandbox mode and everything went good until I sent a payment to a new test account. I received a receipt in the sender's test email but the receiver didn't it... until I verified it. Did you send in sandbox or live mode?
    • Thank you for this example code that PayPal makes nearly impossible to find. At least, I had a hard time finding it...
      • 1
    • Maybe I forgot to mention that I did it from PayPal's sample code, or may be the first commented line has the words: 'code modified from source'. I didn't wanted to give to Deepak or others just a link to official documentation, I gave him a useful code. By the way, I modified the official sample code because the original payment process is made three times, that means: you want to send 10 bucks and that code spends 30, without warnings.