From 22640e94e366f5052a88f11ccf1b99a92cf87031 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Sun, 3 Sep 2023 15:38:28 +0000 Subject: [PATCH] Apply fixes from StyleCI --- ...023_08_26_100000_create_invoices_table.php | 2 +- src/Contracts/Gateway.php | 10 +++---- src/Gateways/Payir.php | 30 +++++++++---------- src/LyraService.php | 15 +++++----- src/Models/Invoice.php | 16 +++++----- 5 files changed, 37 insertions(+), 36 deletions(-) diff --git a/database/migrations/2023_08_26_100000_create_invoices_table.php b/database/migrations/2023_08_26_100000_create_invoices_table.php index 7caf595..1fbb380 100644 --- a/database/migrations/2023_08_26_100000_create_invoices_table.php +++ b/database/migrations/2023_08_26_100000_create_invoices_table.php @@ -20,7 +20,7 @@ public function up(): void $table->string('token', 128)->nullable()->unique(); $table->string('transaction_id', 256)->nullable()->unique(); $table->string('gateway'); - $table->unsignedDecimal('amount',10); + $table->unsignedDecimal('amount', 10); $table->string('status', 32)->default(Status::PENDING); $table->timestamps(); }); diff --git a/src/Contracts/Gateway.php b/src/Contracts/Gateway.php index 3f21b81..782646b 100644 --- a/src/Contracts/Gateway.php +++ b/src/Contracts/Gateway.php @@ -16,11 +16,11 @@ public function __construct( string $mode = null ) { $this->settings = lyra_config('gateways.'.static::class); - $this->client = new Client(['http_errors' => false]); - if ($mode and key_exists($mode, $this->settings[ 'modes' ])) { + $this->client = new Client(['http_errors' => false]); + if ($mode and key_exists($mode, $this->settings['modes'])) { $this->mode = $mode; } else { - $this->mode = $this->settings[ 'mode' ] ?? 'normal'; + $this->mode = $this->settings['mode'] ?? 'normal'; } } @@ -36,7 +36,7 @@ abstract public function errorsList(): array; protected function apis(): array { - return $this->settings[ 'modes' ][ $this->mode ] ?? []; + return $this->settings['modes'][$this->mode] ?? []; } public function isSandboxEnabled(): bool @@ -47,7 +47,7 @@ public function isSandboxEnabled(): bool protected function translateError(int $code, string $default = 'Failed to process the request!'): string { if (key_exists($code, $this->errorsList())) { - return $this->errorsList()[ $code ]; + return $this->errorsList()[$code]; } return $default; diff --git a/src/Gateways/Payir.php b/src/Gateways/Payir.php index 391817b..cd49f2c 100644 --- a/src/Gateways/Payir.php +++ b/src/Gateways/Payir.php @@ -9,19 +9,19 @@ class Payir extends Gateway { public function request(): string { - $data = array_diff_key( + $data = array_diff_key( $this->settings, array_flip(['mode', 'modes']) ); - $data[ 'amount' ] = $this->amount; - $data = array_filter($data, fn ($item) => ! empty($item)); + $data['amount'] = $this->amount; + $data = array_filter($data, fn ($item) => !empty($item)); if ($this->isSandboxEnabled()) { - $data[ 'api' ] = 'test'; + $data['api'] = 'test'; } $response = $this->client->post( - $this->apis()[ 'purchase' ], + $this->apis()['purchase'], [ 'json' => $data, 'headers' => [ @@ -31,13 +31,13 @@ public function request(): string ) ->getBody() ->getContents(); - $result = json_decode($response, true); + $result = json_decode($response, true); - if ($result[ 'status' ] == 1) { - return $result[ 'token' ]; + if ($result['status'] == 1) { + return $result['token']; } - throw new \Exception($this->translateError($result[ 'errorCode' ])); + throw new \Exception($this->translateError($result['errorCode'])); } public function pay(string $token): string @@ -45,14 +45,14 @@ public function pay(string $token): string return str_replace( ':token', $token, - $this->apis()[ 'payment' ] + $this->apis()['payment'] ); } public function verify(Invoice $invoice): bool { $status = request('status'); - $token = $this->getTokenFromRequest(); + $token = $this->getTokenFromRequest(); if ($status != 1) { // User canceled the purchase @@ -66,12 +66,12 @@ public function verify(Invoice $invoice): bool // TODO: Compare $token with stored token on pay stage $data = [ - 'api' => $this->settings[ 'api' ], + 'api' => $this->settings['api'], 'token' => $token, ]; $response = $this->client->post( - $this->apis()[ 'verification' ], + $this->apis()['verification'], [ 'json' => $data, 'headers' => [ @@ -81,9 +81,9 @@ public function verify(Invoice $invoice): bool ) ->getBody() ->getContents(); - $result = json_decode($response, true); + $result = json_decode($response, true); - if ($result[ 'status' ] !== 1) { + if ($result['status'] !== 1) { return false; } diff --git a/src/LyraService.php b/src/LyraService.php index 9d6182a..4188c02 100644 --- a/src/LyraService.php +++ b/src/LyraService.php @@ -22,14 +22,14 @@ public function __construct() public function pay(int $amount): self { - if ( ! isset($this->gateway)) { + if (!isset($this->gateway)) { $this->gateway = $this->setGateway(lyra_config('gateways.default'), $amount); } - $token = $this->gateway->request(); - $this->invoice->token = $token; - $this->invoice->gateway = $this->gateway::class; - $this->invoice->amount = $this->gateway::class; + $token = $this->gateway->request(); + $this->invoice->token = $token; + $this->invoice->gateway = $this->gateway::class; + $this->invoice->amount = $this->gateway::class; $this->gatewayRedirectUrl = $this->gateway->pay($token); return $this; @@ -63,12 +63,13 @@ class_exists($gateway), public function verify(): bool { - if ( ! isset($this->gateway)) { + if (!isset($this->gateway)) { $this->gateway = $this->setGateway(lyra_config('gateways.default')); } $token = $this->gateway->getTokenFromRequest(); if (is_null($token)) { $gatewayClass = get_class($this->gateway); + throw LyraException::make( "Wrong gateway [$gatewayClass] selected for verification!", LyraErrorCode::WRONG_GATEWAY_CLASS_SELECTED @@ -78,7 +79,7 @@ public function verify(): bool $this->invoice = $this->findOrCreateInvoice($token); $this->gateway->setAmount($this->invoice->amount); - if ( ! $this->gateway->verify($this->invoice)) { + if (!$this->gateway->verify($this->invoice)) { throw LyraException::make( "Verifying Invoice #{$this->invoice->number} failed!", LyraErrorCode::FAILED_TO_VERIFYING diff --git a/src/Models/Invoice.php b/src/Models/Invoice.php index 9afa9ff..a7a53d7 100644 --- a/src/Models/Invoice.php +++ b/src/Models/Invoice.php @@ -8,13 +8,13 @@ use Illuminate\Support\Collection; /** - * @property int $id - * @property int $number - * @property string $token - * @property int $transaction_id - * @property string $gateway - * @property int $amount - * @property Status $status + * @property int $id + * @property int $number + * @property string $token + * @property int $transaction_id + * @property string $gateway + * @property int $amount + * @property Status $status * @property Collection $items */ class Invoice extends Model @@ -40,7 +40,7 @@ class Invoice extends Model */ protected $casts = [ 'status' => Status::class, - 'amount' => 'decimal' + 'amount' => 'decimal', ]; /**