Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
23 / 23 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| SyncMercadoPagoPlanCatalogAction | |
100.00% |
23 / 23 |
|
100.00% |
2 / 2 |
11 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
100.00% |
22 / 22 |
|
100.00% |
1 / 1 |
10 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace ImovelZapAi\Billing\Application\Actions; |
| 6 | |
| 7 | use ImovelZapAi\Billing\Application\Contracts\MercadoPagoClient; |
| 8 | use ImovelZapAi\Billing\Domain\Enums\BillingCycle; |
| 9 | use ImovelZapAi\Organizations\Infrastructure\Persistence\Eloquent\SubscriptionPlanModel; |
| 10 | |
| 11 | final class SyncMercadoPagoPlanCatalogAction |
| 12 | { |
| 13 | public function __construct( |
| 14 | private readonly MercadoPagoClient $client, |
| 15 | ) {} |
| 16 | |
| 17 | public function execute(): void |
| 18 | { |
| 19 | if ((string) config('billing.mercadopago.driver') === 'fake') { |
| 20 | // Fake still assigns IDs so checkout paths work locally. |
| 21 | } |
| 22 | |
| 23 | $plans = SubscriptionPlanModel::query()->where('is_active', true)->orderBy('sort_order')->get(); |
| 24 | |
| 25 | foreach ($plans as $plan) { |
| 26 | if ((int) $plan->price_monthly_cents <= 0 && (int) $plan->price_yearly_cents <= 0) { |
| 27 | continue; // Enterprise custom |
| 28 | } |
| 29 | |
| 30 | $updates = []; |
| 31 | |
| 32 | if (blank($plan->mp_preapproval_plan_id_monthly) && (int) $plan->price_monthly_cents > 0) { |
| 33 | $created = $this->client->createPreapprovalPlan( |
| 34 | 'ImóvelZapAi — '.$plan->name.' (mensal)', |
| 35 | (int) $plan->price_monthly_cents, |
| 36 | BillingCycle::Monthly, |
| 37 | ); |
| 38 | $updates['mp_preapproval_plan_id_monthly'] = $created['id']; |
| 39 | } |
| 40 | |
| 41 | if (blank($plan->mp_preapproval_plan_id_yearly) && (int) $plan->price_yearly_cents > 0) { |
| 42 | $created = $this->client->createPreapprovalPlan( |
| 43 | 'ImóvelZapAi — '.$plan->name.' (anual)', |
| 44 | (int) $plan->price_yearly_cents, |
| 45 | BillingCycle::Yearly, |
| 46 | ); |
| 47 | $updates['mp_preapproval_plan_id_yearly'] = $created['id']; |
| 48 | } |
| 49 | |
| 50 | if ($updates !== []) { |
| 51 | $plan->forceFill($updates)->save(); |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | } |