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