|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Finller\Stripe; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use Illuminate\Database\Eloquent\Model; |
| 7 | +use Illuminate\Support\Arr; |
| 8 | +use Stripe\Account; |
| 9 | +use Stripe\Customer; |
| 10 | + |
| 11 | +class ModelRepository |
| 12 | +{ |
| 13 | + public static function findAccount(string $stripeAccountId): ?Model |
| 14 | + { |
| 15 | + return static::findFromModels( |
| 16 | + models: Arr::wrap(config('stripe.models.accounts')), |
| 17 | + columnName: 'stripe_account_id', |
| 18 | + stripeId: $stripeAccountId |
| 19 | + ); |
| 20 | + } |
| 21 | + |
| 22 | + public static function findCustomer(string $stripeCustomerId): ?Model |
| 23 | + { |
| 24 | + return static::findFromModels( |
| 25 | + models: Arr::wrap(config('stripe.models.customers')), |
| 26 | + columnName: 'stripe_customer_id', |
| 27 | + stripeId: $stripeCustomerId |
| 28 | + ); |
| 29 | + } |
| 30 | + |
| 31 | + public static function findAccountFromStripeObject(Account $account): ?Model |
| 32 | + { |
| 33 | + $model = static::findFromStripeObject($account); |
| 34 | + |
| 35 | + if (! $model) { |
| 36 | + return static::findAccount($account->id); |
| 37 | + } |
| 38 | + |
| 39 | + $model_type = get_class($model); |
| 40 | + $model_id = $model->getKey(); |
| 41 | + |
| 42 | + /** @var ?string $model_stripe_account_id */ |
| 43 | + $model_stripe_account_id = $model->stripe_account_id; // @phpstan-ignore-line |
| 44 | + |
| 45 | + if ($model_stripe_account_id !== $account->id) { |
| 46 | + throw new Exception("[{$model_type}:{$model_id}] Conflict between Stripe account ID and Stripe account metadata: {$model_stripe_account_id} !== {$account->id}", 500); |
| 47 | + } |
| 48 | + |
| 49 | + return $model; |
| 50 | + } |
| 51 | + |
| 52 | + public static function findCustomerFromStripeObject(Customer $customer): ?Model |
| 53 | + { |
| 54 | + $model = static::findFromStripeObject($customer); |
| 55 | + |
| 56 | + if (! $model) { |
| 57 | + return static::findCustomer($customer->id); |
| 58 | + } |
| 59 | + |
| 60 | + $model_type = get_class($model); |
| 61 | + $model_id = $model->getKey(); |
| 62 | + |
| 63 | + /** @var ?string $model_stripe_customer_id */ |
| 64 | + $model_stripe_customer_id = $model->stripe_customer_id; // @phpstan-ignore-line |
| 65 | + |
| 66 | + if ($model_stripe_customer_id !== $customer->id) { |
| 67 | + throw new Exception("[{$model_type}:{$model_id}] Conflict between Stripe customer ID and Stripe customer metadata: {$model_stripe_customer_id} !== {$customer->id}", 500); |
| 68 | + } |
| 69 | + |
| 70 | + return $model; |
| 71 | + } |
| 72 | + |
| 73 | + protected static function findFromStripeObject(Account|Customer $object): ?Model |
| 74 | + { |
| 75 | + $model_type = data_get($object->metadata, 'model_type'); |
| 76 | + $model_id = data_get($object->metadata, 'model_id'); |
| 77 | + |
| 78 | + if (! $model_type || ! $model_id) { |
| 79 | + return null; |
| 80 | + } |
| 81 | + |
| 82 | + return $model_type::find($model_id); |
| 83 | + } |
| 84 | + |
| 85 | + protected static function findFromModels( |
| 86 | + array $models, |
| 87 | + string $columnName, |
| 88 | + string $stripeId |
| 89 | + ): ?Model { |
| 90 | + foreach ($models as $model) { |
| 91 | + $instance = $model::query() |
| 92 | + ->where($columnName, $stripeId) |
| 93 | + ->first(); |
| 94 | + |
| 95 | + if ($instance) { |
| 96 | + return $instance; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return null; |
| 101 | + } |
| 102 | +} |
0 commit comments