Skip to content
Merged
Show file tree
Hide file tree
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
22 changes: 19 additions & 3 deletions src/Builders/InvoiceBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,32 @@ public function create(): Invoice
return parent::create();
}


/**
* Set the invoice available payment methods
*
* @param string[] $paymentMethods
*
* @return InvoiceBuilder
*/
public function setAvailablePaymentMethods(array $paymentMethods): InvoiceBuilder
{
$this->model->availablePaymentMethods = $paymentMethods;
return $this;
}

/**
* Set the invoice payment method
* Add the invoice available payment methods
*
* @param string $paymentMethod
*
* @return InvoiceBuilder
*/
public function setPaymentMethod(string $paymentMethod): InvoiceBuilder
public function addAvailablePaymentMethod(string $paymentMethod): InvoiceBuilder
{
$this->model->paymentMethod = $paymentMethod;
$paymentMethods = is_array($this->model->availablePaymentMethods) ? $this->model->availablePaymentMethods : [];
$paymentMethods[] = $paymentMethod;
$this->model->availablePaymentMethods = $paymentMethods;
return $this;
}

Expand Down
29 changes: 26 additions & 3 deletions src/Gateways/IuguGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ public function createInvoice(Invoice $invoice): Invoice
}
}

if (!empty($invoice->paymentMethod)) {
$iuguInvoiceData['payable_with'] = $invoice->paymentMethod;
if (!empty($invoice->availablePaymentMethods)) {
$iuguInvoiceData['payable_with'] = $invoice->availablePaymentMethods;
}

if (!empty($invoice->gatewayAdicionalOptions)) {
Expand All @@ -89,7 +89,11 @@ public function createInvoice(Invoice $invoice): Invoice
}
}

if (!empty($invoice->paymentMethod) && $invoice->paymentMethod == Invoice::PAYMENT_METHOD_CREDIT_CARD) {
if (
!empty($invoice->availablePaymentMethods) &&
in_array(Invoice::PAYMENT_METHOD_CREDIT_CARD, $invoice->availablePaymentMethods) &&
!empty($invoice->creditCard)
) {
if (empty($invoice->creditCard->id)) {
$invoice->creditCard = $this->createCreditCard($invoice->creditCard);
}
Expand Down Expand Up @@ -390,6 +394,25 @@ private function parseInvoice($iuguInvoice, ?Invoice $invoice = null): Invoice
$invoice->paymentMethod = $this->iuguToMultiPaymentPaymentMethod($iuguInvoice->payment_method);
}

if (!empty(($iuguInvoice->payable_with))) {

$payableWith = $iuguInvoice->payable_with;
if (is_string($payableWith)) {
$payableWith = [$payableWith];
}

foreach ($payableWith as $pm) {
$method = $this->iuguToMultiPaymentPaymentMethod($pm);
if (is_null($method) && $pm === 'all') {
$invoice->availablePaymentMethods = [
Invoice::PAYMENT_METHOD_CREDIT_CARD,
Invoice::PAYMENT_METHOD_BANK_SLIP,
Invoice::PAYMENT_METHOD_PIX,
];
}
}
}

if (empty($invoice->customer)) {
$invoice->customer = new Customer();
}
Expand Down
64 changes: 19 additions & 45 deletions src/Models/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ class Invoice extends Model
*/
public ?string $paymentMethod = null;

/**
* @var string[]|null
*/
public ?array $availablePaymentMethods = null;

/**
* @var CreditCard|null
*/
Expand Down Expand Up @@ -175,36 +180,6 @@ public function attributesExtraValidation($attributes): void
if (in_array('amount', $attributes) && in_array('items', $attributes) && empty($this->amount) && empty($this->items)) {
throw ModelAttributeValidationException::required($model, 'amount or items');
}

if (
in_array('paymentMethod', $attributes) &&
!empty($this->paymentMethod) &&
$this->paymentMethod == Invoice::PAYMENT_METHOD_CREDIT_CARD &&
empty($this->creditCard)
) {
throw new ModelAttributeValidationException('The `creditCard` attribute is required for credit_card payment method.');
}

if (
in_array('paymentMethod', $attributes) &&
!empty($this->paymentMethod) &&
(
$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) &&
!empty($this->paymentMethod) &&
$this->paymentMethod == Invoice::PAYMENT_METHOD_BANK_SLIP &&
empty($this->customer->address)
) {
throw new ModelAttributeValidationException('The customer address is required for bank_slip payment method');
}
}

