1 | <?php |
||
2 | |||
3 | namespace Starnerz\LaravelDaraja\Requests; |
||
4 | |||
5 | use Starnerz\LaravelDaraja\MpesaApiClient; |
||
6 | |||
7 | class Reversal extends MpesaApiClient |
||
8 | { |
||
9 | /** |
||
10 | * The Safaricom Reversal API end point for reversing a MPESA transaction. |
||
11 | * |
||
12 | * @var string |
||
13 | */ |
||
14 | protected $reversalEndPoint = 'mpesa/reversal/v1/request'; |
||
15 | |||
16 | /** |
||
17 | * The Safaricom short code initiator name. |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | protected $initiatorName; |
||
22 | |||
23 | /** |
||
24 | * The encrypted initiator security credential. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $securityCredential; |
||
29 | |||
30 | /** |
||
31 | * The URL where Safaricom Reversal API will send result of the transaction. |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $resultURL; |
||
36 | |||
37 | /** |
||
38 | * The URL where Safaricom Reversal API will send notification of the transaction |
||
39 | * timing out while in the Safaricom servers queue. |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $queueTimeoutURL; |
||
44 | |||
45 | /** |
||
46 | * Reversal constructor. |
||
47 | */ |
||
48 | public function __construct() |
||
49 | { |
||
50 | parent::__construct(); |
||
51 | $this->initiatorName = config('laravel-daraja.initiator.name'); |
||
52 | $this->securityCredential = $this->securityCredential(config('laravel-daraja.initiator.credential')); |
||
53 | |||
54 | $this->resultURL = $this->setUrl(config('laravel-daraja.result_url.reversal')); |
||
55 | $this->queueTimeoutURL = $this->setUrl(config('laravel-daraja.queue_timeout_url.reversal')); |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Set initiator other than the one in the laravel-daraja config file. |
||
60 | * |
||
61 | * @param string $initiatorName |
||
62 | * @param string $securityCredential |
||
63 | */ |
||
64 | public function setInitiator($initiatorName, $securityCredential) |
||
65 | { |
||
66 | $this->initiatorName = $initiatorName; |
||
67 | $this->securityCredential = $this->securityCredential($securityCredential); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Set the url that will handle the timeout response from the |
||
72 | * MPESA Reversal API. |
||
73 | * |
||
74 | * @param string $url |
||
75 | */ |
||
76 | public function setQueueTimeoutURL($url) |
||
77 | { |
||
78 | $this->queueTimeoutURL = $url; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Set the url that will handle the result of the transaction |
||
83 | * from the MPESA Reversal API. |
||
84 | * |
||
85 | * @param string $url |
||
86 | */ |
||
87 | public function setResultURL($url) |
||
88 | { |
||
89 | $this->resultURL = $url; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Make a request to reverse a transaction to the Safaricom MPESA Reversal API. |
||
94 | * |
||
95 | * @param string $transactionId |
||
96 | * @param string $amount |
||
97 | * @param string $remarks |
||
98 | * @param null|string $shortCode |
||
99 | * @param string $occasion |
||
100 | * @return mixed |
||
101 | */ |
||
102 | public function reverse($transactionId, $amount, $remarks, $shortCode = null, $occasion = '') |
||
103 | { |
||
104 | $parameters = [ |
||
105 | 'Initiator' => $this->initiatorName, |
||
106 | 'SecurityCredential' => $this->securityCredential, |
||
107 | 'CommandID' => 'TransactionReversal', |
||
108 | 'TransactionID' => $transactionId, |
||
109 | 'Amount' => $amount, |
||
110 | 'ReceiverParty' => is_null($shortCode) ? config('laravel-daraja.initiator.short_code') : $shortCode, |
||
111 | 'RecieverIdentifierType' => '4', |
||
112 | 'ResultURL' => $this->resultURL, |
||
113 | 'QueueTimeoutURL' => $this->queueTimeoutURL, |
||
114 | 'Remarks' => str_limit($remarks, 100, ''), |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
115 | 'Occasion' => str_limit($occasion, 100, ''), |
||
116 | ]; |
||
117 | |||
118 | return $this->call($this->reversalEndPoint, ['json' => $parameters]); |
||
119 | } |
||
120 | } |
||
121 |