Reading list Switch to dark mode

    Implement Adaptive Payments

    Updated 23 February 2024

    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()."||";
    }

    Start your headless eCommerce
    now.
    Find out More
    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home