cola cola - 4 years ago 200
PHP Question

PayPal Chained Payment Help [PHP]

I'm just trying to do a test with paypal chained payments, and it's really frustrating. My goal for this test is to send the primary receiver $15 and then $1 to a secondary receiver. Here is my code:

$api = "https://svcs.sandbox.paypal.com/AdaptivePayments/Pay";

$input = array(
"actionType" => "PAY",
"currencyCode" => "USD",
"feesPayer" => "EACHRECEIVER",
"cancelUrl" => "https://www.google.com", //test url
"returnUrl" => "https://www.google.com", //test url
"receiverList" => array(
"receiver" => array( //send primary receiver $15
"amount" => "15.00",
"email" => "rbxseller1@gmail.com",
"primary" => true
),
"receiver" => array( //send owner of site $1 commission
"amount" => "1.00",
"email" => "rbxowner@gmail.com",
"primary" => false
)
),
"requestEnvelope" => array(
"errorLanguage" => "en_US"
)
);

$headers = array(
"X-PAYPAL-SECURITY-USERID: ".USER_ID, //predefined
"X-PAYPAL-SECURITY-PASSWORD: ".USER_PASS, //predefined
"X-PAYPAL-SECURITY-SIGNATURE: ".USER_SIG, //predefined
"X-PAYPAL-REQUEST-DATA-FORMAT: JSON",
"X-PAYPAL-RESPONSE-DATA-FORMAT: JSON",
"X-PAYPAL-APPLICATION-ID: APP-80W284485P519543T"
);

$ch = curl_init($api);

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($input));

$response = curl_exec($ch);
var_dump($response);


When I try to do this, it works, but in my payment details it only shows the $1 to the secondary receiver, there is no trace of the primary receiver:


{"paymentInfo":[{"receiver":{"amount":"1.00","email":"rbxowner@gmail.com","primary":"false","paymentType":"SERVICE","accountId":"6LBSVJQNVE9DA"},"pendingRefund":"false"}]}


I tried setting the "actionType" to "PAY_PRIMARY" and it gave me this error:


"message":"Invalid request parameter: action type PAY_PRIMARY can only be used in chained payments","parameter":["PAY_PRIMARY"]


It's getting quite frustrating as I've looked it up on youtube, stackoverflow, some forum websites, etc and haven't found much helpful info.

Thank you very much to anyone who will take the time to read this and help me!

Answer Source

Your mistake is in the PHP array syntax here:

"receiverList" => array(    
    "receiver" => array(/*primary receiver info*/),
    "receiver" => array(/*secondary receiver info*/)
),

An associative array can only have one value for each key (to picture why, imagine accessing $receiverList['receiver'] from an array declared this way; it wouldn't know which you wanted).

To PHP, this is the same as writing $foo = 1; $foo = 2; and expecting both 1 and 2 to still be "there" somewhere. So all that gets sent to Paypal is this:

"receiverList" => array(    
    "receiver" => array(/*secondary receiver info*/)
),

You can see this for yourself if you echo out json_encode($input).

I don't know what the array should look like, but it's definitely not that. My best guess without seeing the documentation would be a simple list with no keys specified:

"receiverList" => array(    
    array(/*primary receiver info*/),
    array(/*secondary receiver info*/)
),

Or possibly the "recevier" key needs to be there, and the two entries are inside that:

"receiverList" => array(
    "receiver" => array(
        array(/*primary receiver info*/),
        array(/*secondary receiver info*/)
    )
),

Or perhaps you've misread the docs and there is no "receiverList" key, only "receiver":

"receiver" => array(    
    array(/*primary receiver info*/),
    array(/*secondary receiver info*/)
),

Whichever variant it is, there's no point changing the rest of your query until you get this bit right, because right now you are only sending one set of receiver details to Paypal.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download