Skip to content

Commit

Permalink
Merge pull request #6 from hans-thomas/analysis-Ko6dg2
Browse files Browse the repository at this point in the history
Apply fixes from StyleCI
  • Loading branch information
hans-thomas authored Sep 3, 2023
2 parents d5a8866 + 22640e9 commit 7c18e2e
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
10 changes: 5 additions & 5 deletions src/Contracts/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}

Expand All @@ -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
Expand All @@ -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;
Expand Down
30 changes: 15 additions & 15 deletions src/Gateways/Payir.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand All @@ -31,28 +31,28 @@ 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
{
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
Expand All @@ -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' => [
Expand All @@ -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;
}

Expand Down
15 changes: 8 additions & 7 deletions src/LyraService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
16 changes: 8 additions & 8 deletions src/Models/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,7 +40,7 @@ class Invoice extends Model
*/
protected $casts = [
'status' => Status::class,
'amount' => 'decimal'
'amount' => 'decimal',
];

/**
Expand Down

0 comments on commit 7c18e2e

Please sign in to comment.