-
Notifications
You must be signed in to change notification settings - Fork 7
/
smart_debit_includes.php
145 lines (117 loc) · 4.59 KB
/
smart_debit_includes.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
<?php
/**************************************************************************************************
* Smart Direct PHP Kit Includes File
***************************************************************************************************
***************************************************************************************************
* Change history
* ==============
*
* 10/08/2012 - Parvez Saleh - Created
*
***************************************************************************************************
* Description
* ===========
*
* Functions to allow communication with Smart Debit REST API's
***************************************************************************************************/
ob_start();
//session_start();
/**************************************************************************************************
* Useful functions for all pages in this kit
**************************************************************************************************/
//Function to redirect browser
function redirect($url)
{
if (!headers_sent())
header('Location: '.$url);
else
{
echo '<script type="text/javascript">';
echo 'window.location.href="'.$url.'";';
echo '</script>';
echo '<noscript>';
echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
echo '</noscript>';
}
}
/* Base 64 Encoding function **
** PHP does it natively but just for consistency and ease of maintenance, let's declare our own function **/
function base64Encode($plain) {
// Initialise output variable
$output = "";
// Do encoding
$output = base64_encode($plain);
// Return the result
return $output;
}
/* Base 64 decoding function **
** PHP does it natively but just for consistency and ease of maintenance, let's declare our own function **/
function base64Decode($scrambled) {
// Initialise output variable
$output = "";
// Fix plus to space conversion issue
$scrambled = str_replace(" ","+",$scrambled);
// Do encoding
$output = base64_decode($scrambled);
// Return the result
return $output;
}
// Function to check validity of email address entered in form fields
function is_valid_email($email) {
$result = TRUE;
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) {
$result = FALSE;
}
return $result;
}
/*************************************************************
Send a post request with cURL
$url = URL to send request to
$data = POST data to send (in URL encoded Key=value pairs)
*************************************************************/
function requestPost($url, $data, $username, $password, $path){
// Set a one-minute timeout for this script
set_time_limit(160);
// Initialise output variable
$output = array();
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_POST => true,
CURLOPT_USERPWD => $username . ':' . $password,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_HTTPHEADER => array("Accept: application/xml"),
CURLOPT_USERAGENT => "XYZ Co's PHP iDD Client", // Let SmartDebit see who we are
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_SSL_VERIFYPEER => false,
);
$session = curl_init( $url . $path );
curl_setopt_array( $session, $options );
// Tell curl that this is the body of the POST
curl_setopt ($session, CURLOPT_POSTFIELDS, $data);
// $output contains the output string
$output = curl_exec($session);
$header = curl_getinfo( $session );
//Store the raw response for later as it's useful to see for integration and understanding
$_SESSION["rawresponse"] = $output;
if(curl_errno($session)) {
$resultsArray["Status"] = "FAIL";
$resultsArray['StatusDetail'] = curl_error($session);
}
else {
// Results are XML so turn this into a PHP Array
$resultsArray = json_decode(json_encode((array) simplexml_load_string($output)),1);
// Determine if the call failed or not
switch ($header["http_code"]) {
case 200:
$resultsArray["Status"] = "OK";
break;
default:
$resultsArray["Status"] = "INVALID";
//echo "HTTP Error: " . $header["http_code"];
}
}
// Return the output
return $resultsArray;
} // END function requestPost()
?>