Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/Gateways/IuguGateway.php
#	tests/Unit/Builders/InvoiceBuilderTest.php
  • Loading branch information
andrewalkermo committed Sep 26, 2023
2 parents 5bdc1ef + f752343 commit 5e1d7ca
Show file tree
Hide file tree
Showing 7 changed files with 264 additions and 54 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
name: Tests

on: [pull_request]
on:
push:
branches:
- main
pull_request:

jobs:
phpunit-tests:
Expand Down
37 changes: 33 additions & 4 deletions src/Builders/InvoiceBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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 @@ -104,6 +105,36 @@ 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 @@ -137,8 +168,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();
}
Expand Down Expand Up @@ -271,8 +301,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;
Expand Down
76 changes: 51 additions & 25 deletions src/Gateways/IuguGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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 @@ -64,6 +65,17 @@ 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 @@ -75,11 +87,33 @@ 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);
}

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 {
$iuguCharge = \Iugu_Charge::create($iuguInvoiceData);
Expand All @@ -94,26 +128,6 @@ public function createInvoice(Invoice $invoice): Invoice
throw $exception;
}
$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;
Expand Down Expand Up @@ -157,7 +171,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 {
Expand Down Expand Up @@ -283,7 +297,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 {
Expand Down Expand Up @@ -321,7 +335,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 {
Expand Down Expand Up @@ -419,14 +433,26 @@ 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;
$invoiceItem->quantity = $itemIugu->quantity;
$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 All @@ -440,7 +466,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;
Expand Down
13 changes: 0 additions & 13 deletions src/Models/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 40 additions & 7 deletions src/Models/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ class Invoice extends Model
*/
public ?array $items;

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

/**
* @var string|null
*/
Expand Down Expand Up @@ -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;
Expand All @@ -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']);
Expand Down Expand Up @@ -185,18 +202,19 @@ 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)
) {
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)
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down
19 changes: 19 additions & 0 deletions src/Models/InvoiceCustomVariable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Potelo\MultiPayment\Models;

/**
* Class InvoiceCustomVariable
*/
class InvoiceCustomVariable extends Model
{
/**
* @var string
*/
public string $name;

/**
* @var string
*/
public string $value;
}
Loading

0 comments on commit 5e1d7ca

Please sign in to comment.