@@ -278,29 +278,16 @@ public function createCreditCard(CreditCard $creditCard): CreditCard
278278 throw new GatewayException ('Error creating creditCard: ' , $ iuguCreditCard ->errors );
279279 }
280280
281- $ creditCard ->id = $ iuguCreditCard ->id ?? null ;
282- $ creditCard ->brand = $ iuguCreditCard ->data ->brand ?? null ;
283- $ creditCard ->year = $ iuguCreditCard ->data ->year ?? null ;
284- $ creditCard ->month = $ iuguCreditCard ->data ->month ?? null ;
285- if (!empty ($ iuguCreditCard ->data ->holder_name )) {
286- $ names = explode (' ' , $ iuguCreditCard ->data ->holder_name );
287- $ creditCard ->firstName = $ names [0 ] ?? null ;
288- $ creditCard ->lastName = $ names [array_key_last ($ names )] ?? null ;
289- }
290- $ creditCard ->lastDigits = $ iuguCreditCard ->data ->last_digits ?? substr ($ iuguCreditCard ->data ->display_number , -4 );
291- $ creditCard ->gateway = 'iugu ' ;
292- $ creditCard ->original = $ iuguCreditCard ;
293- $ creditCard ->createdAt = new Carbon ($ iuguCreditCard ->created_at_iso ) ?? null ;
294- return $ creditCard ;
281+ return $ this ->parseIuguCard ($ iuguCreditCard , $ creditCard );
295282 }
296283
297284 /**
298285 * @inheritDoc
299286 */
300- public function getInvoice (string $ id ): Invoice
287+ public function getInvoice (Invoice $ invoice ): Invoice
301288 {
302289 try {
303- $ iuguInvoice = \Iugu_Invoice::fetch ($ id );
290+ $ iuguInvoice = \Iugu_Invoice::fetch ($ invoice -> id );
304291 } catch (\IuguRequestException | IuguObjectNotFound $ e ) {
305292 if (str_contains ($ e ->getMessage (), '502 Bad Gateway ' )) {
306293 throw new GatewayNotAvailableException ($ e ->getMessage ());
@@ -314,7 +301,7 @@ public function getInvoice(string $id): Invoice
314301 throw new GatewayException ('Error getting invoice ' , $ iuguInvoice ->errors );
315302 }
316303
317- return $ this ->parseInvoice ($ iuguInvoice );
304+ return $ this ->parseInvoice ($ iuguInvoice, $ invoice );
318305 }
319306
320307 /**
@@ -587,10 +574,13 @@ private function chargeIuguInvoice(array $iuguInvoiceData)
587574 return $ iuguCharge ->invoice ();
588575 }
589576
590- public function getCustomer (string $ id ): Customer
577+ /**
578+ * @inheritDoc
579+ */
580+ public function getCustomer (Customer $ customer ): Customer
591581 {
592582 try {
593- $ iuguCustomer = Iugu_Customer::fetch ($ id );
583+ $ iuguCustomer = Iugu_Customer::fetch ($ customer -> id );
594584 } catch (\IuguRequestException | IuguObjectNotFound $ e ) {
595585 if (str_contains ($ e ->getMessage (), '502 Bad Gateway ' )) {
596586 throw new GatewayNotAvailableException ($ e ->getMessage ());
@@ -605,7 +595,7 @@ public function getCustomer(string $id): Customer
605595 throw new GatewayException ('Error getting customer ' , $ iuguCustomer ->errors );
606596 }
607597
608- return $ this ->parseCustomer ($ iuguCustomer );
598+ return $ this ->parseCustomer ($ iuguCustomer, $ customer );
609599 }
610600
611601 public function updateCustomer (Customer $ customer ): Customer
@@ -747,6 +737,100 @@ private function customerToIuguData(Customer $customer): array
747737 }
748738 }
749739
740+ if (!empty ($ customer ->defaultCard ) && !empty ($ customer ->defaultCard ->id )) {
741+ $ iuguCustomerData ['default_payment_method_id ' ] = $ customer ->defaultCard ->id ;
742+ }
743+
750744 return $ iuguCustomerData ;
751745 }
746+
747+ /**
748+ * @inheritDoc
749+ */
750+ public function setCustomerDefaultCard (Customer $ customer , string $ cardId ): Customer
751+ {
752+ $ customer ->defaultCard = new CreditCard ();
753+ $ customer ->defaultCard ->id = $ cardId ;
754+
755+ return $ this ->updateCustomer ($ customer );
756+ }
757+
758+ /**
759+ * @inheritDoc
760+ */
761+ public function deleteCreditCard (CreditCard $ creditCard ): void
762+ {
763+ try {
764+ $ iuguCreditCard = new Iugu_PaymentMethod ([
765+ 'id ' => $ creditCard ->id ,
766+ 'customer_id ' => $ creditCard ->customer ->id
767+ ]);
768+ $ iuguCreditCard ->delete ();
769+ } catch (\IuguRequestException | IuguObjectNotFound $ e ) {
770+ if (str_contains ($ e ->getMessage (), '502 Bad Gateway ' )) {
771+ throw new GatewayNotAvailableException ($ e ->getMessage ());
772+ } else {
773+ throw new GatewayException ($ e ->getMessage ());
774+ }
775+ } catch (\IuguAuthenticationException $ e ) {
776+ throw new GatewayNotAvailableException ($ e ->getMessage ());
777+ } catch (\Exception $ e ) {
778+ throw new GatewayException ($ e ->getMessage ());
779+ }
780+ }
781+
782+ /**
783+ * @inheritDoc
784+ */
785+ public function getCreditCard (CreditCard $ creditCard ): CreditCard
786+ {
787+ try {
788+ $ iuguCustomer = new Iugu_Customer (['id ' => $ creditCard ->customer ->id ]);
789+ $ iuguCreditCard = $ iuguCustomer ->payment_methods ()->fetch ($ creditCard ->id );
790+ } catch (\IuguRequestException | IuguObjectNotFound $ e ) {
791+ if (str_contains ($ e ->getMessage (), '502 Bad Gateway ' )) {
792+ throw new GatewayNotAvailableException ($ e ->getMessage ());
793+ } else {
794+ throw new GatewayException ($ e ->getMessage ());
795+ }
796+ } catch (\IuguAuthenticationException $ e ) {
797+ throw new GatewayNotAvailableException ($ e ->getMessage ());
798+ } catch (\Exception $ e ) {
799+ throw new GatewayException ($ e ->getMessage ());
800+ }
801+ if ($ iuguCreditCard ->errors ) {
802+ throw new GatewayException ('Error getting creditCard: ' , $ iuguCreditCard ->errors );
803+ }
804+
805+ return $ this ->parseIuguCard ($ iuguCreditCard , $ creditCard );
806+ }
807+
808+ /**
809+ * @param mixed $iuguCreditCard
810+ * @param \Potelo\MultiPayment\Models\CreditCard|null $creditCard
811+ * @return \Potelo\MultiPayment\Models\CreditCard
812+ */
813+ private function parseIuguCard (mixed $ iuguCreditCard , ?CreditCard $ creditCard = null ): CreditCard
814+ {
815+ if (is_null ($ creditCard )) {
816+ $ creditCard = new CreditCard ();
817+ }
818+
819+ $ creditCard ->id = $ iuguCreditCard ->id ?? null ;
820+ $ creditCard ->brand = $ iuguCreditCard ->data ->brand ?? null ;
821+ $ creditCard ->year = $ iuguCreditCard ->data ->year ?? null ;
822+ $ creditCard ->month = $ iuguCreditCard ->data ->month ?? null ;
823+ $ creditCard ->description = $ iuguCreditCard ->description ?? null ;
824+
825+ if (!empty ($ iuguCreditCard ->data ->holder_name )) {
826+ $ names = explode (' ' , $ iuguCreditCard ->data ->holder_name );
827+ $ creditCard ->firstName = $ names [0 ] ?? null ;
828+ $ creditCard ->lastName = $ names [array_key_last ($ names )] ?? null ;
829+ }
830+ $ creditCard ->lastDigits = $ iuguCreditCard ->data ->last_digits ?? substr ($ iuguCreditCard ->data ->display_number , -4 );
831+ $ creditCard ->gateway = 'iugu ' ;
832+ $ creditCard ->original = $ iuguCreditCard ;
833+ $ creditCard ->createdAt = new Carbon ($ iuguCreditCard ->created_at_iso ) ?? null ;
834+ return $ creditCard ;
835+ }
752836}
0 commit comments