starnerz /
laravel-daraja
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Starnerz\LaravelDaraja\Requests; |
||
| 4 | |||
| 5 | use Starnerz\LaravelDaraja\MpesaApiClient; |
||
| 6 | |||
| 7 | class B2C extends MpesaApiClient |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * Safaricom APIs B2C endpoint. |
||
| 11 | * |
||
| 12 | * @var string |
||
| 13 | */ |
||
| 14 | protected $endPoint = 'mpesa/b2c/v1/paymentrequest'; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Safaricom APIs B2C command id. |
||
| 18 | * |
||
| 19 | * @var string |
||
| 20 | */ |
||
| 21 | protected $commandId; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Safaricom APIs initiator short code username. |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $initiatorName; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Safaricom APIs B2C encrypted initiator short code password. |
||
| 32 | * |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $securityCredential; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Safaricom APIs B2C initiator short code. |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | protected $partyA; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Safaricom APIs B2C queue timeout URI. |
||
| 46 | * |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | protected $queueTimeOutURL; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Where the Safaricom B2C API will post the result of the transaction. |
||
| 53 | * |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | protected $resultURL; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Necessary initializations for B2C transactions from the config file while |
||
| 60 | * also initialize parent constructor. |
||
| 61 | */ |
||
| 62 | public function __construct() |
||
| 63 | { |
||
| 64 | parent::__construct(); |
||
| 65 | $this->initiatorName = config('laravel-daraja.initiator.name'); |
||
| 66 | $this->securityCredential = $this->securityCredential(config('laravel-daraja.initiator.credential')); |
||
| 67 | $this->partyA = config('laravel-daraja.initiator.short_code'); |
||
| 68 | |||
| 69 | $this->queueTimeOutURL = $this->setUrl(config('laravel-daraja.queue_timeout_url.b2c')); |
||
| 70 | $this->resultURL = $this->setUrl(config('laravel-daraja.result_url.b2c')); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Set the initiator short code credentials. |
||
| 75 | * |
||
| 76 | * @param string $name |
||
| 77 | * @param string $credential |
||
| 78 | */ |
||
| 79 | public function setInitiator($name, $credential) |
||
| 80 | { |
||
| 81 | $this->initiatorName = $name; |
||
| 82 | $this->securityCredential = $this->securityCredential($credential); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Set the command ID used by the Safaricom B2C API. |
||
| 87 | * |
||
| 88 | * @param string $command |
||
| 89 | */ |
||
| 90 | public function setCommandId($command) |
||
| 91 | { |
||
| 92 | $this->commandId = $command; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Set the short code to be used for B2C transaction. |
||
| 97 | * |
||
| 98 | * @param string $sender |
||
| 99 | */ |
||
| 100 | public function setSender($sender) |
||
| 101 | { |
||
| 102 | $this->partyA = $sender; |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Set the URI where Safaricom B2C API will send notification |
||
| 107 | * transaction timed out on queue. |
||
| 108 | * |
||
| 109 | * @param string $url |
||
| 110 | */ |
||
| 111 | public function setQueTimeoutUrl($url) |
||
| 112 | { |
||
| 113 | $this->queueTimeOutURL = $url; |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Set the URI where Safaricom B2C API will send result of the transaction. |
||
| 118 | * |
||
| 119 | * @param string $url |
||
| 120 | */ |
||
| 121 | public function setResultUrl($url) |
||
| 122 | { |
||
| 123 | $this->resultURL = $url; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Make a request to Safaricom B2C API with command type SalaryPayment. |
||
| 128 | * |
||
| 129 | * @param string $recipient |
||
| 130 | * @param int $amount |
||
| 131 | * @param string $remarks |
||
| 132 | * @param string $occasion |
||
| 133 | * @return mixed|string |
||
| 134 | */ |
||
| 135 | public function salaryPayment($recipient, $amount, $remarks, $occasion = '') |
||
| 136 | { |
||
| 137 | $this->setCommandId('SalaryPayment'); |
||
| 138 | |||
| 139 | return $this->pay($recipient, $amount, $remarks, $occasion); |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Make a request to Safaricom B2C API with command type BusinessPayment. |
||
| 144 | * |
||
| 145 | * @param string $recipient |
||
| 146 | * @param int $amount |
||
| 147 | * @param string $remarks |
||
| 148 | * @param string $occasion |
||
| 149 | * @return mixed|string |
||
| 150 | */ |
||
| 151 | public function businessPayment($recipient, $amount, $remarks, $occasion = '') |
||
| 152 | { |
||
| 153 | $this->setCommandId('BusinessPayment'); |
||
| 154 | |||
| 155 | return $this->pay($recipient, $amount, $remarks, $occasion); |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Make a request to Safaricom B2C API with command type PromotionPayment. |
||
| 160 | * |
||
| 161 | * @param string $recipient |
||
| 162 | * @param int $amount |
||
| 163 | * @param string $remarks |
||
| 164 | * @param string $occasion |
||
| 165 | * @return mixed|string |
||
| 166 | */ |
||
| 167 | public function promotionPayment($recipient, $amount, $remarks, $occasion = '') |
||
| 168 | { |
||
| 169 | $this->setCommandId('PromotionPayment'); |
||
| 170 | |||
| 171 | return $this->pay($recipient, $amount, $remarks, $occasion); |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Send transaction details to Safaricom B2C API. |
||
| 176 | * |
||
| 177 | * @param string $recipient |
||
| 178 | * @param int $amount |
||
| 179 | * @param string $remarks |
||
| 180 | * @param string $occasion |
||
| 181 | * @return mixed|string |
||
| 182 | */ |
||
| 183 | protected function pay($recipient, $amount, $remarks, $occasion = '') |
||
| 184 | { |
||
| 185 | $parameters = [ |
||
| 186 | 'InitiatorName' => $this->initiatorName, |
||
| 187 | 'SecurityCredential' => $this->securityCredential, |
||
| 188 | 'CommandID' => $this->commandId, |
||
| 189 | 'Amount' => $amount, |
||
| 190 | 'PartyA' => $this->partyA, |
||
| 191 | 'PartyB' => $recipient, |
||
| 192 | // As per the safaricom B2C API specification the $remarks can not be more than 100 characters |
||
| 193 | 'Remarks' => str_limit($remarks, 100, ''), |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 194 | 'QueueTimeOutURL' => $this->queueTimeOutURL, |
||
| 195 | 'ResultURL' => $this->resultURL, |
||
| 196 | 'Occassion' => $occasion, |
||
| 197 | ]; |
||
| 198 | |||
| 199 | return $response = $this->call($this->endPoint, ['json' => $parameters]); |
||
|
0 ignored issues
–
show
|
|||
| 200 | } |
||
| 201 | } |
||
| 202 |