php-commerce/Models/Payment.php

184 lines
4.2 KiB
PHP
Raw Normal View History

2026-01-27 00:24:22 +00:00
<?php
declare(strict_types=1);
namespace Core\Mod\Commerce\Models;
2026-01-27 00:24:22 +00:00
use Core\Mod\Commerce\Database\Factories\PaymentFactory;
use Core\Tenant\Models\Workspace;
2026-01-27 00:24:22 +00:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
/**
* Payment model representing money received.
*
* @property int $id
* @property int $workspace_id
* @property int|null $invoice_id
* @property string $gateway
* @property string|null $gateway_payment_id
* @property string|null $gateway_customer_id
* @property string $currency
* @property float $amount
* @property float $fee
* @property float $net_amount
* @property string $status
* @property string|null $failure_reason
* @property string|null $payment_method_type
* @property string|null $payment_method_last4
* @property string|null $payment_method_brand
* @property array|null $gateway_response
* @property float $refunded_amount
*/
class Payment extends Model
{
use HasFactory;
protected static function newFactory(): PaymentFactory
2026-01-27 00:24:22 +00:00
{
return PaymentFactory::new();
2026-01-27 00:24:22 +00:00
}
protected $fillable = [
'workspace_id',
'invoice_id',
'order_id',
feat(commerce): implement RFC.md — billing, subscriptions, Stripe + BTCPay, Commerce Matrix (#845) Extends prior #860 DunningService with the full RFC.md surface. Lands across 44 modified/new files: * Contracts/PaymentGatewayContract.php — implemented by both Services/StripeGateway.php and Services/BTCPayGateway.php * Boot.php — provider bindings + route groups + Commerce Matrix training mode middleware * Services/WebhookService.php — DB::transaction wrapping + ProcessWebhookEvent job dispatched ->afterCommit; idempotency via webhook_events unique (gateway, event_id) — duplicates rejected silently * Jobs/ProcessWebhookEvent.php * DTOs/ — readonly PHP 8.2+ classes per RFC.dto.md * Services/SubscriptionStateMachine.php — active → suspended (failed payment) → cancelled → expired transitions * Services/ProrationService.php — credit unused old plan time, charge new plan remainder, applied via CreditNote + Invoice * DunningService extended — 1d/3d/7d/14d retry config + cancel * Migrations — guarded migrations for missing short-name billing tables (orders/payments/invoices) + RFC compatibility columns * routes/api.php — /v1/* endpoints * Checkout success/cancel routes * Commerce Matrix training-mode endpoint + record-permissions logic * Console/Commands — RFC.commands.md signatures * Events per RFC.events.md * Models extended php -l clean. composer validate passes. pest unrunnable in sandbox. Co-authored-by: Codex <noreply@openai.com> Closes tasks.lthn.sh/view.php?id=845
2026-04-25 22:55:49 +01:00
'payment_method_id',
2026-01-27 00:24:22 +00:00
'gateway',
'gateway_payment_id',
'gateway_customer_id',
'currency',
'amount',
'fee',
'net_amount',
'status',
'failure_reason',
'payment_method_type',
'payment_method_last4',
'payment_method_brand',
'gateway_response',
'refunded_amount',
'paid_at',
];
protected $casts = [
'amount' => 'decimal:2',
'fee' => 'decimal:2',
'net_amount' => 'decimal:2',
'refunded_amount' => 'decimal:2',
'gateway_response' => 'array',
'paid_at' => 'datetime',
];
// Relationships
public function workspace(): BelongsTo
{
return $this->belongsTo(Workspace::class);
}
public function invoice(): BelongsTo
{
return $this->belongsTo(Invoice::class);
}
public function refunds(): HasMany
{
return $this->hasMany(Refund::class);
}
// Status helpers
public function isPending(): bool
{
return $this->status === 'pending';
}
public function isProcessing(): bool
{
return $this->status === 'processing';
}
public function isSucceeded(): bool
{
return $this->status === 'succeeded';
}
public function isFailed(): bool
{
return $this->status === 'failed';
}
public function isRefunded(): bool
{
return $this->status === 'refunded';
}
public function isPartiallyRefunded(): bool
{
return $this->status === 'partially_refunded';
}
public function canRefund(): bool
{
return $this->isSucceeded() || $this->isPartiallyRefunded();
}
public function isFullyRefunded(): bool
{
return $this->refunded_amount >= $this->amount;
}
public function getRefundableAmount(): float
{
return $this->amount - $this->refunded_amount;
}
// Actions
public function markAsSucceeded(): void
{
$this->update(['status' => 'succeeded']);
}
public function markAsFailed(?string $reason = null): void
{
$this->update([
'status' => 'failed',
'failure_reason' => $reason,
]);
}
public function recordRefund(float $amount): void
{
$newRefundedAmount = $this->refunded_amount + $amount;
$status = $newRefundedAmount >= $this->amount
? 'refunded'
: 'partially_refunded';
$this->update([
'refunded_amount' => $newRefundedAmount,
'status' => $status,
]);
}
// Scopes
public function scopeSucceeded($query)
{
return $query->where('status', 'succeeded');
}
public function scopeForWorkspace($query, int $workspaceId)
{
return $query->where('workspace_id', $workspaceId);
}
public function scopeForGateway($query, string $gateway)
{
return $query->where('gateway', $gateway);
}
}