Completed
Push — master ( 6f39da...ec9035 )
by Baffour Adu
13:02
created

MazzumaPayment   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 303
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 30
lcom 1
cbo 2
dl 0
loc 303
ccs 0
cts 128
cp 0
rs 10
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A send() 0 24 1
A parsePaymentDetails() 0 21 1
A from() 0 7 1
A getFrom() 0 4 1
A to() 0 7 1
A getTo() 0 4 1
A amount() 0 7 1
A getAmount() 0 4 1
A transfer() 0 8 1
A isSuccessful() 0 8 2
A getSenderNetwork() 0 6 1
A getPayeeNetwork() 0 4 1
B setPaymentRoute() 0 35 10
A getFlow() 0 4 1
A validateTelephone() 0 10 3
A validateAmount() 0 8 2
1
<?php
2
3
namespace BaffourAdu\Mazzuma;
4
5
use BaffourAdu\Mazzuma\Exception\AmountValidateException;
6
use BaffourAdu\Mazzuma\Exception\TelephoneValidateException;
7
8
/**
9
 * Class Mazzuma
10
 *
11
 * The main class for API consumption
12
 *
13
 * @package BaffourAdu\Mazzuma
14
 */
15
class MazzumaPayment
16
{
17
    /** @var string The API access token */
18
    private $key;
19
    /** @var string The Network directional flow of payment */
20
    private $flow;
21
    /** @var string The network of the Sender */
22
    private $payeeNetwork;
23
    /** @var string The Sender Telephone Number */
24
    private $from;
25
    /** @var string The Reciever Telephone Number */
26
    private $to;
27
    /** @var integer The amount being Transfered */
28
    private $amount;
29
    /** @var integer The response from the API */
30
    private $apiResponse;
31
32
    /** @var string The API URL */
33
    private $api = 'https://client.teamcyst.com/api_call.php';
34
35
        
36
    /**
37
     * Creates a new MazzumaPayment Instance
38
     *
39
     */
40
    public function __construct($key)
41
    {
42
        $this->key = $key;
43
    }
44
45
    /**
46
     * Calls the API to process the transaction
47
     *
48
     * @return object The Response from the API Call
49
     */
50
    public function send()
51
    {
52
        $data = $this->parsePaymentDetails(
53
            $this->flow,
54
            $this->payeeNetwork,
55
            $this->key,
56
            $this->from,
57
            $this->to,
58
            $this->amount
59
        );
60
        
61
        $additionalHeaders = array(
62
            'Content-Type: application/json'
63
         );
64
65
        $ch = curl_init($this->api);
66
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
67
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
68
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
69
        curl_setopt($ch, CURLOPT_HTTPHEADER, $additionalHeaders);
70
        $this->apiResponse = curl_exec($ch);
71
72
        return $this->apiResponse;
73
    }
74
75
    /**
76
     * Parses the Payment Details into Json for API call
77
     * @param $paymentDirectionalFlow string The Network directional flow of payment
78
     * @param $payeeNetwork string The Network Operator of the Sender
79
     * @param $APIKey string The API access key, as obtained on https://dashboard.mazzuma.com/
80
     * @param $payee string The Sender Telephone Number
81
     * @param $reciever string The Recievers Telephone Number
82
     * @param $amount integer The amount been transacted
83
     * @return object
84
     */
85
    private function parsePaymentDetails(
86
        $paymentDirectionalFlow,
87
        $payeeNetwork,
88
        $APIKey,
89
        $payee,
90
        $reciever,
91
        $amount
92
    ) {
93
        $data = [
94
            "price"=> $amount,
95
            "network"=> $payeeNetwork,
96
            "recipient_number"=> $reciever,
97
            "sender"=> $payee,
98
            "option"=> $paymentDirectionalFlow,
99
            "apikey"=> $APIKey
100
        ];
101
102
        $json_data = json_encode($data);
103
104
        return $json_data;
105
    }
106
107
    /**
108
     * Sets the Sender
109
     * @param $payee string The telephone Number of the Sender
110
     *
111
     * @return object
112
     */
113
    public function from($payee)
114
    {
115
        $this->validateTelephone($payee);
116
        $this->from = $payee;
117
118
        return $this;
119
    }
120
121
    /**
122
     * returns the Sender
123
     *
124
     * @return string
125
     */
126
    public function getFrom()
127
    {
128
        return $this->from;
129
    }
130
131
    /**
132
     * Sets the Reciever
133
     * @param $reciever string The telephone Number of the Reciever
134
     *
135
     * @return object
136
     */
137
    public function to($reciever)
138
    {
139
        $this->validateTelephone($reciever);
140
        $this->to = $reciever;
141
142
        return $this;
143
    }
144
145
    /**
146
     * returns the Reciever
147
     *
148
     * @return object
149
     */
150
    public function getTo()
151
    {
152
        return $this->to;
153
    }
154
155
    /**
156
     * Sets the Amount
157
     * @param $totalAmount string The amount to be sent
158
     *
159
     * @return object
160
     */
161
    public function amount($totalAmount)
162
    {
163
        $this->validateAmount($totalAmount);
164
        $this->amount = $totalAmount;
165
166
        return $this;
167
    }
168
169
    /**
170
     * returns the Amount
171
     * @param $totalAmount string The amount to be sent
172
     *
173
     * @return integer
174
     */
175
    public function getAmount()
176
    {
177
        return $this->amount;
178
    }
179
180
    /**
181
     * Sets the Sender Network
182
     * @param $paymentFlow string The flow of the Payment
183
     *
184
     * @return object
185
     */
186
    public function transfer($paymentFlow)
187
    {
188
        $this->payeeNetwork = $this->getSenderNetwork($paymentFlow);
189
190
        $this->setPaymentRoute($paymentFlow);
191
192
        return $this;
193
    }
194
195
    /**
196
     * Checks if payment was successful
197
     *
198
     * @return boolean
199
     */
200
    public function isSuccessful()
201
    {
202
        if (!$this->apiResponse->status == 'success') {
203
            return false;
204
        }
205
206
        return true;
207
    }
208
209
    /**
210
     * Gets the Sender Network from the payment Flow
211
     *
212
     * @return boolean
213
     */
214
    private function getSenderNetwork($paymentFlow)
215
    {
216
        $networks = explode("_", $paymentFlow);
217
218
        return strtolower($networks[0]);
219
    }
220
221
    /**
222
     * returns the Reciever
223
     *
224
     * @return object
225
     */
226
    public function getPayeeNetwork()
227
    {
228
        return $this->payeeNetwork;
229
    }
230
231
    /**
232
     * Sets the Option Parameter in the Data Payload
233
     *`
234
     * @param string $paymentDirection The flow of the Payment
235
     *
236
     * @return string Returns the ioption value for the payload
237
     */
238
    private function setPaymentRoute($paymentDirection)
239
    {
240
        switch ($paymentDirection) {
241
            case 'MTN_TO_MTN':
242
                $this->flow = 'rmtm';
243
                break;
244
            case 'MTN_TO_AIRTEL':
245
                $this->flow = 'rmta';
246
                break;
247
            case 'MTN_TO_VODAFONE':
248
                $this->flow = 'rmtv';
249
                break;
250
            case 'AIRTEL_TO_MTN':
251
                $this->flow = 'ratm';
252
                break;
253
            case 'AIRTEL_TO_AIRTEL':
254
                $this->flow = 'rata';
255
                break;
256
            case 'AIRTEL_TO_VODAFONE':
257
                $this->flow = 'ratv';
258
                break;
259
            case 'VODAFONE_TO_MTN':
260
                $this->flow = 'rvtm';
261
                break;
262
            case 'VODAFONE_TO_AIRTEL':
263
                $this->flow = 'rvta';
264
                break;
265
            case 'VODAFONE_TO_VODAFONE':
266
                $this->flow = 'rvtv';
267
                break;
268
            default:
269
                $this->flow = null;
270
                break;
271
        }
272
    }
273
274
    /**
275
     * returns the Flow of the Transaction
276
     *
277
     * @return string
278
     */
279
    public function getFlow()
280
    {
281
        return $this->flow;
282
    }
283
284
    /**
285
     * Validates The telephone Numbers
286
     *
287
     * @param string $telephone The telephone number of a reciever or sender
288
     *
289
     * @return boolean
290
     */
291
    private function validateTelephone($telephone)
292
    {
293
        if (!is_string($telephone)) {
294
            throw new TelephoneValidateException('Telephone Number must be a String.');
295
        }
296
        if (strlen($telephone) != 10) {
297
            throw new TelephoneValidateException('Telephone Number is Invalid.');
298
        }
299
        return true;
300
    }
301
302
    /**
303
     * Validates The Amount
304
     *
305
     * @param string $amount The amount been transacted
306
     *
307
     * @return boolean
308
     */
309
    private function validateAmount($amount)
310
    {
311
        if (!is_numeric($amount)) {
312
            throw new AmountValidateException('Amount must be a number.');
313
        }
314
315
        return true;
316
    }
317
}
318