Magento Parallel Payment (Adaptive paypal payment)

Redirect.php (Block file)

<?php
class Biz_Paymentstd_Block_Standard_Redirect extends Mage_Paypal_Block_Standard_Redirect
{
    protected function _toHtml()
    {
   $actionType = "PAY";
   $cancelUrl = "https://mycancelurl"; // TODO - If you are not executing the Pay call
            // for a preapproval, then you must set a valid
            // cancelUrl for the web approval flow that
            // immediately follows this Pay call
   $returnUrl = "https://myreturnurl"; // TODO - If you are not executing the Pay call
            // for a preapproval, then you must set a valid
            // returnUrl for the web approval flow that
            // immediately follows this Pay call
   $currencyCode = "USD";
  
   // A parallel payment can be made among two to six receivers
   // TODO - Specify the receiver emails
   //        Remove or set to an empty string the array entries for receivers that you
   //        do not have
   $receiverEmailArray = array(
     'jaybusiness@thylaksoft.com',
     'jayakumar@thylaksoft.com'
    
    );
  
   // TODO - Specify the receiver amounts as the amount of money, for example, '5' or '5.55'
   //        Remove or set to an empty string the array entries for receivers that you
   //        do not have
   $receiverAmountArray = array(
     '5',
     '3'
    );
  
   // For parallel payment, no primary indicators are needed, so set empty array.
   $receiverPrimaryArray = array();
  
   // TODO - Set invoiceId to uniquely identify the transaction associated with
   //        each receiver
   //        Set the array entries with value for receivers that you have
   //        Each of the array values must be unique
   $receiverInvoiceIdArray = array(
     '',
     '',
     '',
     '',
     '',
     ''
     );
  
   // Request specific optional fields
   //   Provide a value for each field that you want to include in the request;
   //   if left as an empty string, the field will not be passed in the request
   $senderEmail = ""; // TODO - If you are executing the Pay call against a
          // preapprovalKey, you should set senderEmail
          // It is not required if the web approval flow immediately
          // follows this Pay call
   $feesPayer = "";
   $ipnNotificationUrl = "";
   $memo = ""; // maxlength is 1000 characters
   $pin = ""; // TODO - If you are executing the Pay call against an existing
        // preapproval that requires a pin, then you must set this
   $preapprovalKey = ""; // TODO - If you are executing the Pay call against
          // an existing preapproval, set the preapprovalKey here
   $reverseAllParallelPaymentsOnError = ""; // TODO - Set this to "true" if you would
              // like each parallel payment to be reversed
              // if an error occurs
              // Defaults to "false" if you don't specify
   //echo "asfasdf"; exit;
   $trackingId = Mage :: helper ('paypalpayment')->generateTrackingID(); // generateTrackingID function is found
            // in paypalplatform.php
  
  
 
   //-------------------------------------------------
   // Make the Pay API call
   //
   // The CallPay function is defined in the paypalplatform.php file,
   // which is included at the top of this file.
   //-------------------------------------------------
   $resArray = Mage :: helper ('paypalpayment')->CallPay ($actionType, $cancelUrl, $returnUrl, $currencyCode,
      $receiverEmailArray, $receiverAmountArray, $receiverPrimaryArray,
      $receiverInvoiceIdArray, $feesPayer, $ipnNotificationUrl, $memo,
      $pin, $preapprovalKey, $reverseAllParallelPaymentsOnError,
      $senderEmail, $trackingId
   );
  // echo "test".$resArray;
  // print_r($resArray);exit;
    $ack = strtoupper($resArray["responseEnvelope.ack"]);
  
   //exit;
   if($ack=="SUCCESS")
   {
     // redirect for web approval flow
     $cmd .= "cmd=_ap-payment&paykey=" . urldecode($resArray["payKey"]);
     Mage :: helper ('paypalpayment')->RedirectToPayPal ( $cmd );
   }
   else 
   {
    //Display a user-friendly Error on the page using any of the following error
    //information returned by PayPal.
    //TODO - There can be more than 1 error, so check for "error(1).errorId",
    //       then "error(2).errorId", and so on until you find no more errors.
    $ErrorCode = urldecode($resArray["error(0).errorId"]);
    $ErrorMsg = urldecode($resArray["error(0).message"]);
    $ErrorDomain = urldecode($resArray["error(0).domain"]);
    $ErrorSeverity = urldecode($resArray["error(0).severity"]);
    $ErrorCategory = urldecode($resArray["error(0).category"]);
   
    echo "Preapproval API call failed. ";
    echo "Detailed Error Message: " . $ErrorMsg;
    echo "Error Code: " . $ErrorCode;
    echo "Error Severity: " . $ErrorSeverity;
    echo "Error Domain: " . $ErrorDomain;
    echo "Error Category: " . $ErrorCategory;
   }
  }
}

