-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy path_bootstrap.php
175 lines (144 loc) · 4.7 KB
/
_bootstrap.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
<?php
/*
* Copyright (c) 2016 Mastercard
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
error_reporting(E_ALL);
// pull environment vars
$merchantId = getenv('GATEWAY_MERCHANT_ID');
$password = getenv('GATEWAY_API_PASSWORD');
$region = getenv('GATEWAY_REGION');
$apiVersion = getenv('GATEWAY_API_VERSION');
// merchant id must be TEST
$merchantIdPrefix = substr($merchantId, 0, 4);
// if (strcasecmp($merchantIdPrefix, "test") != 0) {
// error(500, 'Only TEST merchant IDs should be used with this software');
// }
// get regional url prefix
$prefix = 'mtf';
if (strcasecmp($region, "ASIA_PACIFIC") == 0) {
$prefix = 'ap';
} else if (strcasecmp($region, "EUROPE") == 0) {
$prefix = 'eu';
} else if (strcasecmp($region, "NORTH_AMERICA") == 0) {
$prefix = 'na';
} else if (strcasecmp($region, "INDIA") == 0) {
$prefix = 'in';
} else if (strcasecmp($region, "CHINA") == 0) {
$prefix = 'cn';
} else if (strcasecmp($region, "MTF") == 0) {
$prefix = 'mtf';
} else if (strcasecmp($region, "QA01") == 0) {
$prefix = 'qa01';
} else if (strcasecmp($region, "QA02") == 0) {
$prefix = 'qa02';
} else if (strcasecmp($region, "QA03") == 0) {
$prefix = 'qa03';
} else if (strcasecmp($region, "QA04") == 0) {
$prefix = 'qa04';
} else if (strcasecmp($region, "QA05") == 0) {
$prefix = 'qa05';
} else if (strcasecmp($region, "QA06") == 0) {
$prefix = 'qa06';
} else if (strcasecmp($region, "PEAT") == 0) {
$prefix = 'perf';
} else {
error(500, "Invalid region provided. Valid values include ASIA_PACIFIC, EUROPE, NORTH_AMERICA, INDIA, MTF");
}
// validate apiVersion is above minimum
if (intval($apiVersion) < 39) {
error(500, "API Version must be >= 39");
}
// build api endpoint url
$gatewayUrl = "https://${prefix}.gateway.mastercard.com/api/rest/version/${apiVersion}/merchant/${merchantId}";
// parse query string
$query = array();
parse_str($_SERVER['QUERY_STRING'], $query);
// build auth headers
$headers = array(
'Content-type: application/json',
'Authorization: Basic ' . base64_encode("merchant.$merchantId:$password")
);
// construct page url
$pageUrl = "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
function intercept($method) {
return strcasecmp($_SERVER['REQUEST_METHOD'], $method) == 0;
}
function doRequest($url, $method, $data = null, $headers = null) {
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
if (!empty($data)) {
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
if (!empty($headers)) {
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
}
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
function doRedirect($url) {
header("Location: " . $url);
exit;
}
function error($code, $message) {
http_response_code($code);
print_r($message);
exit;
}
function requiredQueryParam($param) {
global $query;
if (!array_key_exists($param, $query) || empty($query[$param])) {
error(400, 'Missing required query param: ' . $param);
}
return $query[$param];
}
function getJsonPayload() {
$input = file_get_contents('php://input');
if (!empty($input)) {
json_decode($input);
if (json_last_error() !== JSON_ERROR_NONE) {
error(400, 'Could not parse json payload');
}
}
return $input;
}
function decodeResponse($response) {
$decoded = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
error(400, 'Could not decode json response from gateway');
}
return $decoded;
}
function outputJsonResponse($response) {
global $apiVersion;
header('Content-Type: application/json');
$decoded = decodeResponse($response);
$wrapped = array(
'apiVersion' => $apiVersion,
'gatewayResponse' => $decoded
);
print_r(json_encode($wrapped));
exit;
}
function proxyCall($path) {
global $headers, $gatewayUrl;
// get json payload from request
$payload = getJsonPayload();
// proxy authenticated request
$response = doRequest($gatewayUrl . $path, $_SERVER['REQUEST_METHOD'], $payload, $headers);
// output response
outputJsonResponse($response);
}