Completed
Push — master ( dec4e1...786955 )
by Baffour Adu
11:36
created

MazzumaPayment::parsePaymentDetails()   B

Complexity

Conditions 7
Paths 2

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 0
cts 21
cp 0
rs 8.4906
c 0
b 0
f 0
cc 7
nc 2
nop 6
crap 56
1
<?php
2
3
namespace BaffourAdu\Mazzuma;
4
5
use BaffourAdu\Mazzuma\Exception\AmountValidateException;
6
use BaffourAdu\Mazzuma\Exception\TelephoneValidateException;
7
8
/**
9
 * Class MazzumaPayment
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
        if (!function_exists('curl_version')) {
53
            return "CURL isn't enabled on your Server !";
54
        }
55
56
        $data = $this->parsePaymentDetails(
57
            $this->flow,
58
            $this->payeeNetwork,
59
            $this->key,
60
            $this->from,
61
            $this->to,
62
            $this->amount
63
        );
64
        
65
        $additionalHeaders = array(
66
            'Content-Type: application/json'
67
         );
68
69
        $ch = curl_init($this->api);
70
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
71
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
72
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
73
        curl_setopt($ch, CURLOPT_HTTPHEADER, $additionalHeaders);
74
        $this->apiResponse = curl_exec($ch);
75
76
        return $this->apiResponse;
77
    }
78
79
    /**
80
     * Parses the Payment Details into Json for API call
81
     * @param $paymentDirectionalFlow string The Network directional flow of payment
82
     * @param $payeeNetwork string The Network Operator of the Sender
83
     * @param $APIKey string The API access key, as obtained on https://dashboard.mazzuma.com/
84
     * @param $payee string The Sender Telephone Number
85
     * @param $reciever string The Recievers Telephone Number
86
     * @param $amount integer The amount been transacted
87
     * @return object
88
     */
89
    private function parsePaymentDetails(
90
        $paymentDirectionalFlow,
91
        $payeeNetwork,
92
        $APIKey,
93
        $payee,
94
        $reciever,
95
        $amount
96
    ) {
97
        if (
98
            empty($amount) ||
99
            empty($payeeNetwork) ||
100
            empty($reciever) ||
101
            empty($payee) ||
102
            empty($paymentDirectionalFlow) ||
103
            empty($APIKey)) {
104
            return "Invalid Input !";
105
        }
106
107
        $data = [
108
            "price"=> $amount,
109
            "network"=> $payeeNetwork,
110
            "recipient_number"=> $reciever,
111
            "sender"=> $payee,
112
            "option"=> $paymentDirectionalFlow,
113
            "apikey"=> $APIKey
114
        ];
115
116
        $json_data = json_encode($data);
117
118
        return $json_data;
119
    }
120
121
    /**
122
     * Sets the Sender
123
     * @param $payee string The telephone Number of the Sender
124
     *
125
     * @return object
126
     */
127
    public function from($payee)
128
    {
129
        $this->validateTelephone($payee);
130
        $this->from = trim($payee);
131
132
        return $this;
133
    }
134
135
    /**
136
     * returns the Sender
137
     *
138
     * @return string
139
     */
140
    public function getFrom()
141
    {
142
        return $this->from;
143
    }
144
145
    /**
146
     * Sets the Reciever
147
     * @param $reciever string The telephone Number of the Reciever
148
     *
149
     * @return object
150
     */
151
    public function to($reciever)
152
    {
153
        $this->validateTelephone($reciever);
154
        $this->to = trim($reciever);
155
156
        return $this;
157
    }
158
159
    /**
160
     * returns the Reciever
161
     *
162
     * @return object
163
     */
164
    public function getTo()
165
    {
166
        return $this->to;
167
    }
168
169
    /**
170
     * Sets the Amount
171
     * @param $totalAmount string The amount to be sent
172
     *
173
     * @return object
174
     */
175
    public function amount($totalAmount)
176
    {
177
        $this->validateAmount($totalAmount);
178
        $this->amount = trim($totalAmount);
0 ignored issues
show
Documentation Bug introduced by
The property $amount was declared of type integer, but trim($totalAmount) is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
179
180
        return $this;
181
    }