?>


Helper data.php

<?php
class Biz_Paymentstd_Helper_Data extends Mage_Core_Helper_Abstract
{
 function generateCharacter () {
  $possible = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
  return $char;
 }

 function generateTrackingID () {
 $GUID = $this->generateCharacter().$this->generateCharacter().$this->generateCharacter().$this->generateCharacter().$this->generateCharacter();
$GUID .= $this->generateCharacter().$this->generateCharacter().$this->generateCharacter().$this->generateCharacter();
 return $GUID;
 }

 /*
 '-------------------------------------------------------------------------------
 ' Purpose: Prepares the parameters for the Refund API Call.
 '   The API credentials used in a Pay call can make the Refund call
 '   against a payKey, or a tracking id, or to specific receivers of a payKey or
 '   a tracking id that resulted from the Pay call.
 '
 '   A receiver itself with its own API credentials can make a Refund call against
 '   the transactionId corresponding to their transaction.
 '   The API credentials used in a Pay call cannot use transactionId to issue a refund
 '   for a transaction for which they themselves were not the receiver.
 '
 '   If you do specify specific receivers, you must provide the amounts as well.
 '   If you specify a transactionId, then only the receiver of that transactionId
 '   is affected. Therefore the receiverEmailArray and receiverAmountArray should
 '   have 1 entry each if you do want to give a partial refund.
 ' Inputs:
 '
 ' Conditionally Required:
 '  One of the following:  payKey or trackingId or trasactionId or
 '                         (payKey and receiverEmailArray and receiverAmountArray) or
 '                         (trackingId and receiverEmailArray and receiverAmountArray)
 '                         or (transactionId and receiverEmailArray
 '                         and receiverAmountArray)
 ' Returns:
 '  The NVP Collection object of the Refund call response.
 '------------------------------------------------------------------------------------
 */
 function CallRefund( $payKey, $transactionId, $trackingId,
          $receiverEmailArray, $receiverAmountArray )
 {
  /* Gather the information to make the Refund call.
   The variable nvpstr holds the name-value pairs.
  */

  $nvpstr = "";

  // conditionally required fields
  if ("" != $payKey)
  {
   $nvpstr = "payKey=" . urlencode($payKey);
   if (0 != count($receiverEmailArray))
   {
    reset($receiverEmailArray);
    while (list($key, $value) = each($receiverEmailArray))
    {
     if ("" != $value)
     {
      $nvpstr .= "&receiverList.receiver(" . $key . ").email=" . urlencode($value);
     }
    }
   }
   if (0 != count($receiverAmountArray))
   {
    reset($receiverAmountArray);
    while (list($key, $value) = each($receiverAmountArray))
    {
     if ("" != $value)
     {
      $nvpstr .= "&receiverList.receiver(" . $key . ").amount=" . urlencode($value);
     }
    }
   }
  }
  elseif ("" != $trackingId)
  {
   $nvpstr = "trackingId=" . urlencode($trackingId);
   if (0 != count($receiverEmailArray))
   {
    reset($receiverEmailArray);
    while (list($key, $value) = each($receiverEmailArray))
    {
     if ("" != $value)
     {
      $nvpstr .= "&receiverList.receiver(" . $key . ").email=" . urlencode($value);
     }
    }
   }
   if (0 != count($receiverAmountArray))
   {
    reset($receiverAmountArray);
    while (list($key, $value) = each($receiverAmountArray))
    {
     if ("" != $value)
     {
      $nvpstr .= "&receiverList.receiver(" . $key . ").amount=" . urlencode($value);
     }
    }
   }
  }
  elseif ("" != $transactionId)
  {
   $nvpstr = "transactionId=" . urlencode($transactionId);
   // the caller should only have 1 entry in the email and amount arrays
   if (0 != count($receiverEmailArray))
   {
    reset($receiverEmailArray);
    while (list($key, $value) = each($receiverEmailArray))
    {
     if ("" != $value)
     {
      $nvpstr .= "&receiverList.receiver(" . $key . ").email=" . urlencode($value);
     }
    }
   }
   if (0 != count($receiverAmountArray))
   {
    reset($receiverAmountArray);
    while (list($key, $value) = each($receiverAmountArray))
    {
     if ("" != $value)
     {
      $nvpstr .= "&receiverList.receiver(" . $key . ").amount=" . urlencode($value);
     }
    }
   }
  }

  /* Make the Refund call to PayPal */
  $resArray = $this->hash_call("Refund", $nvpstr);

  /* Return the response array */
  return $resArray;
 }

