Completed
Push — master ( 1d757a...c76d94 )
by Baffour Adu
11:20
created

MazzumaPayment::getFrom()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 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 = null;
19
    /** @var string The Network directional flow of payment */
20
    private $flow = null;
21
    /** @var string The network of the Sender */
22
    private $payeeNetwork = null;
23
    /** @var string The Sender Telephone Number */
24
    private $from = null;
25
    /** @var string The Reciever Telephone Number */
26
    private $to = null;
27
    /** @var integer The amount being Transfered */
28
    private $amount = null;
29
    /** @var integer The response from the API */
30
    private $apiResponse = null;
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
    public function getSenderNetwork($paymentFlow)
215
    {
216
        $networks = explode("_", $paymentFlow);
217
218
        return strtolower($networks[0]);
219
    }
220
221
    /**
222
     * Sets the Option Parameter in the Data Payload
223
     *`
224
     * @param string $paymentDirection The flow of the Payment
225
     *
226
     * @return string Returns the ioption value for the payload
227
     */
228
    private function setPaymentRoute($paymentDirection)
229
    {
230
        switch ($paymentDirection) {
231
            case 'MTN_TO_MTN':
232
                $this->flow = 'rmtm';
233
                break;
234
            case 'MTN_TO_AIRTEL':
235
                $this->flow = 'rmta';
236
                break;
237
            case 'MTN_TO_VODAFONE':
238
                $this->flow = 'rmtv';
239
                break;
240
            case 'AIRTEL_TO_MTN':
241
                $this->flow = 'ratm';
242
                break;
243
            case 'AIRTEL_TO_AIRTEL':
244
                $this->flow = 'rata';
245
                break;
246
            case 'AIRTEL_TO_VODAFONE':
247
                $this->flow = 'ratv';
248
                break;
249
            case 'VODAFONE_TO_MTN':
250
                $this->flow = 'rvtm';
251
                break;
252
            case 'VODAFONE_TO_AIRTEL':
253
                $this->flow = 'rvta';
254
                break;
255
            case 'VODAFONE_TO_VODAFONE':
256
                $this->flow = 'rvtv';
257
                break;
258
            default:
259
                $this->flow = null;
260
                break;
261
        }
262
    }
263
264
    /**
265
     * Validates The telephone Numbers
266
     *
267
     * @param string $telephone The telephone number of a reciever or sender
268
     *
269
     * @return boolean
270
     */
271
    private function validateTelephone($telephone)
272
    {
273
        if (!is_string($telephone)) {
274
            throw new TelephoneValidateException('Telephone Number must be a String.');
275
        }
276
        if (strlen($telephone) != 10) {
277
            throw new TelephoneValidateException('Telephone Number is Invalid.');
278
        }
279
        return true;
280
    }
281
282
    /**
283
     * Validates The Amount
284
     *
285
     * @param string $amount The amount been transacted
286
     *
287
     * @return boolean
288
     */
289
    private function validateAmount($amount)
290
    {
291
        if (!is_numeric($amount)) {
292
            throw new AmountValidateException('Amount must be a number.');
293
        }
294
295
        return true;
296
    }
297
}
298