diff --git a/src/Clients/AbstractClient.php b/src/Clients/AbstractClient.php index 4fcbaa2..414b7b7 100755 --- a/src/Clients/AbstractClient.php +++ b/src/Clients/AbstractClient.php @@ -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; @@ -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); @@ -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 diff --git a/src/Clients/PaymentIntentsClient.php b/src/Clients/PaymentIntentsClient.php new file mode 100644 index 0000000..4450111 --- /dev/null +++ b/src/Clients/PaymentIntentsClient.php @@ -0,0 +1,15 @@ +post('payment-intents/draft', $data->toArray()); + } +} diff --git a/src/MontonioClient.php b/src/MontonioClient.php index 97c9934..f64c259 100644 --- a/src/MontonioClient.php +++ b/src/MontonioClient.php @@ -6,6 +6,7 @@ use Montonio\Clients\AbstractClient; use Montonio\Clients\OrdersClient; +use Montonio\Clients\PaymentIntentsClient; use Montonio\Clients\StoresClient; class MontonioClient extends AbstractClient @@ -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( diff --git a/src/Structs/CreatePaymentIntentDraftData.php b/src/Structs/CreatePaymentIntentDraftData.php new file mode 100644 index 0000000..7b275d1 --- /dev/null +++ b/src/Structs/CreatePaymentIntentDraftData.php @@ -0,0 +1,10 @@ +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()); + } +} diff --git a/tests/Integration/Clients/StoresClientTest.php b/tests/Integration/Clients/StoresClientTest.php index fd4a113..41e69e8 100644 --- a/tests/Integration/Clients/StoresClientTest.php +++ b/tests/Integration/Clients/StoresClientTest.php @@ -8,7 +8,7 @@ class StoresClientTest extends BaseTestCase { - public function testGetPaymentMethods() + public function testGetPaymentMethods(): void { $methods = $this->getMontonioClient()->stores()->getPaymentMethods();