PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/magazine/Libs/mercadopago.php

https://gitlab.com/mvcarvalho/plataforma-e-commerce
PHP | 323 lines | 176 code | 68 blank | 79 comment | 16 complexity | 89efee432ed000480527c8d173cdf128 MD5 | raw file
  1. <?php
  2. /**
  3. * MercadoPago Integration Library
  4. * Access MercadoPago for payments integration
  5. *
  6. * @author hcasatti
  7. *
  8. */
  9. $GLOBALS["LIB_LOCATION"] = dirname(__FILE__);
  10. class MP {
  11. const version = "0.2.4";
  12. private $client_id;
  13. private $client_secret;
  14. private $access_data;
  15. private $sandbox = FALSE;
  16. function __construct($client_id, $client_secret) {
  17. $this->client_id = $client_id;
  18. $this->client_secret = $client_secret;
  19. }
  20. public function sandbox_mode($enable = NULL) {
  21. if (!is_null($enable)) {
  22. $this->sandbox = $enable === TRUE;
  23. }
  24. return $this->sandbox;
  25. }
  26. /**
  27. * Get Access Token for API use
  28. */
  29. public function get_access_token() {
  30. $app_client_values = $this->build_query(array(
  31. 'client_id' => $this->client_id,
  32. 'client_secret' => $this->client_secret,
  33. 'grant_type' => 'client_credentials'
  34. ));
  35. $access_data = MPRestClient::post("/oauth/token", $app_client_values, "application/x-www-form-urlencoded");
  36. if ($access_data["status"] != 200) {
  37. throw new Exception ($access_data['response']['message'], $access_data['status']);
  38. }
  39. $this->access_data = $access_data['response'];
  40. return $this->access_data['access_token'];
  41. }
  42. /**
  43. * Get information for specific payment
  44. * @param int $id
  45. * @return array(json)
  46. */
  47. public function get_payment($id) {
  48. $access_token = $this->get_access_token();
  49. $uri_prefix = $this->sandbox ? "/sandbox" : "";
  50. $payment_info = MPRestClient::get($uri_prefix."/collections/notifications/" . $id . "?access_token=" . $access_token);
  51. return $payment_info;
  52. }
  53. public function get_payment_info($id) {
  54. return $this->get_payment($id);
  55. }
  56. /**
  57. * Get information for specific authorized payment
  58. * @param id
  59. * @return array(json)
  60. */
  61. public function get_authorized_payment($id) {
  62. $access_token = $this->get_access_token();
  63. $authorized_payment_info = MPRestClient::get("/authorized_payments/" . $id . "?access_token=" . $access_token);
  64. return $authorized_payment_info;
  65. }
  66. /**
  67. * Refund accredited payment
  68. * @param int $id
  69. * @return array(json)
  70. */
  71. public function refund_payment($id) {
  72. $access_token = $this->get_access_token();
  73. $refund_status = array(
  74. "status" => "refunded"
  75. );
  76. $response = MPRestClient::put("/collections/" . $id . "?access_token=" . $access_token, $refund_status);
  77. return $response;
  78. }
  79. /**
  80. * Cancel pending payment
  81. * @param int $id
  82. * @return array(json)
  83. */
  84. public function cancel_payment($id) {
  85. $access_token = $this->get_access_token();
  86. $cancel_status = array(
  87. "status" => "cancelled"
  88. );
  89. $response = MPRestClient::put("/collections/" . $id . "?access_token=" . $access_token, $cancel_status);
  90. return $response;
  91. }
  92. /**
  93. * Cancel preapproval payment
  94. * @param int $id
  95. * @return array(json)
  96. */
  97. public function cancel_preapproval_payment($id) {
  98. $access_token = $this->get_access_token();
  99. $cancel_status = array(
  100. "status" => "cancelled"
  101. );
  102. $response = MPRestClient::put("/preapproval/" . $id . "?access_token=" . $access_token, $cancel_status);
  103. return $response;
  104. }
  105. /**
  106. * Search payments according to filters, with pagination
  107. * @param array $filters
  108. * @param int $offset
  109. * @param int $limit
  110. * @return array(json)
  111. */
  112. public function search_payment($filters, $offset = 0, $limit = 0) {
  113. $access_token = $this->get_access_token();
  114. $filters["offset"] = $offset;
  115. $filters["limit"] = $limit;
  116. $filters = $this->build_query($filters);
  117. $uri_prefix = $this->sandbox ? "/sandbox" : "";
  118. $collection_result = MPRestClient::get($uri_prefix."/collections/search?" . $filters . "&access_token=" . $access_token);
  119. return $collection_result;
  120. }
  121. /**
  122. * Create a checkout preference
  123. * @param array $preference
  124. * @return array(json)
  125. */
  126. public function create_preference($preference) {
  127. $access_token = $this->get_access_token();
  128. $preference_result = MPRestClient::post("/checkout/preferences?access_token=" . $access_token, $preference);
  129. return $preference_result;
  130. }
  131. /**
  132. * Update a checkout preference
  133. * @param string $id
  134. * @param array $preference
  135. * @return array(json)
  136. */
  137. public function update_preference($id, $preference) {
  138. $access_token = $this->get_access_token();
  139. $preference_result = MPRestClient::put("/checkout/preferences/{$id}?access_token=" . $access_token, $preference);
  140. return $preference_result;
  141. }
  142. /**
  143. * Get a checkout preference
  144. * @param string $id
  145. * @return array(json)
  146. */
  147. public function get_preference($id) {
  148. $access_token = $this->get_access_token();
  149. $preference_result = MPRestClient::get("/checkout/preferences/{$id}?access_token=" . $access_token);
  150. return $preference_result;
  151. }
  152. /**
  153. * Create a preapproval payment
  154. * @param array $preapproval_payment
  155. * @return array(json)
  156. */
  157. public function create_preapproval_payment($preapproval_payment) {
  158. $access_token = $this->get_access_token();
  159. $preapproval_payment_result = MPRestClient::post("/preapproval?access_token=" . $access_token, $preapproval_payment);
  160. return $preapproval_payment_result;
  161. }
  162. /**
  163. * Get a preapproval payment
  164. * @param string $id
  165. * @return array(json)
  166. */
  167. public function get_preapproval_payment($id) {
  168. $access_token = $this->get_access_token();
  169. $preapproval_payment_result = MPRestClient::get("/preapproval/{$id}?access_token=" . $access_token);
  170. return $preapproval_payment_result;
  171. }
  172. /**
  173. * Update a preapproval payment
  174. * @param string $preapproval_payment, $id
  175. * @return array(json)
  176. */
  177. public function update_preapproval_payment($id, $preapproval_payment) {
  178. $access_token = $this->get_access_token();
  179. $preapproval_payment_result = MPRestClient::put("/preapproval/" . $id . "?access_token=" . $access_token, $preapproval_payment);
  180. return $preapproval_payment_result;
  181. }
  182. /* **************************************************************************************** */
  183. private function build_query($params) {
  184. if (function_exists("http_build_query")) {
  185. return http_build_query($params, "", "&");
  186. } else {
  187. foreach ($params as $name => $value) {
  188. $elements[] = "{$name}=" . urlencode($value);
  189. }
  190. return implode("&", $elements);
  191. }
  192. }
  193. }
  194. /**
  195. * MercadoPago cURL RestClient
  196. */
  197. class MPRestClient {
  198. const API_BASE_URL = "https://api.mercadolibre.com";
  199. private static function get_connect($uri, $method, $content_type) {
  200. if (!extension_loaded ("curl")) {
  201. throw new Exception("cURL extension not found. You need to enable cURL in your php.ini or another configuration you have.");
  202. }
  203. $connect = curl_init(self::API_BASE_URL . $uri);
  204. curl_setopt($connect, CURLOPT_USERAGENT, "MercadoPago PHP SDK v" . MP::version);
  205. //curl_setopt($connect, CURLOPT_CAINFO, $GLOBALS["LIB_LOCATION"] . "/cacert.pem");
  206. //curl_setopt($connect, CURLOPT_SSLVERSION, 3);
  207. curl_setopt($connect, CURLOPT_RETURNTRANSFER, true);
  208. curl_setopt($connect, CURLOPT_CUSTOMREQUEST, $method);
  209. curl_setopt($connect, CURLOPT_HTTPHEADER, array("Accept: application/json", "Content-Type: " . $content_type));
  210. return $connect;
  211. }
  212. private static function set_data($connect, $data, $content_type) {
  213. if ($content_type == "application/json") {
  214. if (gettype($data) == "string") {
  215. json_decode($data, true);
  216. } else {
  217. $data = json_encode($data);
  218. }
  219. if(function_exists('json_last_error')) {
  220. $json_error = json_last_error();
  221. if ($json_error != JSON_ERROR_NONE) {
  222. throw new Exception("JSON Error [{$json_error}] - Data: {$data}");
  223. }
  224. }
  225. }
  226. curl_setopt($connect, CURLOPT_POSTFIELDS, $data);
  227. }
  228. private static function exec($method, $uri, $data, $content_type) {
  229. $connect = self::get_connect($uri, $method, $content_type);
  230. if ($data) {
  231. self::set_data($connect, $data, $content_type);
  232. }
  233. $api_result = curl_exec($connect);
  234. $api_http_code = curl_getinfo($connect, CURLINFO_HTTP_CODE);
  235. $response = array(
  236. "status" => $api_http_code,
  237. "response" => json_decode($api_result, true)
  238. );
  239. if ($response['status'] >= 400) {
  240. throw new Exception ($response['response']['message'], $response['status']);
  241. }
  242. curl_close($connect);
  243. return $response;
  244. }
  245. public static function get($uri, $content_type = "application/json") {
  246. return self::exec("GET", $uri, null, $content_type);
  247. }
  248. public static function post($uri, $data, $content_type = "application/json") {
  249. return self::exec("POST", $uri, $data, $content_type);
  250. }
  251. public static function put($uri, $data, $content_type = "application/json") {
  252. return self::exec("PUT", $uri, $data, $content_type);
  253. }
  254. }
  255. ?>