title: Using Parallel payment/Adaptive payment of Paypal in a CakePHP project
body: <p>I am working in a CakePHP project, where I need to use Paypal's Adaptive payment/parallel payment feature. In this case, my client's payment will be split into 2, one for another client, another for me, the admin.</p>
  
  <p>I am trying, but always getting the "4002 error" message. It says that it can't process the payment now, please try again later.</p>
  
  <p>Here is my controller code :</p>
  
  <pre><code>&lt;?php
      App::uses('AppController','Controller');
  
      Class PaymentsController extends AppController{
  
      public $name='Payments';
      public $helpers=array('Text');
      public $components=array();
  
      public $uses=array('Payment');
  
      var $paypalUrl="https://sandbox.paypal.com/webscr?cmd=_ap-payment&amp;paykey=";//send  the user here after all is setup
      .
      .
      .
      public function pay(){
         $amount1=10.50;
         $email1="client@email.com";
         $amount2=15.80;
         $email2="admin@email.com";
         $finalUrl="http://example.com/successful";
         $cancelUrl="http://example.com/failed";
         $payKey=$this-&gt;Paypal-&gt;splitPay($amount1,$email1,$amount2,$email2,$cancelUrl,$finalUrl);
         $this-&gt;redirect($this-&gt;paypalUrl.$payKey);
      }
      .
      .
      .
  }
  </code></pre>
  
  <p>I put the code for accessing paypal in a model "Payment". Here is it :</p>
  
  <pre><code>&lt;?php
      App::uses('AuthComponent','Controller/Component');
      class Paypal extends AppModel{
        function __construct(){
             $this-&gt;headers=array(
                "X-PAYPAL-SECURITY-USERID: ".PAYPAL_ID,
                "X-PAYPAL-SECURITY-PASSWORD: ".PASSWORD,
                "X-PAYPAL-SECURITY-SIGNATURE: ".SIGNATURE,
                "X-PAYPAL-REQUEST-DATA-FORMAT: JSON",
                "X-PAYPAL-RESPONSE-DATA-FORMAT: JSON",
                "X-PAYPAL-APPLICATION-ID: ".APPLICANT_ID
             );
             $this-&gt;envelope=array(
                "errorLanguage"=&gt;"en-US",
                "detailLevel"=&gt;"ReturnAll"
             );
        }
  
        //wrapper for getting payment details
        function getPaymentOptions($paykey){
            $packet=array(
               "requestEnvelope"=&gt;$this-&gt;envelope,
               "payKey"=&gt;$paykey
            );
  
            return $this-&gt;_paypalSend($packet,"GetPaymentOptions");
        }
  
        //set payment details
        function _paypalSend($data,$call){
            $ch=curl_init();
            curl_setopt($ch,CURLOPT_URL,$this-&gt;apiUrl.$call);
            curl_setopt($ch,CURLOPT_RETURNTRANSFER,TRUE);
            curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
            curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
            curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode($data));
            curl_setopt($ch,CURLOPT_HTTPHEADER,$this-&gt;headers);
  
            return json_decode(curl_exec($ch),TRUE);
        }
  
        //this is where we do work
        function splitPay($amount1,$email1,$amount2,$email2,$cancelUrl,$finalUrl){
            //create the pay request
            $createPacket=array(
               "actionType"=&gt;"PAY",
               "currencyCode"=&gt;"USD",
               "receiverList"=&gt;array(
                   "receiver"=&gt;array(
                       array(
                          "amount"=&gt;$amount1,
                          "email"=&gt;$email1
                       ),
                       array(
                          "amount"=&gt;$amount2,
                          "email"=&gt;$email2
                       ),
                   )
               ),
               "returnUrl"=&gt;$finalUrl,
               "cancelUrl"=&gt;$cancelUrl,
               "requestEnvelope"=&gt;$this-&gt;envelope
            );
            $response=$this-&gt;_paypalSend($createPacket,"Pay");
            $payKey=$response['payKey'];
  
            return $payKey;
        }
  </code></pre>
  
  <p>I don't understand what is the wrong here.</p>