PaymentResult.php
105 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\StoreApi\Payments; |
| 3 | |
| 4 | /** |
| 5 | * PaymentResult class. |
| 6 | */ |
| 7 | class PaymentResult { |
| 8 | /** |
| 9 | * List of valid payment statuses. |
| 10 | * |
| 11 | * @var array |
| 12 | */ |
| 13 | protected $valid_statuses = [ 'success', 'failure', 'pending', 'error' ]; |
| 14 | |
| 15 | /** |
| 16 | * Current payment status. |
| 17 | * |
| 18 | * @var string |
| 19 | */ |
| 20 | protected $status = ''; |
| 21 | |
| 22 | /** |
| 23 | * Array of details about the payment. |
| 24 | * |
| 25 | * @var string |
| 26 | */ |
| 27 | protected $payment_details = []; |
| 28 | |
| 29 | /** |
| 30 | * Redirect URL for checkout. |
| 31 | * |
| 32 | * @var string |
| 33 | */ |
| 34 | protected $redirect_url = ''; |
| 35 | |
| 36 | /** |
| 37 | * Constructor. |
| 38 | * |
| 39 | * @param string $status Sets the payment status for the result. |
| 40 | */ |
| 41 | public function __construct( $status = '' ) { |
| 42 | if ( $status ) { |
| 43 | $this->set_status( $status ); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Magic getter for protected properties. |
| 49 | * |
| 50 | * @param string $name Property name. |
| 51 | */ |
| 52 | public function __get( $name ) { |
| 53 | if ( in_array( $name, [ 'status', 'payment_details', 'redirect_url' ], true ) ) { |
| 54 | return $this->$name; |
| 55 | } |
| 56 | return null; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Get payment status. |
| 61 | * |
| 62 | * @since 10.5.0 |
| 63 | * @return string Current payment status. |
| 64 | */ |
| 65 | public function get_status(): string { |
| 66 | return $this->status; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Set payment status. |
| 71 | * |
| 72 | * @throws \Exception When an invalid status is provided. |
| 73 | * |
| 74 | * @param string $payment_status Status to set. |
| 75 | */ |
| 76 | public function set_status( $payment_status ) { |
| 77 | if ( ! in_array( $payment_status, $this->valid_statuses, true ) ) { |
| 78 | throw new \Exception( sprintf( 'Invalid payment status %s. Use one of %s', $payment_status, implode( ', ', $this->valid_statuses ) ) ); |
| 79 | } |
| 80 | $this->status = $payment_status; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Set payment details. |
| 85 | * |
| 86 | * @param array $payment_details Array of key value pairs of data. |
| 87 | */ |
| 88 | public function set_payment_details( $payment_details = [] ) { |
| 89 | $this->payment_details = []; |
| 90 | |
| 91 | foreach ( $payment_details as $key => $value ) { |
| 92 | $this->payment_details[ (string) $key ] = (string) $value; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Set redirect URL. |
| 98 | * |
| 99 | * @param array $redirect_url URL to redirect the customer to after checkout. |
| 100 | */ |
| 101 | public function set_redirect_url( $redirect_url = [] ) { |
| 102 | $this->redirect_url = esc_url_raw( $redirect_url ); |
| 103 | } |
| 104 | } |
| 105 |