diff --git a/database/migrations/create_payments_table.php.stub b/database/migrations/create_payments_table.php.stub index ee8e087..5ad904c 100644 --- a/database/migrations/create_payments_table.php.stub +++ b/database/migrations/create_payments_table.php.stub @@ -27,7 +27,7 @@ class CreatePaymentsTable extends Migration $table->string('description'); $table->string('state')->default(PaymentState::defaultValue()); - $table->string('provider'); + $table->json('data'); $table->string('gateway_id')->nullable(); diff --git a/src/LaravelPayments.php b/src/LaravelPayments.php new file mode 100644 index 0000000..febb5b9 --- /dev/null +++ b/src/LaravelPayments.php @@ -0,0 +1,41 @@ +handle($payment); + + if ($callback) { + $callback($response); + } + + return $payment; + } + + public static function complete(Payment $payment, Closure $callback = null): Payment + { + $response = (new CompletePaymentHandler())->handle($payment); + + if ($callback) { + $callback($response); + } + + return $payment; + } + + public static function check(Payment $payment): bool + { + return (new CheckPaymentHandler())->handle($payment); + } + +} \ No newline at end of file diff --git a/src/Payments/Payment.php b/src/Payments/Payment.php index 5e9cedc..fbc9af7 100644 --- a/src/Payments/Payment.php +++ b/src/Payments/Payment.php @@ -3,13 +3,10 @@ namespace litvinjuan\LaravelPayments\Payments; use Carbon\Carbon; -use Closure; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\MorphTo; use Illuminate\Database\Eloquent\SoftDeletes; use Konekt\Enum\Eloquent\CastsEnums; -use litvinjuan\LaravelPayments\Handlers\CheckPaymentHandler; -use litvinjuan\LaravelPayments\Handlers\CompletePaymentHandler; -use litvinjuan\LaravelPayments\Handlers\PurchasePaymentHandler; use Money\Money; /** @@ -23,7 +20,7 @@ * @property Money $paid * * @property PaymentState $state - * @property string $provider + * @property string $data * * @property-read Payable $payable * @property-read Payer $payer @@ -36,7 +33,6 @@ class Payment extends Model { use SoftDeletes; use CastsEnums; - use PaymentAttributes; protected $dates = [ 'completed_at' @@ -46,31 +42,34 @@ class Payment extends Model 'state' => PaymentState::class, ]; - public function purchase(Closure $callback = null): self + public function payable(): MorphTo { - $response = (new PurchasePaymentHandler())->handle($this); - - if ($callback) { - $callback($response); - } + return $this->morphTo(); + } - return $this; + public function payer(): MorphTo + { + return $this->morphTo(); } - public function complete(Closure $callback = null): self + public function getPriceAttribute(): Money { - $response = (new CompletePaymentHandler())->handle($this); + return money($this->attributes['price']); + } - if ($callback) { - $callback($response); - } + public function setPriceAttribute(Money $money) + { + $this->attributes['price'] = $money->getAmount(); + } - return $this; + public function getPaidAttribute(): Money + { + return money($this->attributes['paid']); } - public function check(): bool + public function setPaidAttribute(Money $money) { - return (new CheckPaymentHandler())->handle($this); + $this->attributes['paid'] = $money->getAmount(); } } diff --git a/src/Payments/PaymentAttributes.php b/src/Payments/PaymentAttributes.php deleted file mode 100644 index 3c956b0..0000000 --- a/src/Payments/PaymentAttributes.php +++ /dev/null @@ -1,41 +0,0 @@ -morphTo(); - } - - public function payer(): MorphTo - { - return $this->morphTo(); - } - - public function getPriceAttribute(): Money - { - return money($this->attributes['price']); - } - - public function setPriceAttribute(Money $money) - { - $this->attributes['price'] = $money->getAmount(); - } - - public function getPaidAttribute(): Money - { - return money($this->attributes['paid']); - } - - public function setPaidAttribute(Money $money) - { - $this->attributes['paid'] = $money->getAmount(); - } - -}