Skip to content

Commit

Permalink
Merge pull request #20 from QuadraEcommerce/feature/add-partner-key-s…
Browse files Browse the repository at this point in the history
…upport

Feature/add partner key support
  • Loading branch information
joecampo committed May 19, 2020
2 parents 48d9bc4 + 1db9357 commit 1bc7dc5
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 9 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ After installing via composer you will need to publish the configuration:
php artisan vendor:publish
```
This will create the configuration file for your API key and API secret key at ```config/shipstation.php```. You will need to obtain your API & Secret key from ShipStation: [How can I get access to ShipStation's API?](https://help.shipstation.com/hc/en-us/articles/206638917-How-can-I-get-access-to-ShipStation-s-API-)

If ShipStation has provided you with a partner API key, set it in your configuration file.
## Dependencies
LaravelShipStation uses ```GuzzleHttp\Guzzle```
## Endpoints
Expand Down
7 changes: 4 additions & 3 deletions config/config.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?php

return [
'apiURL' => env('SS_URL', 'https://ssapi.shipstation.com'),
'apiKey' => env('SS_KEY', ''),
'apiSecret' => env('SS_SECRET', ''),
'apiURL' => env('SS_URL', 'https://ssapi.shipstation.com'),
'apiKey' => env('SS_KEY', ''),
'partnerApiKey' => env('SS_PARTNER_KEY', ''),
'apiSecret' => env('SS_SECRET', ''),
];
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<php>
<env name="SHIPSTATION_TESTING" value="false"/>
<env name="KEY" value=""/>
<env name="PARTNER_KEY" value=""/>
<env name="SECRET" value=""/>
<env name="API_URL" value=""/>
</php>
Expand Down
16 changes: 12 additions & 4 deletions src/ShipStation.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,29 @@ class ShipStation extends Client
*
* @param string $apiKey
* @param string $apiSecret
* @param string $apiURL
* @param string|null $partnerApiKey
* @throws \Exception
*/
public function __construct($apiKey, $apiSecret, $apiURL)
public function __construct($apiKey, $apiSecret, $apiURL, $partnerApiKey = null)
{
if (!isset($apiKey, $apiSecret)) {
throw new \Exception('Your API key and/or private key are not set. Did you run artisan vendor:publish?');
}

$this->base_uri = $apiURL;

$headers = [
'Authorization' => 'Basic ' . base64_encode("{$apiKey}:{$apiSecret}"),
];

if (! empty($partnerApiKey)) {
$headers['x-partner'] = $partnerApiKey;
}

parent::__construct([
'base_uri' => $this->base_uri,
'headers' => [
'Authorization' => 'Basic ' . base64_encode("{$apiKey}:{$apiSecret}"),
]
'headers' => $headers,
]);
}

Expand Down
3 changes: 2 additions & 1 deletion src/ShipStationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public function register()
return new ShipStation(
config('shipstation.apiKey'),
config('shipstation.apiSecret'),
config('shipstation.apiURL')
config('shipstation.apiURL'),
config('shipstation.partnerApiKey')
);
});
}
Expand Down
22 changes: 21 additions & 1 deletion tests/ShipStationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ public function setUp()
exit();
}

$this->shipStation = new LaravelShipStation\ShipStation(getenv('KEY'), getenv('SECRET'), getenv('API_URL'));
$this->shipStation = new LaravelShipStation\ShipStation(
getenv('KEY'),
getenv('SECRET'),
getenv('API_URL'),
getenv('PARTNER_KEY')
);
}

/** @test */
Expand Down Expand Up @@ -126,4 +131,19 @@ public function rate_limits_are_set_after_request()
$this->assertGreaterThanOrEqual(0, $this->shipStation->getSecondsUntilReset());
$this->assertInternalType('boolean', $this->shipStation->isRateLimited());
}

/** @test */
public function partner_api_key_header_is_set_when_defined()
{
if (empty(getenv('PARTNER_KEY'))) {
// nothing to test
return;
}

$this->shipStation->webhooks->get();

$headers = $this->shipStation->request->getConfig('headers');

$this->assertArrayHasKey('x-partner', $headers);
}
}

0 comments on commit 1bc7dc5

Please sign in to comment.