public
Last active

PaypalAPI DoDirectPayment Sale PHP Implementation :: Untested, wrote at anger! :)

  • Download Gist
PayPalAPI_DoDirectPayment.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
<?php
// ------------------------------------------------------------------------------------------ //
/**
* LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @category Payments
* @package CA\APIs\Paypal
* @author CodeAngry <codeangry at google's mail dot com>
* @copyright Claude "CodeAngry" Adrian
* @license http://www.opensource.org/licenses/mit-license.html
* @version 0.0.0
* @link http://codeangry.com/
*/
// ------------------------------------------------------------------------------------------ //
/**
* __toArray() enforcer.
*/
interface PaypalArray {
/**
* Easy function to cast Properties to arbitrary array.
*
* @return array
*/
public function __toArray();
}
 
/**
* Holds Person information.
*/
class PaypalPerson implements PaypalArray {
/**
* Self-explanatory.
*
* @var string
*/
protected $_FirstName = null;
 
/**
* Self-explanatory.
*
* @var string
*/
protected $_LastName = null;
 
/**
* Self-explanatory.
*
* @var string
*/
protected $_StreetAddress = null;
 
/**
* Self-explanatory.
*
* @var string
*/
protected $_City = null;
 
/**
* Self-explanatory.
*
* @var string
*/
protected $_State = null;
 
/**
* Self-explanatory.
*
* @var string
*/
protected $_CountryCode = null;
 
/**
* Self-explanatory.
*
* @var number
*/
protected $_ZipCode = null;
/**
* Easy __construct()-or that sets up all properties in one strike.
*
* @param string $FirstName
* @param string $LastName
* @param string $StreetAddress
* @param string $City
* @param string $State
* @param string $CountryCode
* @param number $ZipCode
* @return PaypalPerson
*/
public function __construct($FirstName, $LastName, $StreetAddress, $City, $State, $CountryCode, $ZipCode){
// Walk string Arguments and enfore validity
foreach(compact('FirstName', 'LastName', 'StreetAddress', 'City', 'State') as $Name => $Value){
if(!is_string($Value) or !strlen($this->{"_{$Name}"} = trim($Value))){
throw new \InvalidArgumentException("{$Name} must be a non-empty trimmed string.");
}
}
// CountryCode is 2-letter uppercased string
if(!is_string($CountryCode) or !preg_match('~^[A-Z]{2}$~', $this->_CountryCode = trim(strtoupper($CardType)))){
throw new \InvalidArgumentException('$CountryCode must be a non-empty trimmed string.');
}
// Zip is numeric
if(!is_numeric($ZipCode) or (($this->_ZipCode = intval($ZipCode)) < 1)){
throw new \InvalidArgumentException('$ZipCode must be a positive number.');
}
}
 
/**
* Export Properties.
*
* @return array
*/
public function __toArray(){
return array(
'FIRSTNAME' => $this->_FirstName,
'LASTNAME' => $this->_LastName,
'STREET' => $this->_StreetAddress,
'CITY' => $this->_City,
'STATE' => $this->_State,
'COUNTRYCODE' => $this->_CountryCode,
'ZIP' => $this->_ZipCode,
);
}
}
 
