88namespace EncoreDigitalGroup \Stripe \Objects \Customer ;
99
1010use EncoreDigitalGroup \StdLib \Exceptions \NullExceptions \ClassPropertyNullException ;
11+ use EncoreDigitalGroup \StdLib \Exceptions \NullExceptions \VariableNullException ;
1112use EncoreDigitalGroup \StdLib \Objects \Support \Types \Arr ;
13+ use EncoreDigitalGroup \Stripe \Objects \Payment \StripePaymentMethod ;
1214use EncoreDigitalGroup \Stripe \Objects \Subscription \StripeSubscription ;
1315use EncoreDigitalGroup \Stripe \Objects \Support \StripeAddress ;
1416use EncoreDigitalGroup \Stripe \Services \StripeCustomerService ;
17+ use EncoreDigitalGroup \Stripe \Services \StripePaymentMethodService ;
1518use EncoreDigitalGroup \Stripe \Services \StripeSubscriptionService ;
1619use EncoreDigitalGroup \Stripe \Support \Traits \HasGet ;
1720use EncoreDigitalGroup \Stripe \Support \Traits \HasSave ;
@@ -37,6 +40,9 @@ class StripeCustomer
3740 /** @var ?Collection<StripeSubscription> */
3841 private ?Collection $ subscriptions = null ;
3942
43+ /** @var ?Collection<StripePaymentMethod> */
44+ private ?Collection $ paymentMethods = null ;
45+
4046 /**
4147 * Create a StripeCustomer instance from a Stripe API Customer object
4248 */
@@ -51,7 +57,7 @@ public static function fromStripeObject(Customer $stripeCustomer): self
5157 if (isset ($ stripeCustomer ->address )) {
5258 /** @var StripeObject $stripeAddress */
5359 $ stripeAddress = $ stripeCustomer ->address ;
54- $ instance = $ instance ->withAddress (self :: extractAddress ($ stripeAddress ));
60+ $ instance = $ instance ->withAddress (StripeAddress:: fromStripeObject ($ stripeAddress ));
5561 }
5662
5763 if ($ stripeCustomer ->description ?? null ) {
@@ -83,39 +89,13 @@ public static function fromStripeObject(Customer $stripeCustomer): self
8389 return $ instance ;
8490 }
8591
86- private static function extractAddress (StripeObject $ stripeAddress ): StripeAddress
87- {
88- $ address = StripeAddress::make ();
89-
90- if ($ stripeAddress ->line1 ?? null ) {
91- $ address = $ address ->withLine1 ($ stripeAddress ->line1 );
92- }
93- if ($ stripeAddress ->line2 ?? null ) {
94- $ address = $ address ->withLine2 ($ stripeAddress ->line2 );
95- }
96- if ($ stripeAddress ->city ?? null ) {
97- $ address = $ address ->withCity ($ stripeAddress ->city );
98- }
99- if ($ stripeAddress ->state ?? null ) {
100- $ address = $ address ->withState ($ stripeAddress ->state );
101- }
102- if ($ stripeAddress ->postal_code ?? null ) {
103- $ address = $ address ->withPostalCode ($ stripeAddress ->postal_code );
104- }
105- if ($ stripeAddress ->country ?? null ) {
106- return $ address ->withCountry ($ stripeAddress ->country );
107- }
108-
109- return $ address ;
110- }
111-
11292 private static function extractShipping (StripeObject $ stripeShipping ): ?StripeShipping
11393 {
11494 $ shippingAddress = null ;
11595 if (isset ($ stripeShipping ->address )) {
11696 /** @var StripeObject $shippingAddressObj */
11797 $ shippingAddressObj = $ stripeShipping ->address ;
118- $ shippingAddress = self :: extractAddress ($ shippingAddressObj );
98+ $ shippingAddress = StripeAddress:: fromStripeObject ($ shippingAddressObj );
11999 }
120100
121101 // Only create shipping if we have the required fields (address and name)
@@ -134,6 +114,60 @@ private static function extractShipping(StripeObject $stripeShipping): ?StripeSh
134114 return $ shipping ;
135115 }
136116
117+ /** @returns Collection<StripeSubscription> */
118+ public function subscriptions (bool $ refresh = false ): Collection
119+ {
120+ if ($ this ->subscriptions instanceof Collection && !$ refresh ) {
121+ return $ this ->subscriptions ;
122+ }
123+
124+ if (is_null ($ this ->id )) {
125+ throw new ClassPropertyNullException ("id " );
126+ }
127+
128+ $ this ->subscriptions = app (StripeSubscriptionService::class)->getAllForCustomer ($ this ->id );
129+
130+ return $ this ->subscriptions ;
131+ }
132+
133+ /** @returns Collection<StripePaymentMethod> */
134+ public function paymentMethods (bool $ refresh = false ): Collection
135+ {
136+ if ($ this ->paymentMethods instanceof Collection && !$ refresh ) {
137+ return $ this ->paymentMethods ;
138+ }
139+
140+ if (is_null ($ this ->id )) {
141+ throw new ClassPropertyNullException ("id " );
142+ }
143+
144+ $ this ->paymentMethods = app (StripePaymentMethodService::class)->getAllForCustomer ($ this ->id );
145+
146+ return $ this ->paymentMethods ;
147+ }
148+
149+ public function addPaymentMethod (StripePaymentMethod $ paymentMethod ): self
150+ {
151+ $ paymentMethod = app (StripePaymentMethodService::class)->create ($ paymentMethod );
152+ $ paymentMethodId = $ paymentMethod ->id ();
153+
154+ if (is_null ($ paymentMethodId )) {
155+ throw new VariableNullException ("paymentMethodId " );
156+ }
157+
158+ if (is_null ($ this ->id )) {
159+ throw new ClassPropertyNullException ("id " );
160+ }
161+
162+ app (StripePaymentMethodService::class)->attach ($ paymentMethodId , $ this ->id );
163+
164+ if (!is_null ($ this ->paymentMethods )) {
165+ $ this ->paymentMethods (true );
166+ }
167+
168+ return $ this ;
169+ }
170+
137171 public function service (): StripeCustomerService
138172 {
139173 return app (StripeCustomerService::class);
@@ -239,20 +273,4 @@ public function shipping(): ?StripeShipping
239273 {
240274 return $ this ->shipping ;
241275 }
242-
243- /** @returns Collection<StripeSubscription> */
244- public function subscriptions (bool $ refresh = false ): Collection
245- {
246- if ($ this ->subscriptions instanceof Collection && !$ refresh ) {
247- return $ this ->subscriptions ;
248- }
249-
250- if (is_null ($ this ->id )) {
251- throw new ClassPropertyNullException ("id " );
252- }
253-
254- $ this ->subscriptions = app (StripeSubscriptionService::class)->getAllForCustomer ($ this ->id );
255-
256- return $ this ->subscriptions ;
257- }
258276}
0 commit comments