Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
orkhanahmadov committed Nov 11, 2020
1 parent 2e4a74f commit b892217
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 5 deletions.
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github: [orkhanahmadov]
9 changes: 9 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,13 @@

'shop_id' => env('YANDEX_CHECKOUT_SHOP_ID'),
'secret_key' => env('YANDEX_CHECKOUT_SECRET_KEY'),

'events' => [
'enabled' => true,

'created' => \Orkhanahmadov\YandexCheckout\Events\CheckoutCreated::class,
'checked' => \Orkhanahmadov\YandexCheckout\Events\CheckoutChecked::class,
'succeeded' => \Orkhanahmadov\YandexCheckout\Events\CheckoutSucceeded::class,
'canceled' => \Orkhanahmadov\YandexCheckout\Events\CheckoutCanceled::class,
]
];
2 changes: 1 addition & 1 deletion database/migrations/yandex_checkouts_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class CreateYandexCheckoutsTable extends Migration
$table->unsignedBigInteger('payable_id')->nullable();
$table->index(['payable_type', 'payable_id']);
$table->string('payment_id')->index()->unique();
$table->string('status');
$table->string('status')->index();
$table->json('response');
$table->timestamps();
});
Expand Down
5 changes: 5 additions & 0 deletions src/Events/CheckoutCanceled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Orkhanahmadov\YandexCheckout\Events;

class CheckoutCanceled extends Event {}
5 changes: 5 additions & 0 deletions src/Events/CheckoutChecked.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Orkhanahmadov\YandexCheckout\Events;

class CheckoutChecked extends Event {}
5 changes: 5 additions & 0 deletions src/Events/CheckoutCreated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Orkhanahmadov\YandexCheckout\Events;

class CheckoutCreated extends Event {}
5 changes: 5 additions & 0 deletions src/Events/CheckoutSucceeded.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Orkhanahmadov\YandexCheckout\Events;

class CheckoutSucceeded extends Event {}
28 changes: 28 additions & 0 deletions src/Events/Event.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Orkhanahmadov\YandexCheckout\Events;

use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
use Orkhanahmadov\YandexCheckout\Models\YandexCheckout;

abstract class Event
{
use Dispatchable;
use SerializesModels;

/**
* @var YandexCheckout
*/
public $yandexCheckout;

/**
* Create a new event instance.
*
* @param YandexCheckout $yandexCheckout
*/
public function __construct(YandexCheckout $yandexCheckout)
{
$this->yandexCheckout = $yandexCheckout;
}
}
33 changes: 29 additions & 4 deletions src/Models/YandexCheckout.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Config;

/**
* @property int $id
Expand All @@ -21,17 +20,35 @@
*/
class YandexCheckout extends Model
{
public const STATUS_SUCCESSFUL = 'success';
public const STATUS_SUCCEEDED = 'succeeded';
public const STATUS_CANCELED = 'canceled';

protected $guarded = [];

protected $casts = [
'response' => 'array',
];

public static function booted()
{
if (config('yandex-checkout.events.enabled')) {
static::created(function (YandexCheckout $yandexCheckout) {
if ($event = config('yandex-checkout.events.created')) {
$event::dispatch($yandexCheckout);
}
});

static::updated(function (YandexCheckout $yandexCheckout) {
if ($event = config("yandex-checkout.events.{$yandexCheckout->status}")) {
$event::dispatch($yandexCheckout);
}
});
}
}

public function __construct(array $attributes = [])
{
$this->setTable(Config::get('yandex-checkout.table_name'));
$this->setTable(config('yandex-checkout.table_name'));

parent::__construct($attributes);
}
Expand All @@ -53,6 +70,14 @@ public function getConfirmationUrlAttribute(): ?string

public function scopeSuccessful(Builder $builder): Builder
{
return $builder->where('status', self::STATUS_SUCCESSFUL);
return $builder->where('status', self::STATUS_SUCCEEDED);
}

public function scopePending(Builder $builder): Builder
{
return $builder->whereNotIn('status', [
self::STATUS_SUCCEEDED,
self::STATUS_CANCELED,
]);
}
}

0 comments on commit b892217

Please sign in to comment.