-
Notifications
You must be signed in to change notification settings - Fork 43
/
processPayment.php
192 lines (171 loc) · 7.56 KB
/
processPayment.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
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
<?php
session_start() ;
// session_destroy();
// Prevent direct access to this class
define("BASEPATH", 1);
include('lib/rave.php');
include('lib/raveEventHandlerInterface.php');
use Flutterwave\Rave;
use Flutterwave\Rave\EventHandlerInterface;
$URL = (isset($_SERVER['HTTPS']) ? 'https' : 'http') . '://'.$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
$getData = $_GET;
$postData = $_POST;
$publicKey = $postData['publicKey'];
$secretKey = $postData['secretKey'];
$success_url = $postData['successurl'];
$failure_url = $postData['failureurl'];
$env = $postData['env']; // Remember to change this to 'live' when you are going live
if($postData['amount']){
$_SESSION['publicKey'] = $publicKey;
$_SESSION['secretKey'] = $secretKey;
$_SESSION['env'] = $env;
$_SESSION['successurl'] = $success_url;
$_SESSION['failureurl'] = $failure_url;
$_SESSION['currency'] = $postData['currency'];
$_SESSION['amount'] = $postData['amount'];
}
$prefix = 'RV'; // Change this to the name of your business or app
$overrideRef = false;
// Uncomment here to enforce the useage of your own ref else a ref will be generated for you automatically
if($postData['ref']){
$prefix = $postData['ref'];
$overrideRef = true;
}
$payment = new Rave($_SESSION['publicKey'], $_SESSION['secretKey'], $prefix, $_SESSION['env'], $overrideRef);
function getURL($url,$data = array()){
$urlArr = explode('?',$url);
$params = array_merge($_GET, $data);
$new_query_string = http_build_query($params).'&'.$urlArr[1];
$newUrl = $urlArr[0].'?'.$new_query_string;
return $newUrl;
};
// This is where you set how you want to handle the transaction at different stages
class myEventHandler implements EventHandlerInterface{
/**
* This is called when the Rave class is initialized
* */
function onInit($initializationData){
// Save the transaction to your DB.
}
/**
* This is called only when a transaction is successful
* */
function onSuccessful($transactionData){
// Get the transaction from your DB using the transaction reference (txref)
// Check if you have previously given value for the transaction. If you have, redirect to your successpage else, continue
// Comfirm that the transaction is successful
// Confirm that the chargecode is 00 or 0
// Confirm that the currency on your db transaction is equal to the returned currency
// Confirm that the db transaction amount is equal to the returned amount
// Update the db transaction record (includeing parameters that didn't exist before the transaction is completed. for audit purpose)
// Give value for the transaction
// Update the transaction to note that you have given value for the transaction
// You can also redirect to your success page from here
if($transactionData->chargecode === '00' || $transactionData->chargecode === '0'){
if($transactionData->currency == $_SESSION['currency'] && $transactionData->amount == $_SESSION['amount']){
if($_SESSION['publicKey']){
header('Location: '.getURL($_SESSION['successurl'], array('event' => 'successful')));
$_SESSION = array();
session_destroy();
}
}else{
if($_SESSION['publicKey']){
header('Location: '.getURL($_SESSION['failureurl'], array('event' => 'suspicious')));
$_SESSION = array();
session_destroy();
}
}
}else{
$this->onFailure($transactionData);
}
}
/**
* This is called only when a transaction failed
* */
function onFailure($transactionData){
// Get the transaction from your DB using the transaction reference (txref)
// Update the db transaction record (includeing parameters that didn't exist before the transaction is completed. for audit purpose)
// You can also redirect to your failure page from here
if($_SESSION['publicKey']){
header('Location: '.getURL($_SESSION['failureurl'], array('event' => 'failed')));
$_SESSION = array();
session_destroy();
}
}
/**
* This is called when a transaction is requeryed from the payment gateway
* */
function onRequery($transactionReference){
// Do something, anything!
}
/**
* This is called a transaction requery returns with an error
* */
function onRequeryError($requeryResponse){
// Do something, anything!
}
/**
* This is called when a transaction is canceled by the user
* */
function onCancel($transactionReference){
// Do something, anything!
// Note: Somethings a payment can be successful, before a user clicks the cancel button so proceed with caution
if($_SESSION['publicKey']){
header('Location: '.getURL($_SESSION['failureurl'], array('event' => 'canceled')));
$_SESSION = array();
session_destroy();
}
}
/**
* This is called when a transaction doesn't return with a success or a failure response. This can be a timedout transaction on the Rave server or an abandoned transaction by the customer.
* */
function onTimeout($transactionReference, $data){
// Get the transaction from your DB using the transaction reference (txref)
// Queue it for requery. Preferably using a queue system. The requery should be about 15 minutes after.
// Ask the customer to contact your support and you should escalate this issue to the flutterwave support team. Send this as an email and as a notification on the page. just incase the page timesout or disconnects
if($_SESSION['publicKey']){
header('Location: '.getURL($_SESSION['failureurl'], array('event' => 'timedout')));
$_SESSION = array();
session_destroy();
}
}
}
if($postData['amount']){
// Make payment
$payment
->eventHandler(new myEventHandler)
->setAmount($postData['amount'])
->setPaymentMethod($postData['payment_method']) // value can be card, account or both
->setDescription($postData['description'])
->setLogo($postData['logo'])
->setTitle($postData['title'])
->setCountry($postData['country'])
->setCurrency($postData['currency'])
->setEmail($postData['email'])
->setFirstname($postData['firstname'])
->setLastname($postData['lastname'])
->setPhoneNumber($postData['phonenumber'])
->setPayButtonText($postData['pay_button_text'])
->setRedirectUrl($URL)
// ->setMetaData(array('metaname' => 'SomeDataName', 'metavalue' => 'SomeValue')) // can be called multiple times. Uncomment this to add meta datas
// ->setMetaData(array('metaname' => 'SomeOtherDataName', 'metavalue' => 'SomeOtherValue')) // can be called multiple times. Uncomment this to add meta datas
->initialize();
}else{
if($getData['cancelled'] && $getData['txref']){
// Handle canceled payments
$payment
->eventHandler(new myEventHandler)
->requeryTransaction($getData['txref'])
->paymentCanceled($getData['txref']);
}elseif($getData['txref']){
// Handle completed payments
$payment->logger->notice('Payment completed. Now requerying payment.');
$payment
->eventHandler(new myEventHandler)
->requeryTransaction($getData['txref']);
}else{
$payment->logger->warn('Stop!!! Please pass the txref parameter!');
echo 'Stop!!! Please pass the txref parameter!';
}
}
?>