Skip to content

Commit

Permalink
update payment resource
Browse files Browse the repository at this point in the history
  • Loading branch information
Morten Bak committed Jan 8, 2024
1 parent b81417c commit c27468a
Show file tree
Hide file tree
Showing 10 changed files with 234 additions and 22 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
}
],
"require": {
"php": "^8.1",
"php": "^8.2",
"illuminate/contracts": "^10.0",
"quickpay/quickpay-php-client": "^2.0",
"spatie/laravel-package-tools": "^1.14.0"
Expand Down
9 changes: 8 additions & 1 deletion src/DataObjects/Basket.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Netbums\Quickpay\DataObjects;

class Basket
readonly class Basket
{
/**
* @param array<int, BasketItem> $items
Expand All @@ -11,4 +11,11 @@ public function __construct(
array $items = []
) {
}

public function toArray(): array
{
return [
'basket' => $this->items,
];
}
}
11 changes: 11 additions & 0 deletions src/DataObjects/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,15 @@ public function __construct(
public ?OptionalAddress $shipping_address,
) {
}

public function toArray(): array
{
return [
'currency' => $this->currency,
'order_id' => $this->order_id,
'basket' => $this->basket->toArray(),
'invoice_address' => $this->invoice_address?->toArray(),
'shipping_address' => $this->shipping_address?->toArray(),
];
}
}
43 changes: 43 additions & 0 deletions src/DataObjects/PaymentLink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Netbums\Quickpay\DataObjects;

readonly class PaymentLink
{

public function __construct(
public int $id, // transaction id
public int $amount, // Amount to authorize
public ?string $language,
public ?string $continue_url,
public ?string $cancel_url,
public ?string $callback_url,
)
{
}

public static function fromArray(array $data): static
{
return new static(
id: $data['id'],
amount: $data['amount'],
language: $data['language'],
continue_url: $data['continue_url'],
cancel_url: $data['cancel_url'],
callback_url: $data['callback_url'],
);
}

public function toArray(): array
{
return [
'id' => $this->id,
'amount' => $this->amount,
'language' => $this->language,
'continue_url' => $this->continue_url,
'cancel_url' => $this->cancel_url,
'callback_url' => $this->callback_url,
];
}

}
9 changes: 9 additions & 0 deletions src/Exceptions/CreatePaymentFailed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Netbums\Quickpay\Exceptions;

use Exception;

class CreatePaymentFailed extends Exception
{
}
9 changes: 9 additions & 0 deletions src/Exceptions/CreatePaymentLinkFailed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Netbums\Quickpay\Exceptions;

use Exception;

class CreatePaymentLinkFailed extends Exception
{
}
9 changes: 9 additions & 0 deletions src/Exceptions/DeletePaymentLinkFailed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Netbums\Quickpay\Exceptions;

use Exception;

class DeletePaymentLinkFailed extends Exception
{
}
9 changes: 9 additions & 0 deletions src/Exceptions/FetchPaymentFailed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Netbums\Quickpay\Exceptions;

use Exception;

class FetchPaymentFailed extends Exception
{
}
9 changes: 9 additions & 0 deletions src/Exceptions/FetchPaymentsFailed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Netbums\Quickpay\Exceptions;

use Exception;

class FetchPaymentsFailed extends Exception
{
}
146 changes: 126 additions & 20 deletions src/Resources/PaymentResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,79 +3,185 @@
namespace Netbums\Quickpay\Resources;

use Netbums\Quickpay\DataObjects\Payment;
use Netbums\Quickpay\DataObjects\PaymentLink;
use Netbums\Quickpay\Exceptions\CreatePaymentFailed;
use Netbums\Quickpay\Exceptions\CreatePaymentLinkFailed;
use Netbums\Quickpay\Exceptions\DeletePaymentLinkFailed;
use Netbums\Quickpay\Exceptions\FetchPaymentFailed;
use Netbums\Quickpay\Exceptions\FetchPaymentsFailed;
use Netbums\Quickpay\Resources\Concerns\QuickpayApiConsumer;
use Throwable;

class PaymentResource
{
use QuickpayApiConsumer;

public function all()
/**
* @return object
* @throws FetchPaymentsFailed
*/
public function all(): object
{

$this->method = 'get';
$this->endpoint = 'payments';

try {
$response = $this->request($this->method, $this->endpoint);
} catch (Throwable $exception) {
throw new FetchPaymentsFailed(
message: 'The payments could not be fetched.',
code: $exception->getCode(),
previous: $exception
);
}

return $response;
}

public function create(Payment $payment)
/**
* @param Payment $payment
* @return object
* @throws CreatePaymentFailed
*/
public function create(Payment $payment): object
{

$this->method = 'post';
$this->endpoint = 'payments';
$this->data = $payment->toArray();

try {
$response = $this->request($this->method, $this->endpoint, $this->data);
} catch (Throwable $exception) {
throw new CreatePaymentFailed(
message: 'The payment could not be created.',
code: $exception->getCode(),
previous: $exception
);
}

return $response;
}

// Create or update payment link
public function createLink($id)
/**
* Create or Update the Payment Link
* @param int $id
* @param PaymentLink $paymentLink
* @return object
* @throws CreatePaymentLinkFailed
*/
public function createLink(int $id, PaymentLink $paymentLink): object
{

$this->method = 'put';
$this->endpoint = 'payments/' . $id . '/link';
$this->data = $paymentLink->toArray();

try {
$response = $this->request($this->method, $this->endpoint, $this->data);
} catch (Throwable $exception) {
throw new CreatePaymentLinkFailed(
message: 'The payment link could not be created.',
code: $exception->getCode(),
previous: $exception
);
}

return $response;
}

// Delete payment link
public function deleteLink($id)
/**
* Delete payment link
* @param int $id
* @return object
* @throws DeletePaymentLinkFailed
*/
public function deleteLink(int $id): object
{

$this->method = 'delete';
$this->endpoint = 'payments/' . $id . '/link';

try {
$response = $this->request($this->method, $this->endpoint);
} catch (Throwable $exception) {
throw new DeletePaymentLinkFailed(
message: 'The payment link could not be deleted.',
code: $exception->getCode(),
previous: $exception
);
}

return $response;
}

// Get Payment
public function find($id)
/**
* Get Payment
* @param int $id
* @return object
* @throws FetchPaymentFailed
*/
public function find(int $id): object
{

$this->method = 'get';
$this->endpoint = 'payments/' . $id;

try {
$response = $this->request($this->method, $this->endpoint);
} catch (Throwable $exception) {
throw new FetchPaymentFailed(
message: 'The payment with id ' . $id . ' could not be fetched.',
code: $exception->getCode(),
previous: $exception
);
}

return $response;
}

// Create payment session
public function createPaymentSession($id)
{

$this->method = 'post';
$this->endpoint = 'payments/' . $id . '/session';
}

// authorize payment
public function authorize($id)
{

$this->method = 'post';
$this->endpoint = 'payments/' . $id . '/authorize';
}

// capture payment
public function capture($id)
{

$this->method = 'post';
$this->endpoint = 'payments/' . $id . '/capture';
}

// refund payment
public function refund($id)
{

$this->method = 'post';
$this->endpoint = 'payments/' . $id . '/refund';
}

// cancel payment
public function cancel($id)
{

$this->method = 'post';
$this->endpoint = 'payments/' . $id . '/cancel';
}

// renew authorization
public function renew($id)
{

$this->method = 'post';
$this->endpoint = 'payments/' . $id . '/renew';
}

// create fraud confirmation report
public function createFraudConfirmationReport($id)
{

$this->method = 'post';
$this->endpoint = 'payments/' . $id . '/fraud-report';
}
}

0 comments on commit c27468a

Please sign in to comment.