Skip to content

Commit

Permalink
Add static property to set the protocol used with the API URLs.
Browse files Browse the repository at this point in the history
Add getProtocol and setProtocol static methods to USPSBase to allow changing the API protocol as needed.

Signed-off-by: Tristan Hall <[email protected]>
  • Loading branch information
Tristan Hall committed Mar 13, 2021
1 parent 0e293b0 commit 7b9bf94
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/TrackConfirm.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class TrackConfirm extends USPSBase

public function getEndpoint()
{
return self::$testMode ? 'http://production.shippingapis.com/ShippingAPITest.dll' : 'http://production.shippingapis.com/ShippingAPI.dll';
return sprintf('%s://%s', static::getProtocol(), self::$testMode ? 'production.shippingapis.com/ShippingAPITest.dll' : 'production.shippingapis.com/ShippingAPI.dll');
}

/**
Expand Down
31 changes: 28 additions & 3 deletions src/USPSBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
*/
abstract class USPSBase
{
const LIVE_API_URL = 'https://secure.shippingapis.com/ShippingAPI.dll';
const TEST_API_URL = 'http://production.shippingapis.com/ShippingAPITest.dll';
const LIVE_API_URL = 'secure.shippingapis.com/ShippingAPI.dll';
const TEST_API_URL = 'production.shippingapis.com/ShippingAPITest.dll';

/**
* @var string - the usps username provided by the usps website
Expand Down Expand Up @@ -65,6 +65,10 @@ abstract class USPSBase
* @var bool - set whether we are in a test mode or not
*/
public static $testMode = false;
/**
* @var string - set whether to use HTTP or HTTPS
*/
public static $protocol = 'https';
/**
* @var array - different kind of supported api calls by this wrapper
*/
Expand Down Expand Up @@ -223,9 +227,30 @@ protected function doRequest($ch = null)
return $this->getResponse();
}

/**
* Return the protocol (HTTP or HTTPS) to use for the API URLs.
*
* @return string
*/
public static function getProtocol()
{
return static::$protocol;
}

/**
* Set the protocol (HTTP or HTTPS) to use for the API URLs.
*
* @aaram string $protocol
* @return void
*/
public static function setProtocol($protocol)
{
static::$protocol = $protocol;
}

public function getEndpoint()
{
return self::$testMode ? self::TEST_API_URL : self::LIVE_API_URL;
return sprintf('%s://%s', static::getProtocol(), self::$testMode ? self::TEST_API_URL : self::LIVE_API_URL);
}

abstract public function getPostFields();
Expand Down

0 comments on commit 7b9bf94

Please sign in to comment.