-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMPesa.php
159 lines (141 loc) · 4.76 KB
/
MPesa.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
namespace CodeonWeekends\MPesa;
use CodeonWeekends\MPesa\Transactions\C2B;
use CodeonWeekends\MPesa\Transactions\Status;
/**
* Class MPesa
* @package Codeonweekends\MPesa
*/
class MPesa implements Interfaces\MPesaInterface
{
/**
* @var Context|NULL
*/
protected $apiContext;
/**
* @var mixed
*/
protected $config;
protected const TRANSACTION_STATUS_PORT = 18347;
protected const TRANSACTION_STATUS_PATH = '/ipg/v1/queryTxn/';
protected const TRANSACTION_REVERSAL_PORT = 18348;
protected const TRANSACTION_REVERSAL_PATH = '/ipg/v1/reversal/';
public function __construct ()
{
$this->config = require(__DIR__ . '/config.php');
$this->apiContext = new Context();
$this->apiContext->addHeader("Origin", "*");
$this->apiContext->setApiKey($this->config['api_key']);
$this->apiContext->setPublicKey($this->config['public_key']);
}
/**
* Retrieves a transaction status.
*
*
* @param string $queryReference
* @param string $thirdPartyReference
* @return mixed
* @throws \Exception
*/
public function transactionStatus ($thirdPartyReference = '', $queryReference = '')
{
$status = new Status($thirdPartyReference, $queryReference);
$status->setApiContext($this->apiContext);
$status->send();
return $status->getResponse();
}
/**
* Do a customer-to-business transaction.
*
* The default amount is set to 10 because the minimum transaction
* value for the m-pesa payments is 10.
*
* @param string $thirdPartyReference
* @param int $amount
* @param string $customerMSISDN
* @param string $serviceProviderCode
* @param string $transactionReference
* @return mixed
* @throws \Exception
*/
public function c2b ($transactionReference = '', $amount = 10, $customerMSISDN = '', $thirdPartyReference = '', $serviceProviderCode = null)
{
$serviceProviderCode ??= $this->config['service_provider_code'];
$c2b = new C2B($transactionReference, $amount, $customerMSISDN, $thirdPartyReference, $serviceProviderCode);
$c2b->setApiContext($this->apiContext);
$c2b->send();
return $c2b->getResponse();
}
/**
* Reverses a successful transaction
*
* The default amount is set to 10 because the minimum transaction
* value for the m-pesa payments is 10.
*
* @param int $amount
* @param string $serviceProviderCode
* @param string $transactionID
* @param string $securityCredential
* @param string $initiatorIdentifier
* @return mixed
* @throws \Exception
*/
public function transactionReversal ($amount = 10, $serviceProviderCode = '', $transactionID = '', $securityCredential = '', $initiatorIdentifier = '')
{
$context = $this->apiContext;
$context->setPort(self::TRANSACTION_REVERSAL_PORT);
$context->setPath(self::TRANSACTION_REVERSAL_PATH);
$context->setMethodType(MethodType::PUT);
$context->addParameter('input_Amount', $amount);
$context->addParameter('input_ServiceProviderCode', $serviceProviderCode);
$context->addParameter('input_TransactionID', $transactionID);
$context->addParameter('input_SecurityCredential', $securityCredential);
$context->addParameter('input_InitiatorIdentifier', $initiatorIdentifier);
$request = new Request($context);
$response = $request->execute();
return $response->getBody();
}
/**
* @return Context
*/
public function getApiContext(): Context
{
return $this->apiContext;
}
/**
* @param Context $apiContext
*/
public function setApiContext(Context $apiContext): void
{
$this->apiContext = $apiContext;
}
/**
*
* @param $transactionReference
* @param $CustomerMSISDN
* @param $amount
* @param $thirdPartyReference
* @param $serviceProviderCode
* @return mixed
*/
public function b2c($transactionReference, $CustomerMSISDN, $amount, $thirdPartyReference, $serviceProviderCode)
{
// TODO: Implement b2c() method.
}
/**
* The B2B API Call is used as a standard business-to-business transaction.
* Funds from the business’ mobile money wallet will be deducted and transferred
* to the mobile money wallet of the third party business.
*
* @param $transactionReference
* @param $amount
* @param $thirdPartyReference
* @param $primaryPartyCode
* @param $receiverPartyCode
* @return mixed
*/
public function b2b($transactionReference, $amount, $thirdPartyReference, $primaryPartyCode, $receiverPartyCode)
{
// TODO: Implement b2b() method.
}
}