From c27468a478a8e662157d7106b927ae8240733a30 Mon Sep 17 00:00:00 2001 From: Morten Bak Date: Mon, 8 Jan 2024 15:21:48 +0100 Subject: [PATCH] update payment resource --- composer.json | 2 +- src/DataObjects/Basket.php | 9 +- src/DataObjects/Payment.php | 11 ++ src/DataObjects/PaymentLink.php | 43 ++++++ src/Exceptions/CreatePaymentFailed.php | 9 ++ src/Exceptions/CreatePaymentLinkFailed.php | 9 ++ src/Exceptions/DeletePaymentLinkFailed.php | 9 ++ src/Exceptions/FetchPaymentFailed.php | 9 ++ src/Exceptions/FetchPaymentsFailed.php | 9 ++ src/Resources/PaymentResource.php | 146 ++++++++++++++++++--- 10 files changed, 234 insertions(+), 22 deletions(-) create mode 100644 src/DataObjects/PaymentLink.php create mode 100644 src/Exceptions/CreatePaymentFailed.php create mode 100644 src/Exceptions/CreatePaymentLinkFailed.php create mode 100644 src/Exceptions/DeletePaymentLinkFailed.php create mode 100644 src/Exceptions/FetchPaymentFailed.php create mode 100644 src/Exceptions/FetchPaymentsFailed.php diff --git a/composer.json b/composer.json index 32379f1..bec3ef2 100644 --- a/composer.json +++ b/composer.json @@ -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" diff --git a/src/DataObjects/Basket.php b/src/DataObjects/Basket.php index dedb0a6..73abaab 100644 --- a/src/DataObjects/Basket.php +++ b/src/DataObjects/Basket.php @@ -2,7 +2,7 @@ namespace Netbums\Quickpay\DataObjects; -class Basket +readonly class Basket { /** * @param array $items @@ -11,4 +11,11 @@ public function __construct( array $items = [] ) { } + + public function toArray(): array + { + return [ + 'basket' => $this->items, + ]; + } } diff --git a/src/DataObjects/Payment.php b/src/DataObjects/Payment.php index 48f88f3..2e40161 100644 --- a/src/DataObjects/Payment.php +++ b/src/DataObjects/Payment.php @@ -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(), + ]; + } } diff --git a/src/DataObjects/PaymentLink.php b/src/DataObjects/PaymentLink.php new file mode 100644 index 0000000..85cc77f --- /dev/null +++ b/src/DataObjects/PaymentLink.php @@ -0,0 +1,43 @@ + $this->id, + 'amount' => $this->amount, + 'language' => $this->language, + 'continue_url' => $this->continue_url, + 'cancel_url' => $this->cancel_url, + 'callback_url' => $this->callback_url, + ]; + } + +} diff --git a/src/Exceptions/CreatePaymentFailed.php b/src/Exceptions/CreatePaymentFailed.php new file mode 100644 index 0000000..eed7fe0 --- /dev/null +++ b/src/Exceptions/CreatePaymentFailed.php @@ -0,0 +1,9 @@ +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'; } }