/**
* Holds CreditCard information.
*/
class PaypalCreditCard implements PaypalArray {
/**
* Self-explanatory.
*
* @var string
*/
protected $_CardType = null;
 
/**
* Self-explanatory.
*
* @var number
*/
protected $_CardNumber = null;
 
/**
* Self-explanatory.
*
* @var number
*/
protected $_Expiration = null;
/**
* Self-explanatory.
*
* @var number
*/
protected $_CVV2 = null;
/**
* Easy __construct()-or that sets up all properties in one strike.
*
* @param string $CardType
* @param number $CardNumber
* @param number $ExpirationMonth
* @param number $ExpirationYear
* @param number $CVV2
* @return PaypalPerson
*/
public function __construct($CardType, $CardNumber, $ExpirationMonth, $ExpirationYear, $CVV2){
if(!is_string($CardType) or !strlen($this->_CardType = trim($CardType))){
throw new \InvalidArgumentException('$CardType must be a non-empty trimmed string.');
}
if(empty($CardNumber) or !is_numeric($CardNumber) or ($CardNumber < 1)){
throw new \InvalidArgumentException('$CardNumber must be a positive number.');
}
// Change this at will :)
if(!is_numeric($CVV2) or (($this->_CVV2 = intval($CVV2)) < 100) or ($CVV2 > 10000)){
throw new \InvalidArgumentException('$CVV2 must be a number of 3-4 digits.');
}
if(!is_numeric($ExpirationMonth) or (($ExpirationMonth = intval($ExpirationMonth)) < 1) or ($ExpirationMonth > 12)){
throw new \InvalidArgumentException('$ExpirationMonth must be between 1 and 12.');
}
// We check the year again so positive will do here
if(!is_numeric($ExpirationYear) or (($ExpirationYear = intval($ExpirationYear)) < 1)){
throw new \InvalidArgumentException('$ExpirationYear must be between 1 and 12.');
}
// Check future Expiration date
if((($ExpirationYear * 12) + $ExpirationMonth) < ((idate('Y') * 12) + idate('m'))){
throw new \InvalidArgumentException('$ExpirationYear and $ExpirationMonth cannot be in the past.');
}
// Format the Expiration date
$this->_Expiration = sprintf('%02d%04d', $ExpirationMonth, $ExpirationYear);
}
/**
* Export Properties.
*
* @return array
*/
public function __toArray(){
return array(
'CREDITCARDTYPE' => $this->_CardType,
'ACCT' => $this->_CardNumber,
'EXPDATE' => $this->_Expiration,
'CVV2' => $this->_CVV2,
);
}
}
 
/**
* Holds Payment information.
*/
class PaypalPayment implements PaypalArray {
/**
* Self-explanatory.
*
* @var float
*/
protected $_Amount = null;
/**
* Self-explanatory.
*
* @var string
*/
protected $_Currency = null;
 
/**
* Self-explanatory.
*
* @var string
*/
protected $_Description = null;
 
/**
* Easy __construct()-or that sets up all properties in one strike.
*
* @param float $Amount
* @param string $Currency
* @param string/null $Description
* @return PaypalPayment
*/
public function __construct($Amount, $Currency, $Description = null){
// Float Amount, positive so we don't pay the buyer :)
if(!is_numeric($Amount) or (($this->Amount = floatval($Amount)) <= 0)){
throw new \InvalidArgumentException('$Amount must be a positive float.');
}
// Currency is 3-letter uppercased string
if(!is_string($Currency) or !preg_match('~^[A-Z]{3}$~', $this->_Currency = trim(strtoupper($Currency)))){
throw new \InvalidArgumentException('$Currency must be a non-empty trimmed string.');
}
// Zip is numeric
empty($Description) and ($Description = null);
if(!is_null($Description) and (!is_string($Description) or !strlen($Description = trim($Description)))){
throw new \InvalidArgumentException('$Description must be NULL or a non-empty trimmed string.');
}
$this->_Description = $Description;
}
 
/**
* Export Properties.
*
* @return array
*/
public function __toArray(){
return array(
'AMT' => sprintf('%0.2f', $this->_Amount),
'CURRENCYCODE' => $this->_Currency,
'DESC' => $this->_Description
);
}
}
 
