Test Setup Failed
Push — develop ( 3f7bd5...29c8a0 )
by Reüel
03:54
created

Payment::get_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 0
dl 0
loc 2
ccs 0
cts 0
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.1.0
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            $locale        Locale.
151
	 * @param string            $profile_id    Profile ID.
152
	 * @param string            $sequence_type Sequence type.
153
	 * @param object            $links         Links.
154
	 */
155
	public function __construct( $id, $mode, DateTimeInterface $created_at, $status, Amount $amount, $description, $redirect_url, $method, $metadata, $locale, $profile_id, $sequence_type, $links ) {
156
		parent::__construct( $id );
157
158
		$this->mode          = $mode;
159
		$this->created_at    = $created_at;
160
		$this->status        = $status;
161
		$this->amount        = $amount;
162
		$this->description   = $description;
163
		$this->redirect_url  = $redirect_url;
164
		$this->method        = $method;
165
		$this->metadata      = $metadata;
166
		$this->locale        = $locale;
167
		$this->profile_id    = $profile_id;
168
		$this->sequence_type = $sequence_type;
169
		$this->links         = $links;
170
	}
171
172
	/**
173
	 * Get status.
174
	 *
175
	 * @return string
176
	 */
177
	public function get_status() {
178
		return $this->status;
179
	}
180
181
	/**
182
	 * Get method.
183
	 *
184
	 * @return string|null
185
	 */
186
	public function get_method() {
187
		return $this->method;
188
	}
189
190
	/**
191
	 * Get profile ID.
192
	 *
193
	 * @return string
194
	 */
195
	public function get_profile_id() {
196
		return $this->profile_id;
197
	}
198
199
	/**
200
	 * Get customer ID.
201
	 *
202
	 * @return string|null
203
	 */
204
	public function get_customer_id() {
205
		return $this->customer_id;
206
	}
207
208
	/**
209
	 * Set customer ID.
210
	 *
211
	 * @param string|null $customer_id Customer ID.
212
	 * @return void
213
	 */
214
	public function set_customer_id( $customer_id ) {
215
		$this->customer_id = $customer_id;
216
	}
217
218
	/**
219
	 * Get mandate ID.
220
	 *
221
	 * @return string|null
222
	 */
223
	public function get_mandate_id() {
224
		return $this->mandate_id;
225
	}
226
227
	/**
228
	 * Set mandate ID.
229
	 *
230
	 * @param string|null $mandate_id Mandate ID.
231
	 * @return void
232
	 */
233
	public function set_mandate_id( $mandate_id ) {
234
		$this->mandate_id = $mandate_id;
235
	}
236
237
	/**
238
	 * Has chargebacks.
239
	 *
240
	 * @link https://github.com/mollie/mollie-api-php/blob/v2.24.0/src/Resources/Payment.php#L358-L366
241
	 * @return bool True if payment has chargebacks, false otherwise.
242
	 */
243
	public function has_chargebacks() {
244
		return ! empty( $this->links->chargebacks );
245
	}
246
247
	/**
248
	 * Get payment method specific details.
249
	 *
250
	 * @return PaymentDetails|null
251
	 */
252
	public function get_details() {
253
		return $this->details;
254
	}
255
256
	/**
257
	 * Set payment method specific details.
258
	 *
259
	 * @param PaymentDetails|null $details Details.
260
	 * @return void
261
	 */
262
	public function set_details( PaymentDetails $details = null ) {
263
		$this->details = $details;
264
	}
265
266
	/**
267
	 * Get amount refunded.
268
	 *
269
	 * @return Amount|null
270
	 */
271
	public function get_amount_refunded() {
272
		return $this->amount_refunded;
273
	}
274
275
	/**
276
	 * Set amount refunded.
277
	 *
278
	 * @param Amount|null $amount_refunded Amount refunded.
279
	 * @return void
280
	 */
281
	public function set_amount_refunded( Amount $amount_refunded = null ) {
282
		$this->amount_refunded = $amount_refunded;
283
	}
284
285
	/**
286
	 * Get links.
287
	 *
288
	 * @return object
289
	 */
290
	public function get_links() {
291
		return $this->links;
292
	}
293
294
	/**
295
	 * Set links.
296
	 *
297
	 * @param object $links Links.
298
	 * @return void
299
	 */
300
	public function set_links( $links ) {
301
		$this->links = $links;
302
	}
303
304
	/**
305
	 * Create payment from JSON.
306
	 *
307
	 * @link https://docs.mollie.com/reference/v2/payments-api/get-payment
308
	 * @param object $json JSON object.
309
	 * @return Payment
310
	 * @throws \JsonSchema\Exception\ValidationException Throws JSON schema validation exception when JSON is invalid.
311
	 */
312
	public static function from_json( $json ) {
313
		$validator = new \JsonSchema\Validator();
314
315
		$validator->validate(
316
			$json,
317
			(object) array(
318
				'$ref' => 'file://' . realpath( __DIR__ . '/../json-schemas/payment.json' ),
319
			),
320
			\JsonSchema\Constraints\Constraint::CHECK_MODE_EXCEPTIONS
321
		);
322
323
		// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Mollie JSON object.
324
		$payment = new Payment(
325
			$json->id,
326
			$json->mode,
327
			new \DateTimeImmutable( $json->createdAt ),
328
			$json->status,
329
			Amount::from_json( $json->amount ),
330
			$json->description,
331
			$json->redirectUrl,
332
			$json->method,
333
			$json->metadata,
334
			$json->locale,
335
			$json->profileId,
336
			$json->sequenceType,
337
			$json->_links
338
		);
339
340
		if ( \property_exists( $json, 'customerId' ) ) {
341
			$payment->set_customer_id( $json->customerId );
342
		}
343
344
		if ( \property_exists( $json, 'mandateId' ) ) {
345
			$payment->set_mandate_id( $json->mandateId );
346
		}
347
348
		if ( \property_exists( $json, 'details' ) ) {
349
			$payment->set_details( PaymentDetails::from_json( (string) $payment->get_method(), $json->details ) );
350
		}
351
352
		if ( \property_exists( $json, 'amountRefunded' ) ) {
353
			$refunded_amount = Amount::from_json( $json->amountRefunded );
354
355
			$payment->set_amount_refunded( $refunded_amount );
356
		}
357
358
		// phpcs:enable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase -- Mollie JSON object.
359
360
		return $payment;
361
	}
362
}
363