Skip to content

Commit 22ba2c9

Browse files
authored
Add Pay NCCO (#330)
* Test suite * remove unused constant
1 parent 52d68cb commit 22ba2c9

File tree

3 files changed

+411
-4
lines changed

3 files changed

+411
-4
lines changed

src/Voice/NCCO/Action/Connect.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,7 @@ public function __construct(EndpointInterface $endpoint)
6666
$this->endpoint = $endpoint;
6767
}
6868

69-
/**
70-
* @todo Remove the $data parameter as it's useless
71-
*/
72-
public static function factory(EndpointInterface $endpoint, array $data = []): Connect
69+
public static function factory(EndpointInterface $endpoint): Connect
7370
{
7471
return new Connect($endpoint);
7572
}

src/Voice/NCCO/Action/Pay.php

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Vonage\Voice\NCCO\Action;
6+
7+
use InvalidArgumentException;
8+
9+
class Pay implements ActionInterface
10+
{
11+
protected const PERMITTED_VOICE_KEYS = ['language', 'style'];
12+
13+
protected const PERMITTED_ERROR_KEYS = [
14+
'CardNumber' => [
15+
'InvalidCardType',
16+
'InvalidCardNumber',
17+
'Timeout'
18+
],
19+
'ExpirationDate' => [
20+
'InvalidExpirationDate',
21+
'Timeout'
22+
],
23+
'SecurityCode' => [
24+
'InvalidSecurityCode',
25+
'Timeout'
26+
]
27+
];
28+
29+
/**
30+
* @var float
31+
*/
32+
protected float $amount;
33+
34+
/**
35+
* @var string
36+
*/
37+
protected string $currency;
38+
39+
/**
40+
* @var string
41+
*/
42+
protected string $eventUrl;
43+
44+
/**
45+
* @var array
46+
*/
47+
protected array $prompts;
48+
49+
/**
50+
* @var array
51+
*/
52+
protected array $voice;
53+
54+
/**
55+
* @return float
56+
*/
57+
public function getAmount(): float
58+
{
59+
return $this->amount;
60+
}
61+
62+
public function setAmount(float $amount): void
63+
{
64+
$this->amount = $amount;
65+
}
66+
67+
public function getCurrency(): ?string
68+
{
69+
return $this->currency;
70+
}
71+
72+
public function setCurrency(?string $currency): void
73+
{
74+
$this->currency = $currency;
75+
}
76+
77+
public function getEventUrl(): ?string
78+
{
79+
return $this->eventUrl;
80+
}
81+
82+
public function setEventUrl(string $eventUrl): void
83+
{
84+
$this->eventUrl = $eventUrl;
85+
}
86+
87+
/**
88+
* @return array
89+
*/
90+
public function getPrompts(): array
91+
{
92+
return $this->prompts;
93+
}
94+
95+
public function setPrompts(array $prompts): void
96+
{
97+
if (!array_key_exists('type', $prompts)) {
98+
throw new InvalidArgumentException('type is required when setting a text prompt.');
99+
}
100+
101+
if (!array_key_exists($prompts['type'], self::PERMITTED_ERROR_KEYS)) {
102+
throw new InvalidArgumentException('invalid prompt type.');
103+
}
104+
105+
if (!array_key_exists('text', $prompts)) {
106+
throw new InvalidArgumentException('text is required when setting error text prompts..');
107+
}
108+
109+
if (!array_key_exists('errors', $prompts)) {
110+
throw new InvalidArgumentException('error settings are required when setting am error text prompt.');
111+
}
112+
113+
foreach ($prompts['errors'] as $errorPromptKey => $errorPromptData) {
114+
if (!array_key_exists('text', $errorPromptData)) {
115+
throw new InvalidArgumentException('text is required when setting error text prompts.');
116+
}
117+
118+
$permittedErrors = self::PERMITTED_ERROR_KEYS[$prompts['type']];
119+
120+
if (!in_array($errorPromptKey, $permittedErrors, true)) {
121+
throw new InvalidArgumentException('incorrect error type for prompt.');
122+
}
123+
}
124+
125+
$this->prompts = $prompts;
126+
}
127+
128+
/**
129+
* @return ?array
130+
*/
131+
public function getVoice(): array
132+
{
133+
return $this->voice;
134+
}
135+
136+
public function setVoice(array $settings): void
137+
{
138+
foreach (array_keys($settings) as $settingKey) {
139+
if (!in_array($settingKey, self::PERMITTED_VOICE_KEYS, true)) {
140+
throw new InvalidArgumentException($settingKey . ' did not fall under permitted voice settings');
141+
}
142+
}
143+
144+
$this->voice = $settings;
145+
}
146+
147+
public function toNCCOArray(): array
148+
{
149+
$data = [
150+
'action' => 'pay',
151+
'amount' => $this->getAmount()
152+
];
153+
154+
if (isset($this->currency)) {
155+
$data['currency'] = $this->getCurrency();
156+
}
157+
158+
if (isset($this->eventUrl)) {
159+
$data['eventUrl'] = $this->getEventUrl();
160+
}
161+
162+
if (isset($this->prompts)) {
163+
$data['prompts'] = $this->getPrompts();
164+
}
165+
166+
if (isset($this->voice)) {
167+
$data['voice'] = $this->getVoice();
168+
}
169+
170+
return $data;
171+
}
172+
173+
public function jsonSerialize(): array
174+
{
175+
return $this->toNCCOArray();
176+
}
177+
178+
public static function factory(array $data): Pay
179+
{
180+
$pay = new self();
181+
182+
if (array_key_exists('amount', $data)) {
183+
$pay->setAmount($data['amount']);
184+
} else {
185+
throw new InvalidArgumentException('Amount is required for this action.');
186+
}
187+
188+
if (array_key_exists('currency', $data)) {
189+
$pay->setCurrency($data['currency']);
190+
}
191+
192+
if (array_key_exists('eventUrl', $data)) {
193+
$pay->setEventUrl($data['eventUrl']);
194+
}
195+
196+
if (array_key_exists('prompts', $data)) {
197+
$pay->setPrompts($data['prompts']);
198+
}
199+
200+
if (array_key_exists('voice', $data)) {
201+
$pay->setVoice($data['voice']);
202+
}
203+
204+
return $pay;
205+
}
206+
}

0 commit comments

Comments
 (0)