Code Duplication    Length = 34-37 lines in 3 locations

src/Daraja.php 3 locations

@@ 123-156 (lines=34) @@
120
     * @param string $responseType | Default Response type in case the validation URL is unreachable or is undefined. Either Cancelled or Completed as per the safaricom documentation
121
     * @return false|mixed|string
122
     */
123
    public function registerCallbacks($shortCode,$confirmationURL, $validationURL=null, $responseType="Cancelled") {
124
        $environment = $this->environment;
125
        if( $environment =="live"){
126
            $url = "https://api.safaricom.co.ke/mpesa/c2b/v1/registerurl";
127
            $this->generateLiveToken();
128
        }elseif ($environment=="sandbox"){
129
            $url = "https://sandbox.safaricom.co.ke/mpesa/c2b/v1/registerurl";
130
            $this->generateSandBoxToken();
131
        }else{
132
            return json_encode(["Message"=>"Invalid App environment. Please set it via setCredentials() to either live or sandbox."]);
133
        }
134
135
        $token = $this->getAccessToken();
136
        $curl = curl_init();
137
138
        curl_setopt($curl, CURLOPT_URL, $url);
139
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json','Authorization:Bearer '.$token));
140
141
142
        $curl_post_data = array(
143
            "ValidationURL" => $validationURL,
144
            "ConfirmationURL" => $confirmationURL,
145
            "ResponseType" => $responseType,
146
            "ShortCode" => $shortCode
147
        );
148
149
        $data_string = json_encode($curl_post_data);
150
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
151
        curl_setopt($curl, CURLOPT_POST, true);
152
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
153
        curl_setopt($curl, CURLOPT_HEADER, false);
154
        $curl_response = curl_exec($curl);
155
        return json_decode($curl_response);
156
    }
157
158
    /**
159
     * Generate an encrypted security credential from the initiator password on either live or sandbox env.
@@ 344-377 (lines=34) @@
341
     * @param $BillRefNumber | 	Bill Reference Number (Optional).
342
     * @return mixed|string
343
     */
344
    public function c2b($ShortCode, $CommandID, $Amount, $Msisdn, $BillRefNumber ){
345
        $environment = $this->environment;
346
        if( $environment =="live"){
347
            $url = 'https://api.safaricom.co.ke/mpesa/c2b/v1/simulate';
348
            $this->generateLiveToken();
349
        }elseif ($environment=="sandbox"){
350
            $url = 'https://sandbox.safaricom.co.ke/mpesa/c2b/v1/simulate';
351
            $this->generateSandBoxToken();
352
        }else{
353
            return json_encode(["Message"=>"invalid application status. Please set mpesa_env to either sandbox or live"]);
354
        }
355
356
        $token = $this->getAccessToken();
357
        $curl = curl_init();
358
        curl_setopt($curl, CURLOPT_URL, $url);
359
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json','Authorization:Bearer '.$token));
360
361
        $curl_post_data = array(
362
            'ShortCode' => $ShortCode,
363
            'CommandID' => $CommandID,
364
            'Amount' => $Amount,
365
            'Msisdn' => $Msisdn,
366
            'BillRefNumber' => $BillRefNumber
367
        );
368
369
        $data_string = json_encode($curl_post_data);
370
371
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
372
        curl_setopt($curl, CURLOPT_POST, true);
373
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
374
        curl_setopt($curl, CURLOPT_HEADER, false);
375
        $curl_response = curl_exec($curl);
376
        return $curl_response;
377
    }
378
379
380
    /**
@@ 624-660 (lines=37) @@
621
     * @param $timestamp | Timestamp
622
     * @return mixed|string
623
     */
624
    public function STKPushQuery($checkoutRequestID, $businessShortCode, $password, $timestamp){
625
626
        $environment =$this->environment;
627
628
        if( $environment =="live"){
629
            $url = 'https://api.safaricom.co.ke/mpesa/stkpushquery/v1/query';
630
            $this->generateLiveToken();
631
        }elseif ($environment=="sandbox"){
632
            $url = 'https://sandbox.safaricom.co.ke/mpesa/stkpushquery/v1/query';
633
            $this->generateSandBoxToken();
634
        }else{
635
            return json_encode(["Message"=>"invalid application status"]);
636
        }
637
        $token = $this->getAccessToken();
638
639
        $curl = curl_init();
640
        curl_setopt($curl, CURLOPT_URL, $url);
641
        curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json','Authorization:Bearer '.$token));
642
643
        $curl_post_data = array(
644
            'BusinessShortCode' => $businessShortCode,
645
            'Password' => $password,
646
            'Timestamp' => $timestamp,
647
            'CheckoutRequestID' => $checkoutRequestID
648
        );
649
650
        $data_string = json_encode($curl_post_data);
651
652
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
653
        curl_setopt($curl, CURLOPT_POST, true);
654
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
655
        curl_setopt($curl, CURLOPT_HEADER, false);
656
657
        $curl_response = curl_exec($curl);
658
659
        return $curl_response;
660
    }
661
662
    /**
663
     *Use this function to confirm all transactions in callback routes