Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,13 @@ jobs:
strategy:
fail-fast: false
matrix:
php: ['8.2', '8.3', '8.4']
laravel: ['11.*', '12.*']
php: ['8.2', '8.3', '8.4', '8.5']
laravel: ['11.*', '12.*', '13.*']
exclude:
- php: '8.2'
laravel: '12.*'
- php: '8.2'
laravel: '13.*'

name: Laravel ${{ matrix.laravel }} / PHP ${{ matrix.php }}

Expand Down Expand Up @@ -90,7 +92,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: ['8.2', '8.3', '8.4']
php: ['8.2', '8.3', '8.4', '8.5']
symfony: ['7.0', '7.1', '7.2', '8.0']
exclude:
- php: '8.2'
Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
"friendsofphp/php-cs-fixer": "^2.0||^3.0",
"guzzlehttp/guzzle": "^6.3||^7.0",
"guzzlehttp/psr7": "^1.4||^2.0",
"illuminate/http": "^11.0||^12.0",
"illuminate/support": "^11.0||^12.0",
"illuminate/contracts": "^11.0||^12.0",
"orchestra/testbench": "^9.0||^10.0",
"illuminate/http": "^11.0||^12.0||^13.0",
"illuminate/support": "^11.0||^12.0||^13.0",
"illuminate/contracts": "^11.0||^12.0||^13.0",
"orchestra/testbench": "^9.0||^10.0||^11.0",
"symfony/http-kernel": "^7.0||^8.0",
"symfony/config": "^7.0||^8.0",
"symfony/dependency-injection": "^7.0||^8.0",
Expand Down
9 changes: 2 additions & 7 deletions src/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@

enum Environment: string
{
case PRODUCTION = 'https://api.paymentic.com/v1_2';
case SANDBOX = 'https://api.sandbox.paymentic.com/v1_2';

public function getBaseUrl(): string
{
return $this->value;
}
case PRODUCTION = 'PRODUCTION';
case SANDBOX = 'SANDBOX';
}
6 changes: 5 additions & 1 deletion src/PaymenticClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ public function __construct(
RequestFactoryInterface $requestFactory,
StreamFactoryInterface $streamFactory,
Environment $environment = Environment::PRODUCTION,
?string $baseUrl = null,
) {
$internalClient = new HttpClient(
$httpClient,
$requestFactory,
$streamFactory,
$apiKey,
$environment->getBaseUrl(),
$baseUrl ?? match ($environment) {
Environment::PRODUCTION => 'https://api.paymentic.com/v1_2',
Environment::SANDBOX => 'https://api.sandbox.paymentic.com/v1_2',
},
);

$this->paymentClient = new PaymentClient($internalClient);
Expand Down
9 changes: 9 additions & 0 deletions src/PaymenticClientFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class PaymenticClientFactory
{
private string $apiKey;
private Environment $environment = Environment::PRODUCTION;
private ?string $baseUrl = null;
private ?ClientInterface $httpClient = null;
private ?RequestFactoryInterface $requestFactory = null;
private ?StreamFactoryInterface $streamFactory = null;
Expand Down Expand Up @@ -50,6 +51,13 @@ public function withProduction(): self
return $this;
}

public function withBaseUrl(string $baseUrl): self
{
$this->baseUrl = $baseUrl;

return $this;
}

public function withHttpClient(ClientInterface $client): self
{
$this->httpClient = $client;
Expand Down Expand Up @@ -79,6 +87,7 @@ public function build(): PaymenticClient
requestFactory: $this->requestFactory ?? $this->discoverRequestFactory(),
streamFactory: $this->streamFactory ?? $this->discoverStreamFactory(),
environment: $this->environment,
baseUrl: $this->baseUrl,
);
}

Expand Down
22 changes: 2 additions & 20 deletions tests/Unit/EnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,11 @@

