Failed Conditions
Push — develop ( bdcb96...3c540f )
by Remco
05:22
created

src/Payment.php (9 issues)

1
<?php
2
/**
3
 * Payment
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2020 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
	 * Construct payment.
67
	 *
68
	 * @param string            $id            Identifier.
69
	 * @param string            $mode          Mode.
70
	 * @param DateTimeInterface $created_at    Created at.
71
	 * @param string            $status        Status.
72
	 * @param Amount            $amount        Amount.
73
	 * @param string            $description   Description.
74
	 * @param string|null       $redirect_url  Redirect URL.
75
	 * @param string|null       $method        Method.
76
	 * @param string            $metadata      Metadata.
77
	 * @param string            $locale        Locale.
78
	 * @param string            $profile_id    Profile ID.
79
	 * @param string            $sequence_type Sequence type.
80
	 * @param object            $links         Links.
81
	 */
82
	public function __construct( $id, $mode, DateTimeInterface $created_at, $status, Amount $amount, $description, $redirect_url, $method, $metadata, $locale, $profile_id, $sequence_type, $links ) {
83
		parent::__construct( $id );
84
85
		$this->mode          = $mode;
0 ignored issues
show
Bug Best Practice introduced by
The property mode does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
86
		$this->created_at    = $created_at;
0 ignored issues
show
Bug Best Practice introduced by
The property created_at does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
87
		$this->status        = $status;
88
		$this->amount        = $amount;
0 ignored issues
show
Bug Best Practice introduced by
The property amount does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
89
		$this->description   = $description;
0 ignored issues
show
Bug Best Practice introduced by
The property description does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
90
		$this->redirect_url  = $redirect_url;
0 ignored issues
show
Bug Best Practice introduced by
The property redirect_url does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
91
		$this->method        = $method;
92
		$this->metadata      = $metadata;
0 ignored issues
show
Bug Best Practice introduced by
The property metadata does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
93
		$this->locale        = $locale;
0 ignored issues
show
Bug Best Practice introduced by
The property locale does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
94
		$this->profile_id    = $profile_id;
95
		$this->sequence_type = $sequence_type;
0 ignored issues
show
Bug Best Practice introduced by
The property sequence_type does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
96
		$this->links         = $links;
0 ignored issues
show
Bug Best Practice introduced by
The property links does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
97
	}
98
99
	/**
100
	 * Get status.
101
	 *
102
	 * @return string
103
	 */
104
	public function get_status() {
105
		return $this->status;
106
	}
107
108
	/**
109
	 * Get method.
110
	 *
111
	 * @return string|null
112
	 */
113
	public function get_method() {
114
		return $this->method;
115
	}
116
117
	/**
118
	 * Get profile ID.
119
	 *
120
	 * @return string
121
	 */
122
	public function get_profile_id() {
123
		return $this->profile_id;
124
	}
125
126
	/**
127
	 * Get customer ID.
128
	 *
129
	 * @return string|null
130
	 */
131
	public function get_customer_id() {
132
		return $this->customer_id;
133
	}
134
135
	/**
136
	 * Set customer ID.
137
	 *
138
	 * @param string|null $customer_id Customer ID.
139
	 */
140
	public function set_customer_id( $customer_id ) {
141
		$this->customer_id = $customer_id;
142
	}
143
144
	/**
145
	 * Get mandate ID.
146
	 *
147
	 * @return string|null
148
	 */
149
	public function get_mandate_id() {
150
		return $this->mandate_id;
151
	}
152
153
	/**
154
	 * Set mandate ID.
155
	 *
156
	 * @param string|null $mandate_id Mandate ID.
157
	 */
158
	public function set_mandate_id( $mandate_id ) {
159
		$this->mandate_id = $mandate_id;
160
	}
161
162
	/**
163
	 * Has chargebacks.
164
	 *
165
	 * @link https://github.com/mollie/mollie-api-php/blob/v2.24.0/src/Resources/Payment.php#L358-L366
166
	 * @return bool True if payment has chargebacks, false otherwise.
167
	 */
168
	public function has_chargebacks() {
169
		return ! empty( $this->links->chargebacks );
170
	}
171
172
	/**
173
	 * Get payment method specific details.
174
	 *
175
	 * @return PaymentDetails|null
176
	 */
177
	public function get_details() {
178
		return $this->details;
179
	}
180
181
	/**
182
	 * Set payment method specific details.
183
	 *
184
	 * @param PaymentDetails|null $details Details.
185
	 */
186
	public function set_details( PaymentDetails $details = null ) {
187
		$this->details = $details;
188
	}
189
190
	/**
191
	 * Create payment from JSON.
192
	 *
193
	 * @link https://docs.mollie.com/reference/v2/payments-api/get-payment
194
	 * @param object $json JSON object.
195
	 * @return Payment
196
	 * @throws \JsonSchema\Exception\ValidationException Throws JSON schema validation exception when JSON is invalid.
197
	 */
198
	public static function from_json( $json ) {
199
		$validator = new \JsonSchema\Validator();
200
201
		$validator->validate(
202
			$json,
203
			(object) array(
204
				'$ref' => 'file://' . realpath( __DIR__ . '/../json-schemas/payment.json' ),
205
			),
206
			\JsonSchema\Constraints\Constraint::CHECK_MODE_EXCEPTIONS
207
		);
208
209
		$payment = new Payment(
210
			$json->id,
211
			$json->mode,
212
			new \DateTimeImmutable( $json->createdAt ),
213
			$json->status,
214
			Amount::from_json( $json->amount ),
215
			$json->description,
216
			$json->redirectUrl,
217
			$json->method,
218
			$json->metadata,
219
			$json->locale,
220
			$json->profileId,
221
			$json->sequenceType,
222
			$json->_links
223
		);
224
225
		if ( \property_exists( $json, 'customerId' ) ) {
226
			$payment->set_customer_id( $json->customerId );
227
		}
228
229
		if ( \property_exists( $json, 'mandateId' ) ) {
230
			$payment->set_mandate_id( $json->mandateId );
231
		}
232
233
		if ( \property_exists( $json, 'details' ) ) {
234
			$payment->set_details( PaymentDetails::from_json( $payment->get_method(), $json->details ) );
235
		}
236
237
		return $payment;
238
	}
239
}
240