Failed Conditions
Push — master ( 9023e9...faa7df )
by Reüel
08:15 queued 14s
created

Payment::set_amount_refunded()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
/**
3
 * Payment
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2021 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Gateways\Mollie
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Mollie;
12
13
use DateTimeInterface;
14
15
/**
16
 * Payment
17
 *
18
 * @author  Remco Tolsma
19
 * @version 2.2.2
20
 * @since   2.1.0
21
 */
22
class Payment extends BaseResource {
23
	/**
24
	 * The payment’s status.
25
	 *
26
	 * @var string
27
	 */
28
	private $status;
29
30
	/**
31
	 * The payment method used for this payment, either forced on creation by specifying the method parameter, or chosen by the customer on our payment method selection screen.
32
	 *
33
	 * @var string|null
34
	 */
35
	private $method;
36
37
	/**
38
	 * The identifier referring to the profile this payment was created on.
39
	 *
40
	 * @var string
41
	 */
42
	private $profile_id;
43
44
	/**
45
	 * If a customer was specified upon payment creation, the customer’s token will be available here as well.
46
	 *
47
	 * @var string|null
48
	 */
49
	private $customer_id;
50
51
	/**
52
	 * If the payment is a first or recurring payment, this field will hold the ID of the mandate.
53
	 *
54
	 * @var string|null
55
	 */
56
	private $mandate_id;
57
58
	/**
59
	 * Payment method specific details.
60
	 *
61
	 * @var PaymentDetails|null
62
	 */
63
	private $details;
64
65
	/**
66
	 * The mode used to create this payment. Mode determines whether a payment is real (live mode) or a test payment.
67
	 *
68
	 * @var string
69
	 */
70
	private $mode;
71
72
	/**
73
	 * The payment’s date and time of creation, in ISO 8601 format.
74
	 *
75
	 * @var DateTimeInterface
76
	 */
77
	private $created_at;
78
79
	/**
80
	 * The amount of the payment, e.g. {"currency":"EUR", "value":"100.00"} for a €100.00 payment.
81
	 *
82
	 * @var Amount
83
	 */
84
	private $amount;
85
86
	/**
87
	 * A short description of the payment. The description is visible in the Dashboard and will be shown on the customer’s bank or card statement when possible.
88
	 *
89
	 * @var string
90
	 */
91
	private $description;
92
93
	/**
94
	 * The URL your customer will be redirected to after completing or canceling the payment process.
95
	 *
96
	 * @var string|null
97
	 */
98
	private $redirect_url;
99
100
	/**
101
	 * The optional metadata you provided upon payment creation. Metadata can for example be used to link an order to a payment.
102
	 *
103
	 * @var string
104
	 */
105
	private $metadata;
106
107
	/**
108
	 * The customer’s locale, either forced on creation by specifying the `locale` parameter, or detected by us during checkout. Will be a full locale, for example `nl_NL`.
109
	 *
110
	 * @var string
111
	 */
112
	private $locale;
113
114
	/**
115
	 * Indicates which type of payment this is in a recurring sequence.
116
	 * Set to `first` for first payments that allow the customer to agree to automatic recurring charges taking place on their account in the future.
117
	 * Set to `recurring` for payments where the customer’s card is charged automatically.
118
	 * Set to `oneoff` by default, which indicates the payment is a regular non-recurring payment.
119
	 *
120
	 * @var string
121
	 */
122
	private $sequence_type;
123
124
	/**
125
	 * For bank transfer payments, the `_links` object will contain some additional URL objects relevant to the payment.
126
	 *
127
	 * @var object
128
	 */
129
	private $links;
130
131
	/**
132
	 * Amount refunded.
133
	 *
134
	 * @var Amount|null
135
	 */
136
	private $amount_refunded;
137
138
	/**
139
	 * Construct payment.
140
	 *
141
	 * @param string            $id            Identifier.
142
	 * @param string            $mode          Mode.
143
	 * @param DateTimeInterface $created_at    Created at.
144
	 * @param string            $status        Status.
145
	 * @param Amount            $amount        Amount.
146
	 * @param string            $description   Description.
147
	 * @param string|null       $redirect_url  Redirect URL.
148
	 * @param string|null       $method        Method.
149
	 * @param string            $metadata      Metadata.
150
	 * @param string            $profile_id    Profile ID.
151
	 * @param string            $sequence_type Sequence type.
152
	 * @param object            $links         Links.
153
	 */
154
	public function __construct( $id, $mode, DateTimeInterface $created_at, $status, Amount $amount, $description, $redirect_url, $method, $metadata, $profile_id, $sequence_type, $links ) {
155
		parent::__construct( $id );
156
157
		$this->mode          = $mode;
158
		$this->created_at    = $created_at;
159
		$this->status        = $status;
160
		$this->amount        = $amount;
161
		$this->description   = $description;
162
		$this->redirect_url  = $redirect_url;
163
		$this->method        = $method;
164
		$this->metadata      = $metadata;
165
		$this->profile_id    = $profile_id;
166
		$this->sequence_type = $sequence_type;
167
		$this->links         = $links;
168
	}
169
170
	/**
171
	 * Get status.
172
	 *
173
	 * @return string
174
	 */
175
	public function get_status() {
176
		return $this->status;
177
	}
178
179
	/**
180
	 * Get method.
181
	 *
182
	 * @return string|null
183
	 */
184
	public function get_method() {
185
		return $this->method;
186
	}
187
188
	/**
189
	 * Get profile ID.
190
	 *
191
	 * @return string
192
	 */
193
	public function get_profile_id() {
194
		return $this->profile_id;
195
	}
196
197
	/**
198
	 * Get customer ID.
199
	 *
200
	 * @return string|null
201
	 */
202
	public function get_customer_id() {
203
		return $this->customer_id;
204
	}
205
206
	/**
207
	 * Get locale.
208
	 *
209
	 * @return string
210
	 */
211
	public function get_locale() {
212
		return $this->locale;
213
	}
214
215
	/**
216
	 * Set locale.
217
	 *
218
	 * @param string $locale Locale.
219
	 * @return void
220
	 */
221
	public function set_locale( $locale ) {
222
		$this->locale = $locale;
223
	}
224
225
	/**
226
	 * Set customer ID.
227
	 *
228
	 * @param string|null $customer_id Customer ID.
229
	 * @return void
230
	 */
231
	public function set_customer_id( $customer_id ) {
232
		$this->customer_id = $customer_id;
233
	}
234
235
	/**
236
	 * Get mandate ID.
237
	 *
238
	 * @return string|null
239
	 */
240
	public function get_mandate_id() {
241
		return $this->mandate_id;
242
	}
243
244
	/**
245
	 * Set mandate ID.
246
	 *
247
	 * @param string|null $mandate_id Mandate ID.
248
	 * @return void
249
	 */
250
	public function set_mandate_id( $mandate_id ) {
251
		$this->mandate_id = $mandate_id;
252
	}
253
254
	/**
255
	 * Has chargebacks.
256
	 *
257
	 * @link https://github.com/mollie/mollie-api-php/blob/v2.24.0/src/Resources/Payment.php#L358-L366
258
	 * @return bool True if payment has chargebacks, false otherwise.
259
	 */
260
	public function has_chargebacks() {
261
		return ! empty( $this->links->chargebacks );
262
	}
263
264
	/**
265
	 * Get payment method specific details.
266
	 *
267
	 * @return PaymentDetails|null
268
	 */
269
	public function get_details() {
270
		return $this->details;
271
	}
272
273
	/**
274
	 * Set payment method specific details.
275
	 *
276
	 * @param PaymentDetails|null $details Details.
277
	 * @return void
278
	 */
279
	public function set_details( PaymentDetails $details = null ) {
280
		$this->details = $details;
281
	}
282
283
	/**
284
	 * Get amount refunded.
285
	 *
286
	 * @return Amount|null
287
	 */
288
	public function get_amount_refunded() {
289
		return $this->amount_refunded;
290
	}
291
292
	/**
293
	 * Set amount refunded.
294
	 *
295
	 * @param Amount|null $amount_refunded Amount refunded.
296
	 * @return void
297
	 */
298
	public function set_amount_refunded( Amount $amount_refunded = null ) {
299
		$this->amount_refunded = $amount_refunded;
300
	}
301
302
	/**
303
	 * Get links.
304
	 *
305
	 * @return object
306
	 */
307
	public function get_links() {
308
		return $this->links;
309
	}
310
311
	/**
312
	 * Set links.
313
	 *
314
	 * @param object $links Links.
315
	 * @return void
316
	 */
317
	public function set_links( $links ) {
318
		$this->links = $links;
319
	}
320
321
	/**
322
	 * Create payment from JSON.
323
	 *
324
	 * @link https://docs.mollie.com/reference/v2/payments-api/get-payment
325
	 * @param object $json JSON object.
326
	 * @return Payment
327
	 * @throws \JsonSchema\Exception\ValidationException Throws JSON schema validation exception when JSON is invalid.
328
	 */
329
	public static function from_json( $json ) {
330
		$validator = new \JsonSchema\Validator();
331
332
		$validator->validate(
333
			$json,
334
			(object) array(
335
				'$ref' => 'file://' . realpath( __DIR__ . '/../json-schemas/payment.json' ),
336
			),
337
			\JsonSchema\Constraints\Constraint::CHECK_MODE_EXCEPTIONS
338
		);
339
340
		// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Mollie JSON object.
341
		$payment = new Payment(
342
			$json->id,
343
			$json->mode,
344
			new \DateTimeImmutable( $json->createdAt ),
345
			$json->status,
346
			Amount::from_json( $json->amount ),
347
			$json->description,
348
			$json->redirectUrl,
349
			$json->method,
350
			$json->metadata,
351
			$json->profileId,
352
			$json->sequenceType,
353
			$json->_links
354
		);
355
356
		if ( \property_exists( $json, 'locale' ) ) {
357
			$payment->set_locale( $json->locale );
358
		}
359
360
		if ( \property_exists( $json, 'customerId' ) ) {
361
			$payment->set_customer_id( $json->customerId );
362
		}
363
364
		if ( \property_exists( $json, 'mandateId' ) ) {
365
			$payment->set_mandate_id( $json->mandateId );
366
		}
367
368
		if ( \property_exists( $json, 'details' ) ) {
369
			$payment->set_details( PaymentDetails::from_json( (string) $payment->get_method(), $json->details ) );
370
		}
371
372
		if ( \property_exists( $json, 'amountRefunded' ) ) {
373
			$refunded_amount = Amount::from_json( $json->amountRefunded );
374
375
			$payment->set_amount_refunded( $refunded_amount );
376
		}
377
378
		// phpcs:enable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Mollie JSON object.
379
380
		return $payment;
381
	}
382
}
383