Skip to content

Commit

Permalink
Merge pull request #36 from Potelo/revert-v1.1.1
Browse files Browse the repository at this point in the history
remove suporte a "custom variables" em faturas.
  • Loading branch information
jprodrigues70 authored Jan 31, 2024
2 parents 86327a2 + b44740d commit afcfc06
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 162 deletions.
37 changes: 4 additions & 33 deletions src/Builders/InvoiceBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Potelo\MultiPayment\Models\CreditCard;
use Potelo\MultiPayment\Contracts\Gateway;
use Potelo\MultiPayment\Models\InvoiceItem;
use Potelo\MultiPayment\Models\InvoiceCustomVariable;

/**
* invoice builder
Expand Down Expand Up @@ -105,36 +104,6 @@ public function addItem(string $description, int $price, int $quantity): Invoice
return $this;
}

/**
* Set the invoice custom variables
*
* @param InvoiceCustomVariable[] $customVariables
*
* @return InvoiceBuilder
*/
public function setCustomVariables(array $customVariables): InvoiceBuilder
{
$this->model->customVariables = $customVariables;
return $this;
}

/**
* Add an custom variable to the invoice
*
* @param string $name
* @param string $value
*
* @return $this
*/
public function addCustomVariable(string $name, string $value): InvoiceBuilder
{
$invoiceCustomVariable = new InvoiceCustomVariable();
$invoiceCustomVariable->name = $name;
$invoiceCustomVariable->value = $value;
$this->model->customVariables[] = $invoiceCustomVariable;
return $this;
}

/**
* Add a Customer instance to the invoice
* @param Customer $customer
Expand Down Expand Up @@ -168,7 +137,8 @@ public function addCustomer(
?string $phoneArea = null,
?string $phoneNumber = null,
?string $phoneCountryCode = '55'
): InvoiceBuilder {
): InvoiceBuilder
{
if (empty($this->model->customer)) {
$this->model->customer = new Customer();
}
Expand Down Expand Up @@ -301,7 +271,8 @@ public function addCreditCard(
string $lastName,
$customer = null,
string $description = 'Cartão de crédito'
): InvoiceBuilder {
): InvoiceBuilder
{
$this->model->creditCard = new CreditCard();
$this->model->creditCard->number = $number;
$this->model->creditCard->month = $month;
Expand Down
61 changes: 17 additions & 44 deletions src/Gateways/IuguGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use Potelo\MultiPayment\Models\InvoiceItem;
use Potelo\MultiPayment\Exceptions\GatewayException;
use Potelo\MultiPayment\Exceptions\ChargingException;
use Potelo\MultiPayment\Models\InvoiceCustomVariable;
use Potelo\MultiPayment\Exceptions\GatewayNotAvailableException;
use Potelo\MultiPayment\Exceptions\ModelAttributeValidationException;

Expand Down Expand Up @@ -66,17 +65,6 @@ public function createInvoice(Invoice $invoice): Invoice
'price_cents' => $item->price,
];
}

$iuguInvoiceData['custom_variables'] = [];
if (!empty($invoice->customVariables)) {
foreach ($invoice->customVariables as $customVariable) {
$iuguInvoiceData['custom_variables'][] = [
'name' => $customVariable->name,
'value' => $customVariable->value,
];
}
}

$iuguInvoiceData['due_date'] = !empty($invoice->expiresAt)
? $invoice->expiresAt->format('Y-m-d')
: Carbon::now()->format('Y-m-d');
Expand All @@ -99,31 +87,11 @@ public function createInvoice(Invoice $invoice): Invoice
}
}

try {
$iuguInvoice = \Iugu_Invoice::create($iuguInvoiceData);
} 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 ($iuguInvoice->errors) {
throw new GatewayException('Error creating invoice', $iuguInvoice->errors);
}

if (!empty($invoice->paymentMethod) && $invoice->paymentMethod == Invoice::PAYMENT_METHOD_CREDIT_CARD) {
if (empty($invoice->creditCard->id)) {
$invoice->creditCard = $this->createCreditCard($invoice->creditCard);
}
$iuguInvoiceData['customer_payment_method_id'] = $invoice->creditCard->id;
$iuguInvoiceData['invoice_id'] = $iuguInvoice->id;
unset($iuguInvoiceData['items']);

try {
$iuguCharge = \Iugu_Charge::create($iuguInvoiceData);
} catch (\Exception $e) {
Expand All @@ -137,6 +105,23 @@ public function createInvoice(Invoice $invoice): Invoice
throw $exception;
}
$iuguInvoice = $iuguCharge->invoice();
} else {
try {
$iuguInvoice = \Iugu_Invoice::create($iuguInvoiceData);
} 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 ($iuguInvoice->errors) {
throw new GatewayException('Error creating invoice', $iuguInvoice->errors);
}
}

$invoice->id = $iuguInvoice->id;
Expand Down Expand Up @@ -450,18 +435,6 @@ private function parseInvoice($iuguInvoice, ?Invoice $invoice = null): Invoice
$invoice->items[] = $invoiceItem;
}

$invoice->customVariables = [];
if (!empty($iuguInvoice->custom_variables)) {
foreach ($iuguInvoice->custom_variables as $customVariableIugu) {
$customVariable = new InvoiceCustomVariable();
$customVariable->fill([
'name' => $customVariableIugu->name,
'value' => $customVariableIugu->value,
]);
$invoice->customVariables[] = $customVariable;
}
}

$invoice->paymentMethod = $this->iuguToMultiPaymentPaymentMethod($iuguInvoice->payment_method);
$invoice->expiresAt = !empty($iuguInvoice->due_date) ? new Carbon($iuguInvoice->due_date) : null;
$invoice->createdAt = new Carbon($iuguInvoice->created_at_iso);
Expand Down
36 changes: 2 additions & 34 deletions src/Models/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,6 @@ class Invoice extends Model
*/
public ?array $items;

