This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
/
PaymentResult.php
102 lines (90 loc) · 2.05 KB
/
PaymentResult.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
<?php
/**
* Payment result.
*
* @package WooCommerce/Blocks
*/
namespace Automattic\WooCommerce\Blocks\Payments;
defined( 'ABSPATH' ) || exit;
/**
* PaymentResult class.
*/
class PaymentResult {
/**
* List of valid payment statuses.
*
* @var array
*/
protected $valid_statuses = [ 'success', 'failure', 'pending', 'error' ];
/**
* Current payment status.
*
* @var string
*/
protected $status = '';
/**
* Array of details about the payment.
*
* @var string
*/
protected $payment_details = [];
/**
* Redirect URL for checkout.
*
* @var string
*/
protected $redirect_url = '';
/**
* Constructor.
*
* @param string $status Sets the payment status for the result.
*/
public function __construct( $status = '' ) {
if ( $status ) {
$this->set_status( $status );
}
}
/**
* Magic getter for protected properties.
*
* @param string $name Property name.
*/
public function __get( $name ) {
if ( in_array( $name, [ 'status', 'payment_details', 'redirect_url' ], true ) ) {
return $this->$name;
}
return null;
}
/**
* Set payment status.
*
* @throws \Exception When an invalid status is provided.
*
* @param string $payment_status Status to set.
*/
public function set_status( $payment_status ) {
if ( ! in_array( $payment_status, $this->valid_statuses, true ) ) {
throw new \Exception( sprintf( 'Invalid payment status %s. Use one of %s', $payment_status, implode( ', ', $this->valid_statuses ) ) );
}
$this->status = $payment_status;
}
/**
* Set payment details.
*
* @param array $payment_details Array of key value pairs of data.
*/
public function set_payment_details( $payment_details = [] ) {
$this->payment_details = [];
foreach ( $payment_details as $key => $value ) {
$this->payment_details[ (string) $key ] = (string) $value;
}
}
/**
* Set redirect URL.
*
* @param array $redirect_url URL to redirect the customer to after checkout.
*/
public function set_redirect_url( $redirect_url = [] ) {
$this->redirect_url = esc_url_raw( $redirect_url );
}
}