magento paypal chained payment or adaptive payment
<?php
class Pixel_Paypalpayment_Block_Standard_Redirect extends Mage_Core_Block_Abstract
{
protected function _toHtml()
{
$standard = Mage::getModel('paypalpayment/standard');
$form = new Varien_Data_Form();
$trans = $standard->getStandardCheckoutFormFields();
$orders = Mage::getModel('sales/order')->loadByIncrementId($trans['Notes']);
$marging_percent = Mage::getStoreconfig('payment/paypalpayment_standard/margin_ammount');
$marging_ammount = ($orders->getBaseSubtotal() * $marging_percent)/100;
foreach ($orders->getAllItems() as $items)
{
$_product = Mage::getSingleton('catalog/product')->load($items->getProductId());
$vendorid = $_product->getVendorid();
}
$vendormodel = Mage::getModel('vendorpayment/vendorpayment')->load($vendorid);
$primary_seller = $vendormodel->getEmail();
$secondary_seller = Mage::getStoreconfig('payment/paypalpayment_standard/merchant_email');
$buyer_email = $trans['CustomerEmail'];
$total_amount = $trans['TransactionAmount'];
$sandbox = Mage::getStoreconfig('payment/paypalpayment_standard/sandbox');
$actionType = "PAY";
$cancelUrl = $trans['RedirectorFailed'];
$returnUrl = $trans['RedirectorSuccess'];
$currencyCode = "USD";
$receiverEmailArray = array($primary_seller, $secondary_seller);
$receiverAmountArray = array($total_amount, $marging_ammount);
$receiverPrimaryArray = array('true');
$receiverInvoiceIdArray = array('','');
$senderEmail = $buyer_email;
$feesPayer = "PRIMARYRECEIVER";
$ipnNotificationUrl = "";
$memo = "";
$pin = "";
$preapprovalKey = "";
$reverseAllParallelPaymentsOnError = "";
$trackingId = Mage :: helper ('paypalpayment')->generateTrackingID();
$resArray = Mage::helper('paypalpayment')->CallPay($actionType, $cancelUrl, $returnUrl, $currencyCode,
$receiverEmailArray,$receiverAmountArray, $receiverPrimaryArray, $receiverInvoiceIdArray,$feesPayer,
$ipnNotificationUrl, $memo, $pin, $preapprovalKey, $reverseAllParallelPaymentsOnError, $senderEmail,
$trackingId);
$cmd = "cmd=_ap-payment&paykey=".urldecode($resArray["payKey"]);
$payPalURL = "";
if ($sandbox == 1)
{
$payPalURL = "https://www.sandbox.paypal.com/webscr?" . $cmd;
}
else
{
$payPalURL = "https://www.paypal.com/webscr?" . $cmd;
}
$form->setAction($payPalURL)
->setId('paypalpayment_standard_checkout')
->setName('paypalpayment_standard_checkout')
->setMethod('POST')
->setUseContainer(true);
$html = '<html><body>';
$html .= $this->__('You will be redirected to Paypal in a few seconds.');
$html .= $form-> toHtml();
$html .= '<script type="text/javascript">document.getElementById("paypalpayment_standard_checkout").submit();
</script>';
$html .= '</body></html>';
return $html;
}
}