-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring & implement Products API.
- Loading branch information
Showing
13 changed files
with
516 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
namespace Srmklive\Chargify\Traits\ChargifyAPI; | ||
|
||
trait Products | ||
{ | ||
/** | ||
* Create a new product. | ||
* | ||
* @param string $product_family_id | ||
* @param array $data | ||
* | ||
* @return array | ||
*/ | ||
public function product_create(string $product_family_id, array $data): array | ||
{ | ||
$this->apiEndPoint = "/product_families/{$product_family_id}/products.json"; | ||
|
||
$this->verb = 'post'; | ||
|
||
$this->options['json'] = [ | ||
'product' => $data, | ||
]; | ||
|
||
return $this->doChargifyRequest(); | ||
} | ||
|
||
/** | ||
* Get product details. | ||
* | ||
* @param string $product_id | ||
* | ||
* @return array | ||
*/ | ||
public function product_details(string $product_id): array | ||
{ | ||
$this->apiEndPoint = "/products/{$product_id}.json"; | ||
|
||
$this->verb = 'get'; | ||
|
||
return $this->doChargifyRequest(); | ||
} | ||
|
||
/** | ||
* Get product details via API Handle. | ||
* | ||
* @param string $handle | ||
* | ||
* @return array | ||
*/ | ||
public function product_details_by_handle(string $handle): array | ||
{ | ||
$this->apiEndPoint = "/products/handle/{$handle}.json"; | ||
|
||
$this->verb = 'get'; | ||
|
||
return $this->doChargifyRequest(); | ||
} | ||
|
||
/** | ||
* Update product details. | ||
* | ||
* @param string $product_id | ||
* @param array $data | ||
* | ||
* @return array | ||
*/ | ||
public function product_update(string $product_id, array $data): array | ||
{ | ||
$this->apiEndPoint = "/products/{$product_id}.json"; | ||
|
||
$this->verb = 'put'; | ||
|
||
$this->options['json'] = [ | ||
'product' => $data, | ||
]; | ||
|
||
return $this->doChargifyRequest(); | ||
} | ||
|
||
/** | ||
* Archive a product. | ||
* | ||
* @param string $product_id | ||
* | ||
* @return array | ||
*/ | ||
public function product_archive(string $product_id): array | ||
{ | ||
$this->apiEndPoint = "/products/{$product_id}.json"; | ||
|
||
$this->verb = 'delete'; | ||
|
||
return $this->doChargifyRequest(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
|
||
namespace Srmklive\Chargify\Tests\Feature; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Srmklive\Chargify\Services\ChargifyClient; | ||
use Srmklive\Chargify\Tests\MockClientClasses; | ||
use Srmklive\Chargify\Tests\MockRequestPayloads; | ||
use Srmklive\Chargify\Tests\MockResponsePayloads; | ||
|
||
class ProductsTest extends TestCase | ||
{ | ||
use MockClientClasses; | ||
use MockRequestPayloads; | ||
use MockResponsePayloads; | ||
|
||
/** @var \Srmklive\Chargify\Services\ChargifyClient */ | ||
protected $client; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->client = new ChargifyClient($this->getMockApiCredentials()); | ||
parent::setUp(); | ||
} | ||
|
||
|
||
/** @test */ | ||
public function it_can_create_a_product() | ||
{ | ||
$expectedResponse = $this->mockProductDetailsResponse(); | ||
|
||
$expectedParams = $this->mockCreateProductParams(); | ||
|
||
$this->client->setClient( | ||
$this->mock_http_client($expectedResponse) | ||
); | ||
|
||
$response = $this->client->product_create('933860', $expectedParams); | ||
|
||
$this->assertNotEmpty($response); | ||
$this->assertArrayHasKey('product', $response); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_get_details_for_a_product() | ||
{ | ||
$expectedResponse = $this->mockProductDetailsResponse(); | ||
|
||
$this->client->setClient( | ||
$this->mock_http_client($expectedResponse) | ||
); | ||
|
||
$response = $this->client->product_details('4364984'); | ||
|
||
$this->assertNotEmpty($response); | ||
$this->assertArrayHasKey('product', $response); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_get_details_for_a_product_by_product_handle() | ||
{ | ||
$expectedResponse = $this->mockProductDetailsResponse(); | ||
|
||
$this->client->setClient( | ||
$this->mock_http_client($expectedResponse) | ||
); | ||
|
||
$response = $this->client->product_details_by_handle('gold'); | ||
|
||
$this->assertNotEmpty($response); | ||
$this->assertArrayHasKey('product', $response); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_update_details_for_a_product() | ||
{ | ||
$expectedResponse = $this->mockProductDetailsResponse(); | ||
|
||
$expectedParams = $this->mockCreateProductParams(); | ||
|
||
$this->client->setClient( | ||
$this->mock_http_client($expectedResponse) | ||
); | ||
|
||
$response = $this->client->product_update('4364984', $expectedParams); | ||
|
||
$this->assertNotEmpty($response); | ||
$this->assertArrayHasKey('product', $response); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_archive_a_product() | ||
{ | ||
$expectedResponse = $this->mockProductDetailsResponse(); | ||
|
||
$this->client->setClient( | ||
$this->mock_http_client($expectedResponse) | ||
); | ||
|
||
$response = $this->client->product_archive('4364984'); | ||
|
||
$this->assertNotEmpty($response); | ||
$this->assertArrayHasKey('product', $response); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Srmklive\Chargify\Tests\Mocks\Requests; | ||
|
||
trait Products | ||
{ | ||
/** | ||
* @return array | ||
*/ | ||
protected function mockCreateProductParams() | ||
{ | ||
return \GuzzleHttp\json_decode('{ | ||
"name": "Gold Plan", | ||
"handle": "gold", | ||
"description": "This is our gold plan.", | ||
"accounting_code": "123", | ||
"request_credit_card": true, | ||
"price_in_cents": 1000, | ||
"interval": 1, | ||
"interval_unit": "month", | ||
"auto_create_signup_page": true, | ||
"tax_code": "D0000000" | ||
}', true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.