Skip to content

Commit

Permalink
Merge pull request #27 from Potelo/charge-exception
Browse files Browse the repository at this point in the history
Lança exceção quando der erro na cobrança
  • Loading branch information
jprodrigues70 authored Aug 16, 2023
2 parents 18c7168 + 5bdc1ef commit 258d8ed
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 30 deletions.
14 changes: 13 additions & 1 deletion src/Builders/InvoiceBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Potelo\MultiPayment\Builders;

use Carbon\Carbon;
use Potelo\MultiPayment\Models\Model;
use Potelo\MultiPayment\Models\Invoice;
use Potelo\MultiPayment\Models\Address;
use Potelo\MultiPayment\Models\Customer;
Expand All @@ -13,7 +14,6 @@
/**
* invoice builder
*
* @method Invoice create()
* @method Invoice get()
*/
class InvoiceBuilder extends Builder
Expand All @@ -31,6 +31,18 @@ public function __construct($gateway = null)
$this->model = new Invoice();
}

/**
* @return \Potelo\MultiPayment\Models\Invoice
* @throws \Potelo\MultiPayment\Exceptions\GatewayException
* @throws \Potelo\MultiPayment\Exceptions\ChargingException
* @throws \Potelo\MultiPayment\Exceptions\GatewayNotAvailableException
* @throws \Potelo\MultiPayment\Exceptions\ModelAttributeValidationException
*/
public function create(): Invoice
{
return parent::create();
}

/**
* Set the invoice payment method
*
Expand Down
11 changes: 11 additions & 0 deletions src/Exceptions/ChargingException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Potelo\MultiPayment\Exceptions;

class ChargingException extends MultiPaymentException
{
/**
* @var mixed $chargeResponse The charge response from the gateway
*/
public $chargeResponse;
}
7 changes: 6 additions & 1 deletion src/Gateways/IuguGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Potelo\MultiPayment\Contracts\Gateway;
use Potelo\MultiPayment\Models\InvoiceItem;
use Potelo\MultiPayment\Exceptions\GatewayException;
use Potelo\MultiPayment\Exceptions\ChargingException;
use Potelo\MultiPayment\Exceptions\GatewayNotAvailableException;
use Potelo\MultiPayment\Exceptions\ModelAttributeValidationException;

Expand Down Expand Up @@ -46,7 +47,7 @@ public function __construct()

/**
* @inheritDoc
* @throws ModelAttributeValidationException
* @throws ModelAttributeValidationException|ChargingException
*/
public function createInvoice(Invoice $invoice): Invoice
{
Expand Down Expand Up @@ -87,6 +88,10 @@ public function createInvoice(Invoice $invoice): Invoice
}
if ($iuguCharge->errors) {
throw new GatewayException('Error charging invoice', $iuguCharge->errors);
} elseif (!$iuguCharge->success) {
$exception = new ChargingException('Error charging invoice: ' . $iuguCharge->info_message);
$exception->chargeResponse = $iuguCharge;
throw $exception;
}
$iuguInvoice = $iuguCharge->invoice();
} else {
Expand Down
76 changes: 48 additions & 28 deletions tests/Unit/Builders/InvoiceBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,21 @@

use Carbon\Carbon;
use Potelo\MultiPayment\Tests\TestCase;
use Potelo\MultiPayment\Models\Invoice;
use Potelo\MultiPayment\Exceptions\ChargingException;

class InvoiceBuilderTest extends TestCase
{

/**
* Create invoice test.
*
* @dataProvider shouldCreateInvoiceDataProvider
* Create a invoice with mocked data
*
* @param string $gateway
* @param array $data
*
* @return void
* @throws \Potelo\MultiPayment\Exceptions\GatewayException
* @throws \Potelo\MultiPayment\Exceptions\ModelAttributeValidationException
* @return \Potelo\MultiPayment\Models\Invoice
*/
public function testShouldCreateInvoice(string $gateway, array $data): void
private function createInvoice(string $gateway, array $data): Invoice
{
$multiPayment = new \Potelo\MultiPayment\MultiPayment($gateway);
$invoiceBuilder = $multiPayment->newInvoice();
Expand Down Expand Up @@ -64,7 +62,24 @@ public function testShouldCreateInvoice(string $gateway, array $data): void

);
}
$invoice = $invoiceBuilder->create();
return $invoiceBuilder->create();
}

/**
* Create invoice test.
*
* @dataProvider shouldCreateInvoiceDataProvider
*
* @param string $gateway
* @param array $data
*
* @return void
* @throws \Potelo\MultiPayment\Exceptions\GatewayException
* @throws \Potelo\MultiPayment\Exceptions\ModelAttributeValidationException
*/
public function testShouldCreateInvoice(string $gateway, array $data): void
{
$invoice = $this->createInvoice($gateway, $data);
$this->assertInstanceOf(\Potelo\MultiPayment\Models\Invoice::class, $invoice);
$this->assertNotEmpty($invoice->id);
}
Expand Down Expand Up @@ -139,30 +154,35 @@ public function shouldCreateInvoiceDataProvider(): array
];
}

public static function customerWithoutAddress(): array
{
$customer['name'] = 'Fake Customer';
$customer['email'] = '[email protected]';
$customer['taxDocument'] = '20176996915';
$customer['birthDate'] = '1980-01-01';
$customer['phoneArea'] = '71';
$customer['phoneNumber'] = '982345678';
return $customer;
}

public static function companyWithAddress(): array
/**
* Fail to create invoice test.
*
* @dataProvider shouldNotCreateInvoiceDataProvider
*
* @param string $gateway
* @param array $data
*
* @return void
*/
public function testShouldNotCreateInvoice(string $gateway, array $data): void
{
$customer = self::customerWithoutAddress();
$customer['address'] = self::address();
return $customer;
$this->expectException(ChargingException::class);
$this->createInvoice($gateway, $data);
}

public static function companyWithoutAddress(): array
public function shouldNotCreateInvoiceDataProvider(): array
{
$customer['name'] = 'Fake Company';
$customer['email'] = '[email protected]';
$customer['taxDocument'] = '28585583000189';
return $customer;
return [
'iugu - credit card - charge fail' => [
'gateway' => 'iugu',
'data' => [
'items' => [['description' => 'Teste', 'quantity' => 1, 'price' => 10000,]],
'customer' => self::customerWithAddress(),
'paymentMethod' => 'credit_card',
'creditCard' => array_merge(self::creditCard(), ['number' => '4012888888881881']),
],
],
];
}

public static function customerWithAddress(): array
Expand Down

0 comments on commit 258d8ed

Please sign in to comment.