Skip to content

Commit

Permalink
Merge pull request #35 from Guilherme-Franca-de-Souza/main
Browse files Browse the repository at this point in the history
Busca o cartão de crédito do usuário, quando passado o id da forma de pagamento, durante a criação da fatura
  • Loading branch information
andrewalkermo authored May 21, 2024
2 parents 64084a9 + 197d5c2 commit ddc4a88
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/Gateways/IuguGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ public function createInvoice(Invoice $invoice): Invoice
if (!empty($invoice->paymentMethod) && $invoice->paymentMethod == Invoice::PAYMENT_METHOD_CREDIT_CARD) {
if (empty($invoice->creditCard->id)) {
$invoice->creditCard = $this->createCreditCard($invoice->creditCard);
} elseif (!empty($invoice->creditCard->id) && empty($invoice->creditCard->token)) {
$invoice->creditCard = $this->getCreditCard($invoice->creditCard, $invoice->customer->id);
}
$iuguInvoiceData['customer_payment_method_id'] = $invoice->creditCard->id;
try {
Expand Down Expand Up @@ -300,6 +302,51 @@ public function createCreditCard(CreditCard $creditCard): CreditCard
return $creditCard;
}

/**
* Get customer's credit card
*
* @param CreditCard $creditCard
* @param String $customerId
* @return void
*/
public function getCreditCard(CreditCard $creditCard, $customerId)
{
try {
$iuguCreditCard = Iugu_PaymentMethod::search([
'customer_id' => $customerId,
'id' => $creditCard->id,
]);
} catch (\IuguRequestException | IuguObjectNotFound $e) {
if (str_contains($e->getMessage(), '502 Bad Gateway')) {
throw new GatewayNotAvailableException($e->getMessage());
} else {
throw new GatewayException($e->getMessage());
}
} catch (\IuguAuthenticationException $e) {
throw new GatewayNotAvailableException($e->getMessage());
} catch (\Exception $e) {
throw new GatewayException($e->getMessage());
}
if ($iuguCreditCard->errors) {
throw new GatewayException('Error getting creditCard: ', $iuguCreditCard->errors);
}

$creditCard->id = $iuguCreditCard->id ?? null;
$creditCard->brand = $iuguCreditCard->data->brand ?? null;
$creditCard->year = $iuguCreditCard->data->year ?? null;
$creditCard->month = $iuguCreditCard->data->month ?? null;
if (!empty($iuguCreditCard->data->holder_name)) {
$names = explode(' ', $iuguCreditCard->data->holder_name);
$creditCard->firstName = $names[0] ?? null;
$creditCard->lastName = $names[array_key_last($names)] ?? null;
}
$creditCard->lastDigits = $iuguCreditCard->data->last_digits ?? null;
$creditCard->gateway = 'iugu';
$creditCard->original = $iuguCreditCard;
$creditCard->createdAt = new Carbon($iuguCreditCard->created_at_iso) ?? null;
return $creditCard;
}

/**
* @inheritDoc
*/
Expand Down

0 comments on commit ddc4a88

Please sign in to comment.