Skip to content

Commit 8ed25cc

Browse files
Merge pull request #45 from Potelo/feat-card-default
permite adicionar cartão padrão
2 parents 65643ce + 06e8410 commit 8ed25cc

6 files changed

Lines changed: 55 additions & 5 deletions

File tree

src/Builders/CreditCardBuilder.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,13 @@ public function setToken(string $token): self
166166

167167
return $this;
168168
}
169+
170+
/**
171+
* Set as default card.
172+
*/
173+
public function setAsDefault(bool $default = true): self
174+
{
175+
$this->model->default = $default;
176+
return $this;
177+
}
169178
}

src/Gateways/IuguGateway.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,18 @@ public function createCreditCard(CreditCard $creditCard): CreditCard
251251
]);
252252
}
253253

254+
$options = [
255+
'token' => $creditCard->token,
256+
'customer_id' => $creditCard->customer->id,
257+
'description' => $creditCard->description ?? 'CREDIT CARD',
258+
];
259+
260+
if (!empty($creditCard->default)) {
261+
$options['set_as_default'] = $creditCard->default;
262+
}
263+
254264
try {
255-
$iuguCreditCard = Iugu_PaymentMethod::create([
256-
'token' => $creditCard->token,
257-
'customer_id' => $creditCard->customer->id,
258-
'description' => $creditCard->description ?? 'CREDIT CARD',
259-
]);
265+
$iuguCreditCard = Iugu_PaymentMethod::create($options);
260266
} catch (\IuguRequestException | IuguObjectNotFound $e) {
261267
if (str_contains($e->getMessage(), '502 Bad Gateway')) {
262268
throw new GatewayNotAvailableException($e->getMessage());
@@ -684,6 +690,11 @@ private function parseCustomer($iuguCustomer, ?Customer $customer = null): Custo
684690
$customer->address->country = $valuesInsideCustomVariables['country'] ?? null;
685691
}
686692

693+
if (!empty($iuguCustomer->default_payment_method_id)) {
694+
$customer->defaultCard = new CreditCard();
695+
$customer->defaultCard->id = $iuguCustomer->default_payment_method_id;
696+
}
697+
687698
return $customer;
688699
}
689700

src/Models/CreditCard.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ class CreditCard extends Model
7171
*/
7272
public ?string $token = null;
7373

74+
/**
75+
* @var bool|null
76+
*/
77+
public ?bool $default = null;
78+
7479
/**
7580
* @var string|null
7681
*/

src/Models/Customer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ class Customer extends Model
5656
*/
5757
public ?Address $address = null;
5858

59+
/**
60+
* @var \Potelo\MultiPayment\Models\CreditCard|null
61+
*/
62+
public ?CreditCard $defaultCard = null;
63+
5964
/**
6065
* @var string|null
6166
*/

tests/TestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public static function creditCard(): array
8282
$creditCard['firstName'] = 'Faker';
8383
$creditCard['lastName'] = 'Teste';
8484
$creditCard['description'] = 'card description';
85+
$creditCard['default'] = true;
8586
return $creditCard;
8687
}
8788

tests/Unit/Builders/CreditCardBuilderTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,28 @@ public function testShouldCreateACreditCard($gateway, $data)
5252
if (!empty($data['cvv'])) {
5353
$creditCardBuilder->setCvv($data['cvv']);
5454
}
55+
if (!empty($data['description'])) {
56+
$creditCardBuilder->setDescription($data['description']);
57+
}
58+
if (!empty($data['default'])) {
59+
$creditCardBuilder->setAsDefault($data['default']);
60+
}
5561

5662
$creditCard = $creditCardBuilder->create();
63+
5764
$this->assertNotNull($creditCard->id);
65+
$this->assertEquals(substr($data['number'], -4), $creditCard->lastDigits);
66+
$this->assertEquals('Visa', $creditCard->brand);
67+
$this->assertEquals($data['firstName'], $creditCard->firstName);
68+
$this->assertEquals($data['lastName'], $creditCard->lastName);
69+
$this->assertEquals($data['description'], $creditCard->description);
70+
$this->assertEquals($data['default'], $creditCard->default);
71+
72+
if ($data['default']) {
73+
$customer = $customer->get($customer->id);
74+
$this->assertEquals($creditCard->id, $customer->defaultCard->id);
75+
}
76+
5877
$this->assertEquals($gateway, $creditCard->gateway);
5978
}
6079

0 commit comments

Comments
 (0)