1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Money |
4
|
|
|
* |
5
|
|
|
* @author Pronamic <[email protected]> |
6
|
|
|
* @copyright 2005-2018 Pronamic |
7
|
|
|
* @license GPL-3.0-or-later |
8
|
|
|
* @package Pronamic\WordPress\Pay\Gateways\OmniKassa2 |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Pronamic\WordPress\Pay\Gateways\OmniKassa2; |
12
|
|
|
|
13
|
|
|
use InvalidArgumentException; |
14
|
|
|
use stdClass; |
15
|
|
|
use JsonSchema\Constraints\Constraint; |
16
|
|
|
use JsonSchema\Exception\ValidationException; |
17
|
|
|
use JsonSchema\Validator; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Money |
21
|
|
|
* |
22
|
|
|
* @author Remco Tolsma |
23
|
|
|
* @version 2.0.2 |
24
|
|
|
* @since 2.0.2 |
25
|
|
|
*/ |
26
|
|
|
class Money { |
27
|
|
|
/** |
28
|
|
|
* Currency. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $currency; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Amount. |
36
|
|
|
* |
37
|
|
|
* @var int |
38
|
|
|
*/ |
39
|
|
|
private $amount; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Construct notification message. |
43
|
|
|
* |
44
|
|
|
* @param string $currency Authentication. |
45
|
|
|
* @param int $amount Amount in cents. |
46
|
|
|
*/ |
47
|
3 |
|
public function __construct( $currency, $amount ) { |
48
|
3 |
|
$this->currency = $currency; |
49
|
3 |
|
$this->amount = $amount; |
50
|
3 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get currency. |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
3 |
|
public function get_currency() { |
58
|
3 |
|
return $this->currency; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get amount. |
63
|
|
|
* |
64
|
|
|
* @return int |
65
|
|
|
*/ |
66
|
3 |
|
public function get_amount() { |
67
|
3 |
|
return $this->amount; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get JSON. |
72
|
|
|
* |
73
|
|
|
* @return object |
74
|
|
|
*/ |
75
|
|
|
public function get_json() { |
76
|
|
|
return (object) array( |
77
|
|
|
'currency' => $this->get_currency(), |
78
|
|
|
'amount' => $this->get_amount(), |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Create money from object. |
84
|
|
|
* |
85
|
|
|
* @param stdClass $object Object. |
86
|
|
|
* @return Money |
87
|
|
|
* @throws InvalidArgumentException Throws invalid argument exception when object does not contains the required properties. |
88
|
|
|
*/ |
89
|
1 |
|
public static function from_object( stdClass $object ) { |
90
|
1 |
|
if ( ! isset( $object->currency ) ) { |
91
|
|
|
throw new InvalidArgumentException( 'Object must contain `currency` property.' ); |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
if ( ! isset( $object->amount ) ) { |
95
|
|
|
throw new InvalidArgumentException( 'Object must contain `amount` property.' ); |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
return new self( |
99
|
1 |
|
$object->currency, |
100
|
1 |
|
$object->amount |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Create money from JSON string. |
106
|
|
|
* |
107
|
|
|
* @param string $json JSON string. |
108
|
|
|
* @return Money |
109
|
|
|
* @throws \JsonSchema\Exception\ValidationException Throws JSON schema validation exception when JSON is invalid. |
110
|
|
|
*/ |
111
|
|
|
public static function from_json( $json ) { |
112
|
|
|
$data = json_decode( $json ); |
113
|
|
|
|
114
|
|
|
$validator = new Validator(); |
115
|
|
|
|
116
|
|
|
$validator->validate( |
117
|
|
|
$data, |
118
|
|
|
(object) array( |
119
|
|
|
'$ref' => 'file://' . realpath( __DIR__ . '/../json-schemas/json-schema-money.json' ), |
120
|
|
|
), |
121
|
|
|
Constraint::CHECK_MODE_EXCEPTIONS |
122
|
|
|
); |
123
|
|
|
|
124
|
|
|
return self::from_object( $data ); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get signature fields. |
129
|
|
|
* |
130
|
|
|
* @param array $fields Fields. |
131
|
|
|
* @return array |
132
|
|
|
*/ |
133
|
2 |
|
public function get_signature_fields( $fields = array() ) { |
134
|
2 |
|
$fields[] = $this->get_currency(); |
135
|
2 |
|
$fields[] = strval( $this->get_amount() ); |
136
|
|
|
|
137
|
2 |
|
return $fields; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|