|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Samerior\MobileMoney\Mpesa\Library; |
|
4
|
|
|
|
|
5
|
|
|
use Samerior\MobileMoney\Mpesa\Exceptions\MpesaException; |
|
6
|
|
|
use GuzzleHttp\Exception\ServerException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class BulkSender |
|
10
|
|
|
* |
|
11
|
|
|
* @package Samerior\MobileMoney\Mpesa\Library |
|
12
|
|
|
*/ |
|
13
|
|
|
class BulkSender extends ApiCore |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
private $number; |
|
19
|
|
|
/** |
|
20
|
|
|
* @var int |
|
21
|
|
|
*/ |
|
22
|
|
|
private $amount; |
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
private $remarks = 'Some remarks'; |
|
27
|
|
|
/** |
|
28
|
|
|
* @var int |
|
29
|
|
|
*/ |
|
30
|
|
|
private $trials = 3; |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Set number to receive the funds |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $number |
|
37
|
|
|
* @return $this |
|
38
|
|
|
*/ |
|
39
|
|
|
public function to($number): self |
|
40
|
|
|
{ |
|
41
|
|
|
$this->number = $this->formatPhoneNumber($number); |
|
42
|
|
|
return $this; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function withRemarks($remarks): self |
|
46
|
|
|
{ |
|
47
|
|
|
$this->remarks = $remarks; |
|
48
|
|
|
return $this; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* The amount to transact |
|
53
|
|
|
* |
|
54
|
|
|
* @param $amount |
|
55
|
|
|
* @return $this |
|
56
|
|
|
*/ |
|
57
|
|
|
public function amount($amount): self |
|
58
|
|
|
{ |
|
59
|
|
|
$this->amount = $amount; |
|
60
|
|
|
return $this; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param string|null $number |
|
65
|
|
|
* @param int|null $amount |
|
66
|
|
|
* @param string|null $remarks |
|
67
|
|
|
* @return mixed |
|
68
|
|
|
* @throws \Samerior\MobileMoney\Mpesa\Exceptions\MpesaException |
|
69
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
70
|
|
|
* @throws MpesaException |
|
71
|
|
|
*/ |
|
72
|
|
|
public function send($number = null, $amount = null, $remarks = null) |
|
73
|
|
|
{ |
|
74
|
|
|
$body = [ |
|
75
|
|
|
'InitiatorName' => \config('samerior.mpesa.b2c.initiator'), |
|
76
|
|
|
'SecurityCredential' => \config('samerior.mpesa.b2c.security_credential'), |
|
77
|
|
|
'CommandID' => 'BusinessPayment', //SalaryPayment,BusinessPayment,PromotionPayment |
|
78
|
|
|
'Amount' => $amount ?: $this->amount, |
|
79
|
|
|
'PartyA' => \config('samerior.mpesa.b2c.short_code'), |
|
80
|
|
|
'PartyB' => $this->formatPhoneNumber($number ?: $this->number), |
|
81
|
|
|
'Remarks' => $remarks ?: $this->remarks, |
|
82
|
|
|
'QueueTimeOutURL' => \config('samerior.mpesa.b2c.timeout_url') . 'b2c', |
|
83
|
|
|
'ResultURL' => \config('samerior.mpesa.b2c.result_url') . 'b2c', |
|
84
|
|
|
'Occasion' => ' ' |
|
85
|
|
|
]; |
|
86
|
|
|
$this->bulk = true; |
|
87
|
|
|
try { |
|
88
|
|
|
$response = $this->sendRequest($body, 'b2c'); |
|
89
|
|
|
return $this->mpesaRepository->saveB2cRequest($response, $body); |
|
90
|
|
|
} catch (ServerException $exception) { //sometimes this endpoint behaves weird even for a nice request lets retry 1 atleast |
|
91
|
|
|
if ($this->trials > 0) { |
|
92
|
|
|
$this->trials--; |
|
93
|
|
|
return $this->send($number, $amount, $remarks); |
|
94
|
|
|
} |
|
95
|
|
|
throw new MpesaException('Server Error'); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return mixed |
|
101
|
|
|
* @throws \Samerior\MobileMoney\Mpesa\Exceptions\MpesaException |
|
102
|
|
|
* @throws \Exception |
|
103
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
|
104
|
|
|
*/ |
|
105
|
|
|
public function balance() |
|
106
|
|
|
{ |
|
107
|
|
|
$body = [ |
|
108
|
|
|
'CommandID' => 'AccountBalance', |
|
109
|
|
|
'Initiator' => \config('samerior.mpesa.bulk.initiator'), |
|
110
|
|
|
'SecurityCredential' => \config('samerior.mpesa.bulk.security_credential'), |
|
111
|
|
|
'PartyA' => \config('samerior.mpesa.bulk.short_code'), |
|
112
|
|
|
'IdentifierType' => 4, |
|
113
|
|
|
'Remarks' => 'Checking Balance', |
|
114
|
|
|
'QueueTimeOutURL' => \config('samerior.mpesa.bulk.timeout_url') . 'bulk_balance', |
|
115
|
|
|
'ResultURL' => \config('samerior.mpesa.bulk.result_url') . 'bulk_balance', |
|
116
|
|
|
]; |
|
117
|
|
|
$this->bulk = true; |
|
118
|
|
|
return $this->sendRequest($body, 'account_balance'); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|