 /*
 '------------------------------------------------------------------------------------
 ' Purpose: Prepares the parameters for the PaymentDetails API Call.
 '   The PaymentDetails call can be made with either
 '   a payKey, a trackingId, or a transactionId of a previously successful Pay call.
 ' Inputs:
 '
 ' Conditionally Required:
 '  One of the following:  payKey or transactionId or trackingId
 ' Returns:
 '  The NVP Collection object of the PaymentDetails call response.
 '------------------------------------------------------------------------------------
 */
 function CallPaymentDetails( $payKey, $transactionId, $trackingId )
 {
  /* Gather the information to make the PaymentDetails call.
   The variable nvpstr holds the name-value pairs.
  */

  $nvpstr = "";

  // conditionally required fields
  if ("" != $payKey)
  {
   $nvpstr = "payKey=" . urlencode($payKey);
  }
  elseif ("" != $transactionId)
  {
   $nvpstr = "transactionId=" . urlencode($transactionId);
  }
  elseif ("" != $trackingId)
  {
   $nvpstr = "trackingId=" . urlencode($trackingId);
  }

  /* Make the PaymentDetails call to PayPal */
  $resArray = $this->hash_call("PaymentDetails", $nvpstr);

  /* Return the response array */
  return $resArray;
 }

 /*
 '------------------------------------------------------------------------------------
 ' Purpose:  Prepares the parameters for the Pay API Call.
 ' Inputs:
 '
 ' Required:
 '
 ' Optional:
 '
 ' Returns:
 '  The NVP Collection object of the Pay call response.
 '------------------------------------------------------------------------------------
 */
 function CallPay( $actionType, $cancelUrl, $returnUrl, $currencyCode,
     $receiverEmailArray, $receiverAmountArray, $receiverPrimaryArray,
     $receiverInvoiceIdArray, $feesPayer, $ipnNotificationUrl, $memo,
     $pin, $preapprovalKey, $reverseAllParallelPaymentsOnError,
     $senderEmail, $trackingId )
 {
  /* Gather the information to make the Pay call.
   The variable nvpstr holds the name-value pairs.
  */

  // required fields

  $nvpstr = "actionType=" . urlencode($actionType) . "&currencyCode="; 
  $nvpstr .= urlencode($currencyCode) . "&returnUrl=" . urlencode($cancelUrl);
  $nvpstr .= urlencode($returnUrl) . "&cancelUrl=" . urlencode($cancelUrl);

  if (0 != count($receiverAmountArray))
  {
   reset($receiverAmountArray);
   while (list($key, $value) = each($receiverAmountArray))
   {
    if ("" != $value)
    {
     $nvpstr .= "&receiverList.receiver(" . $key . ").amount=" . urlencode($value);
    }
   }
  }

  if (0 != count($receiverEmailArray))
  {
   reset($receiverEmailArray);
   while (list($key, $value) = each($receiverEmailArray))
   {
    if ("" != $value)
    {
     $nvpstr .= "&receiverList.receiver(" . $key . ").email=" . urlencode($value);
    }
   }
  }

  if (0 != count($receiverPrimaryArray))
  {
   reset($receiverPrimaryArray);
   while (list($key, $value) = each($receiverPrimaryArray))
   {
    if ("" != $value)
    {
     $nvpstr = $nvpstr . "&receiverList.receiver(" . $key . ").primary=" .
             urlencode($value);
    }
   }
  }

  if (0 != count($receiverInvoiceIdArray))
  {
   reset($receiverInvoiceIdArray);
   while (list($key, $value) = each($receiverInvoiceIdArray))
   {
    if ("" != $value)
    {
     $nvpstr = $nvpstr . "&receiverList.receiver(" . $key . ").invoiceId=" .
               urlencode($value);
    }
   }
  }
 $resArray = $this->hash_call("Pay", $nvpstr);
  return $resArray;
 }



