diff --git a/src/Builders/CreditCardBuilder.php b/src/Builders/CreditCardBuilder.php index bd9e716..fe8cc4a 100644 --- a/src/Builders/CreditCardBuilder.php +++ b/src/Builders/CreditCardBuilder.php @@ -166,4 +166,13 @@ public function setToken(string $token): self return $this; } + + /** + * Set as default card. + */ + public function setAsDefault(bool $default = true): self + { + $this->model->default = $default; + return $this; + } } \ No newline at end of file diff --git a/src/Gateways/IuguGateway.php b/src/Gateways/IuguGateway.php index 1c76b2c..93670f3 100644 --- a/src/Gateways/IuguGateway.php +++ b/src/Gateways/IuguGateway.php @@ -251,12 +251,18 @@ public function createCreditCard(CreditCard $creditCard): CreditCard ]); } + $options = [ + 'token' => $creditCard->token, + 'customer_id' => $creditCard->customer->id, + 'description' => $creditCard->description ?? 'CREDIT CARD', + ]; + + if (!empty($creditCard->default)) { + $options['set_as_default'] = $creditCard->default; + } + try { - $iuguCreditCard = Iugu_PaymentMethod::create([ - 'token' => $creditCard->token, - 'customer_id' => $creditCard->customer->id, - 'description' => $creditCard->description ?? 'CREDIT CARD', - ]); + $iuguCreditCard = Iugu_PaymentMethod::create($options); } catch (\IuguRequestException | IuguObjectNotFound $e) { if (str_contains($e->getMessage(), '502 Bad Gateway')) { throw new GatewayNotAvailableException($e->getMessage()); @@ -684,6 +690,11 @@ private function parseCustomer($iuguCustomer, ?Customer $customer = null): Custo $customer->address->country = $valuesInsideCustomVariables['country'] ?? null; } + if (!empty($iuguCustomer->default_payment_method_id)) { + $customer->defaultCard = new CreditCard(); + $customer->defaultCard->id = $iuguCustomer->default_payment_method_id; + } + return $customer; } diff --git a/src/Models/CreditCard.php b/src/Models/CreditCard.php index b463356..310caf0 100644 --- a/src/Models/CreditCard.php +++ b/src/Models/CreditCard.php @@ -71,6 +71,11 @@ class CreditCard extends Model */ public ?string $token = null; + /** + * @var bool|null + */ + public ?bool $default = null; + /** * @var string|null */ diff --git a/src/Models/Customer.php b/src/Models/Customer.php index b0c5c1c..37a9768 100644 --- a/src/Models/Customer.php +++ b/src/Models/Customer.php @@ -56,6 +56,11 @@ class Customer extends Model */ public ?Address $address = null; + /** + * @var \Potelo\MultiPayment\Models\CreditCard|null + */ + public ?CreditCard $defaultCard = null; + /** * @var string|null */ diff --git a/tests/TestCase.php b/tests/TestCase.php index 954153f..a80e9ca 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -82,6 +82,7 @@ public static function creditCard(): array $creditCard['firstName'] = 'Faker'; $creditCard['lastName'] = 'Teste'; $creditCard['description'] = 'card description'; + $creditCard['default'] = true; return $creditCard; } diff --git a/tests/Unit/Builders/CreditCardBuilderTest.php b/tests/Unit/Builders/CreditCardBuilderTest.php index f77249a..af8d2d3 100644 --- a/tests/Unit/Builders/CreditCardBuilderTest.php +++ b/tests/Unit/Builders/CreditCardBuilderTest.php @@ -52,9 +52,28 @@ public function testShouldCreateACreditCard($gateway, $data) if (!empty($data['cvv'])) { $creditCardBuilder->setCvv($data['cvv']); } + if (!empty($data['description'])) { + $creditCardBuilder->setDescription($data['description']); + } + if (!empty($data['default'])) { + $creditCardBuilder->setAsDefault($data['default']); + } $creditCard = $creditCardBuilder->create(); + $this->assertNotNull($creditCard->id); + $this->assertEquals(substr($data['number'], -4), $creditCard->lastDigits); + $this->assertEquals('Visa', $creditCard->brand); + $this->assertEquals($data['firstName'], $creditCard->firstName); + $this->assertEquals($data['lastName'], $creditCard->lastName); + $this->assertEquals($data['description'], $creditCard->description); + $this->assertEquals($data['default'], $creditCard->default); + + if ($data['default']) { + $customer = $customer->get($customer->id); + $this->assertEquals($creditCard->id, $customer->defaultCard->id); + } + $this->assertEquals($gateway, $creditCard->gateway); }