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
<?php
namespace AmazonPay;
/* Class HttpCurl
* Handles Curl POST function for all requests
*/
require_once 'HttpCurlInterface.php';
class HttpCurl implements HttpCurlInterface
{
private $config = array();
private $header = false;
private $accessToken = null;
private $curlResponseInfo = null;
private $headerArray = array();
/* Takes user configuration array as input
* Takes configuration for API call or IPN config
*/
public function __construct($config = null)
{
$this->config = $config;
}
/* Setter for boolean header to get the user info */
public function setHttpHeader()
{
$this->header = true;
}
/* Setter for Access token to get the user info */
public function setAccessToken($accesstoken)
{
$this->accessToken = $accesstoken;
}
/* Add the common Curl Parameters to the curl handler $ch
* Also checks for optional parameters if provided in the config
* config['cabundle_file']
* config['proxy_port']
* config['proxy_host']
* config['proxy_username']
* config['proxy_password']
*/
protected function commonCurlParams($url,$userAgent)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PORT, 443);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if (!is_null($this->config['cabundle_file'])) {
curl_setopt($ch, CURLOPT_CAINFO, $this->config['cabundle_file']);
}
if (!empty($userAgent))
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
if ($this->config['proxy_host'] != null && $this->config['proxy_port'] != -1) {
curl_setopt($ch, CURLOPT_PROXY, $this->config['proxy_host'] . ':' . $this->config['proxy_port']);
}
if ($this->config['proxy_username'] != null && $this->config['proxy_password'] != null) {
curl_setopt($ch, CURLOPT_PROXYUSERPWD, $this->config['proxy_username'] . ':' . $this->config['proxy_password']);
}
return $ch;
}
/* POST using curl for the following situations
* 1. API calls
* 2. IPN certificate retrieval
* 3. Get User Info
*/
public function httpPost($url, $userAgent = null, $parameters = null)
{
$ch = $this->commonCurlParams($url,$userAgent);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
curl_setopt($ch, CURLOPT_HEADER, false);
$response = $this->execute($ch);
return $response;
}
/* GET using curl for the following situations
* 1. IPN certificate retrieval
* 2. Get User Info
*/
public function httpGet($url, $userAgent = null)
{
$ch = $this->commonCurlParams($url,$userAgent);
// Setting the HTTP header with the Access Token only for Getting user info
if ($this->header) {
$this->headerArray[] = 'Authorization: bearer ' . $this->accessToken;
}
$response = $this->execute($ch);
return $response;
}
/* Execute Curl request */
/* Protected because will be used by PSP module */
protected function execute($ch)
{
$response = '';
// Ensure we never send the "Expect: 100-continue" header
$this->headerArray[] = 'Expect:';
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headerArray);
$response = curl_exec($ch);
if ($response === false) {
$error_msg = "Unable to post request, underlying exception of " . curl_error($ch);
curl_close($ch);
throw new \Exception($error_msg);
} else {
$this->curlResponseInfo = curl_getinfo($ch);
}
curl_close($ch);
return $response;
}
/* Get the output of Curl Getinfo */
public function getCurlResponseInfo()
{
return $this->curlResponseInfo;
}
}