class-payment-information.php
12.4 KB
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
<?php
/**
* Class Payment_Information
*
* @package WooCommerce\Payments
*/
namespace WCPay;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
use WCPay\Constants\Payment_Type;
use WCPay\Constants\Payment_Initiated_By;
use WCPay\Constants\Payment_Capture_Type;
use WCPay\Exceptions\Invalid_Payment_Method_Exception;
use WCPay\Payment_Methods\CC_Payment_Gateway;
/**
* Mostly a wrapper containing information on a single payment.
*/
class Payment_Information {
/**
* The ID of the payment method used for this payment.
*
* @var string
*/
private $payment_method;
/**
* The order object.
*
* @var \WC_Order/NULL
*/
private $order;
/**
* The payment token used for this payment.
*
* @var \WC_Payment_Token/NULL
*/
private $token;
/**
* The CVC confirmation used for this payment.
*
* @var string
*/
private $cvc_confirmation;
/**
* Indicates whether the payment is merchant-initiated (true) or customer-initiated (false).
*
* @var Payment_Initiated_By
*/
private $payment_initiated_by;
/**
* Indicates whether the payment will be only authorized (true) or captured immediately (false).
*
* @var Payment_Capture_Type
*/
private $manual_capture;
/**
* The type of the payment. `single`, `recurring`, etc.
*
* @var Payment_Type
*/
private $payment_type;
/**
* Indicates whether the payment method should be saved to the store.
*
* @var bool
*/
private $save_payment_method_to_store = false;
/**
* Indicates whether the payment method should be saved to the platform.
*
* @var bool
*/
private $save_payment_method_to_platform = false;
/**
* Indicates whether user is changing payment method for subscriptions order.
*
* @var bool
*/
private $is_changing_payment_method_for_subscription = false;
/**
* The attached fingerprint.
*
* @var string
*/
private $fingerprint = '';
/**
* Payment information constructor.
*
* @param string $payment_method The ID of the payment method used for this payment.
* @param \WC_Order $order The order object.
* @param Payment_Type $payment_type The type of the payment.
* @param \WC_Payment_Token $token The payment token used for this payment.
* @param Payment_Initiated_By $payment_initiated_by Indicates whether the payment is merchant-initiated or customer-initiated.
* @param Payment_Capture_Type $manual_capture Indicates whether the payment will be only authorized or captured immediately.
* @param string $cvc_confirmation The CVC confirmation for this payment method.
* @param string $fingerprint The attached fingerprint.
*
* @throws Invalid_Payment_Method_Exception When no payment method is found in the provided request.
*/
public function __construct(
string $payment_method,
\WC_Order $order = null,
Payment_Type $payment_type = null,
\WC_Payment_Token $token = null,
Payment_Initiated_By $payment_initiated_by = null,
Payment_Capture_Type $manual_capture = null,
string $cvc_confirmation = null,
string $fingerprint = ''
) {
if ( empty( $payment_method ) && empty( $token ) && ! \WC_Payments::is_network_saved_cards_enabled() ) {
// If network-wide cards are enabled, a payment method or token may not be specified and the platform default one will be used.
throw new Invalid_Payment_Method_Exception(
__( 'Invalid payment method. Please input a new card number.', 'woocommerce-payments' ),
'payment_method_not_provided'
);
}
$this->payment_method = $payment_method;
$this->order = $order;
$this->token = $token;
$this->payment_initiated_by = $payment_initiated_by ?? Payment_Initiated_By::CUSTOMER();
$this->manual_capture = $manual_capture ?? Payment_Capture_Type::AUTOMATIC();
$this->payment_type = $payment_type ?? Payment_Type::SINGLE();
$this->cvc_confirmation = $cvc_confirmation;
$this->fingerprint = $fingerprint;
}
/**
* Returns true if payment was initiated by the merchant, false otherwise.
*
* @return bool True if payment was initiated by the merchant, false otherwise.
*/
public function is_merchant_initiated(): bool {
return $this->payment_initiated_by->equals( Payment_Initiated_By::MERCHANT() );
}
/**
* Returns the payment method ID.
*
* @return string The payment method ID.
*/
public function get_payment_method(): string {
// Use the token if we have it.
if ( $this->is_using_saved_payment_method() ) {
return $this->token->get_token();
}
return $this->payment_method;
}
/**
* Returns the order object.
*
* @return \WC_Order The order object.
*/
public function get_order(): \WC_Order {
return $this->order;
}
/**
* Returns the payment token.
*
* TODO: Once php requirement is bumped to >= 7.1.0 change return type to ?\WC_Payment_Token
* since the return type is nullable, as per
* https://www.php.net/manual/en/functions.returning-values.php#functions.returning-values.type-declaration
*
* @return \WC_Payment_Token/NULL The payment token.
*/
public function get_payment_token(): \WC_Payment_Token {
return $this->token;
}
/**
* Update the payment token associated with this payment.
*
* @param \WC_Payment_Token $token The new payment token.
*/
public function set_token( \WC_Payment_Token $token ) {
$this->token = $token;
}
/**
* Returns true if the payment token is not empty, false otherwise.
*
* @return bool True if payment token is not empty, false otherwise.
*/
public function is_using_saved_payment_method(): bool {
return ! empty( $this->token );
}
/**
* Returns true if the payment should be only authorized, false if it should be captured immediately.
*
* @return bool True if the payment should be only authorized, false if it should be captured immediately.
*/
public function is_using_manual_capture(): bool {
return $this->manual_capture->equals( Payment_Capture_Type::MANUAL() );
}
/**
* Payment information constructor.
*
* @param array $request Associative array containing payment request information.
* @param \WC_Order $order The order object.
* @param Payment_Type $payment_type The type of the payment.
* @param Payment_Initiated_By $payment_initiated_by Indicates whether the payment is merchant-initiated or customer-initiated.
* @param Payment_Capture_Type $manual_capture Indicates whether the payment will be only authorized or captured immediately.
*
* @throws \Exception - If no payment method is found in the provided request.
*/
public static function from_payment_request(
array $request,
\WC_Order $order = null,
Payment_Type $payment_type = null,
Payment_Initiated_By $payment_initiated_by = null,
Payment_Capture_Type $manual_capture = null
): Payment_Information {
$payment_method = self::get_payment_method_from_request( $request );
$token = self::get_token_from_request( $request );
$cvc_confirmation = self::get_cvc_confirmation_from_request( $request );
$fingerprint = self::get_fingerprint_from_request( $request );
if ( isset( $request['is_woopay'] ) && $request['is_woopay'] ) {
$order->add_meta_data( 'is_woopay', true, true );
$order->save_meta_data();
}
return new Payment_Information( $payment_method, $order, $payment_type, $token, $payment_initiated_by, $manual_capture, $cvc_confirmation, $fingerprint );
}
/**
* Extracts the payment method from the provided request.
*
* @param array $request Associative array containing payment request information.
*
* @return string
*/
public static function get_payment_method_from_request( array $request ): string {
foreach ( [ 'wcpay-payment-method', 'wcpay-payment-method-sepa' ] as $key ) {
if ( ! empty( $request[ $key ] ) ) {
$normalized = wc_clean( $request[ $key ] );
return is_string( $normalized ) ? $normalized : '';
}
}
return '';
}
/**
* Extract the payment token from the provided request.
*
* TODO: Once php requirement is bumped to >= 7.1.0 set return type to ?\WC_Payment_Token
* since the return type is nullable, as per
* https://www.php.net/manual/en/functions.returning-values.php#functions.returning-values.type-declaration
*
* @param array $request Associative array containing payment request information.
*
* @return \WC_Payment_Token|NULL
*/
public static function get_token_from_request( array $request ) {
$payment_method = $request['payment_method'] ?? null;
$token_request_key = 'wc-' . $payment_method . '-payment-token';
if (
! isset( $request[ $token_request_key ] ) ||
'new' === $request[ $token_request_key ]
) {
return null;
}
//phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash
$token = \WC_Payment_Tokens::get( wc_clean( $request[ $token_request_key ] ) );
// If the token doesn't belong to this gateway or the current user it's invalid.
if ( ! $token || $payment_method !== $token->get_gateway_id() || $token->get_user_id() !== get_current_user_id() ) {
return null;
}
return $token;
}
/**
* Extract the payment CVC confirmation from the provided request.
*
* @param array $request Associative array containing payment request information.
*
* @return string|NULL
*/
public static function get_cvc_confirmation_from_request( array $request ) {
$payment_method = $request['payment_method'] ?? null;
if ( null === $payment_method ) {
return null;
}
$cvc_request_key = 'wc-' . $payment_method . '-payment-cvc-confirmation';
if (
! isset( $request[ $cvc_request_key ] ) ||
'new' === $request[ $cvc_request_key ]
) {
return null;
}
return $request[ $cvc_request_key ];
}
/**
* Extracts the fingerprint data from the provided request.
*
* @param array $request Associative array containing payment request information.
*
* @return string
*/
public static function get_fingerprint_from_request( array $request ) {
if ( ! empty( $request['wcpay-fingerprint'] ) ) {
$normalized = wc_clean( $request['wcpay-fingerprint'] );
return is_string( $normalized ) ? $normalized : '';
}
return '';
}
/**
* Changes the type of the payment.
*
* @param Payment_Type $type The new type.
*/
public function set_payment_type( $type ) {
$this->payment_type = $type;
}
/**
* Retrieves the type of the payment.
*
* @return Payment_Type The payment type.
*/
public function get_payment_type() {
return $this->payment_type;
}
/**
* Forces the payment method to be saved to merchant store when the payment gets processed.
*/
public function must_save_payment_method_to_store() {
$this->save_payment_method_to_store = true;
}
/**
* Forces the payment method to be saved to platform when the payment gets processed.
*/
public function must_save_payment_method_to_platform() {
$this->save_payment_method_to_platform = true;
}
/**
* Indicates whether the payment method needs to be saved to the store for later usage.
*
* @return bool The flag.
*/
public function should_save_payment_method_to_store() {
return ! $this->is_using_saved_payment_method() && $this->save_payment_method_to_store;
}
/**
* Indicates whether the payment method needs to be saved to the platform for later usage.
*
* @return bool The flag.
*/
public function should_save_payment_method_to_platform() {
return ! $this->is_using_saved_payment_method() && $this->save_payment_method_to_platform;
}
/**
* Mark that we are changing payment for subscriptions order or not.
*
* @param bool $is_changing_payment_method_for_subscription Whether or not we are changing payment for subscriptions order.
*/
public function set_is_changing_payment_method_for_subscription( bool $is_changing_payment_method_for_subscription ) {
$this->is_changing_payment_method_for_subscription = $is_changing_payment_method_for_subscription;
}
/**
* Returns the flag of whether or not we are changing payment for subscriptions order.
*
* @return bool Whether or not we are changing payment for subscriptions order.
*/
public function is_changing_payment_method_for_subscription(): bool {
return $this->is_changing_payment_method_for_subscription;
}
/**
* Returns the payment method CVC confirmation.
*
* @return string|NULL The payment method CVC confirmation.
*/
public function get_cvc_confirmation() {
return $this->cvc_confirmation;
}
/**
* Returns the attached fingerprint.
*
* @return string The attached fingerprint.
*/
public function get_fingerprint() {
return $this->fingerprint;
}
}