class Paypal {
var
$receiverList = array();
var
$receiverOption = array();
var
$requestEnvelope = array(
"errorLanguage"
=>
"en_US"
,
"detailLevel"
=>
"ReturnAll"
);
function
__construct() {
define(
"API_USER"
,
"YOUR-USER"
);
define(
"API_PASS"
,
"YOUR-PASS"
);
define(
"API_SIG"
,
"YOUR-SIGNATURE"
);
define(
"APP_ID"
,
"APP-80W284485P519543T"
);
$
this
->headers = array(
"X-PAYPAL-SECURITY-USERID: "
.API_USER,
"X-PAYPAL-SECURITY-PASSWORD: "
.API_PASS,
"X-PAYPAL-SECURITY-SIGNATURE: "
.API_SIG,
"X-PAYPAL-REQUEST-DATA-FORMAT: JSON"
,
"X-PAYPAL-RESPONSE-DATA-FORMAT: JSON"
,
"X-PAYPAL-APPLICATION-ID: "
.APP_ID
);
}
function
getPaymentOptions($paykey) {
$packet = array(
"requestEnvelope"
=> array(
"errorLanguage"
=>
"en_US"
,
"detailLevel"
=>
"ReturnAll"
),
"payKey"
=> $paykey
);
return
$
this
->_paypalSend($packet,
"GetPaymentOptions"
);
}
function
setPaymentOptions() {
}
function
addReceiver($email,$amount,$itemsArr) {
try
{
if
(!filter_var($email, FILTER_VALIDATE_EMAIL) or !is_numeric($amount) or !is_array($itemsArr)) {
throw
new
Exception(
"Email\Payment Details Invalid for : "
. $email);
}
$sum;
foreach ($itemsArr as $key=>$value) {
$sum+=$value[
"price"
];
}
if
($sum != $amount)
throw
new
Exception(
"Items Sum doesn't match the amount"
);
}
catch
(Exception $e) {
echo 'Message:
' .$e->getMessage();
return FALSE;
}
try {
if(count($this->receiverList) < 6) {
$this->receiverList[] = array("amount" => $amount,"email" => $email);
$item = array();
foreach($itemsArr as $key=>$value) {
$item[] = array(
"name"=>$value['
name
'],
"price"=>$value['
price
'],
);
}
$this->receiverOption[] = array(
"receiver"=>array("email"=>$email),
"invoiceData" => array(
"item"=>$item,
),
);
return TRUE;
}
else {
throw new Exception("Maximum amount of receivers reached");
return FALSE;
}
}
catch(Exception $e) {
echo '
Message:
' .$e->getMessage();
return FALSE;
}
}
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);
return json_decode(curl_exec($ch), TRUE);
}
function splitPay($currency,$returnUrl,$cancelUrl) {
$createPacket = array (
"actionType" => "PAY",
"currencyCode" => $currency,
"receiverList" => array(
"receiver" => $this->receiverList
),
"returnUrl" => $returnUrl,
"cancelUrl" => $cancelUrl,
"requestEnvelope" => $this->requestEnvelope
);
$response = $this->_paypalSend($createPacket,"Pay");
//print_r($response);
$payKey = $response['
payKey'];
$detailsPacket = array(
"requestEnvelope"
=> $
this
->requestEnvelope,
"payKey"
=> $payKey,
"receiverOptions"
=> $
this
->receiverOption
);
$response = $
this
->_paypalSend($detailsPacket,
"SetPaymentOptions"
);
$dets = $
this
->getPaymentOptions($payKey);
header(
"Location: "
.$
this
->paypalUrl.$payKey);
}
}