mivita/app/Models/ShoppingPayment.php
Kevin 8e09aaefe8 Stripe-Integration (S7.4): Payment-Dashboard für Stripe
- Provider-Status/Uptime: Stripe-Kachel aktiviert; payment:check-uptime prüft
  zusätzlich api.stripe.com/v1/ (401 ohne Key = erreichbar) inkl. Auto-Incident
- Zahlungen: Anbieter-Filter, Provider-Badges, Umsatz je Provider,
  Stripe-Dashboard-Deep-Links (ShoppingPayment/PaymentTransaction::
  stripeDashboardUrl, pi_-Guard, testmodus-bewusst)
- Rohe Transaktionen: Anbieter-Filter/-Spalte, Erfolgsraten je Anbieter;
  Bugfix: Stringable-Vergleich ließ den txaction-Filter immer mit leerem Wert
  greifen — Liste war dauerhaft leer
- Neue Kachel „Abos je Anbieter" (Index + GF-Ansicht) mit Fehlerzählern und
  Stripe-Anteil-Fortschrittsbalken (Auslaufmodell PAYONE → Stripe ablesbar)
- Incident-Kachel weist Stripe/PAYONE getrennt aus; Log-Viewer mit
  Kanal-Umschalter stripe/payone
- Plan-Doku: S7.4-Umsetzungsstatus, Abo-Go-Live 2026-07-10 vermerkt

Tests: PaymentDashboardProviderTest (9 neu), Dashboard-Suite 49 grün.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-10 16:57:50 +00:00

198 lines
7.1 KiB
PHP

<?php
namespace App\Models;
use App\Services\Util;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Carbon;
/**
* App\Models\ShoppingPayment
*
* @property int $id
* @property int $shopping_order_id
* @property string $clearingtype
* @property string|null $wallettype
* @property string|null $onlinebanktransfertype
* @property string $reference
* @property int $amount
* @property string $currency
* @property string|null $status
* @property string|null $txaction
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @property-read Collection|PaymentTransaction[] $payment_transactions
* @property-read int|null $payment_transactions_count
* @property-read ShoppingOrder $shopping_order
*
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingPayment newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingPayment newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingPayment query()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingPayment whereAmount($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingPayment whereClearingtype($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingPayment whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingPayment whereCurrency($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingPayment whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingPayment whereOnlinebanktransfertype($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingPayment whereReference($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingPayment whereShoppingOrderId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingPayment whereStatus($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingPayment whereTxaction($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingPayment whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingPayment whereWallettype($value)
*
* @property string|null $mode
*
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ShoppingPayment whereMode($value)
*
* @property array|null $carddata
* @property int|null $is_abo
* @property int|null $abo_interval
*
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingPayment whereAboInterval($value)
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingPayment whereCarddata($value)
* @method static \Illuminate\Database\Eloquent\Builder|ShoppingPayment whereIsAbo($value)
*
* @property string|null $identifier
*
* @method static \Illuminate\Database\Eloquent\Builder<static>|ShoppingPayment whereIdentifier($value)
*
* @mixin \Eloquent
*/
class ShoppingPayment extends Model
{
protected $table = 'shopping_payments';
protected $casts = [
'carddata' => 'array',
];
protected $fillable = [
'shopping_order_id',
'payment_provider',
'external_reference',
'clearingtype',
'wallettype',
'onlinebanktransfertype',
'carddata',
'reference',
'amount',
'currency',
'mode',
'is_abo',
'abo_interval',
'identifier',
'stripe_customer_id',
'stripe_payment_method_id',
'user_abo_id',
];
public function shopping_order()
{
return $this->belongsTo('App\Models\ShoppingOrder', 'shopping_order_id');
}
public function payment_transactions()
{
return $this->hasMany('App\Models\PaymentTransaction', 'shopping_payment_id');
}
public function getPaymentType()
{
if ($this->payment_provider === 'stripe') {
return $this->stripePaymentTypeLabel();
}
if ($this->clearingtype === 'wlt') {
if ($this->wallettype === 'PPE') {
return __('payment.paypal');
}
}
if ($this->clearingtype === 'cc') {
return __('payment.credit_card');
}
if ($this->clearingtype === 'vor') {
return __('payment.prepayment');
}
if ($this->clearingtype === 'elv') {
return __('payment.sepa_direct_debit');
}
if ($this->clearingtype === 'sb') {
if ($this->onlinebanktransfertype === 'PNT') {
return __('payment.sofort_bank_transfer');
}
}
if ($this->clearingtype === 'fnc') {
if ($this->onlinebanktransfertype === 'MIV') {
return __('payment.purchase_on_account');
}
}
return __('payment.unknown');
}
public function getPaymentAmount()
{
return Util::formatNumber($this->amount / 100).' '.$this->currency;
}
/**
* Anzeige-Label für Stripe-Zahlungen aus den gespeicherten
* payment_method_details (StripeWebhookHandler::storePaymentMethodDetails).
*/
private function stripePaymentTypeLabel(): string
{
return self::stripeLabelFromSummary(is_array($this->carddata) ? $this->carddata : []);
}
/**
* Statisch nutzbar für alle Stellen mit derselben carddata-Summary
* (z. B. UserAbo::getPaymentType für Stripe-Abos).
*
* @param array<string, mixed> $details
*/
public static function stripeLabelFromSummary(array $details): string
{
$type = $details['type'] ?? null;
$cardSuffix = ! empty($details['brand']) && ! empty($details['last4'])
? ' ('.ucfirst($details['brand']).' •••• '.$details['last4'].')'
: '';
if ($type === 'card') {
$wallet = $details['wallet'] ?? null;
if ($wallet === 'apple_pay') {
return 'Apple Pay'.$cardSuffix;
}
if ($wallet === 'google_pay') {
return 'Google Pay'.$cardSuffix;
}
return __('payment.credit_card').$cardSuffix;
}
return match ($type) {
'paypal' => __('payment.paypal'),
'klarna' => 'Klarna',
'amazon_pay' => 'Amazon Pay',
'sepa_debit' => __('payment.sepa_direct_debit').(! empty($details['last4']) ? ' (•••• '.$details['last4'].')' : ''),
default => __('payment.stripe_online_short'),
};
}
/**
* Deep-Link ins Stripe-Dashboard, sobald ein PaymentIntent zugeordnet ist
* (external_reference; vor dem ersten Webhook steht dort noch die Session-ID).
*/
public function stripeDashboardUrl(): ?string
{
if ($this->payment_provider !== 'stripe' || ! str_starts_with((string) $this->external_reference, 'pi_')) {
return null;
}
$base = $this->mode === 'test' ? 'https://dashboard.stripe.com/test' : 'https://dashboard.stripe.com';
return $base.'/payments/'.$this->external_reference;
}
}