-
Notifications
You must be signed in to change notification settings - Fork 1
/
daraja.php
131 lines (105 loc) · 4.44 KB
/
daraja.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
<?php
/*
M-Pesa Integration with PHP
Part 2: Advanced PHP Integration
Provided by Burst Digital
Learn more on our blog: https://burstdigital.co.ke/2023/10/23/integrating-m-pesa-into-your-website-using-php-step-by-step-guide-part-2/
Contact us at hello@burstdigital.co.ke or call 0708865088 for custom solutions.
lib/daraja.php: This library file simplifies M-Pesa integration by providing predefined functions and methods for working with M-Pesa transactions. It is an essential component of your PHP integration.
How to use:
1. Include this library in your PHP project to streamline your M-Pesa integration.
2. Use the provided functions and methods for initiating payments, verifying callbacks, and processing transactions.
For a detailed guide on integrating M-Pesa with PHP, please refer to our comprehensive blog post.
*/
class Daraja
{
private $consumerKey;
private $consumerSecret;
private $shortcode;
private $lipaNaMpesaOnlinePasskey;
private $lipaNaMpesaOnlineShortcode;
public function __construct($credentials)
{
$this->consumerKey = $credentials['consumerKey'];
$this->consumerSecret = $credentials['consumerSecret'];
$this->shortcode = $credentials['shortcode'];
$this->lipaNaMpesaOnlinePasskey = $credentials['lipaNaMpesaOnlinePasskey'];
$this->lipaNaMpesaOnlineShortcode = $credentials['lipaNaMpesaOnlineShortcode'];
}
/**
* Constructs and returns the payment request.
*
* @param array $transactionDetails Details of the payment transaction.
* @return array An array representing the payment request.
*/
public function lipaNaMpesaOnline($transactionDetails)
{
// Construct the payment request
$paymentRequest = [
'BusinessShortCode' => $this->shortcode,
'Amount' => $transactionDetails['Amount'],
'PartyA' => $transactionDetails['PartyA'],
'PartyB' => $this->shortcode,
'PhoneNumber' => $transactionDetails['PhoneNumber'],
'CallBackURL' => $transactionDetails['CallBackURL'],
'AccountReference' => $transactionDetails['AccountReference'],
'TransactionDesc' => $transactionDetails['TransactionDesc'],
'TransactionType' => $transactionDetails['TransactionType'],
];
return $paymentRequest;
}
/**
* Executes the M-Pesa payment request.
*
* @param array $paymentRequest An array representing the payment request.
* @return mixed The response from the payment request.
*/
public function execute($paymentRequest)
{
$url = 'https://api.safaricom.co.ke/mpesa/stkpush/v1/processrequest';
$headers = [
'Authorization: Basic ' . base64_encode($this->consumerKey . ':' . $this->consumerSecret),
'Content-Type: application/json',
];
$data = json_encode($paymentRequest);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if ($response === false) {
return false;
}
curl_close($ch);
return json_decode($response, true);
}
/**
* Verifies the authenticity of an M-Pesa callback.
*
* @param string $callbackData The raw callback data received from M-Pesa.
* @return bool True if the callback is valid, false otherwise.
*/
public function verifyCallback($callbackData)
{
$url = 'https://api.safaricom.co.ke/safaricom/identity/v1/checkidentity';
$headers = [
'Authorization: Basic ' . base64_encode($this->consumerKey . ':' . $this->consumerSecret),
'Content-Type: application/json',
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $callbackData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if ($response === false) {
return false;
}
curl_close($ch);
$result = json_decode($response, true);
// Check if the result indicates a valid callback
return isset($result['Valid']) && $result['Valid'] === true;
}