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
17 changes: 16 additions & 1 deletion src/Clients/AbstractClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Exception;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use JsonException;
use Montonio\Exception\CurlErrorException;
use Montonio\Exception\RequestException;
use Montonio\MontonioClient;
Expand Down Expand Up @@ -107,8 +108,10 @@ private function execute(CurlHandle $ch): array
{
$response = curl_exec($ch);

// @codeCoverageIgnoreStart
if ($response === false) {
throw new CurlErrorException(curl_error($ch), curl_errno($ch), $ch);
// @codeCoverageIgnoreEnd
}

$httpStatus = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
Expand All @@ -117,8 +120,20 @@ private function execute(CurlHandle $ch): array
return json_decode($response, true);
}

$message = '';

if ($httpStatus === 400) {
try {
$body = json_decode($response, true, 512, JSON_THROW_ON_ERROR);
$message = $body['error'] ?? '';
// @codeCoverageIgnoreStart
} catch (JsonException $e) {
// @codeCoverageIgnoreEnd
}
}

throw new RequestException(
'',
$message,
$httpStatus,
$response,
$ch
Expand Down
15 changes: 15 additions & 0 deletions src/Clients/PaymentIntentsClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Montonio\Clients;

use Montonio\Structs\CreatePaymentIntentDraftData;

class PaymentIntentsClient extends AbstractClient
{
public function createDraft(CreatePaymentIntentDraftData $data): array
{
return $this->post('payment-intents/draft', $data->toArray());
}
}
10 changes: 10 additions & 0 deletions src/MontonioClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Montonio\Clients\AbstractClient;
use Montonio\Clients\OrdersClient;
use Montonio\Clients\PaymentIntentsClient;
use Montonio\Clients\StoresClient;

class MontonioClient extends AbstractClient
Expand All @@ -22,6 +23,15 @@ public function orders(): OrdersClient
);
}

public function paymentIntents(): PaymentIntentsClient
{
return new PaymentIntentsClient(
$this->getAccessKey(),
$this->getSecretKey(),
$this->getEnvironment(),
);
}

public function stores(): StoresClient
{
return new StoresClient(
Expand Down
10 changes: 10 additions & 0 deletions src/Structs/CreatePaymentIntentDraftData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Montonio\Structs;

class CreatePaymentIntentDraftData extends Payment
{

}
33 changes: 33 additions & 0 deletions tests/Integration/Clients/PaymentIntentsClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Clients;

use Montonio\Exception\RequestException;
use Montonio\Structs\CreatePaymentIntentDraftData;
use Montonio\Structs\Payment;
use Tests\BaseTestCase;

class PaymentIntentsClientTest extends BaseTestCase
{
public function testCreateDraft(): void
{
$data = (new CreatePaymentIntentDraftData())
->setMethod(Payment::METHOD_CARD);

$response = $this->getMontonioClient()->paymentIntents()->createDraft($data);

$this->assertArrayHasKey('stripePublicKey', $response);
$this->assertArrayHasKey('stripeClientSecret', $response);
$this->assertArrayHasKey('onBehalfOf', $response);
$this->assertArrayHasKey('uuid', $response);
}

public function testCreateDraft_fails_missingRequiredData(): void
{
$this->expectException(RequestException::class);

$response = $this->getMontonioClient()->paymentIntents()->createDraft(new CreatePaymentIntentDraftData());
}
}
2 changes: 1 addition & 1 deletion tests/Integration/Clients/StoresClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class StoresClientTest extends BaseTestCase
{
public function testGetPaymentMethods()
public function testGetPaymentMethods(): void
{
$methods = $this->getMontonioClient()->stores()->getPaymentMethods();

Expand Down