final class EnvironmentTest extends TestCase
{
#[Test]
public function productionReturnsCorrectBaseUrl(): void
{
$this->assertSame(
'https://api.paymentic.com/v1_2',
Environment::PRODUCTION->getBaseUrl(),
);
}

#[Test]
public function sandboxReturnsCorrectBaseUrl(): void
{
$this->assertSame(
'https://api.sandbox.paymentic.com/v1_2',
Environment::SANDBOX->getBaseUrl(),
);
}

#[Test]
public function productionHasCorrectValue(): void
{
$this->assertSame(
'https://api.paymentic.com/v1_2',
'PRODUCTION',
Environment::PRODUCTION->value,
);
}
Expand All @@ -41,7 +23,7 @@ public function productionHasCorrectValue(): void
public function sandboxHasCorrectValue(): void
{
$this->assertSame(
'https://api.sandbox.paymentic.com/v1_2',
'SANDBOX',
Environment::SANDBOX->value,
);
}
Expand Down
18 changes: 18 additions & 0 deletions tests/Unit/PaymenticClientFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,23 @@ public function buildsClientWithCustomEnvironment(): void
$this->assertInstanceOf(PaymenticClient::class, $client);
}

#[Test]
public function buildsClientWithCustomBaseUrl(): void
{
$psrClient = $this->createStub(ClientInterface::class);
$requestFactory = $this->createStub(RequestFactoryInterface::class);
$streamFactory = $this->createStub(StreamFactoryInterface::class);

$client = PaymenticClientFactory::create('test-api-key')
->withBaseUrl('https://api.staging.paymentic.com/v1_2')
->withHttpClient($psrClient)
->withRequestFactory($requestFactory)
->withStreamFactory($streamFactory)
->build();

$this->assertInstanceOf(PaymenticClient::class, $client);
}

#[Test]
public function autoDiscoversGuzzleWhenNotProvided(): void
{
Expand Down Expand Up @@ -124,6 +141,7 @@ public function supportsFluentInterface(): void
$this->assertSame($factory, $factory->withEnvironment(Environment::SANDBOX));
$this->assertSame($factory, $factory->withSandbox());
$this->assertSame($factory, $factory->withProduction());
$this->assertSame($factory, $factory->withBaseUrl('https://api.staging.paymentic.com/v1_2'));
$this->assertSame($factory, $factory->withHttpClient($psrClient));
$this->assertSame($factory, $factory->withRequestFactory($requestFactory));
$this->assertSame($factory, $factory->withStreamFactory($streamFactory));
Expand Down
78 changes: 78 additions & 0 deletions tests/Unit/PaymenticClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,84 @@ public function usesSandboxEnvironment(): void
$this->assertInstanceOf(PaymentClient::class, $client->payment());
}

#[Test]
public function productionEnvironmentResolvesToProductionBaseUrl(): void
{
$mockClient = new MockPsrClient($this->validPingResponse());
$client = new PaymenticClient(
apiKey: 'test-api-key',
httpClient: $mockClient,
requestFactory: new MockRequestFactory(),
streamFactory: new MockStreamFactory(),
environment: Environment::PRODUCTION,
);

$client->payment()->system()->ping();

$this->assertSame(
'https://api.paymentic.com/v1_2/payment/ping',
(string) $mockClient->getLastRequest()?->getUri(),
);
}

#[Test]
public function sandboxEnvironmentResolvesToSandboxBaseUrl(): void
{
$mockClient = new MockPsrClient($this->validPingResponse());
$client = new PaymenticClient(
apiKey: 'test-api-key',
httpClient: $mockClient,
requestFactory: new MockRequestFactory(),
streamFactory: new MockStreamFactory(),
environment: Environment::SANDBOX,
);

$client->payment()->system()->ping();

$this->assertSame(
'https://api.sandbox.paymentic.com/v1_2/payment/ping',
(string) $mockClient->getLastRequest()?->getUri(),
);
}

#[Test]
public function customBaseUrlOverridesEnvironment(): void
{
$mockClient = new MockPsrClient($this->validPingResponse());
$client = new PaymenticClient(
apiKey: 'test-api-key',
httpClient: $mockClient,
requestFactory: new MockRequestFactory(),
streamFactory: new MockStreamFactory(),
environment: Environment::PRODUCTION,
baseUrl: 'https://api.staging.paymentic.com/v1_2',
);

$client->payment()->system()->ping();

$this->assertSame(
'https://api.staging.paymentic.com/v1_2/payment/ping',
(string) $mockClient->getLastRequest()?->getUri(),
);
}

/**
* @return array<string, mixed>
*/
private function validPingResponse(): array
{
return [
'data' => [
'message' => 'pong',
'environment' => 'sandbox',
'tokenId' => 'token-id',
'clientId' => 'client-id',
'version' => '1.0',
'scopes' => [],
],
];
}

private function createClient(): PaymenticClient
{
return new PaymenticClient(
Expand Down
Loading