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); |
|
|
|
|
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); |
|
|
|
|
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 |
|
|
|
|
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, |
|
|
|
|
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
|
|
|
|