Skip to content

Commit 49f5408

Browse files
committed
feat: [LAR-22] update sponsoring page
1 parent dc57dcf commit 49f5408

24 files changed

+398
-633
lines changed

.env.example

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ MAIL_PASSWORD=null
3636
MAIL_ENCRYPTION=null
3737
MAIL_FROM_ADDRESS=[email protected]
3838
MAIL_FROM_NAME="${APP_NAME}"
39+
MAIL_SUPPORT=[email protected]
3940

4041
MAILGUN_DOMAIN=
4142
MAILGUN_SECRET=
@@ -57,11 +58,6 @@ GITHUB_CLIENT_ID=
5758
GITHUB_CLIENT_SECRET=
5859
GITHUB_REDIRECT=${APP_URL}/auth/github/callback
5960

60-
SLACK_WEBHOOK_URL=
61-
SLACK_TEAM_NAME="Laravel Cameroun"
62-
SLACK_API_TOKEN=
63-
SLACK_TEAM_URL="https://laravelcm.slack.com"
64-
6561
MARKDOWNX_GIPHY_API_KEY=
6662
TORCHLIGHT_TOKEN=
6763
TORCHLIGHT_THEME=moonlight-ii

app/Http/Controllers/SponsoringController.php

Lines changed: 0 additions & 27 deletions
This file was deleted.

app/Livewire/Modals/AnonymousSponsors.php

Lines changed: 0 additions & 116 deletions
This file was deleted.

app/Livewire/Pages/Sponsoring.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace App\Livewire\Pages;
6+
7+
use Illuminate\Contracts\View\View;
8+
use Livewire\Component;
9+
10+
final class Sponsoring extends Component
11+
{
12+
public function render(): View
13+
{
14+
return view('livewire.pages.sponsoring')->title(__('pages/sponsoring.title'));
15+
}
16+
}

app/Livewire/SponsorSubscription.php

Lines changed: 119 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,87 +7,172 @@
77
use App\Enums\PaymentType;
88
use App\Enums\TransactionType;
99
use App\Models\Transaction;
10+
use App\Models\User;
11+
use Filament\Forms;
12+
use Filament\Forms\Concerns\InteractsWithForms;
13+
use Filament\Forms\Contracts\HasForms;
14+
use Filament\Forms\Form;
15+
use Filament\Notifications\Notification;
1016
use Illuminate\Contracts\View\View;
1117
use Illuminate\Support\Facades\Auth;
18+
use Illuminate\Support\Facades\Cache;
1219
use Illuminate\Support\Facades\Log;
1320
use Livewire\Component;
21+
use NotchPay\Exceptions\ApiException;
1422
use NotchPay\NotchPay;
1523
use NotchPay\Payment;
1624

