MazzumaPayment::send()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 0
cts 21
cp 0
rs 9.424
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 6
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
            //TODO: Throw an Exception
54
55
            return "CURL isn't enabled on your Server !";
56
        }
57
58
        $data = $this->parsePaymentDetails(
59
            $this->flow,
60
            $this->payeeNetwork,
61
            $this->key,
62
            $this->from,
63
            $this->to,
64
            $this->amount
65
        );
66
        
67
        $additionalHeaders = array(
68
            'Content-Type: application/json'
69
         );
70
71
        $ch = curl_init($this->api);
72
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
73
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
74
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
75
        curl_setopt($ch, CURLOPT_HTTPHEADER, $additionalHeaders);
76
        $response = curl_exec($ch);
77
        $this->apiResponse = json_decode($response, true);
78
79
        return $this->apiResponse;
80
    }
81
82
    /**
83
     * Parses the Payment Details into Json for API call
84
     * @param string $paymentDirectionalFlow  The Network directional flow of payment
85
     * @param string $payeeNetwork The Network Operator of the Sender
86
     * @param string $APIKey The API access key, as obtained on https://dashboard.mazzuma.com/
87
     * @param string $payee The Sender Telephone Number
88
     * @param string $reciever The Recievers Telephone Number
89
     * @param integer $amount The amount been transacted
90
     *
91
     * @return string
92
     */
93
    private function parsePaymentDetails(
94
        $paymentDirectionalFlow,
95
        $payeeNetwork,
96
        $APIKey,
97
        $payee,
98
        $reciever,
99
        $amount
100
    ) {
101
        if (empty($amount) ||
102
            empty($payeeNetwork) ||
103
            empty($reciever) ||
104
            empty($payee) ||
105
            empty($paymentDirectionalFlow) ||
106
            empty($APIKey)) {
107
            //TODO: Throw an Exception
108
            
109
            return "Invalid Input !";
110
        }
111
112
        $data = [
113
            "price"=> $amount,
114
            "network"=> $payeeNetwork,
115
            "recipient_number"=> $reciever,
116
            "sender"=> $payee,
117
            "option"=> $paymentDirectionalFlow,
118
            "apikey"=> $APIKey
119
        ];
120
121
        $json_data = json_encode($data);
122
123
        return $json_data;
124
    }
125
126
    /**
127
     * Sets the Sender
128
     * @param $payee string The telephone Number of the Sender
129
     *
130
     * @return MazzumaPayment
131
     */
132
    public function from($payee)
133
    {
134
        $this->validateTelephone($payee);
135
        $this->from = trim($payee);
136
137
        return $this;
138
    }
139
140
    /**
141
     * returns the Sender
142
     *
143
     * @return string
144
     */
145
    public function getFrom()
146
    {
147
        return $this->from;
148
    }
149
150
    /**
151
     * Sets the Reciever
152
     * @param $reciever string The telephone Number of the Reciever
153
     *
154
     * @return MazzumaPayment
155
     */
156
    public function to($reciever)
157
    {
158
        $this->validateTelephone($reciever);
159
        $this->to = trim($reciever);
160
161
        return $this;
162
    }
163
164
    /**
165
     * returns the Reciever
166
     *
167
     * @return string
168
     */
169
    public function getTo()
170
    {
171
        return $this->to;
172
    }
173
174
    /**
175
     * Sets the Amount
176
     * @param $totalAmount string The amount to be sent
177
     *
178
     * @return MazzumaPayment
179
     */
180
    public function amount($totalAmount)
181
    {
182
        $this->validateAmount($totalAmount);
183
        $this->amount = $totalAmount;
184
185
        return $this;
186
    }
187
188
    /**
189
     * returns the Amount
190
     * @param $totalAmount string The amount to be sent
191
     *
192
     * @return integer
193
     */
194
    public function getAmount()
195
    {
196
        return $this->amount;
197
    }
198
199
    /**
200
     * Sets the Sender Network
201
     * @param $paymentFlow string The flow of the Payment
202
     *
203
     * @return MazzumaPayment
204
     */
205
    public function transfer($paymentFlow)
206
    {
207
        $this->payeeNetwork = $this->getSenderNetwork($paymentFlow);
208
209
        $this->setPaymentRoute($paymentFlow);
210
211
        return $this;
212
    }
213
214
    /**
215
     * Checks if payment was successful
216
     *
217
     * @return boolean
218
     */
219
    public function isSuccessful()
220
    {
221
        if (!$this->apiResponse['status'] == 'success') {
222
            return false;
223
        }
224
225
        return true;
226
    }
227
228
    /**
229
     * Gets the Sender Network from the payment Flow
230
     *
231
     * @return string
232
     */
233
    private function getSenderNetwork($paymentFlow)
234
    {
235
        $networks = explode("_", trim($paymentFlow));
236
237
        return strtolower($networks[0]);
238
    }
239
240
    /**
241
     * returns the Reciever
242
     *
243
     * @return string
244
     */
245
    public function getPayeeNetwork()
246
    {
247
        return $this->payeeNetwork;
248
    }
249
250
    /**
251
     * Sets the Option Parameter in the Data Payload
252
     *`
253
     * @param string $paymentDirection The flow of the Payment
254
     *
255
     * @return string Returns the ioption value for the payload
256
     */
257
    private function setPaymentRoute($paymentDirection)
258
    {
259
        switch ($paymentDirection) {
260
            case 'MTN_TO_MTN':
261
                $this->flow = 'rmtm';
262
                break;
263
            case 'MTN_TO_AIRTEL':
264
                $this->flow = 'rmta';
265
                break;
266
            case 'MTN_TO_VODAFONE':
267
                $this->flow = 'rmtv';
268
                break;
269
            case 'AIRTEL_TO_MTN':
270
                $this->flow = 'ratm';
271
                break;
272
            case 'AIRTEL_TO_AIRTEL':
273
                $this->flow = 'rata';
274
                break;
275
            case 'AIRTEL_TO_VODAFONE':
276
                $this->flow = 'ratv';
277
                break;
278
            case 'VODAFONE_TO_MTN':
279
                $this->flow = 'rvtm';
280
                break;
281
            case 'VODAFONE_TO_AIRTEL':
282
                $this->flow = 'rvta';
283
                break;
284
            case 'VODAFONE_TO_VODAFONE':
285
                $this->flow = 'rvtv';
286
                break;
287
            default:
288
                throw new PaymentFlowValidateException('Payment Flow is Invalid !.');
289
        }
290
    }
291
292
    /**
293
     * returns the Flow of the Transaction
294
     *
295
     * @return string
296
     */
297
    public function getFlow()
298
    {
299
        return $this->flow;
300
    }
301
302
    /**
303
     * Validates The telephone Numbers
304
     *
305
     * @param string $telephone The telephone number of a reciever or sender
306
     *
307
     * @return boolean
308
     */
309
    private function validateTelephone($telephone)
310
    {
311
        if (preg_match("/^\d{10}$/", $telephone)) {
312
            return true;
313
        } else {
314
            throw new TelephoneValidateException('Telephone Number is Invalid !.');
315
        }
316
    }
317
318
    /**
319
     * Validates The Amount
320
     *
321
     * @param string $amount The amount been transacted
322
     *
323
     * @return boolean
324
     */
325
    private function validateAmount($amount)
326
    {
327
        if (!is_numeric($amount)) {
328
            throw new AmountValidateException('Amount must be a number.');
329
        }
330
331
        return true;
332
    }
333
}
334