From c4e9a778e4f6615ea24de133fa0472c9850d0283 Mon Sep 17 00:00:00 2001 From: Morten Bak Date: Tue, 9 Jan 2024 12:38:22 +0100 Subject: [PATCH] fix client return types --- src/Quickpay.php | 5 +++ src/QuickpayServiceProvider.php | 15 ++++--- .../Concerns/QuickpayApiConsumer.php | 18 ++++---- src/Resources/PaymentResource.php | 43 +++++++++++++------ src/Resources/PayoutResource.php | 14 +++--- 5 files changed, 63 insertions(+), 32 deletions(-) diff --git a/src/Quickpay.php b/src/Quickpay.php index d4ad646..c62b031 100644 --- a/src/Quickpay.php +++ b/src/Quickpay.php @@ -31,6 +31,11 @@ public function __construct() } + public static function api(): static + { + return new static(); + } + public function payments(): PaymentResource { return new PaymentResource( diff --git a/src/QuickpayServiceProvider.php b/src/QuickpayServiceProvider.php index 9da66d9..fd23851 100644 --- a/src/QuickpayServiceProvider.php +++ b/src/QuickpayServiceProvider.php @@ -3,6 +3,7 @@ namespace Netbums\Quickpay; use Netbums\Quickpay\Commands\QuickpayCommand; +use Spatie\LaravelPackageTools\Commands\InstallCommand; use Spatie\LaravelPackageTools\Package; use Spatie\LaravelPackageTools\PackageServiceProvider; @@ -20,15 +21,19 @@ public function configurePackage(Package $package): void $package ->name('laravel-quickpay') ->hasConfigFile() - ->hasViews() - ->hasMigration('create_laravel-quickpay_table') - ->hasCommand(QuickpayCommand::class); + ->publishesServiceProvider('QuickpayServiceProvider') + ->hasInstallCommand(function(InstallCommand $command) { + $command + ->publishConfigFile() + ->copyAndRegisterServiceProviderInApp(); +// ->askToStarRepoOnGitHub('your-vendor/your-repo-name') + }); } - public function boot() + public function boot(): void { $this->publishes([ - __DIR__.'/../config/quickpay.php' => config_path('quickpay.php'), + __DIR__ . '/../config/quickpay.php' => config_path('quickpay.php'), ], 'config'); } diff --git a/src/Resources/Concerns/QuickpayApiConsumer.php b/src/Resources/Concerns/QuickpayApiConsumer.php index 2e7850a..c605891 100644 --- a/src/Resources/Concerns/QuickpayApiConsumer.php +++ b/src/Resources/Concerns/QuickpayApiConsumer.php @@ -8,28 +8,29 @@ trait QuickpayApiConsumer { - public QuickPay $client; - public string $endpoint; public string $method; public array $data; - public function __construct(QuickPay $client) + public function __construct(public QuickPay $client) { } /** + * @param string $method + * @param string $endpoint + * @param array $data + * @return array * @throws CardNotAccepted * @throws QuickPayValidationError */ - public function request(string $method, string $endpoint, array $data = []): object + public function request(string $method, string $endpoint, array $data = []): array { $response = $this->client->request->$method($endpoint, $data); - if ($response->isSuccess()) { - $data = $response->asObject(); + if ($response->status_code >= 200 && $response->status_code < 300) { // if app is in production mode, and the request is not a test, and the card is not accepted, throw an exception if (config('app.env') === 'production' && ! $data->test_mode && ! $data->accepted) { @@ -39,11 +40,12 @@ public function request(string $method, string $endpoint, array $data = []): obj ); } - return $data->getStatus(); + return json_decode($response->response_data, true); + } else { throw new QuickPayValidationError( message: 'The request was not valid', - code: 400 + code: $response->status_code ); } } diff --git a/src/Resources/PaymentResource.php b/src/Resources/PaymentResource.php index d39d188..e7c8838 100644 --- a/src/Resources/PaymentResource.php +++ b/src/Resources/PaymentResource.php @@ -4,6 +4,7 @@ use Netbums\Quickpay\DataObjects\Payment; use Netbums\Quickpay\DataObjects\PaymentLink; +use Netbums\Quickpay\Exceptions\CardNotAccepted; use Netbums\Quickpay\Exceptions\Payments\AuthorizePaymentFailed; use Netbums\Quickpay\Exceptions\Payments\CancelPaymentFailed; use Netbums\Quickpay\Exceptions\Payments\CapturePaymentFailed; @@ -16,6 +17,7 @@ use Netbums\Quickpay\Exceptions\Payments\FetchPaymentsFailed; use Netbums\Quickpay\Exceptions\Payments\RefundPaymentFailed; use Netbums\Quickpay\Exceptions\Payments\RenewPaymentFailed; +use Netbums\Quickpay\Exceptions\QuickPayValidationError; use Netbums\Quickpay\Resources\Concerns\QuickpayApiConsumer; use Throwable; @@ -24,15 +26,21 @@ class PaymentResource use QuickpayApiConsumer; /** + * @return array * @throws FetchPaymentsFailed + * @throws CardNotAccepted + * @throws QuickPayValidationError */ - public function all(): object + public function all(): array { $this->method = 'get'; $this->endpoint = 'payments'; + $response = $this->request($this->method, $this->endpoint); + try { $response = $this->request($this->method, $this->endpoint); + } catch (Throwable $exception) { throw new FetchPaymentsFailed( message: 'The payments could not be fetched.', @@ -45,9 +53,11 @@ public function all(): object } /** + * @param Payment $payment + * @return array * @throws CreatePaymentFailed */ - public function create(Payment $payment): object + public function create(Payment $payment): array { $this->method = 'post'; $this->endpoint = 'payments'; @@ -69,9 +79,12 @@ public function create(Payment $payment): object /** * Create or Update the Payment Link * + * @param int $id + * @param PaymentLink $paymentLink + * @return array * @throws CreatePaymentLinkFailed */ - public function createLink(int $id, PaymentLink $paymentLink): object + public function createLink(int $id, PaymentLink $paymentLink): array { $this->method = 'put'; $this->endpoint = 'payments/'.$id.'/link'; @@ -93,9 +106,11 @@ public function createLink(int $id, PaymentLink $paymentLink): object /** * Delete payment link * + * @param int $id + * @return array * @throws DeletePaymentLinkFailed */ - public function deleteLink(int $id): object + public function deleteLink(int $id): array { $this->method = 'delete'; $this->endpoint = 'payments/'.$id.'/link'; @@ -116,9 +131,11 @@ public function deleteLink(int $id): object /** * Get Payment * + * @param int $id + * @return array * @throws FetchPaymentFailed */ - public function find(int $id): object + public function find(int $id): array { $this->method = 'get'; $this->endpoint = 'payments/'.$id; @@ -139,9 +156,11 @@ public function find(int $id): object /** * Create payment session * + * @param int $id + * @return array * @throws CreatePaymentSessionFailed */ - public function createPaymentSession(int $id): object + public function createPaymentSession(int $id): array { $this->method = 'post'; $this->endpoint = 'payments/'.$id.'/session'; @@ -164,7 +183,7 @@ public function createPaymentSession(int $id): object * * @throws AuthorizePaymentFailed */ - public function authorize(int $id, int $amount): object + public function authorize(int $id, int $amount): array { $this->method = 'post'; $this->endpoint = 'payments/'.$id.'/authorize'; @@ -192,7 +211,7 @@ public function authorize(int $id, int $amount): object * * @throws CapturePaymentFailed */ - public function capture(int $id, int $amount): object + public function capture(int $id, int $amount): array { $this->method = 'post'; $this->endpoint = 'payments/'.$id.'/capture'; @@ -220,7 +239,7 @@ public function capture(int $id, int $amount): object * * @throws RefundPaymentFailed */ - public function refund(int $id, int $amount): object + public function refund(int $id, int $amount): array { $this->method = 'post'; $this->endpoint = 'payments/'.$id.'/refund'; @@ -249,7 +268,7 @@ public function refund(int $id, int $amount): object * * @throws CancelPaymentFailed */ - public function cancel(int $id): object + public function cancel(int $id): array { $this->method = 'post'; $this->endpoint = 'payments/'.$id.'/cancel'; @@ -272,7 +291,7 @@ public function cancel(int $id): object * * @throws RenewPaymentFailed */ - public function renew(int $id): object + public function renew(int $id): array { $this->method = 'post'; $this->endpoint = 'payments/'.$id.'/renew'; @@ -295,7 +314,7 @@ public function renew(int $id): object * * @throws CreateFraudConfirmationReportFailed */ - public function createFraudConfirmationReport(int $id, ?string $description = null): object + public function createFraudConfirmationReport(int $id, ?string $description = null): array { $this->method = 'post'; $this->endpoint = 'payments/'.$id.'/fraud-report'; diff --git a/src/Resources/PayoutResource.php b/src/Resources/PayoutResource.php index 38d6125..bb8da90 100644 --- a/src/Resources/PayoutResource.php +++ b/src/Resources/PayoutResource.php @@ -9,43 +9,43 @@ class PayoutResource use QuickpayApiConsumer; // get payouts - public function all() + public function all(): array { } // create payout - public function create() + public function create(): array { } // create or update payout link - public function createOrUpdateLink($id) + public function createOrUpdateLink($id): array { } // delete payout link - public function delete($id) + public function delete($id): array { } // get single payout - public function find($id) + public function find($id): array { } // update payout - public function update($id) + public function update($id): array { } // authorize a payout - public function authorize($id) + public function authorize($id): array { }