17-
final class SponsorSubscription extends Component
25+
/**
26+
* @property-read Form $form
27+
*/
28+
final class SponsorSubscription extends Component implements HasForms
1829
{
19-
public string $option = 'one-time';
30+
use InteractsWithForms;
2031

21-
public string $amount = '';
32+
public ?array $data = [];
2233

23-
public string $currency = 'XAF';
34+
public function mount(): void
35+
{
36+
$auth = Auth::user();
37+
38+
$this->form->fill([
39+
'email' => $auth?->email,
40+
'name' => $auth?->name,
41+
'website' => $auth?->website,
42+
'profile' => 'developer',
43+
'currency' => 'xaf',
44+
]);
45+
}
2446

25-
public function chooseOption(string $option): void
47+
public function form(Form $form): Form
2648
{
27-
$this->option = $option;
49+
return $form
50+
->schema([
51+
Forms\Components\TextInput::make('name')
52+
->label(__('validation.attributes.name'))
53+
->minLength(5)
54+
->required(),
55+
Forms\Components\TextInput::make('email')
56+
->label(__('validation.attributes.email'))
57+
->email()
58+
->required(),
59+
Forms\Components\ToggleButtons::make('profile')
60+
->label(__('pages/sponsoring.sponsor_form.profile'))
61+
->options([
62+
'developer' => __('validation.attributes.freelance'),
63+
'company' => __('validation.attributes.company'),
64+
])
65+
->icons([
66+
'developer' => 'phosphor-dev-to-logo-duotone',
67+
'company' => 'phosphor-buildings-duotone',
68+
])
69+
->grouped(),
70+
Forms\Components\TextInput::make('website')
71+
->label(__('global.website'))
72+
->prefixIcon('heroicon-m-globe-alt')
73+
->url(),
74+
Forms\Components\Group::make()
75+
->schema([
76+
Forms\Components\Select::make('currency')
77+
->label(__('validation.attributes.currency'))
78+
->live()
79+
->native()
80+
->options([
81+
'xaf' => 'XAF',
82+
'eur' => 'EUR',
83+
'usd' => 'USD',
84+
]),
85+
Forms\Components\TextInput::make('amount')
86+
->label(__('validation.attributes.amount'))
87+
->integer()
88+
->required()
89+
->afterStateUpdated(fn (?int $state) => $state ? abs($state) : 0)
90+
->prefix(fn (Forms\Get $get) => match ($get('currency')) {
91+
'usd' => '$',
92+
default => null
93+
})
94+
->suffix(fn (Forms\Get $get) => match ($get('currency')) {
95+
'eur' => '',
96+
'xaf' => 'FCFA',
97+
default => null,
98+
})
99+
->columnSpan(3),
100+
])
101+
->columns(4)
102+
->columnSpanFull(),
103+
])
104+
->statePath('data')
105+
->columns();
28106
}
29107

30-
public function subscribe(): void
108+
public function submit(): void
31109
{
32-
$this->validate(['amount' => 'required']);
110+
$this->validate();
33111

34-
if (! Auth::check()) {
35-
$this->dispatch('openModal', component: 'modals.anonymous-sponsors', arguments: [
36-
'amount' => $this->amount,
37-
'option' => $this->option,
38-
'currency' => $this->currency,
39-
]);
112+
$email = data_get($this->form->getState(), 'email');
113+
$amount = data_get($this->form->getState(), 'amount');
40114

41-
return;
42-
}
115+
/** @var User $user */
116+
$user = Auth::check() ? Auth::user() : User::findByEmailAddress(config('lcm.support_email'));
43117

44118
NotchPay::setApiKey(apiKey: config('lcm.notch-pay-public-token'));
45119

46120
try {
47121
$payload = Payment::initialize([
48-
'amount' => $this->amount,
49-
'email' => Auth::user()?->email,
50-
'name' => Auth::user()?->name,
51-
'currency' => $this->currency,
52-
'reference' => Auth::id().'-'.Auth::user()?->username().'-'.uniqid(),
122+
'amount' => $amount,
123+
'email' => $email,
124+
'name' => data_get($this->form->getState(), 'name'),
125+
'currency' => data_get($this->form->getState(), 'currency'),
126+
'reference' => $user->id.'-'.$user->username().'-'.uniqid(),
53127
'callback' => route('notchpay-callback'),
54128
'description' => __('Soutien de la communauté Laravel & PHP Cameroun.'),
55129
]);
56130

57131
Transaction::query()->create([
58-
'amount' => $this->amount,
132+
'amount' => $amount,
59133
'status' => $payload->transaction->status,
60134
'transaction_reference' => $payload->transaction->reference,
61-
'user_id' => Auth::id(),
135+
'user_id' => $user->id,
62136
'fees' => $payload->transaction->fee,
63-
'type' => $this->option === 'one-time'
64-
? TransactionType::ONETIME->value
65-
: TransactionType::RECURSIVE->value,
137+
'type' => TransactionType::ONETIME->value,
66138
'metadata' => [
67139
'currency' => $payload->transaction->currency,
68140
'reference' => $payload->transaction->reference,
69141
'merchant' => [
70142
'reference' => $payload->transaction->merchant_reference,
71-
'name' => $payload->transaction->customer->name,
72-
'email' => $payload->transaction->customer->email,
73-
'phone' => $payload->transaction->customer->phone,
74-
'laravel_cm_id' => Auth::id(),
143+
'customer' => $payload->transaction->customer,
144+
'laravel_cm_id' => Auth::id() ?? null,
145+
'profile' => data_get($this->form->getState(), 'profile'),
75146
],
76-
'initiated_at' => $payload->transaction->initiated_at,
147+
'initiated_at' => $payload->transaction->created_at,
77148
'description' => $payload->transaction->description,
78149
'for' => PaymentType::SPONSORING->value,
79150
],
80151
]);
81152

82153
$this->redirect($payload->authorization_url); // @phpstan-ignore-line
83-
} catch (\NotchPay\Exceptions\ApiException $e) {
154+
} catch (ApiException $e) {
84155
Log::error($e->getMessage());
85-
session()->flash('error', __('Impossible de procéder au paiement, veuillez recommencer plus tard. Merci'));
156+
157+
Notification::make()
158+
->title(__('notifications.sponsor_error_title'))
159+
->body(__('notifications.sponsor_error_body'))
160+
->danger()
161+
->send();
86162
}
87163
}
88164

89165
public function render(): View
90166
{
91-
return view('livewire.sponsor-subscription');
167+
return view('livewire.sponsor-subscription', [
168+
'sponsors' => Cache::remember(
169+
key: 'sponsors',
170+
ttl: now()->addWeek(),
171+
callback: fn () => Transaction::with(['user', 'user.media'])
172+
->scopes('complete')
173+
->distinct()
174+
->get(['id', 'user_id', 'metadata'])
175+
),
176+
]);
92177
}
93178
}

0 commit comments

Comments
 (0)