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
12 changes: 12 additions & 0 deletions src/Contracts/InvoiceContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Potelo\MultiPayment\Contracts;

use Carbon\Carbon;
use Potelo\MultiPayment\Models\Invoice;
use Potelo\MultiPayment\Models\Customer;
use Potelo\MultiPayment\Models\CreditCard;
Expand Down Expand Up @@ -57,4 +58,15 @@ public function refundInvoice(Invoice $invoice): Invoice;
* @throws \Potelo\MultiPayment\Exceptions\ModelAttributeValidationException
*/
public function chargeInvoiceWithCreditCard(Invoice $invoice): Invoice;

/**
* Duplicate an invoice
*
* @param Invoice $invoice
* @param \Carbon\Carbon $expiresAt
* @param array $gatewayOptions
* @return Invoice
* @throws \Potelo\MultiPayment\Exceptions\GatewayException
*/
public function duplicateInvoice(Invoice $invoice, Carbon $expiresAt, array $gatewayOptions = []): Invoice;
}
28 changes: 28 additions & 0 deletions src/Gateways/IuguGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,34 @@ public function refundInvoice(Invoice $invoice): Invoice
return $this->parseInvoice($iuguInvoice, $invoice);
}

/**
* @inheritDoc
*/
public function duplicateInvoice(Invoice $invoice, Carbon $expiresAt, array $gatewayOptions = []): Invoice
{
$iuguInvoice = new \Iugu_Invoice(['id' => $invoice->id]);

$params = array_merge($gatewayOptions, [
'due_date' => $expiresAt->format('Y-m-d'),
]);
try {
$iuguInvoice = $iuguInvoice->duplicate($params);
} catch (\IuguRequestException | IuguObjectNotFound $e) {
if (str_contains($e->getMessage(), '502 Bad Gateway')) {
throw new GatewayNotAvailableException($e->getMessage());
} else {
throw new GatewayException($e->getMessage());
}
} catch (\Exception $e) {
throw new GatewayException("Error getting invoice: {$e->getMessage()}");
}
if (!empty($iuguInvoice->errors)) {
throw new GatewayException('Error getting invoice', $iuguInvoice->errors);
}

return $this->parseInvoice($iuguInvoice);
}

/**
* @inheritDoc
*/
Expand Down
15 changes: 15 additions & 0 deletions src/Models/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,4 +284,19 @@ public function chargeInvoiceWithCreditCard(?CreditCard $creditCard = null): Inv

return $gateway->chargeInvoiceWithCreditCard($this);
}

/**
* Duplicate the invoice
*
* @param \Carbon\Carbon $expiresAt
* @param array $gatewayOptions
* @return \Potelo\MultiPayment\Models\Invoice
* @throws \Potelo\MultiPayment\Exceptions\ConfigurationException
* @throws \Potelo\MultiPayment\Exceptions\GatewayException
*/
public function duplicate(Carbon $expiresAt, array $gatewayOptions = []): Invoice
{
$gateway = ConfigurationHelper::resolveGateway($this->gateway);
return $gateway->duplicateInvoice($this, $expiresAt, $gatewayOptions);
}
}
23 changes: 23 additions & 0 deletions src/MultiPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Potelo\MultiPayment;

use Carbon\Carbon;
use Potelo\MultiPayment\Exceptions\MultiPaymentException;
use Potelo\MultiPayment\Models\CreditCard;
use Potelo\MultiPayment\Models\Invoice;
Expand Down Expand Up @@ -106,6 +107,28 @@ public function getInvoice(string $id): Invoice
return Invoice::get($id, $this->gateway);
}

/**
* Duplicate an invoice
*
* @param \Potelo\MultiPayment\Models\Invoice|string $invoice
* @param \Carbon\Carbon $expiresAt
* @param array $gatewayOptions
*
* @return \Potelo\MultiPayment\Models\Invoice
* @throws \Potelo\MultiPayment\Exceptions\ConfigurationException
* @throws \Potelo\MultiPayment\Exceptions\GatewayException
*/
public function duplicateInvoice(Invoice|string $invoice, Carbon $expiresAt, array $gatewayOptions = []): Invoice
{
if (is_string($invoice)) {
$invoiceInstance = new Invoice();
$invoiceInstance->id = $invoice;
$invoice = $invoiceInstance;
}

return $invoice->duplicate($expiresAt, $gatewayOptions);
}

/**
* Return an Customer based on the customer ID
*
Expand Down
23 changes: 23 additions & 0 deletions tests/Unit/MultiPaymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,29 @@ public function testShouldGetInvoice()
$this->assertEquals($invoiceFetched->id, $invoice->id);
}

/**
* Test if can duplicate the invoice
*
* @return void
* @throws \Potelo\MultiPayment\Exceptions\GatewayException
*/
public function testShouldDuplicateInvoice()
{
$gateway = 'iugu';
$invoice = MultiPayment::setGateway($gateway)->newInvoice()
->addAvailablePaymentMethod(Invoice::PAYMENT_METHOD_PIX)
->addCustomer('Fake Customer', 'email@exemplo.com', '20176996915')
->addItem('teste', 1000, 1)
->create();

$multiPayment = new \Potelo\MultiPayment\MultiPayment($gateway);
$new = $multiPayment->duplicateInvoice($invoice->id, now()->addDays(7));
$this->assertNotEquals($new->id, $invoice->id);
$this->assertEquals($new->status, Invoice::STATUS_PENDING);
$this->assertTrue($new->expiresAt->isSameDay((now()->addDays(7))));

}

/**
* Test if thorws an exception when not find the invoice
*
Expand Down