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
9 changes: 2 additions & 7 deletions src/Clients/AbstractClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ?? '';
Expand All @@ -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
Expand Down
15 changes: 15 additions & 0 deletions src/Clients/PaymentLinksClient.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\CreatePaymentLinkData;

class PaymentLinksClient extends AbstractClient
{
public function createPaymentLink(CreatePaymentLinkData $data): array
{
return $this->post('payment-links', $data->toArray());
}
}
10 changes: 10 additions & 0 deletions src/MontonioClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down
77 changes: 77 additions & 0 deletions src/Structs/CreatePaymentLinkData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace Montonio\Structs;

use Montonio\Structs\Fields\Amount;
use Montonio\Structs\Fields\Currency;
use Montonio\Structs\Fields\Locale;
use Montonio\Structs\Fields\NotificationUrl;
use Montonio\Structs\Fields\PaymentReference;
use Montonio\Structs\Fields\PreferredProvider;

class CreatePaymentLinkData extends AbstractStruct
{
use Amount, Currency, Locale, NotificationUrl, PaymentReference, PreferredProvider;

protected string $description;
protected bool $askAdditionalInfo;
protected string $expiresAt;

/**
* Description of the payment link that is shown on the payment page
*/
public function getDescription(): ?string
{
return $this->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;
}
}
28 changes: 28 additions & 0 deletions tests/Integration/Clients/PaymentLinksClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Clients;

use Montonio\Structs\CreatePaymentLinkData;
use Tests\BaseTestCase;

class PaymentLinksClientTest extends BaseTestCase
{
public function testCreatePaymentLink(): void
{
$data = (new CreatePaymentLinkData())
->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);
}
}