1 | <?php |
||
2 | |||
3 | namespace Bmatovu\Ussd\Simulator; |
||
4 | |||
5 | use Bmatovu\Ussd\Contracts\Aggregator; |
||
6 | use GuzzleHttp\Client; |
||
7 | use GuzzleHttp\Exception\RequestException; |
||
8 | use GuzzleHttp\Exception\TransferException; |
||
9 | use Illuminate\Support\Str; |
||
10 | |||
11 | /** |
||
12 | * Comviva HTTP Pull Flares API |
||
13 | * Should work for MTN and Airtel. |
||
14 | */ |
||
15 | class Comviva implements Aggregator |
||
16 | { |
||
17 | public function call(string $uri, array $simulator): ?string |
||
18 | { |
||
19 | try { |
||
20 | $response = (new Client())->request('POST', $uri, [ |
||
21 | 'headers' => [ |
||
22 | 'Accept' => 'text/xml', |
||
23 | 'Content-Type' => 'text/xml; charset=UTF8', |
||
24 | ], |
||
25 | 'body' => $this->buildXml([ |
||
26 | 'name' => 'request', |
||
27 | 'attributes' => [ |
||
28 | 'type' => 'pull', |
||
29 | ], |
||
30 | ], [ |
||
31 | 'sessionId' => $simulator['session_id'], |
||
32 | 'transactionId' => null, |
||
33 | 'msisdn' => $simulator['phone_number'], |
||
34 | 'newRequest' => $simulator['new_session'] ? 1 : 0, |
||
35 | 'flowState' => $simulator['new_session'] ? 'FD' : 'FE', |
||
36 | 'subscriberInput' => $simulator['input'], // $simulator['new_session'] ? substr($simulator['input'], 4) : $simulator['input'], |
||
37 | ]), |
||
38 | ]); |
||
39 | |||
40 | $body = (string) $response->getBody(); |
||
41 | |||
42 | $body = new \SimpleXMLElement($body); |
||
43 | |||
44 | $flow = $body->freeflow->freeflowState->__toString(); |
||
45 | |||
46 | $applicationResponse = $body->applicationResponse->__toString(); |
||
47 | |||
48 | if ('FB' === $flow) { |
||
49 | throw new \Exception($applicationResponse); |
||
50 | } |
||
51 | } catch (\Throwable $th) { |
||
52 | throw new \Exception(Str::limit($th->getMessage(), 120, '...')); |
||
53 | } |
||
54 | |||
55 | // } catch (RequestException $ex) { |
||
56 | // $response = $ex->getResponse(); |
||
57 | // $body = (string) $response->getBody(); |
||
58 | // $message = $body ?? $response->getReasonPhrase(); |
||
59 | |||
60 | // throw new \Exception(sprintf('%s . %s', $message, $response->getStatusCode())); |
||
61 | // } catch (TransferException $ex) { |
||
62 | // throw new \Exception(sprintf('%s . %s', $ex->getMessage(), $ex->getCode())); |
||
63 | // } |
||
64 | |||
65 | return $applicationResponse; |
||
66 | } |
||
67 | |||
68 | protected function buildXml(array|string $root, array $elements): string |
||
69 | { |
||
70 | $domDoc = new \DOMDocument('1.0', 'UTF-8'); |
||
71 | $domDoc->xmlStandalone = true; |
||
72 | $domDoc->preserveWhiteSpace = false; |
||
73 | $domDoc->formatOutput = true; |
||
74 | |||
75 | if (\is_string($root)) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
76 | $rootEl = $domDoc->createElement($root); |
||
77 | } else { |
||
78 | $rootEl = $domDoc->createElement($root['name']); |
||
79 | foreach ($root['attributes'] ?? [] as $key => $value) { |
||
80 | $rootEl->setAttribute($key, (string) $value); |
||
81 | } |
||
82 | } |
||
83 | |||
84 | foreach ($elements as $key => $value) { |
||
85 | if (! \is_array($value)) { |
||
86 | $elem = $domDoc->createElement($key, (string) $value); |
||
87 | } else { |
||
88 | $elem = $domDoc->createElement($key); |
||
89 | foreach ($value as $key => $value) { |
||
0 ignored issues
–
show
|
|||
90 | $subElem = $domDoc->createElement($key, (string) $value); |
||
91 | $elem->appendChild($subElem); |
||
92 | } |
||
93 | } |
||
94 | $rootEl->appendChild($elem); |
||
95 | } |
||
96 | |||
97 | $domDoc->appendChild($rootEl); |
||
98 | |||
99 | return $domDoc->saveXML(); |
||
100 | } |
||
101 | } |
||
102 |