/**
* Allows easy PaypalAPI calls.
* It's directly __invoke()-able.
*/
class PaypalAPI {
/**
* Self-explanatory.
*
* @var string
*/
private $_Username = null;
 
/**
* Self-explanatory.
*
* @var string
*/
private $_Password = null;
 
/**
* Self-explanatory.
*
* @var string
*/
private $_Signature = null;
 
/**
* Self-explanatory.
*
* @var float
*/
private $_Version = null;
 
/**
* Self-explanatory.
*
* @var string
*/
private $_Endpoint = null;
/**
* Easy __construct()-or that sets up all properties in one strike.
*
* @param bool $Sandbox
* @param string $Username
* @param string $Password
* @param string $Signature
* @param float $Version
* @return PaypalDirectPayment
*/
public function __construct($Sandbox, $Username, $Password, $Signature, $Version = '85.0'){
if(!is_bool($Sandbox)){
throw new \InvalidArgumentException('$Sandbox needs to be BOOL, so do not make me guess.');
}
$this->_Endpoint = $Sandbox ? 'https://api-3t.sandbox.paypal.com/nvp' : 'https://api-3t.paypal.com/nvp';
if(!is_string($Username) or !strlen($this->_Username = trim($Username))){
throw new \InvalidArgumentException('$Username must be a non-empty trimmed string.');
}
if(!is_string($Password) or !strlen($this->_Password = trim($Password))){
throw new \InvalidArgumentException('$Password must be a non-empty trimmed string.');
}
if(!is_string($Signature) or !strlen($this->_Signature = trim($Signature))){
throw new \InvalidArgumentException('$Signature must be a non-empty trimmed string.');
}
$this->_Version = sprintf('%0.1f', floatval($Version)); // Force ##.# output to fix int usage
}
/**
* Does API calls. Method and Arguments.
* $Response will contain raw Response to our Request.
*
* @param string $Method
* @param array $Arguments
* @param reference $Response
* @return array/bool/null
*/
public function __invoke($Method, array $Arguments, &$Response = null){
$Response = null;
if(!is_string($Method) or !strlen($Method = trim($Method))){
throw new \InvalidArgumentException('$Sandbox must be a non-empty trimmed string.');
}
$Arguments = array_merge(array(
'METHOD' => $Method,
'USER' => $this->_Username,
'PWD' => $this->_Password,
'SIGNATURE' => $this->_Signature,
'VERSION' => sprintf('%0.1f', $this->_Version), // Just to make sure .0 decimal stays
), $Arguments);
// Really bad if this happens
if(!($cURL = curl_init($this->_Endpoint))){
return false;
}
curl_setopt($cURL, CURLOPT_VERBOSE, false);
curl_setopt($cURL, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($cURL, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($cURL, CURLOPT_TIMEOUT, 30);
curl_setopt($cURL, CURLOPT_URL, $this->_Endpoint); // Redundant, I know
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cURL, CURLOPT_HEADER, false);
curl_setopt($cURL, CURLOPT_PORT, true);
curl_setopt($cURL, CURLOPT_POSTFIELDS, $Arguments);
curl_setopt($cURL, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($cURL, CURLOPT_FORBID_REUSE, true);
curl_setopt($cURL, CURLOPT_FRESH_CONNECT, true);
curl_setopt($cURL, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($cURL, CURLOPT_TIMEOUT, 60);
curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
'Connection: close',
'Expect: ',
));
$Response = curl_exec($cURL);
$Status = (int)curl_getinfo($cURL, CURLINFO_HTTP_CODE);
curl_close($cURL);
if($Status != 200){
return false; // Bad status
}
if(empty($Response) or !is_string($Response) or !strlen($Response = trim($Response))){
return null; // Empty data
}
parse_str($Response, $NVPs);
if(!isset($NVPs) or empty($NVPs) or !is_array($NVPs)){
return null; // Bad data
}
return $NVPs;
}
/**
* DoDirectPayment sale API Method.
*
* @param PaypalPayment $Payment
* @param PayPalCreditCard $CreditCard
* @param PaypalPerson $Person
* @return array
*/
public function DoDirectPayment(PaypalPayment $Payment, PayPalCreditCard $CreditCard, PaypalPerson $Person, &$Response = null) {
// Merge Arguments and Parameters :)
$Arguments = array_merge(array(
'PAYMENTACTION' => 'Sale',
'IPADDRESS' => $_SERVER['REMOTE_ADDR'],
), $Person->__toArray(), $CreditCard->__toArray(), $Payment->__toArray());
return $this('DoDirectPayment', $Arguments, $Response);
}
}
?>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.