-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathPaymentContext.php
388 lines (346 loc) · 8.23 KB
/
PaymentContext.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<?php
/**
* Class PaymentContext
*
* @package WooCommerce\Payments
*/
namespace WCPay\Internal\Payment;
use WC_Payments_API_Abstract_Intention;
use WCPay\Internal\Payment\PaymentMethod\PaymentMethodInterface;
use WCPay\Internal\Payment\Change;
use WCPay\Internal\Payment\Transition;
/**
* A context object, which is shared between payment states.
*/
class PaymentContext {
/**
* ID of the order, receiving a payment.
*
* @var int
*/
private $order_id;
/**
* Contains all the context's data.
*
* @var array
*/
private $data = [];
/**
* Stores the transitions of state.
*
* @var Transition[]
*/
private $transitions = [];
/**
* Stores exception that is triggered before error state is created.
*
* @var \Exception
*/
private $exception;
/**
* Constructs the class, receiving an order ID.
*
* @param int $order_id ID of the order, receiving a payment.
*/
public function __construct( int $order_id ) {
$this->order_id = $order_id;
$this->transitions[] = new Transition( time(), null );
}
/**
* Returns the ID of the order requiring payment.
*
* @return int
*/
public function get_order_id(): int {
return $this->order_id;
}
/**
* Stores the payment amount.
*
* @param int $amount Payment amount in cents.
*/
public function set_amount( int $amount ) {
$this->set( 'amount', $amount );
}
/**
* Returns the payment amount.
*
* @return int|null Amount in cents.
*/
public function get_amount(): ?int {
return $this->get( 'amount' );
}
/**
* Stores the payment currency.
*
* @param string $currency Lowercase payment currency.
*/
public function set_currency( string $currency ) {
$this->set( 'currency', $currency );
}
/**
* Returns the payment currency in lowercase.
*
* @return string|null
*/
public function get_currency(): ?string {
return $this->get( 'currency' );
}
/**
* Controls whether automatic capture is enabled.
*
* @param bool $automatic_capture Whether to enable it or not.
*/
public function toggle_automatic_capture( bool $automatic_capture ) {
$this->set( 'automatic_capture', $automatic_capture );
}
/**
* Indicates whether the payment should be captured automatically.
*
* @return bool
*/
public function should_capture_automatically(): bool {
return $this->get( 'automatic_capture' ) ?? false;
}
/**
* Stores the order metadata.
*
* @param array $metadata Metadata to sent to the API.
*/
public function set_metadata( array $metadata ) {
$this->set( 'metadata', $metadata );
}
/**
* Returns the order level 3 data if set.
*
* @return array|null
*/
public function get_level3_data(): ?array {
return $this->get( 'level3_data' );
}
/**
* Stores the order level 3 data.
*
* @param array $level3_data level3_data to sent to the API.
*/
public function set_level3_data( array $level3_data ) {
$this->set( 'level3_data', $level3_data );
}
/**
* Returns the order metadata if set.
*
* @return array|null
*/
public function get_metadata(): ?array {
return $this->get( 'metadata' );
}
/**
* Stores the CVC confirmation.
*
* @param string $cvc_confirmation The confirmation.
*/
public function set_cvc_confirmation( string $cvc_confirmation = null ) {
$this->set( 'cvc_confirmation', $cvc_confirmation );
}
/**
* Returns the CVC confirmation if set.
*
* @return string|null
*/
public function get_cvc_confirmation(): ?string {
return $this->get( 'cvc_confirmation' );
}
/**
* Stores a payment's fingerprint.
*
* @param string $fingerprint The fingerprint.
*/
public function set_fingerprint( string $fingerprint ) {
$this->set( 'fingerprint', $fingerprint );
}
/**
* Returns a payment's fingerprint if set.
*
* @return string|null
*/
public function get_fingerprint(): ?string {
return $this->get( 'fingerprint' );
}
/**
* Stores a payment method within the context.
*
* @param PaymentMethodInterface $payment_method The payment method to use.
*/
public function set_payment_method( PaymentMethodInterface $payment_method ) {
$this->set( 'payment_method', $payment_method );
}
/**
* Returns the stored payment method object, if any.
*
* @return PaymentMethodInterface|null
*/
public function get_payment_method(): ?PaymentMethodInterface {
return $this->get( 'payment_method' );
}
/**
* Stores the WP user ID, associated with the payment.
*
* @param int|null $user_id ID of the user.
*/
public function set_user_id( ?int $user_id ) {
$this->set( 'user_id', $user_id );
}
/**
* Returns the ID of the user if any.
*
* @return int|null
*/
public function get_user_id(): ?int {
return $this->get( 'user_id' );
}
/**
* Stores the remote customer ID.
*
* @param string $customer_id ID of the customer.
*/
public function set_customer_id( string $customer_id ) {
$this->set( 'customer_id', $customer_id );
}
/**
* Returns the remote customer ID.
*
* @return string|null
*/
public function get_customer_id(): ?string {
return $this->get( 'customer_id' );
}
/**
* Sets the previous paid duplicate order ID.
*
* @param int $duplicate_order_id Duplicate order ID.
*
* @return void
*/
public function set_duplicate_order_id( int $duplicate_order_id ) {
$this->set( 'duplicate_order_id', $duplicate_order_id );
}
/**
* Gets the previous paid duplicate order ID.
*
* @return int|null
*/
public function get_duplicate_order_id(): ?int {
return $this->get( 'duplicate_order_id' );
}
/**
* Sets the detected authorized intent flag to true.
*
* @return void
*/
public function set_detected_authorized_intent(): void {
$this->set( 'detected_authorized_intent', true );
}
/**
* Checks whether the currently attached intent, that is authorized, gets detected.
*
* @return bool
*/
public function is_detected_authorized_intent(): bool {
return $this->get( 'detected_authorized_intent' ) ?? false;
}
/**
* Stores the payment intent object.
*
* @param WC_Payments_API_Abstract_Intention $intent Instance of intent.
*/
public function set_intent( WC_Payments_API_Abstract_Intention $intent ) {
$this->set( 'intent', $intent );
}
/**
* Returns the payment intent object.
*
* @return WC_Payments_API_Abstract_Intention|null
*/
public function get_intent(): ?WC_Payments_API_Abstract_Intention {
return $this->get( 'intent' );
}
/**
* Returns the transitions array.
*
* @return Transition[]
*/
public function get_transitions(): array {
return $this->transitions;
}
/**
* Stores state exception.
*
* @param \Exception $exception Exception to store.
*/
public function set_exception( $exception ) {
$this->set( 'exception', $exception );
}
/**
* Get state exception
*
* @return \Exception|null
*/
public function get_exception() {
return $this->get( 'exception' );
}
/**
* Sets the mode (test or prod).
*
* @param string $mode mode.
*/
public function set_mode( string $mode ) {
$this->set( 'mode', $mode );
}
/**
* Returns the mode (test or prod).
*
* @return string|null mode.
*/
public function get_mode(): ?string {
return $this->get( 'mode' );
}
/**
* Updates previous transition with the next state and creates new transition.
*
* @param string $state The state.
*/
public function log_state_transition( string $state ): void {
$last_transition = end( $this->transitions );
$last_transition->set_to_state( $state );
$this->transitions[] = new Transition( time(), $state );
}
/**
* Stores an internal value.
*
* @param string $key Property name.
* @param mixed $value Value to store.
*/
private function set( string $key, $value ) : void {
$this->log_change( $key, $value );
$this->data[ $key ] = $value;
}
/**
* Log the change to a transition
*
* @param string $key Property name.
* @param mixed $value Value to store.
*/
private function log_change( string $key, $value ) : void {
$last_transition = end( $this->transitions );
$last_transition->add_change( new Change( $key, $this->get( $key ), $value ) );
}
/**
* Retrieves an internal value, if any.
*
* @param string $key Key of the property.
* @return mixed|null Either the stored value, or null if not set.
*/
private function get( string $key ) {
return $this->data[ $key ] ?? null;
}
}