 /**
   '----------------------------------------------------------------------------------
   * hash_call: Function to perform the API call to PayPal using API signature
   * @methodName is name of API method.
   * @nvpStr is nvp string.
   * returns an associative array containing the response from the server.
   '----------------------------------------------------------------------------------
 */
 function hash_call($methodName_, $nvpStr_)
 {  //declaring of global variables
 // global $API_Endpoint, $API_UserName, $API_Password, $API_Signature, $API_AppID;
  //global $USE_PROXY, $PROXY_HOST, $PROXY_PORT;

 $Sandbox = Mage::getStoreConfig('paypal/wpp/sandbox_flag');

 if($Sandbox == 1)
 {
  $url = "https://svcs.sandbox.paypal.com/AdaptivePayments/Pay";
 }else
 {
  $url = "https://svcs.paypal.com/AdaptivePayments/Pay";
 }
 $API_UserName = Mage::getStoreConfig('paypal/wpp/api_username');
    $API_Password = Mage::getStoreConfig('paypal/wpp/api_password');
    $API_Signature = Mage::getStoreConfig('paypal/wpp/api_signature');
    $API_AppID = "APP-80W284485P519543T";

      //global $Env;
          
            // Set up your API credentials, PayPal end point, and API version.
            $this->API_Endpoint .= "/" . $methodName;
          
            // Set the curl parameters.
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_VERBOSE, 1);
      
