Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

INTEG-251: checkout find #64

Closed
Closed
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
32 changes: 32 additions & 0 deletions Tests/Feature/Checkout/Mock/CheckoutShowMockClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace PlugAndPay\Sdk\Tests\Feature\Checkout\Mock;

use PlugAndPay\Sdk\Tests\Feature\ClientMock;

class CheckoutShowMockClient extends ClientMock
{
public const BASIC_CHECKOUT = [
'id' => 1,
'is_active' => true,
'is_expired' => false,
'name' => 'lorem-ipsum-test',
'preview_url' => 'https://example.com/preview-url',
'primary_color' => '#ff0000',
'return_url' => 'https://example.com/return-url',
'secondary_color' => '#00ff00',
'slug' => 'lorem-ipsum-test',
'url' => 'https://example.com/url',
'created_at' => '2019-01-16T00:00:00.000000Z',
'updated_at' => '2019-01-16T00:00:00.000000Z',
'deleted_at' => '2019-01-16T00:00:00.000000Z',
];
protected string $path;

public function __construct(int $status = 200, array $data = [])
{
parent::__construct($status, $data);

$this->responseBody = ['data' => $data + self::BASIC_CHECKOUT];
}
}
66 changes: 66 additions & 0 deletions Tests/Feature/Checkout/ShowCheckoutTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace PlugAndPay\Sdk\Tests\Feature\Checkout;

use PHPUnit\Framework\TestCase;
use PlugAndPay\Sdk\Entity\Response;
use PlugAndPay\Sdk\Exception\NotFoundException;
use PlugAndPay\Sdk\Exception\UnauthenticatedException;
use PlugAndPay\Sdk\Service\CheckoutService;
use PlugAndPay\Sdk\Tests\Feature\Checkout\Mock\CheckoutShowMockClient;

class ShowCheckoutTest extends TestCase
{
/** @test */
public function it_should_throw_unauthorized_exception(): void
{
$client = new CheckoutShowMockClient(status: Response::HTTP_UNAUTHORIZED);
$service = new CheckoutService($client);
$exception = null;

try {
$service->find(999);
} catch (UnauthenticatedException $exception) {
}

static::assertEquals('Unable to connect with Plug&Pay. Request is unauthenticated.', $exception->getMessage());
}

/** @test */
public function it_should_throw_not_found_when_checkout_not_found(): void
{
$client = new CheckoutShowMockClient(status: Response::HTTP_NOT_FOUND);
$service = new CheckoutService($client);
$exception = null;

try {
$service->find(999);
} catch (NotFoundException $exception) {
}

static::assertEquals('Not found', $exception->getMessage());
}

/** @test */
public function it_should_return_basic_order(): void
{
$client = new CheckoutShowMockClient(status: 200, data: ['id' => 1]);
$service = new CheckoutService($client);

$checkout = $service->find(1);

static::assertSame(1, $checkout->id());
static::assertTrue($checkout->isActive());
static::assertFalse($checkout->isExpired());
static::assertSame('lorem-ipsum-test', $checkout->name());
static::assertSame('https://example.com/preview-url', $checkout->previewUrl());
static::assertSame('#ff0000', $checkout->primaryColor());
static::assertSame('https://example.com/return-url', $checkout->returnUrl());
static::assertSame('#00ff00', $checkout->secondaryColor());
static::assertSame('lorem-ipsum-test', $checkout->slug());
static::assertSame('https://example.com/url', $checkout->url());
static::assertSame('2019-01-16 00:00:00', $checkout->createdAt()->format('Y-m-d H:i:s'));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hoe kan je eigenlijk ->createdAt()->format('Y-m-d H:i:s') doen, we hebben niks van carbon toch?

static::assertSame('2019-01-16 00:00:00', $checkout->updatedAt()->format('Y-m-d H:i:s'));
static::assertSame('2019-01-16 00:00:00', $checkout->deletedAt()->format('Y-m-d H:i:s'));
}
}
18 changes: 18 additions & 0 deletions src/Enum/CheckoutIncludes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace PlugAndPay\Sdk\Enum;

enum CheckoutIncludes: string
{
case CONFIRMATION = 'confirmation';
case CUSTOM_FIELDS = 'custom_fields';
case ORDER_BUMPS = 'order_bumps';
case POPUPS = 'popups';
case PRODUCT = 'product';
case PRODUCT_PRICING = 'product_pricing';
case SETTINGS = 'settings';
case STATISTICS = 'statistics';
case TEMPLATE = 'template';
case UPSELLS = 'upsells';
case WEBHOOKS = 'webhooks';
}
38 changes: 38 additions & 0 deletions src/Service/CheckoutService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace PlugAndPay\Sdk\Service;

use PlugAndPay\Sdk\Contract\ClientInterface;
use PlugAndPay\Sdk\Director\BodyTo\BodyToCheckout;
use PlugAndPay\Sdk\Entity\Checkout;
use PlugAndPay\Sdk\Enum\CheckoutIncludes;
use PlugAndPay\Sdk\Exception\DecodeResponseException;
use PlugAndPay\Sdk\Support\Parameters;

class CheckoutService
{
private ClientInterface $client;

public function __construct(ClientInterface $client)
{
$this->client = $client;
}

public function include(CheckoutIncludes ...$includes): self
{
$this->includes = $includes;

return $this;
}

/** @throws DecodeResponseException */
public function find(int $id): Checkout
{
$query = Parameters::toString(['include' => $this->includes]);
$response = $this->client->get("/v2/checkouts/$id$query");

return BodyToCheckout::build($response->body()['data']);
}
}