-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #164 from OpenSynergic/157-submission-payment
157 submission payment
- Loading branch information
Showing
60 changed files
with
2,053 additions
and
173 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace App\Facades; | ||
|
||
use App\Managers\PaymentManager; | ||
use Illuminate\Support\Facades\Facade; | ||
|
||
class Payment extends Facade | ||
{ | ||
protected static function getFacadeAccessor() | ||
{ | ||
return PaymentManager::class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace App\Interfaces; | ||
|
||
use App\Models\Payment; | ||
use Filament\Forms\Form; | ||
|
||
interface PaymentDriver | ||
{ | ||
public function getName(): string; | ||
|
||
public function getPaymentFormSchema(): array; | ||
|
||
public function getSettingFormSchema(): array; | ||
|
||
public function getSettingFormFill(): array; | ||
|
||
public function saveSetting(array $data): void; | ||
|
||
public function handlePayment(Payment $payment); | ||
|
||
// public function fillSettingForm(Form $form): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace App\Managers; | ||
|
||
use App\Interfaces\PaymentDriver; | ||
use App\Models\Payment; | ||
use App\Models\PaymentItem; | ||
use App\Models\User; | ||
use App\Services\Payments\ManualPayment; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Support\Facades\App; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Manager; | ||
|
||
class PaymentManager extends Manager | ||
{ | ||
public function getDefaultDriver(): string | ||
{ | ||
return App::getCurrentConference()?->getMeta('payment.method') ?? 'manual'; | ||
} | ||
|
||
public function createManualDriver(): PaymentDriver | ||
{ | ||
return new ManualPayment; | ||
} | ||
|
||
public function createPayment(Model $model, User $user, string $currencyId, array $items, ?string $paymentMethod = null): Payment | ||
{ | ||
try { | ||
DB::beginTransaction(); | ||
|
||
$items = PaymentItem::whereIn('id', $items)->get(); | ||
|
||
$payment = $model->payment ?? new Payment; | ||
$payment->amount = $items->sum(fn ($item) => $item->getAmount($currencyId)); | ||
$payment->currency_id = $currencyId; | ||
$payment->payment_method = $paymentMethod ?? $this->getDefaultDriver(); | ||
if (! $payment->exists) { | ||
$payment->user()->associate($user); | ||
$payment->payable()->associate($model); | ||
} | ||
$payment->save(); | ||
|
||
$payment->setMeta('items', $items->map(fn ($item) => $item->name.': '.$item->getFormattedAmount($currencyId))); | ||
|
||
DB::commit(); | ||
} catch (\Throwable $th) { | ||
DB::rollBack(); | ||
|
||
throw $th; | ||
} | ||
|
||
return $payment; | ||
} | ||
|
||
public function getAllDriverNames(): \Illuminate\Support\Collection | ||
{ | ||
return collect(['manual' => 'manual', ...$this->customCreators])->mapWithKeys(function ($driver, $key) { | ||
return [$key => $this->driver($key)->getName()]; | ||
}); | ||
} | ||
|
||
/** | ||
* Create a new driver instance. | ||
* | ||
* @param string $driver | ||
* @return mixed | ||
* | ||
* @throws \InvalidArgumentException | ||
*/ | ||
protected function createDriver($driver) | ||
{ | ||
try { | ||
return parent::createDriver($driver); | ||
} catch (\Throwable $th) { | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace App\Models\Concerns; | ||
|
||
use App\Models\Payment; | ||
use Illuminate\Database\Eloquent\Relations\MorphOne; | ||
|
||
trait InteractsWithPayment | ||
{ | ||
public function payment(): MorphOne | ||
{ | ||
return $this->morphOne(Payment::class, 'payable'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace App\Models\Enums; | ||
|
||
use App\Models\Enums\Concern\UsefulEnums; | ||
use Filament\Support\Contracts\HasLabel; | ||
|
||
enum PaymentState: string implements HasLabel | ||
{ | ||
use UsefulEnums; | ||
|
||
case Unpaid = 'Unpaid'; | ||
case Processing = 'Processing'; | ||
case Waived = 'Waived'; | ||
case Paid = 'Paid'; | ||
|
||
public function getLabel(): ?string | ||
{ | ||
return $this->name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace App\Models\Enums; | ||
|
||
use App\Models\Enums\Concern\UsefulEnums; | ||
|
||
enum PaymentType: string | ||
{ | ||
use UsefulEnums; | ||
|
||
case Submission = 'Submission'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace App\Models\Interfaces; | ||
|
||
use Illuminate\Database\Eloquent\Relations\MorphOne; | ||
|
||
interface HasPayment | ||
{ | ||
public function payment(): MorphOne; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace App\Models\Meta; | ||
|
||
use App\Models\Meta; | ||
|
||
class PaymentMeta extends Meta | ||
{ | ||
protected $table = 'payment_meta'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use App\Models\Concerns\BelongsToConference; | ||
use App\Models\Enums\PaymentState; | ||
use App\Models\Meta\PaymentMeta; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
use Illuminate\Database\Eloquent\Relations\MorphTo; | ||
use Plank\Metable\Metable; | ||
use Spatie\MediaLibrary\HasMedia; | ||
use Spatie\MediaLibrary\InteractsWithMedia; | ||
|
||
class Payment extends Model implements HasMedia | ||
{ | ||
use BelongsToConference, InteractsWithMedia, Metable; | ||
|
||
/** | ||
* The model's default values for attributes. | ||
* | ||
* @var array | ||
*/ | ||
protected $attributes = [ | ||
'state' => PaymentState::Unpaid, | ||
]; | ||
|
||
/** | ||
* The attributes that should be cast. | ||
* | ||
* @var array | ||
*/ | ||
protected $casts = [ | ||
'state' => PaymentState::class, | ||
'paid_at' => 'datetime', | ||
]; | ||
|
||
public static function booted() | ||
{ | ||
// static::saving(function (Model $model) { | ||
// if($model->state === PaymentState::Paid){ | ||
// $model->paid_at = now(); | ||
// } | ||
// }); | ||
} | ||
|
||
public function payable(): MorphTo | ||
{ | ||
return $this->morphTo(); | ||
} | ||
|
||
public function user(): BelongsTo | ||
{ | ||
return $this->belongsTo(User::class); | ||
} | ||
|
||
protected function getMetaClassName(): string | ||
{ | ||
return PaymentMeta::class; | ||
} | ||
|
||
public function isCompleted(): bool | ||
{ | ||
return $this->state->isOneOf(PaymentState::Paid, PaymentState::Waived); | ||
} | ||
} |
Oops, something went wrong.