-
Notifications
You must be signed in to change notification settings - Fork 21
/
check_coupon.php
144 lines (120 loc) · 3.79 KB
/
check_coupon.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
<?php
/**
* Coupon Check
* JSON Response
*/
$api_id = '';
$secret_key = '';
$api_url = 'http://www.example.com/api/';
if (isset($_POST['coupon'])) {
$coupon = $_POST['coupon'];
}
elseif (isset($_GET['coupon'])) {
$coupon = $_GET['coupon'];
}
else {
$coupon = FALSE;
}
if (isset($_POST['plan_id'])) {
$plan_id = $_POST['plan_id'];
}
elseif (isset($_GET['plan_id'])) {
$plan_id = $_GET['plan_id'];
}
else {
$plan_id = FALSE;
}
if (isset($_POST['amount'])) {
$amount = $_POST['amount'];
}
elseif (isset($_GET['amount'])) {
$amount = $_GET['amount'];
}
else {
$amount = FALSE;
}
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<request>
<authentication>
<api_id>' . $api_id . '</api_id>
<secret_key>' . $secret_key . '</secret_key>
</authentication>
<type>CouponValidate</type>';
if ($coupon != FALSE) {
$xml .= '<coupon>' . $coupon . '</coupon>';
}
if ($plan_id != FALSE) {
$xml .= '<plan_id>' . $plan_id . '</plan_id>';
}
if ($amount != FALSE) {
$xml .= '<amount>' . $amount . '</amount>';
}
$xml .= '</request>';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
$data = curl_exec($ch);
$response = convert_to_array($data);
echo convert_to_json($response);
function convert_to_json ($arr) {
if(function_exists('json_encode')) return json_encode($arr); //Lastest versions of PHP already has this functionality.
$parts = array();
$is_list = false;
//Find out if the given array is a numerical array
$keys = array_keys($arr);
$max_length = count($arr)-1;
if(($keys[0] == 0) and ($keys[$max_length] == $max_length)) {//See if the first key is 0 and last key is length - 1
$is_list = true;
for($i=0; $i<count($keys); $i++) { //See if each key correspondes to its position
if($i != $keys[$i]) { //A key fails at position check.
$is_list = false; //It is an associative array.
break;
}
}
}
foreach($arr as $key=>$value) {
if(is_array($value)) { //Custom handling for arrays
if($is_list) $parts[] = array2json($value); /* :RECURSION: */
else $parts[] = '"' . $key . '":' . array2json($value); /* :RECURSION: */
} else {
$str = '';
if(!$is_list) $str = '"' . $key . '":';
//Custom handling for multiple data types
if(is_numeric($value)) $str .= $value; //Numbers
elseif($value === false) $str .= 'false'; //The booleans
elseif($value === true) $str .= 'true';
else $str .= '"' . addslashes($value) . '"'; //All other things
// :TODO: Is there any more datatype we should be in the lookout for? (Object?)
$parts[] = $str;
}
}
$json = implode(',',$parts);
if($is_list) return '[' . $json . ']';//Return numerical JSON
return '{' . $json . '}';//Return associative JSON
}
function convert_to_array ($xml) {
if (is_string($xml)) $xml = new SimpleXMLElement($xml);
$children = $xml->children();
if ( !$children ) return (string) $xml;
$arr = array();
foreach ($children as $key => $node) {
$node = convert_to_array($node);
// support for 'anon' non-associative arrays
if ($key == 'anon') {
$key = count($arr);
}
// if the node is already set, put it into an array
if (isset($arr[$key])) {
if (!is_array($arr[$key]) || !isset($arr[$key][0]) || $arr[$key][0] == null) {
$arr[$key] = array($arr[$key]);
}
$arr[$key][] = $node;
} else {
$arr[$key] = $node;
}
}
return $arr;
}