|
| 1 | +<?php namespace Icepay\API; |
| 2 | + |
| 3 | +/** |
| 4 | + * ICEPAY REST API for PHP |
| 5 | + * |
| 6 | + * @version 0.0.1 |
| 7 | + * @authors Ricardo Jacobs <ricardozegt@gmail.com> |
| 8 | + * @license BSD-2-Clause, see LICENSE.md |
| 9 | + * @copyright (c) 2015, ICEPAY B.V. All rights reserved. |
| 10 | + */ |
| 11 | + |
| 12 | +use Icepay\API\Resources\Payment; |
| 13 | +use Icepay\API\Resources\Refund; |
| 14 | + |
| 15 | +class Client |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @var $ch |
| 19 | + */ |
| 20 | + protected $ch; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var $api_key |
| 24 | + */ |
| 25 | + public $api_key; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var $api_secret |
| 29 | + */ |
| 30 | + public $api_secret; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var $api_completed_url |
| 34 | + */ |
| 35 | + public $api_completed_url; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var $api_error_url |
| 39 | + */ |
| 40 | + public $api_error_url; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var $api_endpoint string |
| 44 | + */ |
| 45 | + public $api_endpoint = 'https://connect.icepay.com/webservice/api/v1/'; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var $api_version string |
| 49 | + */ |
| 50 | + public $api_version = '0.0.1'; |
| 51 | + |
| 52 | + /** |
| 53 | + * Supported curl methods |
| 54 | + */ |
| 55 | + public $api_get = "GET"; |
| 56 | + public $api_post = "POST"; |
| 57 | + |
| 58 | + /** |
| 59 | + * Set the API Key and trim whitespaces |
| 60 | + * |
| 61 | + * @param $api_key |
| 62 | + */ |
| 63 | + public function setApiKey($api_key) |
| 64 | + { |
| 65 | + $this->api_key = trim($api_key); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Set the API Secret code and trim whitespaces |
| 70 | + * |
| 71 | + * @param $api_secret |
| 72 | + */ |
| 73 | + public function setApiSecret($api_secret) |
| 74 | + { |
| 75 | + $this->api_secret = trim($api_secret); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Set the completed url after a succesfull payment and trim whitespaces. |
| 80 | + * |
| 81 | + * @param $url |
| 82 | + */ |
| 83 | + public function setCompletedURL($url) |
| 84 | + { |
| 85 | + $this->api_completed_url = trim($url); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Set the error url after a error in the payment and trim whitespaces. |
| 90 | + * |
| 91 | + * @param $url |
| 92 | + */ |
| 93 | + public function setErrorURL($url) |
| 94 | + { |
| 95 | + $this->api_error_url = trim($url); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * API Constructor |
| 100 | + */ |
| 101 | + public function __construct() |
| 102 | + { |
| 103 | + $this->payment = new Payment($this); |
| 104 | + $this->refund = new Refund($this); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Request function to call our API Rest Payment Server |
| 109 | + * |
| 110 | + * @param $method |
| 111 | + * @param $api_method |
| 112 | + * @param $body |
| 113 | + * @param $checksum |
| 114 | + * |
| 115 | + * @return mixed |
| 116 | + * @throws \Exception |
| 117 | + */ |
| 118 | + public function request($method, $api_method, $body = NULL, $checksum) |
| 119 | + { |
| 120 | + /** |
| 121 | + * Check if the Merchant ID is set |
| 122 | + */ |
| 123 | + if (empty($this->api_key)) { |
| 124 | + throw new \Exception("Please configure your ICEPAY Merchant ID."); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Check if the Secret Code is set |
| 129 | + */ |
| 130 | + if (empty($this->api_secret)) { |
| 131 | + throw new \Exception("Please configure your ICEPAY Secret Code."); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Check if the CompletedURL is set |
| 136 | + */ |
| 137 | + if (empty($this->api_completed_url)) { |
| 138 | + throw new \Exception("Please configure your setCompletedURL()"); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Check if the ErrorURL is set |
| 143 | + */ |
| 144 | + if (empty($this->api_error_url)) { |
| 145 | + throw new \Exception("Please configure your setErrorURL()"); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Start a curl session |
| 150 | + */ |
| 151 | + if (empty($this->ch) || !function_exists("curl_reset")) { |
| 152 | + $this->ch = curl_init(); |
| 153 | + } else { |
| 154 | + curl_reset($this->ch); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Set the curl options |
| 159 | + */ |
| 160 | + curl_setopt($this->ch, CURLOPT_URL, $this->api_endpoint . $api_method); |
| 161 | + curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, TRUE); |
| 162 | + curl_setopt($this->ch, CURLOPT_TIMEOUT, 15); |
| 163 | + curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, $method); |
| 164 | + |
| 165 | + /** |
| 166 | + * Possible output: 5.6.9 |
| 167 | + */ |
| 168 | + $php_version = phpversion(); |
| 169 | + |
| 170 | + /** |
| 171 | + * Prepare the curl headers |
| 172 | + */ |
| 173 | + $api_headers = array( |
| 174 | + "MerchantID: {$this->api_key}", |
| 175 | + "Checksum: {$checksum}", |
| 176 | + "User-Agent: ICEPAY API/{$this->api_version} PHP/{$php_version}", |
| 177 | + "Accept: application/json" |
| 178 | + ); |
| 179 | + |
| 180 | + /** |
| 181 | + * If the body is not null, let curl post the request as json content |
| 182 | + */ |
| 183 | + if ($body !== NULL) { |
| 184 | + $api_headers[] = "Content-Type: application/json"; |
| 185 | + |
| 186 | + curl_setopt($this->ch, CURLOPT_POST, 1); |
| 187 | + curl_setopt($this->ch, CURLOPT_POSTFIELDS, json_encode($body)); |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * Set the curl headers for the payment server |
| 192 | + */ |
| 193 | + curl_setopt($this->ch, CURLOPT_HTTPHEADER, $api_headers); |
| 194 | + |
| 195 | + /** |
| 196 | + * Set the SSL options |
| 197 | + */ |
| 198 | + curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, 2); |
| 199 | + curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, TRUE); |
| 200 | + |
| 201 | + /** |
| 202 | + * Execute the request |
| 203 | + */ |
| 204 | + $response = curl_exec($this->ch); |
| 205 | + |
| 206 | + /** |
| 207 | + * Invalid or no certificate authority found, using bundled information |
| 208 | + */ |
| 209 | + if (curl_errno($this->ch) == 77 /* CURLE_SSL_CACERT_BADFILE */ || curl_errno($this->ch) == 60 /* CURLE_SSL_CACERT */) { |
| 210 | + curl_setopt($this->ch, CURLOPT_CAINFO, realpath(dirname(__FILE__) . '/Assets/icepay.pem')); |
| 211 | + |
| 212 | + $response = curl_exec($this->ch); |
| 213 | + } |
| 214 | + |
| 215 | + /** |
| 216 | + * Verifying peer certificate fails on OpenSSL 0.9 or earlier. |
| 217 | + * We done all we could to check the certificate on the host. |
| 218 | + * This webserver simply will not accept it and we need to connect. |
| 219 | + */ |
| 220 | + if (strpos(curl_error($this->ch), 'certificate subject name') !== FALSE) { |
| 221 | + curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, 0); |
| 222 | + |
| 223 | + $response = curl_exec($this->ch); |
| 224 | + } |
| 225 | + |
| 226 | + /** |
| 227 | + * Check if we got any error, if so, exception it |
| 228 | + */ |
| 229 | + if (curl_errno($this->ch)) { |
| 230 | + curl_close($this->ch); |
| 231 | + $this->ch = NULL; |
| 232 | + |
| 233 | + throw new \Exception('Unable to reach the ICEPAY payment server (' . curl_errno($this->ch) . '):' . curl_error($this->ch)); |
| 234 | + } |
| 235 | + |
| 236 | + /** |
| 237 | + * Close the connection |
| 238 | + */ |
| 239 | + if (!function_exists("curl_reset")) { |
| 240 | + curl_close($this->ch); |
| 241 | + $this->ch = null; |
| 242 | + } else { |
| 243 | + curl_reset($this->ch); |
| 244 | + $this->ch = null; |
| 245 | + } |
| 246 | + |
| 247 | + /** |
| 248 | + * Return the decoded json response |
| 249 | + */ |
| 250 | + return json_decode($response); |
| 251 | + } |
| 252 | + |
| 253 | + /** |
| 254 | + * Close resources if they are open |
| 255 | + */ |
| 256 | + public function __destruct() |
| 257 | + { |
| 258 | + if (is_resource($this->ch)) { |
| 259 | + curl_close($this->ch); |
| 260 | + } |
| 261 | + } |
| 262 | +} |
0 commit comments