From 1003529e81a38499272cc734e84bd509d3b6e325 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 12 Jul 2023 12:51:34 -0300 Subject: [PATCH 01/12] invoiceCustomVariable class --- src/Models/InvoiceCustomVariable.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/Models/InvoiceCustomVariable.php diff --git a/src/Models/InvoiceCustomVariable.php b/src/Models/InvoiceCustomVariable.php new file mode 100644 index 0000000..7e23ebd --- /dev/null +++ b/src/Models/InvoiceCustomVariable.php @@ -0,0 +1,19 @@ + Date: Wed, 12 Jul 2023 15:29:52 -0300 Subject: [PATCH 02/12] insere custom variables --- src/Builders/InvoiceBuilder.php | 38 +++++++++++++++++++++++--- src/Gateways/IuguGateway.php | 19 +++++++++---- src/Models/Invoice.php | 47 ++++++++++++++++++++++++++++----- 3 files changed, 88 insertions(+), 16 deletions(-) diff --git a/src/Builders/InvoiceBuilder.php b/src/Builders/InvoiceBuilder.php index 52761f2..42acf5a 100644 --- a/src/Builders/InvoiceBuilder.php +++ b/src/Builders/InvoiceBuilder.php @@ -9,6 +9,7 @@ use Potelo\MultiPayment\Models\CreditCard; use Potelo\MultiPayment\Contracts\Gateway; use Potelo\MultiPayment\Models\InvoiceItem; +use Potelo\MultiPayment\Models\InvoiceCustomVariable; /** * invoice builder @@ -92,6 +93,37 @@ 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 @@ -125,8 +157,7 @@ public function addCustomer( ?string $phoneArea = null, ?string $phoneNumber = null, ?string $phoneCountryCode = '55' - ): InvoiceBuilder - { + ): InvoiceBuilder { if (empty($this->model->customer)) { $this->model->customer = new Customer(); } @@ -259,8 +290,7 @@ 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; diff --git a/src/Gateways/IuguGateway.php b/src/Gateways/IuguGateway.php index f76ed8a..fb3363e 100644 --- a/src/Gateways/IuguGateway.php +++ b/src/Gateways/IuguGateway.php @@ -63,6 +63,15 @@ public function createInvoice(Invoice $invoice): Invoice 'price_cents' => $item->price, ]; } + + $iuguInvoiceData['custom_variables'] = []; + 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'); @@ -95,7 +104,7 @@ public function createInvoice(Invoice $invoice): Invoice } try { $iuguInvoice = \Iugu_Invoice::create($iuguInvoiceData); - } catch (\IuguRequestException|IuguObjectNotFound $e) { + } catch (\IuguRequestException | IuguObjectNotFound $e) { if (str_contains($e->getMessage(), '502 Bad Gateway')) { throw new GatewayNotAvailableException($e->getMessage()); } else { @@ -152,7 +161,7 @@ public function createCustomer(Customer $customer): Customer try { $iuguCustomer = Iugu_Customer::create($iuguCustomerData); - } catch (\IuguRequestException|IuguObjectNotFound $e) { + } catch (\IuguRequestException | IuguObjectNotFound $e) { if (str_contains($e->getMessage(), '502 Bad Gateway')) { throw new GatewayNotAvailableException($e->getMessage()); } else { @@ -278,7 +287,7 @@ public function createCreditCard(CreditCard $creditCard): CreditCard 'customer_id' => $creditCard->customer->id, 'description' => $creditCard->description ?? 'CREDIT CARD', ]); - } catch (\IuguRequestException|IuguObjectNotFound $e) { + } catch (\IuguRequestException | IuguObjectNotFound $e) { if (str_contains($e->getMessage(), '502 Bad Gateway')) { throw new GatewayNotAvailableException($e->getMessage()); } else { @@ -316,7 +325,7 @@ public function getInvoice(string $id): Invoice { try { $iuguInvoice = \Iugu_Invoice::fetch($id); - } catch (\IuguRequestException|IuguObjectNotFound $e ) { + } catch (\IuguRequestException | IuguObjectNotFound $e) { if (str_contains($e->getMessage(), '502 Bad Gateway')) { throw new GatewayNotAvailableException($e->getMessage()); } else { @@ -414,7 +423,7 @@ private function parseInvoice($iuguInvoice, ?Invoice $invoice = null): Invoice $invoice->items = []; - foreach($iuguInvoice->items as $itemIugu) { + foreach ($iuguInvoice->items as $itemIugu) { $invoiceItem = new InvoiceItem(); $invoiceItem->description = $itemIugu->description; $invoiceItem->price = $itemIugu->price_cents; diff --git a/src/Models/Invoice.php b/src/Models/Invoice.php index 24c5541..ca9ebba 100644 --- a/src/Models/Invoice.php +++ b/src/Models/Invoice.php @@ -61,6 +61,11 @@ class Invoice extends Model */ public ?array $items; + /** + * @var InvoiceCustomVariable[]|null + */ + public ?array $customVariables; + /** * @var string|null */ @@ -128,8 +133,7 @@ public function fill(array $data): void ]); $this->items[] = $invoiceItem; unset($data['amount']); - } - elseif (!empty($data['items'])) { + } elseif (!empty($data['items'])) { $this->items = []; foreach ($data['items'] as $item) { $invoiceItem = $item; @@ -142,6 +146,19 @@ 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']); @@ -185,10 +202,10 @@ public function attributesExtraValidation($attributes): void throw new ModelAttributeValidationException('The `creditCard` attribute is required for credit_card payment method.'); } - if (in_array('paymentMethod', $attributes) && + 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) @@ -196,7 +213,8 @@ public function attributesExtraValidation($attributes): void throw new ModelAttributeValidationException('The `expiresAt` attribute is required for bank_slip or pix payment method.'); } - if (in_array('paymentMethod', $attributes) && + if ( + in_array('paymentMethod', $attributes) && !empty($this->paymentMethod) && $this->paymentMethod == Invoice::PAYMENT_METHOD_BANK_SLIP && empty($this->customer->address) @@ -229,6 +247,21 @@ 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 @@ -242,7 +275,7 @@ public function validatePaymentMethodAttribute() ])) { throw ModelAttributeValidationException::invalid( 'Invoice', - 'paymentMethod' , + 'paymentMethod', 'paymentMethod must be one of: ' . implode(', ', [ Invoice::PAYMENT_METHOD_CREDIT_CARD, Invoice::PAYMENT_METHOD_BANK_SLIP, From e6d0b82906940be438bddce44d8767aaa56cff69 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 19 Jul 2023 16:21:53 -0300 Subject: [PATCH 03/12] Cria a fatura para a cobranca --- src/Gateways/IuguGateway.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Gateways/IuguGateway.php b/src/Gateways/IuguGateway.php index fb3363e..b22a15c 100644 --- a/src/Gateways/IuguGateway.php +++ b/src/Gateways/IuguGateway.php @@ -90,6 +90,8 @@ public function createInvoice(Invoice $invoice): Invoice $iuguInvoiceData['customer_payment_method_id'] = $invoice->creditCard->id; try { + $iuguInvoiceData['invoice_id'] = \Iugu_Invoice::create($iuguInvoiceData); + unset($iuguInvoiceData['items']); $iuguCharge = \Iugu_Charge::create($iuguInvoiceData); } catch (\Exception $e) { throw new GatewayException($e->getMessage()); From 34a5abeb1b0a98779f3057035938ce44e1a89af2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Oliveira?= Date: Thu, 17 Aug 2023 09:53:05 -0300 Subject: [PATCH 04/12] =?UTF-8?q?remove=20valida=C3=A7=C3=A3o=20de=20taman?= =?UTF-8?q?ho=20do=20cpf/cnpj?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Models/Customer.php | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/Models/Customer.php b/src/Models/Customer.php index d2a12b9..e72a933 100644 --- a/src/Models/Customer.php +++ b/src/Models/Customer.php @@ -82,19 +82,6 @@ protected function validateEmailAttribute(): void } } - /** - * @return void - * @throws ModelAttributeValidationException - */ - protected function validateTaxDocumentAttribute(): void - { - //regex for 11 or 14 digits - $pattern = '/^[0-9]{11,14}$/'; - if (!preg_match($pattern, $this->taxDocument)) { - throw ModelAttributeValidationException::invalid($this->getClassName(), 'taxDocument', 'The taxDocument must be a string with 11 or 14 digits.'); - } - } - /** * @return void * @throws ModelAttributeValidationException From 0bb771d0903c3de6a6f60b4e12271917cf6c5412 Mon Sep 17 00:00:00 2001 From: Andre Walker Date: Mon, 25 Sep 2023 22:32:13 -0300 Subject: [PATCH 05/12] =?UTF-8?q?fix:=20corrige=20erro=20ao=20tentar=20ace?= =?UTF-8?q?ssar=20vari=C3=A1vel=20n=C3=A3o=20inicializada?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Gateways/IuguGateway.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Gateways/IuguGateway.php b/src/Gateways/IuguGateway.php index b22a15c..57f57e3 100644 --- a/src/Gateways/IuguGateway.php +++ b/src/Gateways/IuguGateway.php @@ -65,11 +65,13 @@ public function createInvoice(Invoice $invoice): Invoice } $iuguInvoiceData['custom_variables'] = []; - foreach ($invoice->customVariables as $customVariable) { - $iuguInvoiceData['custom_variables'][] = [ - 'name' => $customVariable->name, - 'value' => $customVariable->value, - ]; + if (!empty($invoice->customVariables)) { + foreach ($invoice->customVariables as $customVariable) { + $iuguInvoiceData['custom_variables'][] = [ + 'name' => $customVariable->name, + 'value' => $customVariable->value, + ]; + } } $iuguInvoiceData['due_date'] = !empty($invoice->expiresAt) From 09f54e56499803671cca0a29ed2d193932fd9335 Mon Sep 17 00:00:00 2001 From: Andre Walker Date: Mon, 25 Sep 2023 22:33:51 -0300 Subject: [PATCH 06/12] test: faz assert de mais dados da fatura e testa customVariable --- tests/Unit/Builders/InvoiceBuilderTest.php | 69 ++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/tests/Unit/Builders/InvoiceBuilderTest.php b/tests/Unit/Builders/InvoiceBuilderTest.php index 0e41bc9..8b3d6d9 100644 --- a/tests/Unit/Builders/InvoiceBuilderTest.php +++ b/tests/Unit/Builders/InvoiceBuilderTest.php @@ -64,9 +64,66 @@ public function testShouldCreateInvoice(string $gateway, array $data): void ); } + if (isset($data['customVariables'])) { + foreach ($data['customVariables'] as $key => $value) { + $invoiceBuilder->addCustomVariable($key, $value); + } + } + $invoice = $invoiceBuilder->create(); + $this->assertInstanceOf(\Potelo\MultiPayment\Models\Invoice::class, $invoice); $this->assertNotEmpty($invoice->id); + $this->assertEquals($data['customer']['name'], $invoice->customer->name); + $this->assertEquals($data['customer']['email'], $invoice->customer->email); + $this->assertEquals($data['customer']['taxDocument'], $invoice->customer->taxDocument); + $this->assertEquals($data['customer']['birthDate'], $invoice->customer->birthDate); + $this->assertEquals($data['customer']['phoneArea'], $invoice->customer->phoneArea); + $this->assertEquals($data['customer']['phoneNumber'], $invoice->customer->phoneNumber); + + if (isset($data['customer']['address'])) { + $this->assertEquals($data['customer']['address']['zipCode'], $invoice->customer->address->zipCode); + $this->assertEquals($data['customer']['address']['street'], $invoice->customer->address->street); + $this->assertEquals($data['customer']['address']['number'], $invoice->customer->address->number); + $this->assertEquals($data['customer']['address']['complement'], $invoice->customer->address->complement); + $this->assertEquals($data['customer']['address']['district'], $invoice->customer->address->district); + $this->assertEquals($data['customer']['address']['city'], $invoice->customer->address->city); + $this->assertEquals($data['customer']['address']['state'], $invoice->customer->address->state); + $this->assertEquals($data['customer']['address']['country'], $invoice->customer->address->country); + } + + foreach ($data['items'] as $key => $item) { + $this->assertEquals($item['description'], $invoice->items[$key]->description); + $this->assertEquals($item['price'], $invoice->items[$key]->price); + $this->assertEquals($item['quantity'], $invoice->items[$key]->quantity); + } + + if (isset($data['expiresAt'])) { + $this->assertEquals($data['expiresAt'], $invoice->expiresAt->format('Y-m-d')); + } + + if (isset($data['paymentMethod'])) { + $this->assertEquals($data['paymentMethod'], $invoice->paymentMethod); + } + + if (isset($data['creditCard'])) { + $this->assertEquals($data['creditCard']['number'], $invoice->creditCard->number); + $this->assertEquals($data['creditCard']['month'], $invoice->creditCard->month); + $this->assertEquals($data['creditCard']['year'], $invoice->creditCard->year); + $this->assertEquals($data['creditCard']['cvv'], $invoice->creditCard->cvv); + $this->assertEquals($data['creditCard']['firstName'], $invoice->creditCard->firstName); + $this->assertEquals($data['creditCard']['lastName'], $invoice->creditCard->lastName); + $this->assertNotEmpty($invoice->creditCard->token); + $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); + } + } + $this->assertNotEmpty($invoice->status); } /** @@ -83,6 +140,18 @@ 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 - company with address without payment method' => [ 'gateway' => 'iugu', 'data' => [ From d1c2c356d2b170c515de551769cc49239c6797f4 Mon Sep 17 00:00:00 2001 From: Andre Walker Date: Mon, 25 Sep 2023 22:57:13 -0300 Subject: [PATCH 07/12] =?UTF-8?q?refactor:=20restrutura=20l=C3=B3gica=20de?= =?UTF-8?q?=20cobran=C3=A7a=20que=20sempre=20cria=20fatura=20antes=20de=20?= =?UTF-8?q?tentar=20cobrar?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Gateways/IuguGateway.php | 46 +++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/src/Gateways/IuguGateway.php b/src/Gateways/IuguGateway.php index 57f57e3..45a8f42 100644 --- a/src/Gateways/IuguGateway.php +++ b/src/Gateways/IuguGateway.php @@ -85,15 +85,37 @@ public function createInvoice(Invoice $invoice): Invoice } } + if (!empty($invoice->paymentMethod)) { + $iuguInvoiceData['payable_with'] = $invoice->paymentMethod; + } + + 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); + } + + // se for cartão de crédito, tenta cobrar 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'] = \Iugu_Invoice::create($iuguInvoiceData); + unset($iuguInvoiceData['items']); try { - $iuguInvoiceData['invoice_id'] = \Iugu_Invoice::create($iuguInvoiceData); - unset($iuguInvoiceData['items']); $iuguCharge = \Iugu_Charge::create($iuguInvoiceData); } catch (\Exception $e) { throw new GatewayException($e->getMessage()); @@ -102,26 +124,6 @@ public function createInvoice(Invoice $invoice): Invoice throw new GatewayException('Error charging invoice', $iuguCharge->errors); } $iuguInvoice = $iuguCharge->invoice(); - } else { - if (!empty($invoice->paymentMethod)) { - $iuguInvoiceData['payable_with'] = $invoice->paymentMethod; - } - 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; From 12f798dd209f090d6a6a162848648cccb5990fe8 Mon Sep 17 00:00:00 2001 From: Andre Walker Date: Tue, 26 Sep 2023 09:48:52 -0300 Subject: [PATCH 08/12] fix: recupera custom_variables no fetch de fatura --- src/Gateways/IuguGateway.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Gateways/IuguGateway.php b/src/Gateways/IuguGateway.php index 45a8f42..48c5c14 100644 --- a/src/Gateways/IuguGateway.php +++ b/src/Gateways/IuguGateway.php @@ -18,6 +18,7 @@ use Potelo\MultiPayment\Contracts\Gateway; use Potelo\MultiPayment\Models\InvoiceItem; use Potelo\MultiPayment\Exceptions\GatewayException; +use Potelo\MultiPayment\Models\InvoiceCustomVariable; use Potelo\MultiPayment\Exceptions\GatewayNotAvailableException; use Potelo\MultiPayment\Exceptions\ModelAttributeValidationException; @@ -437,6 +438,18 @@ 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); From 23cf156c8600a9607ad73a21a73b8b1c8ad8b34b Mon Sep 17 00:00:00 2001 From: Andre Walker Date: Tue, 26 Sep 2023 09:49:15 -0300 Subject: [PATCH 09/12] fix: corrige typo em district --- src/Gateways/IuguGateway.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Gateways/IuguGateway.php b/src/Gateways/IuguGateway.php index 48c5c14..f201fae 100644 --- a/src/Gateways/IuguGateway.php +++ b/src/Gateways/IuguGateway.php @@ -463,7 +463,7 @@ private function parseInvoice($iuguInvoice, ?Invoice $invoice = null): Invoice $invoice->customer->address->zipCode = $iuguInvoice->payer_address_zip_code; $invoice->customer->address->street = $iuguInvoice->payer_address_street; $invoice->customer->address->number = $iuguInvoice->payer_address_number; - $invoice->customer->address->district = $iuguInvoice->payer_address_disctrict; + $invoice->customer->address->district = $iuguInvoice->payer_address_district; $invoice->customer->address->city = $iuguInvoice->payer_address_city; $invoice->customer->address->state = $iuguInvoice->payer_address_state; $invoice->customer->address->complement = $iuguInvoice->payer_address_complement; From e0e3a89036d9fe88faeb245053f3c40e40c2fb81 Mon Sep 17 00:00:00 2001 From: Andre Walker Date: Tue, 26 Sep 2023 09:51:57 -0300 Subject: [PATCH 10/12] =?UTF-8?q?test:=20faz=20assert=20de=20dados=20atual?= =?UTF-8?q?izados=20na=20cria=C3=A7=C3=A3o=20da=20fatura?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/Unit/Builders/InvoiceBuilderTest.php | 52 ++++++++++++++++++++-- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/tests/Unit/Builders/InvoiceBuilderTest.php b/tests/Unit/Builders/InvoiceBuilderTest.php index 8b3d6d9..1286ccf 100644 --- a/tests/Unit/Builders/InvoiceBuilderTest.php +++ b/tests/Unit/Builders/InvoiceBuilderTest.php @@ -72,8 +72,11 @@ public function testShouldCreateInvoice(string $gateway, array $data): void $invoice = $invoiceBuilder->create(); + $this->assertInstanceOf(\Potelo\MultiPayment\Models\Invoice::class, $invoice); $this->assertNotEmpty($invoice->id); + $this->assertNotEmpty($invoice->status); + $this->assertEquals($data['customer']['name'], $invoice->customer->name); $this->assertEquals($data['customer']['email'], $invoice->customer->email); $this->assertEquals($data['customer']['taxDocument'], $invoice->customer->taxDocument); @@ -123,7 +126,48 @@ public function testShouldCreateInvoice(string $gateway, array $data): void $this->assertEquals($data['customVariables'][$customVariable->name], $customVariable->value); } } + + // Verifica se a fatura foi criada no gateway com os dados corretos + $invoice = $invoice->get($invoice->id, $gateway); + $this->assertNotEmpty($invoice->status); + + $this->assertEquals($data['customer']['name'], $invoice->customer->name); + $this->assertEquals($data['customer']['email'], $invoice->customer->email); + $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); + $this->assertEquals($item['quantity'], $invoice->items[$key]->quantity); + } + + if (isset($data['expiresAt'])) { + $this->assertEquals($data['expiresAt'], $invoice->expiresAt->format('Y-m-d')); + } + + if (isset($data['paymentMethod']) && $invoice->status === $invoice::STATUS_PAID) { + $this->assertEquals($data['paymentMethod'], $invoice->paymentMethod); + } + + if (isset($data['customer']['address'])) { + $this->assertEquals($data['customer']['address']['zipCode'], $invoice->customer->address->zipCode); + $this->assertEquals($data['customer']['address']['street'], $invoice->customer->address->street); + $this->assertEquals($data['customer']['address']['number'], $invoice->customer->address->number); + $this->assertEquals($data['customer']['address']['complement'], $invoice->customer->address->complement); + $this->assertEquals($data['customer']['address']['district'], $invoice->customer->address->district); + $this->assertEquals($data['customer']['address']['city'], $invoice->customer->address->city); + $this->assertEquals($data['customer']['address']['state'], $invoice->customer->address->state); + $this->assertEquals($data['customer']['address']['country'], $invoice->customer->address->country); + } } /** @@ -244,12 +288,12 @@ public static function customerWithAddress(): array public static function address(): array { $address['zipCode'] = '41820330'; - $address['street'] = 'Rua Exemplo'; + $address['street'] = 'Rua Deputado Mário Lima'; $address['number'] = '123'; - $address['district'] = 'Bairro Exemplo'; + $address['district'] = 'Caminho das Arvores'; $address['complement'] = 'Apto. 123'; - $address['city'] = 'Cidade Exemplo'; - $address['state'] = 'Estado'; + $address['city'] = 'Salvador'; + $address['state'] = 'BA'; $address['country'] = 'Brasil'; return $address; } From 8bdfd8a4a09ae2e93bd421abffc90a5883902407 Mon Sep 17 00:00:00 2001 From: Andre Walker Date: Tue, 26 Sep 2023 09:53:17 -0300 Subject: [PATCH 11/12] docs: remove quebra de linha --- src/Builders/InvoiceBuilder.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Builders/InvoiceBuilder.php b/src/Builders/InvoiceBuilder.php index 42acf5a..8c21436 100644 --- a/src/Builders/InvoiceBuilder.php +++ b/src/Builders/InvoiceBuilder.php @@ -110,7 +110,6 @@ public function setCustomVariables(array $customVariables): InvoiceBuilder * Add an custom variable to the invoice * * @param string $name - * * @param string $value * * @return $this From f752343cf0926707181786c405b1650311635fb3 Mon Sep 17 00:00:00 2001 From: Andre Walker Date: Tue, 26 Sep 2023 10:16:22 -0300 Subject: [PATCH 12/12] ci: roda testes na branch main --- .github/workflows/test.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 00282da..43bc24f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,6 +1,10 @@ name: Tests -on: [pull_request] +on: + push: + branches: + - main + pull_request: jobs: phpunit-tests: