5

I have spent a long time looking for a way to transfer money from a business paypal via an API to several users' paypal accounts. I.e. I have the recipient's paypal email address and I would like to transfer X funds from our account to theirs via API.

PayPal adaptive payments seems along the right lines, but I can't see the right commands to get it to work (and avoid the user having to verify a step, i.e. the whole process cant be automated)

There are lots of other similar questions on SO, but none have satisfactory responses, especially since MassPay cant be used outside of the US paypal told me on the phone today.

Any help or experience would be hugely appreciated - thank you!

Community
  • 1
  • 1
hobailey
  • 861
  • 1
  • 12
  • 22

1 Answers1

5

As it is, it isn't tested, but it comes from a piece of code that works well, I just didn't want to copy and paste that ;-) This code uses the Adaptive Payments PayPal API.

Here's a link to source

$payLoad=array();

//prepare the receivers
$receiverList=array();
$counter=0;
$receiverList["receiver"][$counter]["amount"]=$r["withdrawalAmount"];
$receiverList["receiver"][$counter]["email"]=$r["paypalEmail"];
$receiverList["receiver"][$counter]["paymentType"]="SERVICE";//this could be SERVICE or PERSONAL (which makes it free!)
$receiverList["receiver"][$counter]["invoiceId"]=$r["withdrawalID"];//NB that this MUST be unique otherwise paypal will reject it and get shitty. However it is a totally optional field

//prepare the call
$payLoad["actionType"]="PAY";
$payLoad["cancelUrl"]="http://www.example.com";//this is required even though it isnt used
$payLoad["returnUrl"]="http://www.example.com";//this is required even though it isnt used
$payLoad["currencyCode"]="EUR";
$payLoad["receiverList"]=$receiverList;
$payLoad["feesPayer"]="EACHRECEIVER";//this could be SENDER or EACHRECEIVER
//$payLoad["fundingConstraint"]=array("allowedFundingType"=>array("fundingTypeInfo"=>array("fundingType"=>"BALANCE")));//defaults to ECHECK but this takes ages and ages, so better to reject the payments if there isnt enough money in the account and then do a manual pull of bank funds through; more importantly, echecks have to be accepted/rejected by the user and i THINK balance doesnt
$payLoad["sender"]["email"]=$ppemail;//the paypal email address of the where the money is coming from

//run the call
$API_Endpoint = "https://svcs$ppapicall.paypal.com/AdaptivePayments/Pay";
$payLoad["requestEnvelope"]=array("errorLanguage"=>urlencode("en_US"),"detailLevel"=>urlencode("ReturnAll"));//add some debugging info the payLoad and setup the requestEnvelope
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $API_Endpoint);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,  array(
    'X-PAYPAL-REQUEST-DATA-FORMAT: JSON',
    'X-PAYPAL-RESPONSE-DATA-FORMAT: JSON',
    'X-PAYPAL-SECURITY-USERID: '. $ppuserid,
    'X-PAYPAL-SECURITY-PASSWORD: '. $pppass,
    'X-PAYPAL-SECURITY-SIGNATURE: '. $ppsig,
    'X-PAYPAL-APPLICATION-ID: '. $ppappid
));  
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payLoad));//
$response = curl_exec($ch);
$response = json_decode($response, 1);

//analyse the output
$payKey = $response["payKey"];
$paymentExecStatus=$response["paymentExecStatus"];
$correlationId=$response["responseEnvelope"]["correlationId"];
$paymentInfoList = isset($response["paymentInfoList"]) ? $response["paymentInfoList"] : null;

if ($paymentExecStatus<>"ERROR") {

foreach($paymentInfoList["paymentInfo"] as $paymentInfo) {//they will only be in this array if they had a paypal account
$receiverEmail = $paymentInfo["receiver"]["email"];
$receiverAmount = $paymentInfo["receiver"]["amount"];
$withdrawalID = $paymentInfo["receiver"]["invoiceId"];
$transactionId = $paymentInfo["transactionId"];//what shows in their paypal account
$senderTransactionId = $paymentInfo["senderTransactionId"];//what shows in our paypal account
$senderTransactionStatus = $paymentInfo["senderTransactionStatus"];
$pendingReason = isset($paymentInfo["pendingReason"]) ? $paymentInfo["pendingReason"] : null;
}

}else{
//deal with it
}
hobailey
  • 861
  • 1
  • 12
  • 22
  • 1
    I think the point is to have the answer on-site no matter the pain. Otherwise that would make this the link exchange. – mAsT3RpEE Apr 02 '14 at 13:01
  • Ok, fair enough - but given that it was so much code and when I tried to add it as a code block it broke the SO code formatting. Since I didnt have long to answer the question, either I didnt answer it and gave up or I pasted the code somewhere else to at least help others :-) – hobailey Apr 02 '14 at 16:36
  • 1
    Just try to add an explanation of what you did. key points here and there are enough. That way we can all benefit from a better stackoverflow. You don't have to post code. everyone knows it's annoying. – mAsT3RpEE Apr 02 '14 at 18:54
  • Agreed with @mAsT3RpEE. A clear explanation and optionally links to some resources to get started would have sufficed. What Paypal API is this BTW, Adaptive payments? – Czar Pino Apr 04 '14 at 06:53
  • @CzarPino, yes it is indeed - have just updated answer to state that. – hobailey Apr 04 '14 at 09:11
  • @hobailey good example but i am facing one issue with it. When I pay amount to other paypal it deduct from my account but receiver does not receive that amount and in my account, it also displaying unclaimed payment status – Pankaj Sharma Nov 24 '17 at 06:01
  • @PankajSharma my memory is a little rusty now, but one thing that springs to mind (and it's completely off topic btw ;-) ) - is the receiver's account in the same currency as the funds being sent? If not, that would give the symptoms you describe. – hobailey Nov 24 '17 at 09:47
  • @hobailey thanx for replying it's working now, the issue has my email id is not verified on paypal so that'why it had not worked – Pankaj Sharma Nov 24 '17 at 10:57
  • But still i have one query if you will interested than plz reply. – Pankaj Sharma Nov 24 '17 at 10:58
  • I want to integrate adaptive payment but in paypal, this message is displayed : "Adaptive Payments is not available for new integrations. PayPal provides this documentation to support existing integrations.".This means that i can not use adaptive payment for my new integration... right? I want to use this how can i use ? – RK12 Mar 03 '20 at 07:21
  • is it possible to do a transfer from a paypal account to another one programmatically ? and using what paypal api? – user666 Jan 26 '22 at 07:06