1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PatricPoba\MtnMomo; |
4
|
|
|
|
5
|
|
|
use PatricPoba\MtnMomo\Exceptions\MtnConfigException; |
6
|
|
|
use PatricPoba\MtnMomo\Exceptions\WrongProductException; |
7
|
|
|
use PatricPoba\MtnMomo\Utilities\AttributesMassAssignable; |
8
|
|
|
|
9
|
|
|
class MtnConfig |
10
|
|
|
{ |
11
|
|
|
use AttributesMassAssignable; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var string target environment |
15
|
|
|
*/ |
16
|
|
|
public $baseUrl; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string the currency of http calls |
20
|
|
|
*/ |
21
|
|
|
public $targetEnvironment; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string The MTN OpenApi currency |
25
|
|
|
*/ |
26
|
|
|
public $currency; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string The MTN product suscribed to. |
30
|
|
|
* collection|disbursement|remittance |
31
|
|
|
*/ |
32
|
|
|
public $product; |
33
|
|
|
|
34
|
|
|
const PRODUCTS = ['collection', 'disbursement', 'remittance']; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string The MTN OpenApi collections credentials |
38
|
|
|
* - API Secret. |
39
|
|
|
* - Primary Key |
40
|
|
|
* - User Id |
41
|
|
|
* - callback Url : can be overriden by a url set in the params array |
42
|
|
|
*/ |
43
|
|
|
public $collectionApiSecret; |
44
|
|
|
|
45
|
|
|
public $collectionPrimaryKey; |
46
|
|
|
|
47
|
|
|
public $collectionUserId; |
48
|
|
|
|
49
|
|
|
public $collectionCallbackUrl; |
50
|
|
|
|
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var string The MTN OpenApi remittance credentials |
54
|
|
|
* - API Secret. |
55
|
|
|
* - Primary Key |
56
|
|
|
* - User Id |
57
|
|
|
* - callback Url : can be overriden by a url set in the params array |
58
|
|
|
*/ |
59
|
|
|
public $remittanceApiSecret; |
60
|
|
|
|
61
|
|
|
public $remittancePrimaryKey; |
62
|
|
|
|
63
|
|
|
public $remittanceUserId; |
64
|
|
|
|
65
|
|
|
public $remittanceCallbackUrl; |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var string The MTN OpenApi disbursements primary Key |
70
|
|
|
* - API Secret. |
71
|
|
|
* - Primary Key |
72
|
|
|
* - User Id |
73
|
|
|
* - callback Url : can be overriden by a url set in the params array |
74
|
|
|
*/ |
75
|
|
|
public $disbursementApiSecret; |
76
|
|
|
|
77
|
|
|
public $disbursementPrimaryKey; |
78
|
|
|
|
79
|
|
|
public $disbursementUserId; |
80
|
|
|
|
81
|
|
|
public $disbursementCallbackUrl; |
82
|
|
|
|
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Set values of configs |
86
|
|
|
* |
87
|
|
|
* @param array $configArray |
88
|
|
|
*/ |
89
|
|
|
public function __construct($configArray) |
90
|
|
|
{ |
91
|
|
|
$this->massAssignAttributes($configArray); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Checck if all credentials are filled for the necessary |
96
|
|
|
* product |
97
|
|
|
* |
98
|
|
|
* @param string $product disbursement | remittance | collection |
99
|
|
|
* @throws \Exception |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
|
|
public function validate($product) |
103
|
|
|
{ |
104
|
|
|
if (! in_array($product, static::PRODUCTS) ) { |
105
|
|
|
throw new WrongProductException("Wrong product chosen. product must be " . implode(' or ', static::PRODUCTS)); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$missingCredentials = []; |
109
|
|
|
|
110
|
|
|
foreach ($this->requiredConfigs($product) as $config) { |
111
|
|
|
|
112
|
|
|
if (is_null($this->{$config}) ) { |
113
|
|
|
$missingCredentials[] = $config ; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if( ! empty($missingCredentials)){ |
118
|
|
|
throw new MtnConfigException("The followig credentials are missing: " . implode(', ', $missingCredentials) ); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get set of configs required according to the product being used. |
126
|
|
|
* |
127
|
|
|
* @param string $product disbursement | remittance | collection |
128
|
|
|
* @return array |
129
|
|
|
*/ |
130
|
|
|
protected function requiredConfigs($product) |
131
|
|
|
{ |
132
|
|
|
return [ |
133
|
|
|
'baseUrl', |
134
|
|
|
'currency', |
135
|
|
|
'targetEnvironment', |
136
|
|
|
$product . 'ApiSecret', |
137
|
|
|
$product . 'PrimaryKey', |
138
|
|
|
$product . 'UserId' |
139
|
|
|
]; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Get specific config dynamicsally |
144
|
|
|
* |
145
|
|
|
* @param string $product 'collection'|'disbursement'|'remittance' |
146
|
|
|
* @param string $configKey 'primaryKey' | 'ApiSecret' | 'UserId' |
147
|
|
|
* $throws PatricPoba\MtnMomo\Exceptions\MtnConfigException\MtnConfigException |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
|
|
public function getValue($product, $configKey) |
151
|
|
|
{ |
152
|
|
|
// if $product = 'collection' and $configKey= 'primaryKey', $configKey = 'collectionPrimaryKey' |
153
|
|
|
$configKey = strtolower($product) . ucfirst($configKey); |
154
|
|
|
|
155
|
|
|
if ( ! isset($this->$configKey)) { |
156
|
|
|
throw new MtnConfigException($configKey . " does not exist or is empty on this " . static::class . " instance") ; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $this->$configKey; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|