diff --git a/src/Builders/InvoiceBuilder.php b/src/Builders/InvoiceBuilder.php index 52761f2..170299f 100644 --- a/src/Builders/InvoiceBuilder.php +++ b/src/Builders/InvoiceBuilder.php @@ -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; @@ -13,7 +14,6 @@ /** * invoice builder * - * @method Invoice create() * @method Invoice get() */ class InvoiceBuilder extends Builder @@ -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 * diff --git a/src/Exceptions/ChargingException.php b/src/Exceptions/ChargingException.php new file mode 100644 index 0000000..26e0332 --- /dev/null +++ b/src/Exceptions/ChargingException.php @@ -0,0 +1,11 @@ +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 { diff --git a/tests/Unit/Builders/InvoiceBuilderTest.php b/tests/Unit/Builders/InvoiceBuilderTest.php index 0e41bc9..ba336fa 100644 --- a/tests/Unit/Builders/InvoiceBuilderTest.php +++ b/tests/Unit/Builders/InvoiceBuilderTest.php @@ -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(); @@ -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); } @@ -139,30 +154,35 @@ public function shouldCreateInvoiceDataProvider(): array ]; } - public static function customerWithoutAddress(): array - { - $customer['name'] = 'Fake Customer'; - $customer['email'] = 'email@exemplo.com'; - $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@exemplo.com'; - $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