kkuni
kkuni

Reputation: 67

magento - integrating paypal adaptive payments (chained payments)

I am currently working on integrating paypal's chained payment method into magento. https://cms.paypal.com/ca/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_api_APIntro

The payment flow would be: buyer pays seller's paypal account -> pp adaptive payment gateway -> 85% goes to seller's paypal, 15% goes to site's default paypal account (buyer not aware of this split).

I already have the api function take takes 2 paypal accounts (seller's & site's default), and payment amount, and am looking to integrate this.

Has anyone integrated adaptive payments before, or point me to where I should integrate this logic? Would I overwrite one of the functions in /app/code/core/Mage/paypal ?

I basically need to get the total cost in the current shopping cart, and the paypal email of the current store, and pass that over to my function.

Upvotes: 0

Views: 1489

Answers (2)

webkul
webkul

Reputation: 2864

First of all you need to create a seprate payment method for magento i would suggest to create a payment module for it after that you need to signup in to the paypal sandbox account . i am attaching sample code for adaptive payment integration also some useful links for the flow how it will work

ini_set("track_errors", true);
//set PayPal Endpoint to sandbox
$sandbox="";
$API_AppID = XXXXXXXXXXXXXXXXXXX;//your adaptive payment app Id
//value for check sandbox enable or disable
$sandboxstatus=1;
if($sandboxstatus==1){
    $sandbox="sandbox.";
    $API_AppID="APP-80W284485P519543T";
}
$url = trim("https://svcs.".$sandbox."paypal.com/AdaptivePayments/Pay");
//PayPal API Credentials
$API_UserName = XXXXXXXXXXXXXXXXXXX;//TODO
$API_Password = XXXXXXXXXXXXXXXXXXX;//TODO
$API_Signature = XXXXXXXXXXXXXXXXXXX;//TODO 
//Default App ID for Sandbox    
$API_RequestFormat = "NV";
$API_ResponseFormat = "NV";

$bodyparams = array (
    "requestEnvelope.errorLanguage" => "en_US",
    "actionType" => "PAY",
    "currencyCode" => "USD",//currency Code
    "cancelUrl" => "",// cancle url
    "returnUrl" => "paymentsuccess",//return url
    "ipnNotificationUrl" => "paymentnotify"//notification url that return all data related to payment
);

$finalcart=array(
        array('paypalid'=>"partner1",'price'=>50),
        array('paypalid'=>"partner2",'price'=>50),
        array('paypalid'=>"partner3",'price'=>50),
        array('paypalid'=>"partner4",'price'=>50),
        array('paypalid'=>"partner5",'price'=>50)
      );

$i=0;
foreach($finalcart as $partner){
    $temp=array("receiverList.receiver($i).email"=>$partner['paypalid'],"receiverList.receiver($i).amount"=>$partner['price']);
    $bodyparams+=$temp; 
    $i++;
}

// convert payload array into url encoded query string
$body_data = http_build_query($bodyparams, "", chr(38));
try{
   //create request and add headers
   $params = array("http" => array( 
      "method" => "POST",
      "content" => $body_data,
      "header" => "X-PAYPAL-SECURITY-USERID: " . $API_UserName . "\r\n" .
                     "X-PAYPAL-SECURITY-SIGNATURE: " . $API_Signature . "\r\n" .
                     "X-PAYPAL-SECURITY-PASSWORD: " . $API_Password . "\r\n" .
                     "X-PAYPAL-APPLICATION-ID: " . $API_AppID . "\r\n" .
                     "X-PAYPAL-REQUEST-DATA-FORMAT: " . $API_RequestFormat . "\r\n" .
                     "X-PAYPAL-RESPONSE-DATA-FORMAT: " . $API_ResponseFormat . "\r\n"
   ));
   //create stream context
   $ctx = stream_context_create($params);
   //open the stream and send request
   $fp = @fopen($url, "r", false, $ctx);
   //get response
   $response = stream_get_contents($fp);
   //check to see if stream is open
   if ($response === false) {
      throw new Exception("php error message = " . "$php_errormsg");
   }
   //close the stream
   fclose($fp);
   //parse the ap key from the response
   $keyArray = explode("&", $response);
   foreach ($keyArray as $rVal){
       list($qKey, $qVal) = explode ("=", $rVal);
       $kArray[$qKey] = $qVal;
   }
   //set url to approve the transaction
   $payPalURL = "https://www.".$sandbox."paypal.com/webscr?cmd=_ap-payment&paykey=" . $kArray["payKey"];
   //print the url to screen for testing purposes
   If ( $kArray["responseEnvelope.ack"] == "Success") {
    echo '<p><a id="paypalredirect" href="' . $payPalURL . '"> Click here if you are not redirected within 10 seconds...</a> </p>';
    echo '<script type="text/javascript"> 
            function redirect(){
           document.getElementById("paypalredirect").click();
            }
            setTimeout(redirect, 2000);
              </script>';
   }
   else {
     echo 'ERROR Code: ' .  $kArray["error(0).errorId"] . " <br/>";
     echo 'ERROR Message: ' .  urldecode($kArray["error(0).message"]) . " <br/>";
  }
}
catch(Exception $e) {
    echo "Message: ||" .$e->getMessage()."||";
}

you may ignore the module flow but you can follow the steps for setting up the sandbox account for paypal payment hope it will help

Upvotes: 1

Drew Angell
Drew Angell

Reputation: 26056

You're probably going to need to write a completely new payment module for that. From what I've seen (and I'll admit it's been a little bit since I've looked at it in detail) the current PayPal integration in Magento does not support chained payments.

You could still extend the existing PayPal module(s) if you want, but you'll need to write the API requests to handle the adaptive payments calls accordingly. Again, there aren't any existing functions that you would simply override in your extended module. You'll just need to create your own from the start.

If the latest version of Magento added some adaptive payments then I could be wrong on that. If that's the case, you'd probably see something in the /paypal directory directly referring to it, and you can study the functions in there to see what you could override with your own. That said, if they've already adaptive payments included as a payment module then you really shouldn't need to customize it in code.

Upvotes: 0

Related Questions