1 | <?php |
||
2 | |||
3 | namespace Srmklive\PayPal\Traits\PayPalAPI\InvoiceSearch; |
||
4 | |||
5 | use Carbon\Carbon; |
||
6 | |||
7 | trait Filters |
||
8 | { |
||
9 | /** |
||
10 | * @var array |
||
11 | */ |
||
12 | protected $invoice_search_filters = []; |
||
13 | |||
14 | /** |
||
15 | * @var array |
||
16 | */ |
||
17 | protected $invoices_date_types = [ |
||
18 | 'invoice_date', |
||
19 | 'due_date', |
||
20 | 'payment_date', |
||
21 | 'creation_date', |
||
22 | ]; |
||
23 | |||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $invoices_status_types = [ |
||
28 | 'DRAFT', |
||
29 | 'SENT', |
||
30 | 'SCHEDULED', |
||
31 | 'PAID', |
||
32 | 'MARKED_AS_PAID', |
||
33 | 'CANCELLED', |
||
34 | 'REFUNDED', |
||
35 | 'PARTIALLY_PAID', |
||
36 | 'PARTIALLY_REFUNDED', |
||
37 | 'MARKED_AS_REFUNDED', |
||
38 | 'UNPAID', |
||
39 | 'PAYMENT_PENDING', |
||
40 | ]; |
||
41 | |||
42 | /** |
||
43 | * @param string $email |
||
44 | * |
||
45 | * @return \Srmklive\PayPal\Services\PayPal |
||
46 | */ |
||
47 | public function addInvoiceFilterByRecipientEmail(string $email): \Srmklive\PayPal\Services\PayPal |
||
48 | { |
||
49 | $this->invoice_search_filters['recipient_email'] = $email; |
||
50 | |||
51 | return $this; |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * @param string $name |
||
56 | * |
||
57 | * @return \Srmklive\PayPal\Services\PayPal |
||
58 | */ |
||
59 | public function addInvoiceFilterByRecipientFirstName(string $name): \Srmklive\PayPal\Services\PayPal |
||
60 | { |
||
61 | $this->invoice_search_filters['recipient_first_name'] = $name; |
||
62 | |||
63 | return $this; |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @param string $name |
||
68 | * |
||
69 | * @return \Srmklive\PayPal\Services\PayPal |
||
70 | */ |
||
71 | public function addInvoiceFilterByRecipientLastName(string $name): \Srmklive\PayPal\Services\PayPal |
||
72 | { |
||
73 | $this->invoice_search_filters['recipient_last_name'] = $name; |
||
74 | |||
75 | return $this; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @param string $name |
||
80 | * |
||
81 | * @return \Srmklive\PayPal\Services\PayPal |
||
82 | */ |
||
83 | public function addInvoiceFilterByRecipientBusinessName(string $name): \Srmklive\PayPal\Services\PayPal |
||
84 | { |
||
85 | $this->invoice_search_filters['recipient_business_name'] = $name; |
||
86 | |||
87 | return $this; |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @param string $invoice_number |
||
92 | * |
||
93 | * @return \Srmklive\PayPal\Services\PayPal |
||
94 | */ |
||
95 | public function addInvoiceFilterByInvoiceNumber(string $invoice_number): \Srmklive\PayPal\Services\PayPal |
||
96 | { |
||
97 | $this->invoice_search_filters['invoice_number'] = $invoice_number; |
||
98 | |||
99 | return $this; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * @param array $status |
||
104 | * |
||
105 | * @throws \Exception |
||
106 | * |
||
107 | * @return \Srmklive\PayPal\Services\PayPal |
||
108 | * |
||
109 | * @see https://developer.paypal.com/docs/api/invoicing/v2/#definition-invoice_status |
||
110 | */ |
||
111 | public function addInvoiceFilterByInvoiceStatus(array $status): \Srmklive\PayPal\Services\PayPal |
||
112 | { |
||
113 | $invalid_status = false; |
||
114 | |||
115 | foreach ($status as $item) { |
||
116 | if (!in_array($item, $this->invoices_status_types)) { |
||
117 | $invalid_status = true; |
||
118 | } |
||
119 | } |
||
120 | |||
121 | if ($invalid_status === true) { |
||
122 | throw new \Exception('status should be always one of these: '.implode(',', $this->invoices_date_types)); |
||
123 | } |
||
124 | |||
125 | $this->invoice_search_filters['status'] = $status; |
||
126 | |||
127 | return $this; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * @param string $reference |
||
132 | * @param bool $memo |
||
133 | * |
||
134 | * @return \Srmklive\PayPal\Services\PayPal |
||
135 | */ |
||
136 | public function addInvoiceFilterByReferenceorMemo(string $reference, bool $memo = false): \Srmklive\PayPal\Services\PayPal |
||
137 | { |
||
138 | $field = ($memo === false) ? 'reference' : 'memo'; |
||
139 | |||
140 | $this->invoice_search_filters[$field] = $reference; |
||
141 | |||
142 | return $this; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * @param string $currency_code |
||
147 | * |
||
148 | * @return \Srmklive\PayPal\Services\PayPal |
||
149 | */ |
||
150 | public function addInvoiceFilterByCurrencyCode(string $currency_code = ''): \Srmklive\PayPal\Services\PayPal |
||
151 | { |
||
152 | $currency = !isset($currency_code) ? $this->getCurrency() : $currency_code; |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
153 | |||
154 | $this->invoice_search_filters['currency_code'] = $currency; |
||
155 | |||
156 | return $this; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @param float $start_amount |
||
161 | * @param float $end_amount |
||
162 | * @param string $amount_currency |
||
163 | * |
||
164 | * @throws \Exception |
||
165 | * |
||
166 | * @return \Srmklive\PayPal\Services\PayPal |
||
167 | */ |
||
168 | public function addInvoiceFilterByAmountRange(float $start_amount, float $end_amount, string $amount_currency = ''): \Srmklive\PayPal\Services\PayPal |
||
169 | { |
||
170 | if ($start_amount > $end_amount) { |
||
171 | throw new \Exception('Starting amount should always be less than end amount!'); |
||
172 | } |
||
173 | |||
174 | $currency = !isset($amount_currency) ? $this->getCurrency() : $amount_currency; |
||
175 | |||
176 | $this->invoice_search_filters['total_amount_range'] = [ |
||
177 | 'lower_amount' => [ |
||
178 | 'currency_code' => $currency, |
||
179 | 'value' => $start_amount, |
||
180 | ], |
||
181 | 'upper_amount' => [ |
||
182 | 'currency_code' => $currency, |
||
183 | 'value' => $end_amount, |
||
184 | ], |
||
185 | ]; |
||
186 | |||
187 | return $this; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * @param string $start_date |
||
192 | * @param string $end_date |
||
193 | * @param string $date_type |
||
194 | * |
||
195 | * @throws \Exception |
||
196 | * |
||
197 | * @return \Srmklive\PayPal\Services\PayPal |
||
198 | */ |
||
199 | public function addInvoiceFilterByDateRange(string $start_date, string $end_date, string $date_type): \Srmklive\PayPal\Services\PayPal |
||
200 | { |
||
201 | $start_date_obj = Carbon::parse($start_date); |
||
202 | $end_date_obj = Carbon::parse($end_date); |
||
203 | |||
204 | if ($start_date_obj->gt($end_date_obj)) { |
||
205 | throw new \Exception('Starting date should always be less than the end date!'); |
||
206 | } |
||
207 | |||
208 | if (!in_array($date_type, $this->invoices_date_types)) { |
||
209 | throw new \Exception('date type should be always one of these: '.implode(',', $this->invoices_date_types)); |
||
210 | } |
||
211 | |||
212 | $this->invoice_search_filters["{$date_type}_range"] = [ |
||
213 | 'start' => $start_date, |
||
214 | 'end' => $end_date, |
||
215 | ]; |
||
216 | |||
217 | return $this; |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * @param bool $archived |
||
222 | * |
||
223 | * @return \Srmklive\PayPal\Services\PayPal |
||
224 | */ |
||
225 | public function addInvoiceFilterByArchivedStatus(bool $archived = null): \Srmklive\PayPal\Services\PayPal |
||
226 | { |
||
227 | $this->invoice_search_filters['archived'] = $archived; |
||
228 | |||
229 | return $this; |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * @param array $fields |
||
234 | * |
||
235 | * @return \Srmklive\PayPal\Services\PayPal |
||
236 | * |
||
237 | * @see https://developer.paypal.com/docs/api/invoicing/v2/#definition-field |
||
238 | */ |
||
239 | public function addInvoiceFilterByFields(array $fields): \Srmklive\PayPal\Services\PayPal |
||
240 | { |
||
241 | $this->invoice_search_filters['status'] = $fields; |
||
242 | |||
243 | return $this; |
||
244 | } |
||
245 | } |
||
246 |