Skip to content

Commit

Permalink
fix client return types
Browse files Browse the repository at this point in the history
  • Loading branch information
mortenebak committed Jan 9, 2024
1 parent 145f3dc commit c4e9a77
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 32 deletions.
5 changes: 5 additions & 0 deletions src/Quickpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public function __construct()

}

public static function api(): static
{
return new static();
}

public function payments(): PaymentResource
{
return new PaymentResource(
Expand Down
15 changes: 10 additions & 5 deletions src/QuickpayServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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');
}

Expand Down
18 changes: 10 additions & 8 deletions src/Resources/Concerns/QuickpayApiConsumer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
);
}
}
Expand Down
43 changes: 31 additions & 12 deletions src/Resources/PaymentResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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.',
Expand All @@ -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';
Expand All @@ -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';
Expand All @@ -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';
Expand All @@ -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;
Expand All @@ -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';
Expand All @@ -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';
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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';
Expand Down Expand Up @@ -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';
Expand All @@ -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';
Expand All @@ -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';
Expand Down
14 changes: 7 additions & 7 deletions src/Resources/PayoutResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{

}
Expand Down

0 comments on commit c4e9a77

Please sign in to comment.