/**
* @var InvoiceCustomVariable[]|null
*/
public ?array $customVariables;

/**
* @var string|null
*/
Expand Down Expand Up @@ -146,19 +141,6 @@ public function fill(array $data): void
unset($data['items']);
}

if (!empty($data['custom_variables']) && is_array($data['custom_variables'])) {
$this->customVariables = [];
foreach ($data['custom_variables'] as $customVariable) {
$invoiceCustomVariable = $customVariable;
if (!empty($customVariable) && is_array($customVariable)) {
$invoiceCustomVariable = new InvoiceCustomVariable();
$invoiceCustomVariable->fill($customVariable);
}
$this->customVariables[] = $invoiceCustomVariable;
}
unset($data['custom_variables']);
}

if (!empty($data['customer']) && is_array($data['customer'])) {
$this->customer = new Customer();
$this->customer->fill($data['customer']);
Expand Down Expand Up @@ -205,7 +187,8 @@ public function attributesExtraValidation($attributes): void
if (
in_array('paymentMethod', $attributes) &&
!empty($this->paymentMethod) &&
($this->paymentMethod == Invoice::PAYMENT_METHOD_BANK_SLIP ||
(
$this->paymentMethod == Invoice::PAYMENT_METHOD_BANK_SLIP ||
$this->paymentMethod == Invoice::PAYMENT_METHOD_PIX
) &&
empty($this->expiresAt)
Expand Down Expand Up @@ -247,21 +230,6 @@ public function validateItemsAttribute()
}
}

/**
* @return void
* @throws ModelAttributeValidationException
*/
public function validateCustomVariablesAttribute()
{
foreach ($this->customVariables as $customVariable) {
if ($customVariable instanceof InvoiceCustomVariable) {
$customVariable->validate();
} else {
throw ModelAttributeValidationException::invalid('Invoice', 'customVariables', 'items must be an array of InvoiceCustomVariables');
}
}
}

/**
* @return void
* @throws ModelAttributeValidationException
Expand Down
19 changes: 0 additions & 19 deletions src/Models/InvoiceCustomVariable.php

This file was deleted.

32 changes: 0 additions & 32 deletions tests/Unit/Builders/InvoiceBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,6 @@ private function createInvoice(string $gateway, array $data): Invoice
);
}

if (isset($data['customVariables'])) {
foreach ($data['customVariables'] as $key => $value) {
$invoiceBuilder->addCustomVariable($key, $value);
}
}

if (isset($data['gatewayAdicionalOptions'])) {
$invoiceBuilder->setGatewayAdicionalOptions($data['gatewayAdicionalOptions']);
}
Expand Down Expand Up @@ -138,13 +132,6 @@ public function testShouldCreateInvoice(string $gateway, array $data): void
$this->assertNotEmpty($invoice->creditCard->id);
}

if (isset($data['customVariables'])) {
foreach ($invoice->customVariables as $customVariable) {
$this->assertArrayHasKey($customVariable->name, $data['customVariables']);
$this->assertEquals($data['customVariables'][$customVariable->name], $customVariable->value);
}
}

if (isset($data['gatewayAdicionalOptions'])) {
$this->assertEquals($data['gatewayAdicionalOptions'], $invoice->gatewayAdicionalOptions);
if ($gateway == 'iugu') {
Expand All @@ -166,13 +153,6 @@ public function testShouldCreateInvoice(string $gateway, array $data): void
$this->assertEquals($data['customer']['phoneArea'], $invoice->customer->phoneArea);
$this->assertEquals($data['customer']['phoneNumber'], $invoice->customer->phoneNumber);

if (isset($data['customVariables'])) {
foreach ($invoice->customVariables as $customVariable) {
$this->assertArrayHasKey($customVariable->name, $data['customVariables']);
$this->assertEquals($data['customVariables'][$customVariable->name], $customVariable->value);
}
}

foreach ($data['items'] as $key => $item) {
$this->assertEquals($item['description'], $invoice->items[$key]->description);
$this->assertEquals($item['price'], $invoice->items[$key]->price);
Expand Down Expand Up @@ -213,18 +193,6 @@ public function shouldCreateInvoiceDataProvider(): array
'customer' => self::customerWithAddress(),
]
],
'iugu - without payment method - with custom variable' => [
'gateway' => 'iugu',
'data' => [
'expiresAt' => Carbon::now()->addWeekday()->format('Y-m-d'),
'items' => [['description' => 'Teste', 'quantity' => 1, 'price' => 10000,]],
'customer' => self::customerWithAddress(),
'customVariables' => [
'custom_variable_1' => 'value_1',
'custom_variable_2' => 'value_2',
]
]
],
'iugu - without payment method - with adicional options' => [
'gateway' => 'iugu',
'data' => [
Expand Down

0 comments on commit afcfc06

Please sign in to comment.