182
183
    /**
184
     * returns the Amount
185
     * @param $totalAmount string The amount to be sent
186
     *
187
     * @return integer
188
     */
189
    public function getAmount()
190
    {
191
        return $this->amount;
192
    }
193
194
    /**
195
     * Sets the Sender Network
196
     * @param $paymentFlow string The flow of the Payment
197
     *
198
     * @return object
199
     */
200
    public function transfer($paymentFlow)
201
    {
202
        $this->payeeNetwork = $this->getSenderNetwork($paymentFlow);
203
204
        $this->setPaymentRoute($paymentFlow);
205
206
        return $this;
207
    }
208
209
    /**
210
     * Checks if payment was successful
211
     *
212
     * @return boolean
213
     */
214
    public function isSuccessful()
215
    {
216
        if (!$this->apiResponse['status'] == 'success') {
217
            return false;
218
        }
219
220
        return true;
221
    }
222
223
    /**
224
     * Gets the Sender Network from the payment Flow
225
     *
226
     * @return boolean
227
     */
228
    private function getSenderNetwork($paymentFlow)
229
    {
230
        $networks = explode("_", trim($paymentFlow));
231
232
        return strtolower($networks[0]);
233
    }
234
235
    /**
236
     * returns the Reciever
237
     *
238
     * @return object
239
     */
240
    public function getPayeeNetwork()
241
    {
242
        return $this->payeeNetwork;
243
    }
244
245
    /**
246
     * Sets the Option Parameter in the Data Payload
247
     *`
248
     * @param string $paymentDirection The flow of the Payment
249
     *
250
     * @return string Returns the ioption value for the payload
251
     */
252
    private function setPaymentRoute($paymentDirection)
253
    {
254
        switch ($paymentDirection) {
255
            case 'MTN_TO_MTN':
256
                $this->flow = 'rmtm';
257
                break;
258
            case 'MTN_TO_AIRTEL':
259
                $this->flow = 'rmta';
260
                break;
261
            case 'MTN_TO_VODAFONE':
262
                $this->flow = 'rmtv';
263
                break;
264
            case 'AIRTEL_TO_MTN':
265
                $this->flow = 'ratm';
266
                break;
267
            case 'AIRTEL_TO_AIRTEL':
268
                $this->flow = 'rata';
269
                break;
270
            case 'AIRTEL_TO_VODAFONE':
271
                $this->flow = 'ratv';
272
                break;
273
            case 'VODAFONE_TO_MTN':
274
                $this->flow = 'rvtm';
275
                break;
276
            case 'VODAFONE_TO_AIRTEL':
277
                $this->flow = 'rvta';
278
                break;
279
            case 'VODAFONE_TO_VODAFONE':
280
                $this->flow = 'rvtv';
281
                break;
282
            default:
283
                $this->flow = null;
284
                break;
285
        }
286
    }
287
288
    /**
289
     * returns the Flow of the Transaction
290
     *
291
     * @return string
292
     */
293
    public function getFlow()
294
    {
295
        return $this->flow;
296
    }
297
298
    /**
299
     * Validates The telephone Numbers
300
     *
301
     * @param string $telephone The telephone number of a reciever or sender
302
     *
303
     * @return boolean
304
     */
305
    private function validateTelephone($telephone)
306
    {
307
        if (!is_numeric($telephone)) {
308
            throw new TelephoneValidateException('Telephone Number must be a String.');
309
        }
310
        if (strlen($telephone) != 10) {
311
            throw new TelephoneValidateException('Telephone Number is Invalid.');
312
        }
313
        return true;
314
    }
315
316
    /**
317
     * Validates The Amount
318
     *
319
     * @param string $amount The amount been transacted
320
     *
321
     * @return boolean
322
     */
323
    private function validateAmount($amount)
324
    {
325
        if (!is_numeric($amount)) {
326
            throw new AmountValidateException('Amount must be a number.');
327
        }
328
329
        return true;
330
    }
331
}
332