C2B::simulate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 2
nc 2
nop 4
1
<?php
2
3
namespace Starnerz\LaravelDaraja\Requests;
4
5
use Starnerz\LaravelDaraja\MpesaApiClient;
6
7
class C2B extends MpesaApiClient
8
{
9
    /**
10
     * The Safaricom C2B API end point for registering the confirmation
11
     * and validation URLs.
12
     *
13
     * @var string
14
     */
15
    protected $urlRegistrationEndPoint = 'mpesa/c2b/v1/registerurl';
16
17
    /**
18
     * The Safaricom C2B API end point for simulating a C2B transaction.
19
     *
20
     * @var string
21
     */
22
    protected $simulationEndpoint = 'mpesa/c2b/v1/simulate';
23
24
    /**
25
     * The Safaricom C2B API command ID.
26
     *
27
     * @var string
28
     */
29
    protected $commandID;
30
31
    /**
32
     * C2B constructor.
33
     */
34
    public function __construct()
35
    {
36
        parent::__construct();
37
    }
38
39
    /**
40
     * Register the confirmation and validation URLs to the Safaricom C2B API.
41
     *
42
     * @param  string  $confirmationUrl
43
     * @param  string  $validationUrl
44
     * @param  string  $responseType
45
     * @param  null|string|int  $shortCode
46
     * @return mixed
47
     */
48
    public function registerUrls($confirmationUrl, $validationUrl, $responseType = 'Completed', $shortCode = null)
49
    {
50
        $parameters = [
51
            'ShortCode' => is_null($shortCode) ? config('laravel-daraja.initiator.short_code') : $shortCode,
52
            'ResponseType' => $responseType,
53
            'ConfirmationURL' => $this->setUrl($confirmationUrl),
54
            'ValidationURL' => $this->setUrl($validationUrl),
55
        ];
56
57
        return $this->call($this->urlRegistrationEndPoint, ['json' => $parameters]);
58
    }
59
60
    /**
61
     * Set the command ID to be used for the transaction.
62
     *
63
     * @param  string  $commandId
64
     */
65
    public function setCommandId($commandId)
66
    {
67
        $this->commandID = $commandId;
68
    }
69
70
    /**
71
     * Simulate customer payment to a pay bill number through Safaricom C2B API.
72
     *
73
     * @param  string  $phoneNumber
74
     * @param  string  $amount
75
     * @param  string  $reference
76
     * @param  null|string  $shortCode
77
     * @return mixed
78
     */
79
    public function simulatePaymentToPaybill($phoneNumber, $amount, $reference, $shortCode = null)
80
    {
81
        $this->setCommandId('CustomerPayBillOnline');
82
83
        return $this->simulate($phoneNumber, $amount, $reference, $shortCode);
0 ignored issues
show
Bug introduced by
It seems like $shortCode can also be of type string; however, parameter $shortCode of Starnerz\LaravelDaraja\Requests\C2B::simulate() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

83
        return $this->simulate($phoneNumber, $amount, $reference, /** @scrutinizer ignore-type */ $shortCode);
Loading history...
84
    }
85
86
    /**
87
     * Simulate customer payment to a till number through Safaricom C2B API.
88
     *
89
     * @param  string  $phoneNumber
90
     * @param  string  $amount
91
     * @param  string  $reference
92
     * @param  null|string  $shortCode
93
     * @return mixed
94
     */
95
    public function simulatePaymentToTill($phoneNumber, $amount, $reference, $shortCode = null)
96
    {
97
        $this->setCommandId('CustomerBuyGoodsOnline');
98
99
        return $this->simulate($phoneNumber, $amount, $reference, $shortCode);
0 ignored issues
show
Bug introduced by
It seems like $shortCode can also be of type string; however, parameter $shortCode of Starnerz\LaravelDaraja\Requests\C2B::simulate() does only seem to accept null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

99
        return $this->simulate($phoneNumber, $amount, $reference, /** @scrutinizer ignore-type */ $shortCode);
Loading history...
100
    }
101
102
    /**
103
     * Send the transaction to be simulated to the Safaricom C2B API.
104
     *
105
     * @param $phoneNumber
106
     * @param $amount
107
     * @param $reference
108
     * @param  null  $shortCode
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $shortCode is correct as it would always require null to be passed?
Loading history...
109
     * @return mixed
110
     */
111
    protected function simulate($phoneNumber, $amount, $reference, $shortCode = null)
112
    {
113
        $parameters = [
114
            'ShortCode' => is_null($shortCode) ? config('laravel-daraja.initiator.short_code') : $shortCode,
0 ignored issues
show
introduced by
The condition is_null($shortCode) is always true.
Loading history...
115
            'CommandID' => $this->commandID,
116
            'Amount' => $amount,
117
            'Msisdn' => $phoneNumber,
118
            'BillRefNumber' => $reference,
119
        ];
120
121
        return $this->call($this->simulationEndpoint, ['json' => $parameters]);
122
    }
123
}
124