├── README.md
└── lipanampesa
├── Checkout.wsdl
├── callback.php
├── config
└── Constant.php
├── example.php
└── lib
└── MpesaAPI.php
/README.md:
--------------------------------------------------------------------------------
1 | # Mpesa Online Checkout API Library
2 |
3 | [](https://gitter.im/drizzentic/Mpesa-API-Library?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
4 |
5 |
6 | This library provide Online Checkout features for the Mpesa Api.
7 |
8 | ##Overview
9 | * [Features](#features)
10 |
11 |
12 |
--------------------------------------------------------------------------------
/lipanampesa/Checkout.wsdl:
--------------------------------------------------------------------------------
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 |
160 |
161 |
162 |
163 |
164 |
165 |
166 |
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
175 |
176 |
177 |
178 |
179 |
180 |
181 |
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 |
190 |
191 |
192 |
193 |
194 |
195 |
196 |
197 |
--------------------------------------------------------------------------------
/lipanampesa/callback.php:
--------------------------------------------------------------------------------
1 | getNamespaces(true);
11 | $soap = $xml->children($ns['SOAP-ENV']);
12 | $sbody = $soap->Body;
13 | $mpesa_response = $sbody->children($ns['ns1']);
14 | $rstatus = $mpesa_response->processCheckOutResponse;
15 | $status = $rstatus->children();
16 | $s_msisdn = $status->MSISDN;
17 | $s_date = $status->{'M-PESA_TRX_DATE'};
18 | $s_transactionid = $status->{'M-PESA_TRX_ID'};
19 | $s_status = $status->TRX_STATUS;
20 | $s_returncode = $status->RETURN_CODE;
21 | $s_description = $status->DESCRIPTION;
22 | $s_merchant_transaction_id = $status->MERCHANT_TRANSACTION_ID;
23 | $s_encparams = $status->ENC_PARAMS;
24 | $s_txID = $status->TRX_ID;
25 |
26 | //Save the returned data into the database or use it to finish certain operation.
27 |
28 | if($s_status=="Success"){
29 | //Perfomr X operation
30 |
31 | }else{
32 | //Perform X operation
33 | }
34 |
35 |
36 |
37 | ?>
--------------------------------------------------------------------------------
/lipanampesa/config/Constant.php:
--------------------------------------------------------------------------------
1 |
6 | */
7 |
8 | /**
9 | * Constants for authentication
10 | */
11 | define("MERCHANT_ID",''); //Put the unique merchant ID provided by the service provider.
12 | define("PASSKEY", ''); //Put the passkey provided for SAG access
13 | define("URL", "https://www.safaricom.co.ke/mpesa_online/lnmo_checkout_server.php?wsdl"); //Put the api endpint.
14 | define('Cert_location','');
15 | /**
16 | * Function to generate the password
17 | *
18 | */
19 | /**
20 | *
21 | */
22 | class Constant
23 | {
24 | public function generateHash(){
25 |
26 | $TIMESTAMP=new DateTime();
27 | $datetime=$TIMESTAMP->format('YmdHis');
28 | $password=base64_encode(hash("sha256", MERCHANT_ID.PASSKEY.$datetime));
29 |
30 | return $password;
31 | }
32 | }
33 |
34 |
35 | ?>
36 |
--------------------------------------------------------------------------------
/lipanampesa/example.php:
--------------------------------------------------------------------------------
1 |
6 | */
7 | require_once('config/Constant.php');
8 | require_once('lib/MpesaAPI.php');
9 |
10 |
11 |
12 | //Get the server address for callback
13 | $host=gethostname();
14 | $ip = gethostbyname($host);
15 |
16 | $Password=Constant::generateHash();
17 | $mpesaclient=new MpesaAPI;
18 |
19 | /**
20 | * Make payment
21 | */
22 | if($_GET['transactionType']=='checkout'){
23 | //Replace the data with relevant information
24 | $mpesaclient->processCheckOutRequest($Password,MERCHANT_ID,"XsdscRTysjssdkg","123454","10","254713038301",$ip);
25 | }
26 | else if ($_GET['transactionType']=='txStatus') {
27 | //Replace the data with relevant information
28 | $TXID=$_GET['txid'];
29 | $MERCHANT_TRANSACTION_ID=$_GET['mt_id'];
30 | $mpesaclient->statusRequest($Password,MERCHANT_ID,$TXID,$MERCHANT_TRANSACTION_ID);
31 | }
32 | else{
33 |
34 | echo json_encode("No operation selected")
35 | }
36 |
37 |
38 |
39 |
40 | ?>
41 |
--------------------------------------------------------------------------------
/lipanampesa/lib/MpesaAPI.php:
--------------------------------------------------------------------------------
1 |
7 | */
8 |
9 | /**
10 | * Class to allow access to lipa na mpesa online checkout
11 | */
12 | class MpesaApi
13 | {
14 |
15 | public function processCheckOutRequest($password,$MERCHANT_ID,$MERCHANT_TRANSACTION_ID,$REFERENCE_ID,$AMOUNT,$MSISDN,$CALL_BACK_URL){
16 | $TIMESTAMP=new DateTime();
17 | $datetime=$TIMESTAMP->format('YmdHis');
18 |
19 | $post_string='
20 |
21 |
22 | '.$MERCHANT_ID.'
23 | '.$password.'
24 | '.$datetime.'
25 |
26 |
27 |
28 |
29 | '.$MERCHANT_TRANSACTION_ID.'
30 | '.$REFERENCE_ID.'
31 | '.$AMOUNT.'
32 | '.$MSISDN.'
33 |
34 | '.$CALL_BACK_URL.'/callback.php
35 | xml
36 | '.$datetime.'
37 |
38 |
39 | ';
40 | /*
41 | Headers
42 | */
43 | $headers = array(
44 | "Content-type: text/xml",
45 | "Content-length: ".strlen($post_string),
46 | "Content-transfer-encoding: text",
47 | "SOAPAction: \"processCheckOutRequest\"",
48 | );
49 | /*
50 | To get the feedback from the process request system
51 | For debug purposes only
52 | */
53 | $response=$this->submitRequest(URL,$post_string,$headers);
54 | echo $response;
55 | /*
56 | To get the feedback from the process transaction system
57 | For debug purposes only
58 | */
59 | echo "Confirm transaction feedback: ".$this->confirmTransaction($response,$datetime,$password,$MERCHANT_ID);
60 |
61 | }
62 | /*
63 | The Merchant makes a SOAP call to the SAG to confirm an online checkout transaction
64 | */
65 | public function confirmTransaction($checkoutResponse,$datetime,$password,$MERCHANT_ID){
66 | $xml = simplexml_load_string($checkoutResponse);
67 | $ns = $xml->getNamespaces(true);
68 | $soap = $xml->children($ns['SOAP-ENV']);
69 | $sbody = $soap->Body;
70 | $mpesa_response = $sbody->children($ns['ns1']);
71 | $rstatus = $mpesa_response->processCheckOutResponse;
72 | $status = $rstatus->children();
73 | $s_returncode = $status->RETURN_CODE;
74 | $s_description = $status->DESCRIPTION;
75 | $s_transactionid = $status->TRX_ID;
76 | $s_enryptionparams = $status->ENC_PARAMS;
77 | $s_customer_message = $status->CUST_MSG;
78 | if($s_returncode==42){
79 |
80 | return json_encode("Authentication Failed",401);
81 | }
82 | $confirmTransactionResponse='
83 |
84 |
85 |
86 | '.$MERCHANT_ID.'
87 | '.$password.'
88 | '.$datetime.'
89 |
90 |
91 |
92 |
93 |
94 | '.$s_transactionid.'
95 |
96 |
97 |
98 |
99 | ';
100 |
101 | $headers = array(
102 | "Content-type: text/xml",
103 | "Content-length: ".strlen($confirmTransactionResponse),
104 | "Content-transfer-encoding: text",
105 | "SOAPAction: \"transactionConfirmRequest\"",
106 | );
107 |
108 | //Do whatever you want with the data. You can as well pass it as Xml data
109 | echo $this->submitRequest(URL,$confirmTransactionResponse,$headers);
110 |
111 | }
112 |
113 | public function statusRequest($Password,$MERCHANT_ID,$TXID,$MERCHANT_TRANSACTION_ID){
114 | $TIMESTAMP=new DateTime();
115 | $datetime=$TIMESTAMP->format('YmdHis');
116 |
117 | $post_string='
118 |
119 |
120 | '.$MERCHANT_ID.'
121 | '.$Password.'
122 | '.$datetime.'
123 |
124 |
125 |
126 |
127 |
128 | '.$TXID.'
129 |
130 | '.$MERCHANT_TRANSACTION_ID.'
131 |
132 |
133 | ';
134 |
135 | $headers = array(
136 | "Content-type: text/xml",
137 | "Content-length: ".strlen($post_string),
138 | "Content-transfer-encoding: text",
139 | "SOAPAction: \"transactionStatusRequest\"",
140 | );
141 |
142 | echo $this->submitRequest(URL,$post_string,$headers);
143 | }
144 |
145 | function submitRequest($url,$post_string,$headers){
146 | $ch = curl_init();
147 | curl_setopt($ch, CURLOPT_URL,$url);
148 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
149 | curl_setopt($ch, CURLOPT_TIMEOUT, 10);
150 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
151 | curl_setopt($ch, CURLOPT_POST,TRUE);
152 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
153 | curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
154 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
155 | curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
156 |
157 | $data = curl_exec($ch);
158 | if($data === FALSE)
159 | {
160 | $err = 'Curl error: ' . curl_error($ch);
161 | curl_close($ch);
162 | echo "Error \n".$err;
163 | }
164 | else
165 | {
166 | curl_close($ch);
167 | $body = $data;
168 |
169 | }
170 | return $body;
171 | }
172 |
173 | }
174 | ?>
175 |
--------------------------------------------------------------------------------