Skip to content

Commit

Permalink
Special ENDPOINT property added for setting different endpoints per c…
Browse files Browse the repository at this point in the history
…onfigurations
  • Loading branch information
Craig Dennis committed Dec 14, 2015
1 parent ce1c8ef commit c8af582
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Description: For making PayPal API calls with ease

```php
// PayPalHelper construct (with endpoint url and version passed)
$PayPalHelper = new PayPalHelper("https://api-3t.sandbox.paypal.com/nvp", "109.0");
$PayPalHelper = new PayPalHelper("https://api-3t.sandbox.paypal.com/nvp", "202");

// Config that will be passed to all API calls
$PayPalHelper->SetConfig(array(
Expand All @@ -33,24 +33,28 @@ else

**Example of using different modes**

Note that ENDPOINT is a special config property that will override the default passed in the constructor.


```php
// Define PayPal config modes
define("SANDBOXMODE", 0);
define("LIVEMODE", 1);

// PayPalHelper construct (with endpoint url and version passed)
$PayPalHelper = new PayPalHelper("https://api-3t.sandbox.paypal.com/nvp", "109.0");
// PayPalHelper construct (no endpoint or version passed, version is currently 202 by default)
$PayPalHelper = new PayPalHelper();

// Set sandbox config
$PayPalHelper->SetConfig(array(
$PayPalHelper->SetConfig(array(
"ENDPOINT" => "https://api-3t.sandbox.paypal.com/nvp",
"USER" => "SANDBOX-USERNAME",
"PWD" => "SANDBOX-PASSWORD",
"SIGNATURE" => "SANDBOX-SIGNATURE"
), SANDBOXMODE);

// Set live config
$PayPalHelper->SetConfig(array(
$PayPalHelper->SetConfig(array(
"ENDPOINT" => "https://api-3t.paypal.com/nvp",
"USER" => "USERNAME",
"PWD" => "PASSWORD",
"SIGNATURE" => "SIGNATURE"
Expand Down
21 changes: 20 additions & 1 deletion class.paypalhelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class PayPalHelper
public $Result;

// Construct
public function __construct($Endpoint = "", $Version = "109.0")
public function __construct($Endpoint = "", $Version = "202")
{
$this->SetMode(0);
$this->SetEndpoint($Endpoint);
Expand Down Expand Up @@ -80,6 +80,14 @@ public function GetVersion()
public function SetMode($Mode)
{
$this->Mode = $Mode;

// Update endpoint if set in this config mode
if(isset($this->Config[$this->Mode]["ENDPOINT"])
&& is_string($this->Config[$this->Mode]["ENDPOINT"])
&& strlen($this->Config[$this->Mode]["ENDPOINT"]) > 0)
{
$this->Endpoint = $this->Config[$this->Mode]["ENDPOINT"];
}
}

/*
Expand Down Expand Up @@ -127,6 +135,13 @@ public function DoCall()
// Get all parameters
$Args = func_get_args();

// Unset ENDPOINT in if set so it doesn't get passed as an option
if(isset($this->Config[$this->Mode]["ENDPOINT"]))
{
$this->Endpoint = $this->Config[$this->Mode]["ENDPOINT"];
unset($this->Config[$this->Mode]["ENDPOINT"]);
}

// Apply VERSION and current config array to RequestParams
$RequestParams = array_merge(
array("VERSION" => $this->Version),
Expand Down Expand Up @@ -175,6 +190,10 @@ public function DoCall()
else
$this->Success = false;

// Put ENDPOINT back into array
if(isset($this->Endpoint))
$this->Config[$this->Mode]["ENDPOINT"] = $this->Endpoint;

return $this->Result;
}
}

0 comments on commit c8af582

Please sign in to comment.