            // Turn off the server and peer verification (TrustManager Concept).
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
      
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch,CURLOPT_POSTFIELDS,$request);
            curl_setopt($ch, CURLOPT_HTTPHEADER,  array(
                'X-PAYPAL-REQUEST-DATA-FORMAT: NV',
                'X-PAYPAL-RESPONSE-DATA-FORMAT: NV',
                'X-PAYPAL-SECURITY-USERID: '.$API_UserName,
                'X-PAYPAL-SECURITY-PASSWORD: '.$API_Password,
                'X-PAYPAL-SECURITY-SIGNATURE: '.$API_Signature,
                'X-PAYPAL-SERVICE-VERSION: 1.3.0',
                'X-PAYPAL-APPLICATION-ID: '.$API_AppID
                ));
            if($USE_PROXY)
            curl_setopt ($ch, CURLOPT_PROXY, $PROXY_HOST. ":" . $PROXY_PORT);

        // RequestEnvelope fields
        $detailLevel    = urlencode("ReturnAll");    // See DetailLevelCode in the WSDL for valid enumerations
        $errorLanguage    = urlencode("en_US");        // This should be the standard RFC 3066 language identification tag, e.g., en_US

        // NVPRequest for submitting to server
   
     $nvpreq = "requestEnvelope.errorLanguage=$errorLanguage&requestEnvelope.detailLevel=$detailLevel";
        $nvpreq .= "&$nvpStr_";



        //setting the nvpreq as POST FIELD to curl
        curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq);

        //getting response from server
        $response = curl_exec($ch);

        //converting NVPResponse to an Associative Array
        $nvpResArray=$this->deformatNVP($response);
      
        $nvpReqArray=$this->deformatNVP($nvpreq);
        $_SESSION['nvpReqArray']=$nvpReqArray;

        if (curl_errno($ch))
        {
            // moving to display page to display curl errors
        echo      $_SESSION['curl_error_no']=curl_errno($ch) ;
        echo      $_SESSION['curl_error_msg']=curl_error($ch);

              //Execute the Error handling module to display errors.
        }
        else
        {
             //closing the curl
              curl_close($ch);
        }
        return $nvpResArray;
 }

 /*'----------------------------------------------------------------------------
  Purpose: Redirects to PayPal.com site.
  Inputs:  $cmd is the querystring
  Returns:
 -------------------------------------------------------------------------------
 */
 function RedirectToPayPal ( $cmd )
 {
  // Redirect to paypal.com here
 $payPalURL = "";
 $Sandbox = Mage::getStoreConfig('paypal/wpp/sandbox_flag');

 if($Sandbox == 1)
 {
  $payPalURL = "https://www.sandbox.paypal.com/webscr?" . $cmd;
 }else
 {
  $payPalURL = "https://www.paypal.com/webscr?" . $cmd;
 }
 echo "<script>window.location = '$payPalURL'</script>";
 //echo $payPalURL;
 //header( "Location: ".$payPalURL );
 // header("Location: ".$payPalURL);

 }


 /*'----------------------------------------------------------------------------
   * This function will take NVPString and convert it to an Associative Array
   * and then will decode the response.
   * It is useful to search for a particular key and display arrays.
   * @nvpstr is NVPString.
   * @nvpArray is Associative Array.
    ----------------------------------------------------------------------------
   */
 function deformatNVP($nvpstr)
 {
  $intial=0;
  $nvpArray = array();

  while(strlen($nvpstr))
  {
   //postion of Key
   $keypos= strpos($nvpstr,'=');
   //position of value
   $valuepos = strpos($nvpstr,'&') ? strpos($nvpstr,'&'): strlen($nvpstr);

   /*getting the Key and Value values and storing in a Associative Array*/
   $keyval=substr($nvpstr,$intial,$keypos);
   $valval=substr($nvpstr,$keypos+1,$valuepos-$keypos-1);
   //decoding the respose
   $nvpArray[urldecode($keyval)] =urldecode( $valval);
   $nvpstr=substr($nvpstr,$valuepos+1,strlen($nvpstr));
      }
  return $nvpArray;

 }
}?>


Config.xml (overwrite)

<?xml version="1.0"?>
<config>
    <modules>
        <Biz_Paymentstd>
            <version>0.1.0</version>
        </Biz_Paymentstd>
    </modules>
   
    <global>
        <blocks>
            <paymentstd>
                <class>Biz_Paymentstd_Block</class>
            </paymentstd>
   <paypal>
   <rewrite>
                    <standard_redirect>Biz_Paymentstd_Block_Standard_Redirect</standard_redirect>
                </rewrite>
   </paypal>
        </blocks>
   <helpers>
           <paypalpayment>
                <class>Biz_Paymentstd_Helper</class>
            </paypalpayment>
        </helpers>
 </global>
</config>



Share this

Related Posts

Previous
Next Post »

Pageviews from the past week