|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Samerior\MobileMoney\Mpesa\Library; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
|
6
|
|
|
use Samerior\MobileMoney\Mpesa\Exceptions\MpesaException; |
|
7
|
|
|
use Samerior\MobileMoney\Mpesa\Repositories\EndpointsRepository; |
|
8
|
|
|
use Samerior\MobileMoney\Mpesa\Repositories\Mpesa; |
|
9
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class ApiCore |
|
13
|
|
|
* |
|
14
|
|
|
* @package Samerior\MobileMoney\Mpesa\Library |
|
15
|
|
|
*/ |
|
16
|
|
|
class ApiCore |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var Core |
|
20
|
|
|
*/ |
|
21
|
|
|
private $engine; |
|
22
|
|
|
/** |
|
23
|
|
|
* @var bool |
|
24
|
|
|
*/ |
|
25
|
|
|
public $bulk = false; |
|
26
|
|
|
/** |
|
27
|
|
|
* @var Mpesa |
|
28
|
|
|
*/ |
|
29
|
|
|
public $mpesaRepository; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* ApiCore constructor. |
|
33
|
|
|
* |
|
34
|
|
|
* @param Core $engine |
|
35
|
|
|
* @param Mpesa $mpesa |
|
36
|
|
|
*/ |
|
37
|
2 |
|
public function __construct(Core $engine, Mpesa $mpesa) |
|
38
|
|
|
{ |
|
39
|
2 |
|
$this->engine = $engine; |
|
40
|
2 |
|
$this->mpesaRepository = $mpesa; |
|
41
|
2 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param string $number |
|
45
|
|
|
* @param bool $strip_plus |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
1 |
|
protected function formatPhoneNumber($number, $strip_plus = true): string |
|
49
|
|
|
{ |
|
50
|
1 |
|
$number = preg_replace('/\s+/', '', $number); |
|
51
|
|
|
$replace = static function ($needle, $replacement) use (&$number) { |
|
52
|
1 |
|
if (Str::startsWith($number, $needle)) { |
|
53
|
1 |
|
$pos = strpos($number, $needle); |
|
54
|
1 |
|
$length = \strlen($needle); |
|
55
|
1 |
|
$number = substr_replace($number, $replacement, $pos, $length); |
|
56
|
|
|
} |
|
57
|
1 |
|
}; |
|
58
|
1 |
|
$replace('2547', '+2547'); |
|
59
|
1 |
|
$replace('07', '+2547'); |
|
60
|
1 |
|
if ($strip_plus) { |
|
61
|
1 |
|
$replace('+254', '254'); |
|
62
|
|
|
} |
|
63
|
1 |
|
return $number; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param array $body |
|
68
|
|
|
* @param string $endpoint |
|
69
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
70
|
|
|
* @throws \Samerior\MobileMoney\Mpesa\Exceptions\MpesaException |
|
71
|
|
|
* @throws \Exception |
|
72
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
73
|
|
|
*/ |
|
74
|
1 |
|
private function makeRequest($body, $endpoint) |
|
75
|
|
|
{ |
|
76
|
1 |
|
return $this->engine->client->request( |
|
77
|
1 |
|
'POST', |
|
78
|
1 |
|
$endpoint, |
|
79
|
|
|
[ |
|
80
|
|
|
'headers' => [ |
|
81
|
1 |
|
'Authorization' => 'Bearer ' . $this->engine->auth->authenticate($this->bulk), |
|
82
|
|
|
'Content-Type' => 'application/json', |
|
83
|
|
|
], |
|
84
|
|
|
'json' => $body, |
|
85
|
|
|
] |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @param array $body |
|
91
|
|
|
* @param string $endpoint |
|
92
|
|
|
* @return mixed |
|
93
|
|
|
* @throws MpesaException |
|
94
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
95
|
|
|
*/ |
|
96
|
1 |
|
public function sendRequest($body, $endpoint) |
|
97
|
|
|
{ |
|
98
|
1 |
|
$endpoint = EndpointsRepository::build($endpoint); |
|
99
|
|
|
try { |
|
100
|
1 |
|
$response = $this->makeRequest($body, $endpoint); |
|
101
|
|
|
$_body = \json_decode($response->getBody()); |
|
102
|
|
|
if ($response->getStatusCode() !== 200) { |
|
103
|
|
|
throw new MpesaException($_body->errorMessage ? $_body->errorCode . ' - ' . $_body->errorMessage : $response->getBody()); |
|
104
|
|
|
} |
|
105
|
|
|
return $_body; |
|
106
|
1 |
|
} catch (ClientException $exception) { |
|
107
|
|
|
throw $this->generateException($exception); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param ClientException $exception |
|
113
|
|
|
* @return MpesaException |
|
114
|
|
|
*/ |
|
115
|
|
|
private function generateException(ClientException $exception): MpesaException |
|
116
|
|
|
{ |
|
117
|
|
|
return new MpesaException($exception->getResponse()->getBody()); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|