6 |
rodolico |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
if(!strlen(session_id())){
|
|
|
4 |
session_start();
|
|
|
5 |
}
|
|
|
6 |
|
|
|
7 |
Class PayPalInterface{
|
|
|
8 |
var $amount;
|
|
|
9 |
var $currency = 'USD';
|
|
|
10 |
var $paymentType = 'Sale';
|
|
|
11 |
var $returnURL;
|
|
|
12 |
var $cancelURL;
|
|
|
13 |
|
|
|
14 |
var $API_Endpoint;
|
|
|
15 |
var $version = '2.3';
|
|
|
16 |
var $API_UserName;
|
|
|
17 |
var $API_Password;
|
|
|
18 |
var $API_Signature;
|
|
|
19 |
var $USE_PROXY = FALSE;
|
|
|
20 |
var $PROXY_HOST;
|
|
|
21 |
var $PROXY_PORT;
|
|
|
22 |
var $PROXY_PORT = '808';
|
|
|
23 |
var $gv_ApiErrorURL;
|
|
|
24 |
var $sBNCode = 'PP-ECWizard';
|
|
|
25 |
|
|
|
26 |
|
|
|
27 |
function PayPalInterface($sandbox=FALSE){
|
|
|
28 |
if($sandbox){
|
|
|
29 |
$this->API_Endpoint = "https://api-3t.sandbox.paypal.com/nvp";
|
|
|
30 |
$this->PAYPAL_URL = "https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&token=";
|
|
|
31 |
}else{
|
|
|
32 |
$this->API_Endpoint = "https://api-3t.paypal.com/nvp";
|
|
|
33 |
$this->PAYPAL_URL = "https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=";
|
|
|
34 |
}
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
function set($details){
|
|
|
38 |
foreach($details AS $k=>$v){
|
|
|
39 |
$this->$k = $v;
|
|
|
40 |
}
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
function process(){
|
|
|
44 |
$success = FALSE;
|
|
|
45 |
$resArray = $this->CallShortcutExpressCheckout();
|
|
|
46 |
$ack = strtoupper($resArray["ACK"]);
|
|
|
47 |
if($ack=="SUCCESS"){
|
|
|
48 |
$this->RedirectToPayPal($resArray["TOKEN"]);
|
|
|
49 |
}else{
|
|
|
50 |
return FALSE;
|
|
|
51 |
}
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
function RedirectToPayPal($token){
|
|
|
55 |
header("Location: ".$this->PAYPAL_URL.$token);
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
function CallShortcutExpressCheckout(){
|
|
|
59 |
// Construct the parameter string that describes the SetExpressCheckout API call in the shortcut implementation
|
|
|
60 |
|
|
|
61 |
$nvpstr = '&Amt='.$this->amount.'&PAYMENTACTION='.$this->paymentType.'&ReturnUrl='.$this->returnURL.'&CANCELURL='.$this->cancelURL.'&CURRENCYCODE='.$this->currency;
|
|
|
62 |
|
|
|
63 |
$_SESSION["currencyCodeType"] = $this->currency;
|
|
|
64 |
$_SESSION["PaymentType"] = $this->paymentType;
|
|
|
65 |
|
|
|
66 |
//'---------------------------------------------------------------------------------------------------------------
|
|
|
67 |
//' Make the API call to PayPal
|
|
|
68 |
//' If the API call succeded, then redirect the buyer to PayPal to begin to authorize payment.
|
|
|
69 |
//' If an error occured, show the resulting errors
|
|
|
70 |
//'---------------------------------------------------------------------------------------------------------------
|
|
|
71 |
$resArray = $this->hash_call('SetExpressCheckout', $nvpstr);
|
|
|
72 |
$ack = strtoupper($resArray["ACK"]);
|
|
|
73 |
if($ack == "SUCCESS"){
|
|
|
74 |
$token = urldecode($resArray["TOKEN"]);
|
|
|
75 |
$_SESSION['TOKEN']=$token;
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
return $resArray;
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
function hash_call($methodName,$nvpStr){
|
|
|
82 |
//setting the curl parameters.
|
|
|
83 |
$ch = curl_init();
|
|
|
84 |
curl_setopt($ch, CURLOPT_URL, $this->API_Endpoint);
|
|
|
85 |
curl_setopt($ch, CURLOPT_VERBOSE, 1);
|
|
|
86 |
|
|
|
87 |
//turning off the server and peer verification(TrustManager Concept).
|
|
|
88 |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
|
|
|
89 |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
|
|
|
90 |
|
|
|
91 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
|
|
|
92 |
curl_setopt($ch, CURLOPT_POST, 1);
|
|
|
93 |
|
|
|
94 |
//if USE_PROXY constant set to TRUE in Constants.php, then only proxy will be enabled.
|
|
|
95 |
//Set proxy name to PROXY_HOST and port number to PROXY_PORT in constants.php
|
|
|
96 |
if($this->USE_PROXY){
|
|
|
97 |
curl_setopt ($ch, CURLOPT_PROXY, $this->PROXY_HOST. ":" . $this->PROXY_PORT);
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
//NVPRequest for submitting to server
|
|
|
101 |
$nvpreq = 'METHOD='.urlencode($methodName).'&VERSION='.urlencode($this->version).'&PWD='.urlencode($this->API_Password).'&USER='.urlencode($this->API_UserName).'&SIGNATURE='.urlencode($this->API_Signature).$nvpStr.'&BUTTONSOURCE='.urlencode($this->sBNCode);
|
|
|
102 |
|
|
|
103 |
//setting the nvpreq as POST FIELD to curl
|
|
|
104 |
curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq);
|
|
|
105 |
|
|
|
106 |
//getting response from server
|
|
|
107 |
$response = curl_exec($ch);
|
|
|
108 |
|
|
|
109 |
//convrting NVPResponse to an Associative Array
|
|
|
110 |
$nvpResArray = $this->deformatNVP($response);
|
|
|
111 |
$nvpReqArray = $this->deformatNVP($nvpreq);
|
|
|
112 |
$_SESSION['nvpReqArray'] = $nvpReqArray;
|
|
|
113 |
|
|
|
114 |
if(curl_errno($ch)){
|
|
|
115 |
// moving to display page to display curl errors
|
|
|
116 |
$_SESSION['curl_error_no']=curl_errno($ch) ;
|
|
|
117 |
$_SESSION['curl_error_msg']=curl_error($ch);
|
|
|
118 |
//Execute the Error handling module to display errors.
|
|
|
119 |
}else{
|
|
|
120 |
//closing the curl
|
|
|
121 |
curl_close($ch);
|
|
|
122 |
}
|
|
|
123 |
|
|
|
124 |
return $nvpResArray;
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
function deformatNVP($nvpstr){
|
|
|
128 |
$intial=0;
|
|
|
129 |
$nvpArray = array();
|
|
|
130 |
|
|
|
131 |
while(strlen($nvpstr)){
|
|
|
132 |
//postion of Key
|
|
|
133 |
$keypos= strpos($nvpstr,'=');
|
|
|
134 |
//position of value
|
|
|
135 |
$valuepos = strpos($nvpstr,'&') ? strpos($nvpstr,'&'): strlen($nvpstr);
|
|
|
136 |
|
|
|
137 |
/*getting the Key and Value values and storing in a Associative Array*/
|
|
|
138 |
$keyval=substr($nvpstr,$intial,$keypos);
|
|
|
139 |
$valval=substr($nvpstr,$keypos+1,$valuepos-$keypos-1);
|
|
|
140 |
//decoding the respose
|
|
|
141 |
$nvpArray[urldecode($keyval)] =urldecode( $valval);
|
|
|
142 |
$nvpstr=substr($nvpstr,$valuepos+1,strlen($nvpstr));
|
|
|
143 |
}
|
|
|
144 |
return $nvpArray;
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
function getStatus(){
|
|
|
148 |
$token = "";
|
|
|
149 |
if(isset($_REQUEST['token'])){
|
|
|
150 |
$token = $_REQUEST['token'];
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
// If the Request object contains the variable 'token' then it means that the user is coming from PayPal site.
|
|
|
154 |
if($token != ""){
|
|
|
155 |
$resArray = GetShippingDetails($token);
|
|
|
156 |
$ack = strtoupper($resArray["ACK"]);
|
|
|
157 |
return $ack;
|
|
|
158 |
}
|
|
|
159 |
}
|
|
|
160 |
}
|
|
|
161 |
|
|
|
162 |
//A debugging function
|
|
|
163 |
function pr($r){
|
|
|
164 |
echo '<pre>';
|
|
|
165 |
print_r($r);
|
|
|
166 |
echo '</pre>';
|
|
|
167 |
}
|
|
|
168 |
?>
|