Skip to content

Commit

Permalink
rename gateway to method
Browse files Browse the repository at this point in the history
  • Loading branch information
myagmarsurensedjav committed Aug 17, 2023
1 parent d1905d2 commit e743024
Show file tree
Hide file tree
Showing 24 changed files with 128 additions and 128 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' => [
'methods' => [
'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 method, 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' => [
'methods' => [
'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('method', 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('method_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\WithMethodData;
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\Methods\AbstractGateway;
use MyagmarsurenSedjav\SimplePayment\Methods\AbstractPaymentMethod;
use MyagmarsurenSedjav\SimplePayment\PendingPayment;

class CreatePayment
{
public function __invoke(AbstractGateway $gateway, Payable $payable, array $options = []): PendingPayment
public function __invoke(AbstractPaymentMethod $method, 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($method, $payable, $options));
}

private function process(AbstractGateway $gateway, Payable $payable, array $options = []): PendingPayment
private function process(AbstractPaymentMethod $method, 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(),
'method' => $method->name(),
'options' => $options,
]);

// Төлбөрийг тухайн төлбөрийн гарцад бүртгэж өгнө.
$pendingPayment = $gateway->register($payment, $options);
$pendingPayment = $method->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 WithMethodData) {
$attributesShouldBeUpdated['method_data'] = $pendingPayment->getMethodData();
}

// Тухайн төлбөрийн гарц дуусах хугацаатай бол хадгалж авна.
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\Methods\AbstractGateway;
use MyagmarsurenSedjav\SimplePayment\Methods\AbstractPaymentMethod;
use MyagmarsurenSedjav\SimplePayment\Payment;

class VerifyPayment
{
public function __invoke(AbstractGateway $gateway, Payment $payment): CheckedPayment
public function __invoke(AbstractPaymentMethod $method, Payment $payment): CheckedPayment
{
$result = $gateway->check($payment);
$result = $method->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 $response = [])
{
}

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,
'response' => $this->response,
];
}
}
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 $method): Payment
{
return app(CreatePayment::class)($gateway, $this);
return app(CreatePayment::class)($method, $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 WithMethodData
{
public function getGatewayData(): array;
public function getMethodData(): array;
}
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\Methods\AbstractGateway;
use MyagmarsurenSedjav\SimplePayment\Methods\AbstractPaymentMethod;
use MyagmarsurenSedjav\SimplePayment\PendingPayment;
use MyagmarsurenSedjav\SimplePayment\SimplePaymentManager;

/**
* @method static AbstractGateway driver($model, array $options = [])
* @method static AbstractPaymentMethod driver($model, array $options = [])
* @method static PendingPayment create($model, array $options = [])
* @method static mixed onBrowserReturn(\Closure $handler)
* @method static string paymentModel()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use MyagmarsurenSedjav\SimplePayment\Payment;
use MyagmarsurenSedjav\SimplePayment\PendingPayment;

abstract class AbstractGateway
abstract class AbstractPaymentMethod
{
public function __construct(public readonly string $name, protected readonly array $config)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Methods/Golomt/GolomtCheckedPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ public function status(): PaymentStatus

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

private function errorCode()
{
return Arr::get($this->gatewayResponse, 'errorCode');
return Arr::get($this->response, 'errorCode');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace MyagmarsurenSedjav\SimplePayment\Methods\Golomt;

use MyagmarsurenSedjav\SimplePayment\CheckedPayment;
use MyagmarsurenSedjav\SimplePayment\Methods\AbstractGateway;
use MyagmarsurenSedjav\SimplePayment\Methods\AbstractPaymentMethod;
use MyagmarsurenSedjav\SimplePayment\Payment;
use MyagmarsurenSedjav\SimplePayment\PendingPayment;

class GolomtGateway extends AbstractGateway
class GolomtPaymentMethod extends AbstractPaymentMethod
{
private GolomtClient $client;

Expand Down
2 changes: 1 addition & 1 deletion src/Methods/Golomt/GolomtPendingPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function asSocialPay(): static

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

public function getExpiresAt(): Carbon
Expand Down
4 changes: 2 additions & 2 deletions src/Methods/Qpay/QpayCheckedPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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->response, 'count') > 0
&& Arr::get($this->response, 'paid_amount') > 0;
}

public function status(): PaymentStatus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
namespace MyagmarsurenSedjav\SimplePayment\Methods\Qpay;

use MyagmarsurenSedjav\SimplePayment\CheckedPayment;
use MyagmarsurenSedjav\SimplePayment\Methods\AbstractGateway;
use MyagmarsurenSedjav\SimplePayment\Methods\AbstractPaymentMethod;
use MyagmarsurenSedjav\SimplePayment\Payment;
use MyagmarsurenSedjav\SimplePayment\PendingPayment;

class QpayGateway extends AbstractGateway
class QpayPaymentMethod extends AbstractPaymentMethod
{
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
8 changes: 4 additions & 4 deletions src/Methods/Qpay/QpayPendingPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ class QpayPendingPayment extends PendingPayment implements ShouldRender, WithBas
{
public function getBase64QrImage(): string
{
return $this->gatewayResponse['qr_image'];
return $this->response['qr_image'];
}

public function getRedirectUrl(): string
{
return $this->gatewayResponse['qPay_shortUrl'];
return $this->response['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->response['urls'],
]);
}

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

public function getTransactionFee(): float
Expand Down
20 changes: 10 additions & 10 deletions src/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,26 @@
use MyagmarsurenSedjav\SimplePayment\Contracts\Payable;
use MyagmarsurenSedjav\SimplePayment\Enums\PaymentStatus;
use MyagmarsurenSedjav\SimplePayment\Facades\SimplePayment;
use MyagmarsurenSedjav\SimplePayment\Methods\AbstractGateway;
use MyagmarsurenSedjav\SimplePayment\Methods\AbstractPaymentMethod;
use MyagmarsurenSedjav\SimplePayment\Support\PaymentFactory;

/**
* @property string $id
* @property float $amount
* @property string $gateway_transaction_id
* @property string $transaction_id
* @property ?Payable $payable
* @property string $error_message
* @property PaymentStatus $status
* @property Carbon $created_at
* @property string $description
* @property array|mixed $qpay
* @property int $verifies_count
* @property string $gateway
* @property string $method
* @property Carbon $paid_at
* @property Carbon $verified_at
* @property Carbon $expires_at
* @property Carbon $gateway_transaction_fee
* @property array $gateway_data
* @property Carbon $transaction_fee
* @property array $method_data
* @property string $user_id
* @property string $payable_type
* @property string $payable_id
Expand All @@ -53,7 +53,7 @@ class Payment extends Model

protected $casts = [
'status' => PaymentStatus::class,
'gateway_data' => 'array',
'method_data' => 'array',
'paid_at' => 'datetime',
'verified_at' => 'datetime',
'expires_at' => 'datetime',
Expand Down Expand Up @@ -100,17 +100,17 @@ public function isPaid(): bool

public function verify(): CheckedPayment
{
return $this->gateway()->verify($this);
return $this->method()->verify($this);
}

public function check(): CheckedPayment
{
return $this->gateway()->check($this);
return $this->method()->check($this);
}

public function gateway(): AbstractGateway
public function method(): AbstractPaymentMethod
{
return SimplePayment::driver($this->gateway);
return SimplePayment::driver($this->method);
}

public function setOptionsAttribute(array $options)
Expand Down
4 changes: 2 additions & 2 deletions src/PendingPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

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

Expand All @@ -19,7 +19,7 @@ public function toArray(): array
'handler' => $this instanceof ShouldRedirect ? 'redirect' : 'render',
'redirect_url' => $this instanceof ShouldRedirect ? $this->getRedirectUrl() : null,
'payment' => $this->payment,
'gateway_response' => $this->gatewayResponse,
'response' => $this->response,
];
}

Expand Down
Loading

0 comments on commit e743024

Please sign in to comment.