| 1 |
<?php |
| 2 |
class usces_paypal { |
| 3 |
|
| 4 |
var $options; |
| 5 |
|
| 6 |
var $API_UserName; |
| 7 |
var $API_Password; |
| 8 |
var $API_Signature; |
| 9 |
|
| 10 |
var $sBNCode; |
| 11 |
var $version; |
| 12 |
|
| 13 |
var $method; |
| 14 |
var $data; |
| 15 |
var $nvpreq; |
| 16 |
var $resArray; |
| 17 |
|
| 18 |
function usces_paypal() { |
| 19 |
$this->options = get_option('usces'); |
| 20 |
$this->API_UserName = urlencode($this->options['acting_settings']['paypal']['user']); |
| 21 |
$this->API_Password = urlencode($this->options['acting_settings']['paypal']['pwd']); |
| 22 |
$this->API_Signature = urlencode($this->options['acting_settings']['paypal']['signature']); |
| 23 |
$this->sBNCode = urlencode("uscons_cart_EC_JP"); |
| 24 |
$this->version = urlencode("66.0");//20110412ysk |
| 25 |
$this->method = ''; |
| 26 |
$this->data = ''; |
| 27 |
$this->nvpreq = ''; |
| 28 |
$this->resArray = array(); |
| 29 |
} |
| 30 |
|
| 31 |
function setMethod($method) {$this->method = $method;} |
| 32 |
function setData($data) {$this->data = $data;} |
| 33 |
function getResponse() {return $this->resArray;} |
| 34 |
|
| 35 |
function doExpressCheckout() { |
| 36 |
$status = true; |
| 37 |
|
| 38 |
$this->nvpreq = "METHOD=".$this->method |
| 39 |
."&VERSION=".$this->version |
| 40 |
."&USER=".$this->API_UserName |
| 41 |
."&PWD=".$this->API_Password |
| 42 |
."&SIGNATURE=".$this->API_Signature |
| 43 |
.$this->data |
| 44 |
."&BUTTONSOURCE=".$this->sBNCode; |
| 45 |
|
| 46 |
if(extension_loaded('curl')) { |
| 47 |
//setting the curl parameters. |
| 48 |
$ch = curl_init(); |
| 49 |
curl_setopt($ch, CURLOPT_URL, $this->options['acting_settings']['paypal']['api_endpoint']); |
| 50 |
curl_setopt($ch, CURLOPT_VERBOSE, 1); |
| 51 |
|
| 52 |
//turning off the server and peer verification(TrustManager Concept). |
| 53 |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); |
| 54 |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); |
| 55 |
|
| 56 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
| 57 |
curl_setopt($ch, CURLOPT_POST, 1); |
| 58 |
|
| 59 |
//setting the nvpreq as POST FIELD to curl |
| 60 |
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->nvpreq); |
| 61 |
|
| 62 |
//getting response from server |
| 63 |
$response = curl_exec($ch); |
| 64 |
|
| 65 |
if(curl_errno($ch)) { |
| 66 |
usces_log('PayPal : API call failed. curl_error_no:['.curl_errno($ch).'] curl_error_msg:'.curl_error($ch), 'acting_transaction.log'); |
| 67 |
$status = false; |
| 68 |
|
| 69 |
} else { |
| 70 |
//closing the curl |
| 71 |
curl_close($ch); |
| 72 |
} |
| 73 |
|
| 74 |
$this->resArray = $this->deformatNVP($response); |
| 75 |
|
| 76 |
} else { |
| 77 |
//usces_log(urldecode($this->nvpreq), 'acting_transaction.log'); |
| 78 |
$r = new usces_httpRequest($this->options['acting_settings']['paypal']['api_host'], '/nvp', 'POST', true); |
| 79 |
$result = $r->connect($this->nvpreq); |
| 80 |
if($result >= 400) { |
| 81 |
usces_log('PayPal : API call failed. result:['.$result.']', 'acting_transaction.log'); |
| 82 |
$status = false; |
| 83 |
} |
| 84 |
|
| 85 |
$this->resArray = $this->deformatNVP($r->getContent()); |
| 86 |
} |
| 87 |
return $status; |
| 88 |
} |
| 89 |
|
| 90 |
private function deformatNVP($nvpstr) { |
| 91 |
$intial = 0; |
| 92 |
$nvpArray = array(); |
| 93 |
|
| 94 |
while(strlen($nvpstr)) { |
| 95 |
//postion of Key |
| 96 |
$keypos = strpos($nvpstr, '='); |
| 97 |
//position of value |
| 98 |
$valuepos = strpos($nvpstr, '&') ? strpos($nvpstr, '&') : strlen($nvpstr); |
| 99 |
|
| 100 |
/*getting the Key and Value values and storing in a Associative Array*/ |
| 101 |
$keyval = substr($nvpstr, $intial, $keypos); |
| 102 |
$valval = substr($nvpstr, $keypos+1, $valuepos-$keypos-1); |
| 103 |
//decoding the respose |
| 104 |
$nvpArray[urldecode($keyval)] = urldecode( $valval); |
| 105 |
$nvpstr = substr($nvpstr, $valuepos+1, strlen($nvpstr)); |
| 106 |
} |
| 107 |
return $nvpArray; |
| 108 |
} |
| 109 |
} |
| 110 |
?> |