/**
Expand Down Expand Up @@ -235,22 +210,21 @@ public function validateItemsAttribute()
* @return void
* @throws ModelAttributeValidationException
*/
public function validatePaymentMethodAttribute()
public function validateAvailablePaymentMethodsAttribute()
{
if (!in_array($this->paymentMethod, [
Invoice::PAYMENT_METHOD_CREDIT_CARD,
Invoice::PAYMENT_METHOD_BANK_SLIP,
Invoice::PAYMENT_METHOD_PIX,
])) {
throw ModelAttributeValidationException::invalid(
'Invoice',
'paymentMethod',
'paymentMethod must be one of: ' . implode(', ', [
Invoice::PAYMENT_METHOD_CREDIT_CARD,
Invoice::PAYMENT_METHOD_BANK_SLIP,
Invoice::PAYMENT_METHOD_PIX,
])
);
$meethods = [
self::PAYMENT_METHOD_CREDIT_CARD,
self::PAYMENT_METHOD_BANK_SLIP,
self::PAYMENT_METHOD_PIX,
];

if (!is_array($this->availablePaymentMethods)) {
throw ModelAttributeValidationException::invalid('Invoice', 'availablePaymentMethods', 'availablePaymentMethods must be an array of payment methods');
}
foreach ($this->availablePaymentMethods as $method) {
if (!in_array($method, $meethods)) {
throw ModelAttributeValidationException::invalid('Invoice', 'availablePaymentMethods', 'availablePaymentMethods must be one of: ' . implode(', ', $meethods));
}
}
}

Expand Down
21 changes: 11 additions & 10 deletions tests/Unit/Builders/InvoiceBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ private function createInvoice(string $gateway, array $data): Invoice
if (isset($data['expiresAt'])) {
$invoiceBuilder->setExpiresAt($data['expiresAt']);
}
if (isset($data['paymentMethod'])) {
$invoiceBuilder->setPaymentMethod($data['paymentMethod']);
if (isset($data['availablePaymentMethods'])) {
$invoiceBuilder->setAvailablePaymentMethods($data['availablePaymentMethods']);
}
if (isset($data['creditCard'])) {
$invoiceBuilder->addCreditCard(
Expand Down Expand Up @@ -137,15 +137,16 @@ public function testShouldCreateInvoice(string $gateway, array $data): void
$this->assertNotEmpty($invoice->creditCard->id);
}

if ((isset($data['paymentMethod']) && $data['paymentMethod'] === 'bank_slip') || (isset($data['gatewayAdicionalOptions']) && in_array('payable_with', $data['gatewayAdicionalOptions']) && in_array('bank_slip', $data['gatewayAdicionalOptions']['payable_with']))) {
if ((isset($data['availablePaymentMethods']) && in_array('bank_slip', $data['availablePaymentMethods'])) || (isset($data['paymentMethod']) && $data['paymentMethod'] === 'bank_slip') || (isset($data['gatewayAdicionalOptions']) && in_array('payable_with', $data['gatewayAdicionalOptions']) && in_array('bank_slip', $data['gatewayAdicionalOptions']['payable_with']))) {
$this->assertNotEmpty($invoice->bankSlip);
$this->assertNotEmpty($invoice->bankSlip->url);
$this->assertNotEmpty($invoice->bankSlip->number);
$this->assertNotEmpty($invoice->bankSlip->barcodeData);
$this->assertNotEmpty($invoice->bankSlip->barcodeImage);
}

if ((isset($data['paymentMethod']) && $data['paymentMethod'] === 'pix') || (isset($data['gatewayAdicionalOptions']) && in_array('payable_with', $data['gatewayAdicionalOptions']) && in_array('pix', $data['gatewayAdicionalOptions']['payable_with']))) {
if ((isset($data['availablePaymentMethods']) && in_array('pix', $data['availablePaymentMethods'])) || (isset($data['paymentMethod']) && $data['paymentMethod'] === 'pix') || (isset($data['gatewayAdicionalOptions']) && in_array('payable_with', $data['gatewayAdicionalOptions']) && in_array('pix', $data['gatewayAdicionalOptions']['payable_with']))) {

$this->assertNotEmpty($invoice->pix);
$this->assertNotEmpty($invoice->pix->qrCodeImageUrl);
$this->assertNotEmpty($invoice->pix->qrCodeText);
Expand Down Expand Up @@ -258,7 +259,7 @@ public function shouldCreateInvoiceDataProvider(): array
'data' => [
'items' => [['description' => 'Teste', 'quantity' => 1, 'price' => 10000,]],
'customer' => self::customerWithoutAddress(),
'paymentMethod' => 'credit_card',
'availablePaymentMethods' => ['credit_card'],
'creditCard' => self::creditCard(),
]
],
Expand All @@ -267,7 +268,7 @@ public function shouldCreateInvoiceDataProvider(): array
'data' => [
'items' => [['description' => 'Teste', 'quantity' => 1, 'price' => 10000,]],
'customer' => self::customerWithAddress(),
'paymentMethod' => 'credit_card',
'availablePaymentMethods' => ['credit_card'],
'creditCard' => self::creditCard(),
]
],
Expand All @@ -277,7 +278,7 @@ public function shouldCreateInvoiceDataProvider(): array
'expiresAt' => Carbon::now()->addWeekday()->format('Y-m-d'),
'items' => [['description' => 'Teste', 'quantity' => 1, 'price' => 10000,]],
'customer' => self::customerWithAddress(),
'paymentMethod' => 'bank_slip',
'availablePaymentMethods' => ['bank_slip'],
]
],
'iugu - pix with address' => [
Expand All @@ -286,7 +287,7 @@ public function shouldCreateInvoiceDataProvider(): array
'expiresAt' => Carbon::now()->addWeekday()->format('Y-m-d'),
'items' => [['description' => 'Teste', 'quantity' => 1, 'price' => 10000,]],
'customer' => self::customerWithAddress(),
'paymentMethod' => 'pix',
'availablePaymentMethods' => ['pix'],
]
],
'iugu - pix without address' => [
Expand All @@ -295,7 +296,7 @@ public function shouldCreateInvoiceDataProvider(): array
'expiresAt' => Carbon::now()->addWeekday()->format('Y-m-d'),
'items' => [['description' => 'Teste', 'quantity' => 1, 'price' => 10000,]],
'customer' => self::customerWithoutAddress(),
'paymentMethod' => 'pix',
'availablePaymentMethods' => ['pix'],
]
],
];
Expand Down Expand Up @@ -325,7 +326,7 @@ public function shouldNotCreateInvoiceDataProvider(): array
'data' => [
'items' => [['description' => 'Teste', 'quantity' => 1, 'price' => 10000,]],
'customer' => self::customerWithAddress(),
'paymentMethod' => 'credit_card',
'availablePaymentMethods' => ['credit_card'],
'creditCard' => array_merge(self::creditCard(), ['number' => '4012888888881881']),
],
],
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/MultiPaymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function testShouldGetInvoice()
{
$gateway = 'iugu';
$invoice = MultiPayment::setGateway($gateway)->newInvoice()
->setPaymentMethod(Invoice::PAYMENT_METHOD_CREDIT_CARD)
->addAvailablePaymentMethod(Invoice::PAYMENT_METHOD_CREDIT_CARD)
->addCustomer('Fake Customer', 'email@exemplo.com', '20176996915')
->addItem('teste', 1000, 1)
->addCreditCardToken(self::iuguCreditCardToken())
Expand Down Expand Up @@ -90,7 +90,7 @@ public function testShouldRefundInvoice(string $gateway, array $data, string $st
$invoiceBuilder->addItem($item['description'], $item['price'], $item['quantity']);
$total += $item['price'] * $item['quantity'];
}
$invoiceBuilder->setPaymentMethod($data['paymentMethod']);
$invoiceBuilder->addAvailablePaymentMethod($data['paymentMethod']);
$invoiceBuilder->addCreditCard(
$data['creditCard']['number'] ?? null,
$data['creditCard']['month'] ?? null,
Expand Down