1+ <?php
2+
3+ /*
4+ * Copyright (c) 2025. Encore Digital Group.
5+ * All Right Reserved.
6+ */
7+
8+ namespace EncoreDigitalGroup \Stripe \Objects \Payment ;
9+
10+ use Carbon \CarbonImmutable ;
11+ use EncoreDigitalGroup \StdLib \Objects \Support \Types \Arr ;
12+ use EncoreDigitalGroup \Stripe \Enums \PaymentMethodType ;
13+ use EncoreDigitalGroup \Stripe \Enums \SetupIntentStatus ;
14+ use EncoreDigitalGroup \Stripe \Enums \SetupIntentUsage ;
15+ use EncoreDigitalGroup \Stripe \Services \StripeSetupIntentService ;
16+ use EncoreDigitalGroup \Stripe \Support \Traits \HasGet ;
17+ use EncoreDigitalGroup \Stripe \Support \Traits \HasMetadata ;
18+ use EncoreDigitalGroup \Stripe \Support \Traits \HasSave ;
19+ use EncoreDigitalGroup \Stripe \Support \Traits \HasTimestamps ;
20+ use Illuminate \Support \Collection ;
21+ use PHPGenesis \Common \Traits \HasMake ;
22+ use Stripe \SetupIntent ;
23+
24+ class StripeSetupIntent
25+ {
26+ use HasGet;
27+ use HasMake;
28+ use HasMetadata;
29+ use HasSave;
30+ use HasTimestamps;
31+
32+ private ?string $ id = null ;
33+ private ?string $ customer = null ;
34+ private ?string $ description = null ;
35+ private ?string $ paymentMethod = null ;
36+ private ?SetupIntentStatus $ status = null ;
37+ private ?SetupIntentUsage $ usage = null ;
38+ private ?CarbonImmutable $ created = null ;
39+ private ?string $ clientSecret = null ;
40+ private ?array $ lastSetupError = null ;
41+
42+ /** @var ?Collection<PaymentMethodType> */
43+ private ?Collection $ paymentMethodTypes = null ;
44+
45+ public static function fromStripeObject (SetupIntent $ setupIntent ): self
46+ {
47+ $ instance = self ::make ();
48+
49+ $ instance = self ::setBasicProperties ($ instance , $ setupIntent );
50+ $ instance = self ::setEnumProperties ($ instance , $ setupIntent );
51+ $ instance = self ::setRelationProperties ($ instance , $ setupIntent );
52+
53+ return self ::setAdditionalProperties ($ instance , $ setupIntent );
54+ }
55+
56+ private static function setBasicProperties (self $ instance , SetupIntent $ setupIntent ): self
57+ {
58+ if ($ setupIntent ->id ) {
59+ $ instance = $ instance ->withId ($ setupIntent ->id );
60+ }
61+
62+ if ($ setupIntent ->description ?? null ) {
63+ $ instance = $ instance ->withDescription ($ setupIntent ->description );
64+ }
65+
66+ if ($ setupIntent ->client_secret ?? null ) {
67+ return $ instance ->withClientSecret ($ setupIntent ->client_secret );
68+ }
69+
70+ return $ instance ;
71+ }
72+
73+ private static function setEnumProperties (self $ instance , SetupIntent $ setupIntent ): self
74+ {
75+ if ($ setupIntent ->status ?? null ) {
76+ $ instance = $ instance ->withStatus (SetupIntentStatus::from ($ setupIntent ->status ));
77+ }
78+
79+ if ($ setupIntent ->usage ?? null ) {
80+ return $ instance ->withUsage (SetupIntentUsage::from ($ setupIntent ->usage ));
81+ }
82+
83+ return $ instance ;
84+ }
85+
86+ private static function setRelationProperties (self $ instance , SetupIntent $ setupIntent ): self
87+ {
88+ if ($ setupIntent ->customer ?? null ) {
89+ $ customerId = is_string ($ setupIntent ->customer ) ? $ setupIntent ->customer : $ setupIntent ->customer ->id ;
90+ $ instance = $ instance ->withCustomer ($ customerId );
91+ }
92+
93+ if ($ setupIntent ->payment_method ?? null ) {
94+ $ paymentMethodId = is_string ($ setupIntent ->payment_method ) ? $ setupIntent ->payment_method : $ setupIntent ->payment_method ->id ;
95+ $ instance = $ instance ->withPaymentMethod ($ paymentMethodId );
96+ }
97+
98+ return $ instance ;
99+ }
100+
101+ private static function setAdditionalProperties (self $ instance , SetupIntent $ setupIntent ): self
102+ {
103+ if ($ setupIntent ->created ?? null ) {
104+ $ created = self ::timestampToCarbon ($ setupIntent ->created );
105+ if ($ created instanceof CarbonImmutable) {
106+ $ instance = $ instance ->withCreated ($ created );
107+ }
108+ }
109+
110+ if (isset ($ setupIntent ->last_setup_error )) {
111+ $ errorJson = json_encode ($ setupIntent ->last_setup_error );
112+ $ lastSetupError = $ errorJson !== false ? json_decode ($ errorJson , true ) : null ;
113+ $ instance = $ instance ->withLastSetupError ($ lastSetupError );
114+ }
115+
116+ if (isset ($ setupIntent ->payment_method_types )) {
117+ $ paymentMethodTypes = collect ($ setupIntent ->payment_method_types )
118+ ->map (fn (string $ type ): PaymentMethodType => PaymentMethodType::from ($ type ));
119+ $ instance = $ instance ->withPaymentMethodTypes ($ paymentMethodTypes );
120+ }
121+
122+ if (isset ($ setupIntent ->metadata )) {
123+ return $ instance ->withMetadata (self ::extractMetadata ($ setupIntent ));
124+ }
125+
126+ return $ instance ;
127+ }
128+
129+ public function toArray (): array
130+ {
131+ $ array = [
132+ "id " => $ this ->id ,
133+ "customer " => $ this ->customer ,
134+ "description " => $ this ->description ,
135+ "payment_method " => $ this ->paymentMethod ,
136+ "status " => $ this ->status ?->value,
137+ "usage " => $ this ->usage ?->value,
138+ "created " => self ::carbonToTimestamp ($ this ->created ),
139+ "client_secret " => $ this ->clientSecret ,
140+ "last_setup_error " => $ this ->lastSetupError ,
141+ "payment_method_types " => $ this ->paymentMethodTypes ?->map(fn (PaymentMethodType $ type ): string => $ type ->value )?->toArray(),
142+ "metadata " => $ this ->metadata ,
143+ ];
144+
145+ return Arr::whereNotNull ($ array );
146+ }
147+
148+ public function service (): StripeSetupIntentService
149+ {
150+ return app (StripeSetupIntentService::class);
151+ }
152+
153+ public function withId (string $ id ): self
154+ {
155+ $ this ->id = $ id ;
156+
157+ return $ this ;
158+ }
159+
160+ public function id (): ?string
161+ {
162+ return $ this ->id ;
163+ }
164+
165+ public function withCustomer (string $ customer ): self
166+ {
167+ $ this ->customer = $ customer ;
168+
169+ return $ this ;
170+ }
171+
172+ public function customer (): ?string
173+ {
174+ return $ this ->customer ;
175+ }
176+
177+ public function withDescription (string $ description ): self
178+ {
179+ $ this ->description = $ description ;
180+
181+ return $ this ;
182+ }
183+
184+ public function description (): ?string
185+ {
186+ return $ this ->description ;
187+ }
188+
189+ public function withPaymentMethod (string $ paymentMethod ): self
190+ {
191+ $ this ->paymentMethod = $ paymentMethod ;
192+
193+ return $ this ;
194+ }
195+
196+ public function paymentMethod (): ?string
197+ {
198+ return $ this ->paymentMethod ;
199+ }
200+
201+ public function withStatus (SetupIntentStatus $ status ): self
202+ {
203+ $ this ->status = $ status ;
204+
205+ return $ this ;
206+ }
207+
208+ public function status (): ?SetupIntentStatus
209+ {
210+ return $ this ->status ;
211+ }
212+
213+ public function withUsage (SetupIntentUsage $ usage ): self
214+ {
215+ $ this ->usage = $ usage ;
216+
217+ return $ this ;
218+ }
219+
220+ public function usage (): ?SetupIntentUsage
221+ {
222+ return $ this ->usage ;
223+ }
224+
225+ public function withCreated (CarbonImmutable $ created ): self
226+ {
227+ $ this ->created = $ created ;
228+
229+ return $ this ;
230+ }
231+
232+ public function created (): ?CarbonImmutable
233+ {
234+ return $ this ->created ;
235+ }
236+
237+ public function withClientSecret (string $ clientSecret ): self
238+ {
239+ $ this ->clientSecret = $ clientSecret ;
240+
241+ return $ this ;
242+ }
243+
244+ public function clientSecret (): ?string
245+ {
246+ return $ this ->clientSecret ;
247+ }
248+
249+ public function withLastSetupError (?array $ lastSetupError ): self
250+ {
251+ $ this ->lastSetupError = $ lastSetupError ;
252+
253+ return $ this ;
254+ }
255+
256+ public function lastSetupError (): ?array
257+ {
258+ return $ this ->lastSetupError ;
259+ }
260+
261+ /**
262+ * @param Collection<int, PaymentMethodType> $paymentMethodTypes
263+ */
264+ public function withPaymentMethodTypes (Collection $ paymentMethodTypes ): self
265+ {
266+ $ this ->paymentMethodTypes = $ paymentMethodTypes ;
267+
268+ return $ this ;
269+ }
270+
271+ /**
272+ * @return ?Collection<PaymentMethodType>
273+ */
274+ public function paymentMethodTypes (): ?Collection
275+ {
276+ return $ this ->paymentMethodTypes ;
277+ }
278+ }
0 commit comments