From 446f2290aa50f9e6af713bd7123969699d6f55b7 Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 3 Oct 2024 22:58:09 +0300 Subject: [PATCH] feat: payment links --- src/Clients/AbstractClient.php | 9 +-- src/Clients/PaymentLinksClient.php | 15 ++++ src/MontonioClient.php | 10 +++ src/Structs/CreatePaymentLinkData.php | 77 +++++++++++++++++++ .../Clients/PaymentLinksClientTest.php | 28 +++++++ 5 files changed, 132 insertions(+), 7 deletions(-) create mode 100644 src/Clients/PaymentLinksClient.php create mode 100644 src/Structs/CreatePaymentLinkData.php create mode 100644 tests/Integration/Clients/PaymentLinksClientTest.php diff --git a/src/Clients/AbstractClient.php b/src/Clients/AbstractClient.php index 414b7b7..dc2519d 100755 --- a/src/Clients/AbstractClient.php +++ b/src/Clients/AbstractClient.php @@ -122,7 +122,7 @@ private function execute(CurlHandle $ch): array $message = ''; - if ($httpStatus === 400) { + if (400 <= $httpStatus && $httpStatus <= 499) { try { $body = json_decode($response, true, 512, JSON_THROW_ON_ERROR); $message = $body['error'] ?? ''; @@ -132,12 +132,7 @@ private function execute(CurlHandle $ch): array } } - throw new RequestException( - $message, - $httpStatus, - $response, - $ch - ); + throw new RequestException($message, $httpStatus, $response, $ch); } protected function isSandbox(): bool diff --git a/src/Clients/PaymentLinksClient.php b/src/Clients/PaymentLinksClient.php new file mode 100644 index 0000000..da3946d --- /dev/null +++ b/src/Clients/PaymentLinksClient.php @@ -0,0 +1,15 @@ +post('payment-links', $data->toArray()); + } +} diff --git a/src/MontonioClient.php b/src/MontonioClient.php index f64c259..88d4e6e 100644 --- a/src/MontonioClient.php +++ b/src/MontonioClient.php @@ -7,6 +7,7 @@ use Montonio\Clients\AbstractClient; use Montonio\Clients\OrdersClient; use Montonio\Clients\PaymentIntentsClient; +use Montonio\Clients\PaymentLinksClient; use Montonio\Clients\StoresClient; class MontonioClient extends AbstractClient @@ -32,6 +33,15 @@ public function paymentIntents(): PaymentIntentsClient ); } + public function paymentLinks(): PaymentLinksClient + { + return new PaymentLinksClient( + $this->getAccessKey(), + $this->getSecretKey(), + $this->getEnvironment(), + ); + } + public function stores(): StoresClient { return new StoresClient( diff --git a/src/Structs/CreatePaymentLinkData.php b/src/Structs/CreatePaymentLinkData.php new file mode 100644 index 0000000..a1a7221 --- /dev/null +++ b/src/Structs/CreatePaymentLinkData.php @@ -0,0 +1,77 @@ +description ?? null; + } + + /** + * Description of the payment link that is shown on the payment page + */ + public function setDescription(string $description): self + { + $this->description = $description; + + return $this; + } + + /** + * If true, the payment page will ask for additional information from the payer (eg. first name, last name, etc.). + * If false, the payment page will not ask for additional information from the payer + */ + public function getAskAdditionalInfo(): ?bool + { + return $this->askAdditionalInfo ?? null; + } + + /** + * If true, the payment page will ask for additional information from the payer (eg. first name, last name, etc.). + * If false, the payment page will not ask for additional information from the payer + */ + public function setAskAdditionalInfo(bool $askAdditionalInfo): self + { + $this->askAdditionalInfo = $askAdditionalInfo; + + return $this; + } + + /** + * Timestamp in ISO 8601 format. The time when we will no longer accept any orders for this payment-link. + */ + public function getExpiresAt(): ?string + { + return $this->expiresAt ?? null; + } + + /** + * Timestamp in ISO 8601 format. The time when we will no longer accept any orders for this payment-link. + */ + public function setExpiresAt(string $expiresAt): self + { + $this->expiresAt = $expiresAt; + + return $this; + } +} diff --git a/tests/Integration/Clients/PaymentLinksClientTest.php b/tests/Integration/Clients/PaymentLinksClientTest.php new file mode 100644 index 0000000..ed6981c --- /dev/null +++ b/tests/Integration/Clients/PaymentLinksClientTest.php @@ -0,0 +1,28 @@ +setDescription('Hello') + ->setCurrency('EUR') + ->setAmount(10.00) + ->setLocale('fi') + ->setAskAdditionalInfo(true) + ->setExpiresAt(date('Y-m-d H:i:s', strtotime('+1 hour'))); + + $return = $this->getMontonioClient()->paymentLinks()->createPaymentLink($data); + + $this->assertArrayHasKey('uuid', $return); + $this->assertArrayHasKey('url', $return); + $this->assertArrayHasKey('shortUrl', $return); + } +}