Tuesday, March 22, 2016

PayPal - Adaptive Chained Payments

<?php

class PaypalTest {

public $api_user = "XXXXXXXXXXXXXXXXXXXXXX";
public $api_pass = "XXXXXXXXXXXXXXXXX";
public $api_sig = "XXXXXXXXXXXXXXXXXXXXXXX";
public $app_id = "APP-80W284485P519543T";
public $apiUrl = 'https://svcs.sandbox.paypal.com/AdaptivePayments/';
//public $paypalUrl="https://www.paypal.com/webscr?cmd=_ap-payment&paykey=";
public $paypalUrl="https://sandbox.paypal.com/webscr?cmd=_ap-payment&paykey=";
public $headers;

public function __construct(){
    $this->headers = array(
        "X-PAYPAL-SECURITY-USERID: ".$this->api_user,
        "X-PAYPAL-SECURITY-PASSWORD: ".$this->api_pass,
        "X-PAYPAL-SECURITY-SIGNATURE: ".$this->api_sig,
        "X-PAYPAL-REQUEST-DATA-FORMAT: JSON",
        "X-PAYPAL-RESPONSE-DATA-FORMAT: JSON",
        "X-PAYPAL-APPLICATION-ID: ".$this->app_id,
    );
  
    $this->envelope= array(
            "errorLanguage" => "en_US",
            "detailLevel" => "ReturnAll",
        );
  
  
}

public function getPaymentOptions($paykey){

//echo $paykey; die;
   $packet = array(
          "requestEnvelope" => $this->envelope ,
          "payKey" => $paykey
   );
 
   return $this->_paypalSend( $packet ,'GetPaymentOptions');
}

public function _paypalSend($data,$call){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $this->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->headers);
    $response = json_decode(curl_exec($ch),true);
    return $response;

}

public function splitPay(){


    // create the pay request
    $createPacket = array(
        "actionType" =>"PAY_PRIMARY",
        "applicationId" =>"APP-80W284485P519543T",
        "currencyCode" => "USD",
        "receiverList" => array(
            "receiver" => array(
               array(
                    "amount"=> "7.00",
                    "primary" => true,
                    "email"=>"primary-reciever@gmail.com"
                ),
                array(
                    "amount"=> "3.00",
                    "primary" => false,
                    "email"=>"secondary-reciever@gmail.com"
                ),
            ),
        ),
        "returnUrl" => "http://your-site-url/confirm.php",
        "cancelUrl" => "http://your-site-url//cancel.php",
        "requestEnvelope" => $this->envelope
    );

    $response = $this->_paypalSend($createPacket,"Pay");
  
  
    
    $paykey =  $response['payKey'];
    //echo '9999<pre>'; print_r($response);
  
    //SET payment details
  
     $detailsPacket=array(
"requestEnvelope" =>$this->envelope,
"payKey" => $paykey,
"receiverOptions" => array(
    array(
    "receiver" => array("email" => "primary-reciever@gmail.com"),
            "invoiceData" => array(
                "item" => array(
                    array(
                        "name" => "product1",
                        "price" => "7.00",
                        "identifier" => "p1"
                    ),
                   )
            )
    ),
    array(
        "receiver" => array("email" => 'secondary-reciever@gmail.com'),
        "invoiceData" => array(
        "item" => array(
            array(
                "name" => "product1",
                "price" => "3.00",
                "identifier" => "p1"
            ),
        )
      )
    ),
)
);


    $response1 = $this->_paypalSend($detailsPacket,"SetPaymentOptions");
  
  
    //$dets= $this->getPaymentOptions($paykey);
    $dets= $this->getPaymentOptions($paykey);
  
  

    $msg = $paykey;
  
    mail("amu02.aftab@gmail.com","My subject",$msg);
    //echo $this->paypalUrl.$paykey; die;
    header("Location: ".$this->paypalUrl.$paykey);
}

    public function _paymentExecute($url, $data){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        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->headers);
        $response = json_decode(curl_exec($ch),true);
        return $response;
    }

}


if($_GET['exe_pay'] == 1){
  
    $url = "https://svcs.sandbox.paypal.com/AdaptivePayments/ExecutePayment";
  
    $payment = new PaypalTest();
    
    $packet = array(
          "requestEnvelope" => $payment->envelope,
          "payKey" => "AP-96831732KV5898725"
   );
    $response = $payment->_paymentExecute($url, $packet);
    echo '<pre>';print_r($response);
}elseif($_GET['get_info'] == 1){
    $url = "https://svcs.sandbox.paypal.com/AdaptivePayments/PaymentDetails";
  
    $payment = new PaypalTest();
  
    $packet = array(
          "requestEnvelope" => $payment->envelope,
          "payKey" => "AP-96831732KV5898725"
   );
    $response = $payment->_paymentExecute($url, $packet);
    echo '<pre>';print_r($response);
}else{
      
      
    $payment = new PaypalTest();
    $payment->splitPay();
  
}?>

No comments :

Post a Comment