1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Payment response |
4
|
|
|
* |
5
|
|
|
* @author Pronamic <[email protected]> |
6
|
|
|
* @copyright 2005-2019 Pronamic |
7
|
|
|
* @license GPL-3.0-or-later |
8
|
|
|
* @package Pronamic\WordPress\Pay\Gateways\Adyen |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Pronamic\WordPress\Pay\Gateways\Adyen; |
12
|
|
|
|
13
|
|
|
use JsonSchema\Constraints\Constraint; |
14
|
|
|
use JsonSchema\Exception\ValidationException; |
15
|
|
|
use JsonSchema\Validator; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Payment response |
19
|
|
|
* |
20
|
|
|
* @link https://docs.adyen.com/api-explorer/#/PaymentSetupAndVerificationService/v41/payments |
21
|
|
|
* |
22
|
|
|
* @author Remco Tolsma |
23
|
|
|
* @version 1.0.0 |
24
|
|
|
* @since 1.0.0 |
25
|
|
|
*/ |
26
|
|
|
class PaymentResponse extends ResponseObject { |
27
|
|
|
/** |
28
|
|
|
* Adyen's 16-character string reference associated with the transaction/request. This value is globally unique; quote it when communicating with us about this request. |
29
|
|
|
* |
30
|
|
|
* `pspReference` is returned only for non-redirect payment methods. |
31
|
|
|
* |
32
|
|
|
* @var string|null |
33
|
|
|
*/ |
34
|
|
|
private $psp_reference; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* When the payment flow requires a redirect, this object contains information about the redirect URL. |
38
|
|
|
* |
39
|
|
|
* @var RedirectInformation|null |
40
|
|
|
*/ |
41
|
|
|
private $redirect; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The result of the payment. |
45
|
|
|
* |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
private $result_code; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Construct payment response object. |
52
|
|
|
* |
53
|
|
|
* @param string $result_code Result code. |
54
|
|
|
*/ |
55
|
2 |
|
public function __construct( $result_code ) { |
56
|
2 |
|
$this->result_code = $result_code; |
57
|
2 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get result code. |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
2 |
|
public function get_result_code() { |
65
|
2 |
|
return $this->result_code; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get PSP reference. |
70
|
|
|
* |
71
|
|
|
* @return string|null |
72
|
|
|
*/ |
73
|
|
|
public function get_psp_reference() { |
74
|
|
|
return $this->psp_reference; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Set PSP reference. |
79
|
|
|
* |
80
|
|
|
* @param string|null $psp_reference PSP reference. |
81
|
|
|
*/ |
82
|
|
|
public function set_psp_reference( $psp_reference ) { |
83
|
|
|
$this->psp_reference = $psp_reference; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get redirect. |
88
|
|
|
* |
89
|
|
|
* @return RedirectInformation|null |
90
|
|
|
*/ |
91
|
2 |
|
public function get_redirect() { |
92
|
2 |
|
return $this->redirect; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Set redirect. |
97
|
|
|
* |
98
|
|
|
* @param RedirectInformation|null $redirect Redirect information. |
99
|
|
|
*/ |
100
|
2 |
|
public function set_redirect( RedirectInformation $redirect = null ) { |
101
|
2 |
|
$this->redirect = $redirect; |
102
|
2 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Create payment repsonse from object. |
106
|
|
|
* |
107
|
|
|
* @param object $object Object. |
108
|
|
|
* @return PaymentResponse |
109
|
|
|
* @throws ValidationException Throws validation exception when object does not contains the required properties. |
110
|
|
|
*/ |
111
|
2 |
|
public static function from_object( $object ) { |
112
|
2 |
|
$validator = new Validator(); |
113
|
|
|
|
114
|
2 |
|
$validator->validate( |
115
|
2 |
|
$object, |
116
|
|
|
(object) array( |
117
|
2 |
|
'$ref' => 'file://' . realpath( __DIR__ . '/../json-schemas/payment-response.json' ), |
118
|
|
|
), |
119
|
2 |
|
Constraint::CHECK_MODE_EXCEPTIONS |
120
|
|
|
); |
121
|
|
|
|
122
|
2 |
|
$payment_response = new self( $object->resultCode ); |
123
|
|
|
|
124
|
2 |
|
if ( isset( $object->pspReference ) ) { |
125
|
|
|
$payment_response->set_psp_reference( $object->pspReference ); |
126
|
|
|
} |
127
|
|
|
|
128
|
2 |
|
if ( isset( $object->redirect ) ) { |
129
|
2 |
|
$payment_response->set_redirect( RedirectInformation::from_object( $object->redirect ) ); |
130
|
|
|
} |
131
|
|
|
|
132
|
2 |
|
return $payment_response; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|