-
Notifications
You must be signed in to change notification settings - Fork 0
/
InvoiceBuilder.php
95 lines (85 loc) · 2.69 KB
/
InvoiceBuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
namespace IuguLaravel\Builders;
use Exception;
use IuguLaravel\Builders\Contracts\Builder;
use IuguLaravel\Http\ResponseHttp;
/**
* Class FaturaBuilder
*
* En:
* The `FaturaBuilder` class is responsible for building and interacting with Iugu invoices.
* It allows you to create, cancel, capture, refund, and duplicate invoices, as well as set invoice items and payer information.
*
* Pt:
* A classe `FaturaBuilder` é responsável por construir e interagir com faturas da Iugu.
* Ela permite criar, cancelar, capturar, reembolsar e duplicar faturas, além de definir itens da fatura e informações do pagador.
*
* @package IuguLaravel\Builders
*/
class InvoiceBuilder extends Builder
{
protected string $endpoint = "/v1/invoices";
/**
* En:
* Define an item to be added to the invoice, including its description, quantity, and price in cents.
*
* Pt:
* Define um item a ser adicionado à fatura, incluindo sua descrição, quantidade e preço em centavos.
*
*/
public function setItem(string $description, int $quantity, int $priceCents): self
{
$this->data->items[] = [
"description" => $description,
"quantity" => $quantity,
"price_cents" => $priceCents
];
return $this;
}
/**
* Set payer information for the invoice.
*
* En:
* Define payer information for the invoice, including the payer's name and document number (CPF/CNPJ).
*
* Pt:
* Define informações do pagador para a fatura, incluindo o nome e o número do documento do pagador (CPF/CNPJ).
*
*/
public function setPayer(string $name, string $documentNumber): self
{
$this->data->payer = [
"name" => $name,
"cpf_cnpj" => $documentNumber
];
return $this;
}
/**
* En:
* Create a new invoice based on the provided data.
*
* Pt:
* Crie uma nova fatura com base nos dados fornecidos.
*
* @link https://dev.iugu.com/reference/criar-fatura
*/
public function create(): ResponseHttp
{
$requiredFields = [
"due_date",
"email",
"items",
"payer"
];
foreach ($requiredFields as $field) {
if (empty($this->data->{$field})) {
throw new Exception("$field is required");
}
}
$result = $this->api->post(path: $this->endpoint, data: get_object_vars($this->data));
if ($result?->status === 200 && $result?->content && $this->persistence?->model) {
$result->localModel = $this->saveLocally(attributes: $result->content);
}
return $result;
}
}