Skip to content

Commit

Permalink
rename gateway to driver
Browse files Browse the repository at this point in the history
  • Loading branch information
myagmarsurensedjav committed Aug 19, 2023
1 parent 32e4784 commit c0ad4c2
Show file tree
Hide file tree
Showing 27 changed files with 137 additions and 137 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ This is the contents of the published config file:
return [
'default' => env('SIMPLE_PAYMENT_DEFAULT', 'qpay'),

'gateways' => [
'drivers' => [
'qpay' => [
'env' => env('QPAY_ENV', 'fake'),
'username' => env('QPAY_USERNAME'),
Expand Down Expand Up @@ -78,7 +78,7 @@ Route::get('/invoices/{invoice}/payment', function (Invoice $invoice) {
});
```

If you need specific gateway, you can use `driver` method.
If you need specific driver, you can use `driver` method.

```php
SimplePayment::driver('socialpay')->create($invoice);
Expand Down
2 changes: 1 addition & 1 deletion config/simple-payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
return [
'default' => env('SIMPLE_PAYMENT_DEFAULT', 'qpay'),

'gateways' => [
'drivers' => [
'qpay' => [
'env' => env('QPAY_ENV', 'fake'),
'username' => env('QPAY_USERNAME'),
Expand Down
8 changes: 4 additions & 4 deletions database/migrations/create_payments_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ return new class extends Migration
{
Schema::create('payments', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('gateway', 20)->index();
$table->string('driver', 20)->index();
$table->string('status');
$table->decimal('amount', 12, 2);
$table->foreignIdFor(config('simple-payment.user_model'), 'user_id')->nullable();
$table->uuidMorphs('payable');
$table->string('description');
$table->string('gateway_transaction_id')->nullable();
$table->decimal('gateway_transaction_fee', 12, 2)->nullable();
$table->string('transaction_id')->nullable();
$table->decimal('transaction_fee', 12, 2)->nullable();
$table->text('error_message')->nullable();
$table->json('gateway_data')->nullable();
$table->json('driver_data')->nullable();
$table->timestamp('expires_at')->nullable();
$table->timestamp('paid_at')->nullable();
$table->timestamp('verified_at')->nullable();
Expand Down
22 changes: 11 additions & 11 deletions src/Actions/CreatePayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,28 @@
use MyagmarsurenSedjav\SimplePayment\Contracts\CanBePaidPartially;
use MyagmarsurenSedjav\SimplePayment\Contracts\Payable;
use MyagmarsurenSedjav\SimplePayment\Contracts\Results\WithExpiresAt;
use MyagmarsurenSedjav\SimplePayment\Contracts\Results\WithGatewayData;
use MyagmarsurenSedjav\SimplePayment\Contracts\Results\WithDriverData;
use MyagmarsurenSedjav\SimplePayment\Contracts\Results\WithTransactionFee;
use MyagmarsurenSedjav\SimplePayment\Contracts\Results\WithTransactionId;
use MyagmarsurenSedjav\SimplePayment\Exceptions\NothingToPay;
use MyagmarsurenSedjav\SimplePayment\Facades\SimplePayment;
use MyagmarsurenSedjav\SimplePayment\Gateways\AbstractGateway;
use MyagmarsurenSedjav\SimplePayment\Drivers\AbstractDriver;
use MyagmarsurenSedjav\SimplePayment\PendingPayment;

class CreatePayment
{
public function __invoke(AbstractGateway $gateway, Payable $payable, array $options = []): PendingPayment
public function __invoke(AbstractDriver $driver, Payable $payable, array $options = []): PendingPayment
{
if ($payable->getPaymentAmount() <= 0) {
throw new NothingToPay(__('Payment amount cannot be zero.'));
}

$this->guardAgainstInvalidAmountOption($options, $payable);

return DB::transaction(fn () => $this->process($gateway, $payable, $options));
return DB::transaction(fn () => $this->process($driver, $payable, $options));
}

private function process(AbstractGateway $gateway, Payable $payable, array $options = []): PendingPayment
private function process(AbstractDriver $driver, Payable $payable, array $options = []): PendingPayment
{
// Урьдчилаад хүлээгдэж байгаа төлбөрийг өгөгдлийн санд үүсгээд өгнө.
$payment = SimplePayment::paymentModel()::create([
Expand All @@ -38,29 +38,29 @@ private function process(AbstractGateway $gateway, Payable $payable, array $opti
'description' => $payable->getPaymentDescription(),
'payable_type' => $payable->getMorphClass(),
'payable_id' => $payable->getKey(),
'gateway' => $gateway->name(),
'driver' => $driver->name(),
'options' => $options,
]);

// Төлбөрийг тухайн төлбөрийн гарцад бүртгэж өгнө.
$pendingPayment = $gateway->register($payment, $options);
$pendingPayment = $driver->register($payment, $options);

$attributesShouldBeUpdated = [];

// Хэрэв тухайн төлбөрийн хэлбэр нь гүйлгээг шалгахад өөрийн гүйлгээний
// дугаарыг ашиглахыг шаарддаг бол хадгалж авах хэрэгтэй болно.
if ($pendingPayment instanceof WithTransactionId) {
$attributesShouldBeUpdated['gateway_transaction_id'] = $pendingPayment->getTransactionId();
$attributesShouldBeUpdated['transaction_id'] = $pendingPayment->getTransactionId();
}

// Тухайн төлбөрийн гарц гүйлгээг хийхэд шимтгэл авдаг бол хадгалж авна.
if ($pendingPayment instanceof WithTransactionFee) {
$attributesShouldBeUpdated['gateway_transaction_fee'] = $pendingPayment->getTransactionFee();
$attributesShouldBeUpdated['transaction_fee'] = $pendingPayment->getTransactionFee();
}

// Тухайн төлбөрийн гарц нэмэлт өгөгдөлтэй бол хадгалж авна.
if ($pendingPayment instanceof WithGatewayData) {
$attributesShouldBeUpdated['gateway_data'] = $pendingPayment->getGatewayData();
if ($pendingPayment instanceof WithDriverData) {
$attributesShouldBeUpdated['driver_data'] = $pendingPayment->getDriverData();
}

// Тухайн төлбөрийн гарц дуусах хугацаатай бол хадгалж авна.
Expand Down
6 changes: 3 additions & 3 deletions src/Actions/VerifyPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

use MyagmarsurenSedjav\SimplePayment\CheckedPayment;
use MyagmarsurenSedjav\SimplePayment\Events\PaymentWasMade;
use MyagmarsurenSedjav\SimplePayment\Gateways\AbstractGateway;
use MyagmarsurenSedjav\SimplePayment\Drivers\AbstractDriver;
use MyagmarsurenSedjav\SimplePayment\Payment;

class VerifyPayment
{
public function __invoke(AbstractGateway $gateway, Payment $payment): CheckedPayment
public function __invoke(AbstractDriver $driver, Payment $payment): CheckedPayment
{
$result = $gateway->check($payment);
$result = $driver->check($payment);

$attributesShouldBeUpdated = [
'status' => $result->status(),
Expand Down
4 changes: 2 additions & 2 deletions src/CheckedPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

abstract class CheckedPayment implements Arrayable
{
public function __construct(public Payment $payment, public array $gatewayResponse = [])
public function __construct(public Payment $payment, public array $driverResponse = [])
{
}

Expand All @@ -26,7 +26,7 @@ public function toArray(): array
'status' => $this->status(),
'error_message' => $this->errorMessage(),
'payment' => $this->payment->toArray(),
'gateway_response' => $this->gatewayResponse,
'driver_response' => $this->driverResponse,
];
}
}
4 changes: 2 additions & 2 deletions src/Concerns/InteractsWithPayments.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public function payments(): MorphMany
return $this->morphMany(SimplePayment::paymentModel(), 'payable');
}

public function createPayment(string $gateway): Payment
public function createPayment(string $driver): Payment
{
return app(CreatePayment::class)($gateway, $this);
return app(CreatePayment::class)($driver, $this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace MyagmarsurenSedjav\SimplePayment\Contracts\Results;

interface WithGatewayData
interface WithDriverData
{
public function getGatewayData(): array;
public function getDriverData(): array;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace MyagmarsurenSedjav\SimplePayment\Gateways;
namespace MyagmarsurenSedjav\SimplePayment\Drivers;

use MyagmarsurenSedjav\SimplePayment\Actions\CreatePayment;
use MyagmarsurenSedjav\SimplePayment\Actions\VerifyPayment;
Expand All @@ -9,7 +9,7 @@
use MyagmarsurenSedjav\SimplePayment\Payment;
use MyagmarsurenSedjav\SimplePayment\PendingPayment;

abstract class AbstractGateway
abstract class AbstractDriver
{
public function __construct(public readonly string $name, protected readonly array $config)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace MyagmarsurenSedjav\SimplePayment\Gateways\Golomt;
namespace MyagmarsurenSedjav\SimplePayment\Drivers\Golomt;

use Illuminate\Support\Arr;
use MyagmarsurenSedjav\SimplePayment\CheckedPayment;
Expand All @@ -25,11 +25,11 @@ public function status(): PaymentStatus

public function errorMessage(): string|null
{
return Arr::get($this->gatewayResponse, 'errorDesc');
return Arr::get($this->driverResponse, 'errorDesc');
}

private function errorCode()
{
return Arr::get($this->gatewayResponse, 'errorCode');
return Arr::get($this->driverResponse, 'errorCode');
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace MyagmarsurenSedjav\SimplePayment\Gateways\Golomt;
namespace MyagmarsurenSedjav\SimplePayment\Drivers\Golomt;

use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Http;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

namespace MyagmarsurenSedjav\SimplePayment\Gateways\Golomt;
namespace MyagmarsurenSedjav\SimplePayment\Drivers\Golomt;

use MyagmarsurenSedjav\SimplePayment\CheckedPayment;
use MyagmarsurenSedjav\SimplePayment\Gateways\AbstractGateway;
use MyagmarsurenSedjav\SimplePayment\Drivers\AbstractDriver;
use MyagmarsurenSedjav\SimplePayment\Payment;
use MyagmarsurenSedjav\SimplePayment\PendingPayment;

class GolomtGateway extends AbstractGateway
class GolomtDriver extends AbstractDriver
{
private GolomtClient $client;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace MyagmarsurenSedjav\SimplePayment\Gateways\Golomt;
namespace MyagmarsurenSedjav\SimplePayment\Drivers\Golomt;

use Carbon\Carbon;
use MyagmarsurenSedjav\SimplePayment\Contracts\Results\ShouldRedirect;
Expand All @@ -26,7 +26,7 @@ public function asSocialPay(): static

public function getTransactionId(): string
{
return $this->gatewayResponse['invoice'];
return $this->driverResponse['invoice'];
}

public function getExpiresAt(): Carbon
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace MyagmarsurenSedjav\SimplePayment\Gateways\Qpay;
namespace MyagmarsurenSedjav\SimplePayment\Drivers\Qpay;

use Illuminate\Support\Arr;
use MyagmarsurenSedjav\SimplePayment\CheckedPayment;
Expand All @@ -10,8 +10,8 @@ class QpayCheckedPayment extends CheckedPayment
{
private function isPaidOnQpay(): bool
{
return Arr::get($this->gatewayResponse, 'count') > 0
&& Arr::get($this->gatewayResponse, 'paid_amount') > 0;
return Arr::get($this->driverResponse, 'count') > 0
&& Arr::get($this->driverResponse, 'paid_amount') > 0;
}

public function status(): PaymentStatus
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace MyagmarsurenSedjav\SimplePayment\Gateways\Qpay;
namespace MyagmarsurenSedjav\SimplePayment\Drivers\Qpay;

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

namespace MyagmarsurenSedjav\SimplePayment\Gateways\Qpay;
namespace MyagmarsurenSedjav\SimplePayment\Drivers\Qpay;

use MyagmarsurenSedjav\SimplePayment\CheckedPayment;
use MyagmarsurenSedjav\SimplePayment\Gateways\AbstractGateway;
use MyagmarsurenSedjav\SimplePayment\Drivers\AbstractDriver;
use MyagmarsurenSedjav\SimplePayment\Payment;
use MyagmarsurenSedjav\SimplePayment\PendingPayment;

class QpayGateway extends AbstractGateway
class QpayDriver extends AbstractDriver
{
private QpayClient $client;

Expand All @@ -33,7 +33,7 @@ public function register(Payment $payment, array $options): PendingPayment

public function check(Payment $payment): CheckedPayment
{
$checkedPayment = $this->client->checkPayment($payment->gateway_transaction_id);
$checkedPayment = $this->client->checkPayment($payment->transaction_id);

return new QpayCheckedPayment($payment, $checkedPayment);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace MyagmarsurenSedjav\SimplePayment\Gateways\Qpay;
namespace MyagmarsurenSedjav\SimplePayment\Drivers\Qpay;

use Illuminate\View\View;
use MyagmarsurenSedjav\SimplePayment\Contracts\Results\ShouldRender;
Expand All @@ -14,12 +14,12 @@ class QpayPendingPayment extends PendingPayment implements ShouldRender, WithBas
{
public function getBase64QrImage(): string
{
return $this->gatewayResponse['qr_image'];
return $this->driverResponse['qr_image'];
}

public function getRedirectUrl(): string
{
return $this->gatewayResponse['qPay_shortUrl'];
return $this->driverResponse['qPay_shortUrl'];
}

public function render(): View
Expand All @@ -28,13 +28,13 @@ public function render(): View
'payment' => $this->payment,
'base64QrImage' => $this->getBase64QrImage(),
'redirectUrl' => $this->getRedirectUrl(),
'urls' => $this->gatewayResponse['urls'],
'urls' => $this->driverResponse['urls'],
]);
}

public function getTransactionId(): string
{
return $this->gatewayResponse['invoice_id'];
return $this->driverResponse['invoice_id'];
}

public function getTransactionFee(): float
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions src/Facades/SimplePayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
namespace MyagmarsurenSedjav\SimplePayment\Facades;

use Illuminate\Support\Facades\Facade;
use MyagmarsurenSedjav\SimplePayment\Gateways\AbstractGateway;
use MyagmarsurenSedjav\SimplePayment\Drivers\AbstractDriver;
use MyagmarsurenSedjav\SimplePayment\PendingPayment;
use MyagmarsurenSedjav\SimplePayment\SimplePaymentManager;

/**
* @method static AbstractGateway driver($model, array $options = [])
* @method static AbstractDriver driver($model, array $options = [])
* @method static PendingPayment create($model, array $options = [])
* @method static mixed onBrowserReturn(\Closure $handler)
* @method static string paymentModel()
Expand Down
Loading

0 comments on commit c0ad4c2

Please sign in to comment.