Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Busca o cartão de crédito do usuário, quando passado o id da forma de pagamento, durante a criação da fatura #39

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading