Source for file PayPalPro.class.php
* This class encapsulates all of the functions for interacting with PayPal Pro. Requires PHP 5.
/********************************* Public Variables **********************************************/
* Gets or sets the customer's address.
* Sets the ammount to charge.
* Sets the URL to return to if the customer clicks the cancel link in PayPal.
* Sets the credit card number.
* Gets or sets the customer's city.
* Gets or sets the customer's country.
* Sets the currency code. For a list of valid currency codes, see: https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_api_nvp_currency_codes
* Gets or sets the custom order information.
* Sets the order description.
* Gets or sets the customer's email address.
* Sets the credit card expiration month.
* Sets the credit card expiration year.
* Gets or sets the customer's first name.
* Sets the client's PayPal API Username.
* The client's PayPal API Password
* The client's PayPal API Signature
* Gets or sets the invoice number.
* Sets the URL for Instant Payment Notifications.
* Gets or sets the customer's last name.
* Gets any special notes entered by the client in their PayPal account, returned from the GetExpressCheckoutDetails function.
* Gets or sets the customer's PayPal Payer ID.
* Gets or sets the customer's phone number.
* Sets the URL to return to from PayPal once the customer completes the transaction.
* Sets the credit card security code (CVV2).
* Sets the shipping amount of the order.
* Sets whether or not to show the shipping fields in PayPal.
* Gets or sets the customer's state.
* Sets the sub-total of the order.
* Sets the sales tax amount.
* Sets whether or not to use test mode. If true, the transaction will be sent to the PayPal sandbox.
* Gets or sets the Toekn used by PayPal for express checkout.
* Gets or sets the transaction ID.
* Gets or sets the customer's zip/postal code.
//////////////////// public return variables ////////////////////////////
* Gets the returned approval code.
* Gets the last error that ocurred.
* Gets the last request that was sent to PayPal in raw format.
* Gets the last response returned from PayPal in raw format.
* Gets the reason for the last response.
/********************************* Private Variables **********************************************/
private $sTestAPIEndpoint =
"https://api-3t.sandbox.paypal.com/nvp";
private $sTestPayPalURL =
"https://www.sandbox.paypal.com/webscr?cmd=_express-checkout&token=";
private $sProductionAPIEndpoint =
"https://api-3t.paypal.com/nvp";
private $sProductionPayPalURL =
"https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=";
/********************************* Public Methods **********************************************/
* Constructor for the class.
* @param string The client's API Username with PayPal.
* @param string The client's API Password with PayPal.
* @param string The client's API Signature with PayPal.
function __construct($GatewayMerchantID,$GatewayPassword,$GatewaySignature)
* Processes a direct credit card payment. Returns true on successful payment, otherwise false.
//clear the request and response fields
//validate the credit card expiration date
throw
new Exception("The expiration date is invalid.");
throw
new Exception("The credit card expiration date has already passed.");
//validate the credit card number and determine the card type
if(preg_match("/^4[0-9]{12}(?:[0-9]{3})?$/",$this->CardNumber)) $sCardType=
"Visa"; //All Visa card numbers start with a 4. New cards have 16 digits. Old cards have 13.
elseif(preg_match("/^5[1-5][0-9]{14}$/",$this->CardNumber)) $sCardType=
"MasterCard"; // All MasterCard numbers start with the numbers 51 through 55. All have 16 digits.
elseif(preg_match("/^3[47][0-9]{13}$/",$this->CardNumber)) $sCardType=
"Amex"; // American Express card numbers start with 34 or 37 and have 15 digits.
elseif(preg_match("/^6(?:011|5[0-9]{2})[0-9]{12}$/",$this->CardNumber)) $sCardType=
"Discover"; //Discover card numbers begin with 6011 or 65. All have 16 digits.
else throw
new Exception("The credit card number is not valid.");
if(!$this->Amount) $aErrors[]=
"The amount to charge is required.";
elseif($this->Amount<
.5) $aErrors[]=
"The amount to charge is required.";
if(!$this->CardNumber) $aErrors[]=
"The credit card number is required.";
if(!$this->SecurityCode) $aErrors[]=
"The security code is required.";
if(!$this->FirstName) $aErrors[]=
"The first name is required.";
if(!$this->LastName) $aErrors[]=
"The last name is required.";
if(!$this->Address) $aErrors[]=
"The address is required.";
if(!$this->City) $aErrors[]=
"The city is required.";
if(!$this->State) $aErrors[]=
"The state is required.";
if(!$this->Zip) $aErrors[]=
"The zip code is required.";
if(count($aErrors)>
0) throw
new Exception(implode(", ",$aErrors));
//sanitize the credit card number
$aFields["METHOD"]=
"DoDirectPayment";
//information specific to this transaction
$aFields["AMT"]=
$this->Amount;
$aFields["PAYMENTACTION"]=
"Sale";
$aFields["CREDITCARDTYPE"]=
$sCardType;
$aFields["EXPDATE"]=
date("mY",$iExpirationTime);
$aFields["CITY"]=
$this->City;
$aFields["STATE"]=
$this->State;
$aFields["COUNTRYCODE"]=
$this->Country;
$aFields["IPADDRESS"]=
$_SERVER['REMOTE_ADDR'];
$aFields["SHIPPINGAMT"]=
$this->Shipping;
$aFields["TAXAMT"]=
$this->Tax;
if($this->IPN) $aFields["NOTIFYURL"]=
$this->IPN;
//submit the request to PayPal
$this->SubmitRequest($aFields);
//break the response into an associative array
if(count($aResponseParts)>
0)
foreach($aResponseParts as $sResponsePart)
list
($sKey,$sValue)=
explode("=",$sResponsePart,2);
else throw
new Exception("An invalid response was returned.");
if($sResult==
"SUCCESS" ||
$sResult==
"SUCCESSWITHWARNING")
* Completes a previously begun Express Checkout transaction. Returns true on success, false on failure.
//clear the request and response fields
$aFields["METHOD"]=
"DoExpressCheckoutPayment";
$aFields["TOKEN"]=
$this->Token;
$aFields["PAYERID"]=
$this->PayerID;
$aFields["PAYMENTREQUEST_0_PAYMENTACTION"]=
"Sale";
$aFields["PAYMENTREQUEST_0_AMT"]=
$this->Amount;
$aFields["PAYMENTREQUEST_0_CURRENCYCODE"]=
$this->CurrencyCode;
$aFields["IPADDRESS"]=
$_SERVER['REMOTE_ADDR'];
if(isset
($this->SubTotal)) $aFields["PAYMENTREQUEST_0_ITEMAMT"]=
$this->SubTotal;
if(isset
($this->Shipping)) $aFields["PAYMENTREQUEST_0_SHIPPINGAMT"]=
$this->Shipping;
if(isset
($this->Tax)) $aFields["PAYMENTREQUEST_0_TAXAMT"]=
$this->Tax;
if(isset
($this->IPN)) $aFields["PAYMENTREQUEST_0_NOTIFYURL"]=
$this->IPN;
if(isset
($this->Custom)) $aFields["PAYMENTREQUEST_0_CUSTOM"]=
$this->Custom;
//submit the request to PayPal
$this->SubmitRequest($aFields);
//break the response into an associative array
if(count($aResponseParts)>
0)
foreach($aResponseParts as $sResponsePart)
list
($sKey,$sValue)=
explode("=",$sResponsePart,2);
else throw
new Exception("An invalid response was returned.");
if($sResult==
"SUCCESS" ||
$sResult==
"SUCCESSWITHWARNING")
if($aResult["PAYMENTINFO_0_PAYMENTSTATUS"]==
"Completed")
if($aReturn["PAYMENTINFO_0_PENDINGREASON"]==
"address") $sReason=
"The payment is pending because your customer did not include a confirmed shipping address and your Payment Receiving Preferences is set such that you want to manually accept or deny each of these payments. To change your preference, go to the Preferences section of your Profile. ";
elseif($aReturn["PAYMENTINFO_0_PENDINGREASON"]==
"echeck") $sReason=
"The payment is pending because it was made by an eCheck that has not yet cleared. ";
elseif($aReturn["PAYMENTINFO_0_PENDINGREASON"]==
"intl") $sReason=
"The payment is pending because you hold a non-U.S. account and do not have a withdrawal mechanism. You must manually accept or deny this payment from your Account Overview. ";
elseif($aReturn["PAYMENTINFO_0_PENDINGREASON"]==
"multi-currency") $sReason=
"You do not have a balance in the currency sent, and you do not have your Payment Receiving Preferences set to automatically convert and accept this payment. You must manually accept or deny this payment. ";
elseif($aReturn["PAYMENTINFO_0_PENDINGREASON"]==
"verify") $sReason=
"The payment is pending because you are not yet verified. You must verify your account before you can accept this payment. ";
else $sReason=
"The payment is pending for an unknown reason. For more information, contact PayPal customer service.";
$sMessage=
'The order was submitted, but payment has not yet been completed. You will be notified once payment has been completed. '.
$sReason;
throw
new Exception($sMessage);
* Retrieves information from PayPal regarding the current express checkout user's account. Returns true on success, false on failure.
* @param string The Token received from PayPal. The Token is originally generated by the SetExpressCheckout method, but is sent back in a GET variable from PayPal upon return. Returns true on success, otherwise false.
//clear the request and response fields
$aFields["METHOD"]=
"GetExpressCheckoutDetails";
$aFields["TOKEN"]=
$Token;
//submit the request to PayPal
$this->SubmitRequest($aFields);
//break the response into an associative array
if(count($aResponseParts)>
0)
foreach($aResponseParts as $sResponsePart)
list
($sKey,$sValue)=
explode("=",$sResponsePart,2);
else throw
new Exception("An invalid response was returned.");
if($sResult==
"SUCCESS" ||
$sResult==
"SUCCESSWITHWARNING")
$this->Email=
$aReturn["EMAIL"];
$this->Country=
$aReturn["COUNTRYCODE"];
$this->Address=
$aReturn["SHIPTOSTREET"];
$this->City=
$aReturn["SHIPTOCITY"];
$this->State=
$aReturn["SHIPTOSTATE"];
$this->Zip=
$aReturn["SHIPTOZIP"];
$this->PayerID=
$aReturn["PAYERID"];
$this->Custom=
$aReturn["CUSTOM"];
$this->Note=
$aReturn["PAYMENTREQUEST_0_NOTETEXT"];
* Notifies PayPal to begin an Express checkout, and generates the token. Returns true on successful payment, otherwise false.
//clear the request and response fields
if(count($aErrors)>
0) throw
new Exception(implode(", ",$aErrors));
$aFields["METHOD"]=
"SetExpressCheckout";
//information specific to this transaction
$aFields["PAYMENTREQUEST_0_AMT"]=
$this->Amount;
$aFields["PAYMENTREQUEST_0_CURRENCYCODE"]=
$this->CurrencyCode;
$aFields["PAYMENTREQUEST_0_PAYMENTACTION"]=
"Sale";
if(isset
($this->Address)) $aFields["PAYMENTREQUEST_0_SHIPTOSTREET"]=
$this->Address;
if(isset
($this->City)) $aFields["PAYMENTREQUEST_0_SHIPTOCITY"]=
$this->City;
if(isset
($this->State)) $aFields["PAYMENTREQUEST_0_SHIPTOSTATE"]=
$this->State;
if(isset
($this->Zip)) $aFields["PAYMENTREQUEST_0_SHIPTOZIP"]=
$this->Zip;
if(isset
($this->Country)) $aFields["PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE"]=
$this->Country;
if(isset
($this->Email)) $aFields["EMAIL"]=
$this->Email;
if(isset
($this->SubTotal)) $aFields["PAYMENTREQUEST_0_ITEMAMT"]=
$this->SubTotal;
if(isset
($this->Shipping)) $aFields["PAYMENTREQUEST_0_SHIPPINGAMT"]=
$this->Shipping;
if(isset
($this->Tax)) $aFields["PAYMENTREQUEST_0_TAXAMT"]=
$this->Tax;
if(isset
($this->Custom)) $aFields["PAYMENTREQUEST_0_CUSTOM"]=
$this->Custom;
if(isset
($this->IPN)) $aFields["PAYMENTREQUEST_0_NOTIFYURL"]=
$this->IPN;
//submit the request to PayPal
$this->SubmitRequest($aFields);
//break the response into an associative array
if(count($aResponseParts)>
0)
foreach($aResponseParts as $sResponsePart)
list
($sKey,$sValue)=
explode("=",$sResponsePart,2);
else throw
new Exception("An invalid response was returned.");
if($sResult==
"SUCCESS" ||
$sResult==
"SUCCESSWITHWARNING")
$this->Token=
$aReturn["TOKEN"];
/********************************* Private Methods **********************************************/
private function ClearLog()
private function SubmitRequest($aFields)
//authentication information
$aFields["VERSION"]=
"65";
//encode the values to ensure they are passed properly
foreach($aFields as $key =>
$value)
if(($key==
"CANCELURL") ||
($key==
"RETURNURL")) $aFields[$key]=
urlencode($key).
"=".
$value;
//initialize CURL and set the options
$sURL=
(true==
$this->Test?
$this->sTestAPIEndpoint:
$this->sProductionAPIEndpoint);
//turning off the server and peer verification
//setting the nvpreq as POST FIELD to curl
//getting response from server
throw
new Exception("An error ocurred while communicating with PayPal.");
Documentation generated on Wed, 26 Jan 2011 15:24:44 -0700 by phpDocumentor 1.4.3