Simplify_PaymentsApi

Copyright (c) 2013 - 2015 MasterCard International Incorporated All rights reserved.

Defined (1)

The class is defined in the following location(s).

/includes/gateways/simplify-commerce/includes/Simplify/PaymentsApi.php  
  1. class Simplify_PaymentsApi 
  2.  
  3. /** 
  4. * @ignore 
  5. */ 
  6. public static $methodMap = array( 
  7. 'create' => 'POST',  
  8. 'delete' => 'DELETE',  
  9. 'list' => 'GET',  
  10. 'show' => 'GET',  
  11. 'update' => 'PUT' 
  12. ); 
  13.  
  14. /** 
  15. * @ignore 
  16. */ 
  17. static public function createObject($object, $authentication = null) 
  18. $paymentsApi = new Simplify_PaymentsApi(); 
  19.  
  20. $jsonObject = $paymentsApi->execute("create", $object, $authentication); 
  21.  
  22. $o = $paymentsApi->convertFromHashToObject($jsonObject, $object->getClazz()); 
  23.  
  24. return $o; 
  25.  
  26. /** 
  27. * @ignore 
  28. */ 
  29. static public function findObject($object, $authentication = null) 
  30. $paymentsApi = new Simplify_PaymentsApi(); 
  31.  
  32. $jsonObject = $paymentsApi->execute("show", $object, $authentication); 
  33. $o = $paymentsApi->convertFromHashToObject($jsonObject, $object->getClazz()); 
  34.  
  35. return $o; 
  36.  
  37. /** 
  38. * @ignore 
  39. */ 
  40. static public function updateObject($object, $authentication = null) { 
  41. $paymentsApi = new Simplify_PaymentsApi(); 
  42.  
  43. $jsonObject = $paymentsApi->execute("update", $object, $authentication); 
  44. $o = $paymentsApi->convertFromHashToObject($jsonObject, $object->getClazz()); 
  45.  
  46. return $o; 
  47.  
  48. /** 
  49. * @ignore 
  50. */ 
  51. static public function deleteObject($object, $authentication = null) { 
  52. $paymentsApi = new Simplify_PaymentsApi(); 
  53.  
  54. $jsonObject = $paymentsApi->execute("delete", $object, $authentication); 
  55.  
  56. return $jsonObject; 
  57.  
  58. /** 
  59. * @ignore 
  60. */ 
  61. static public function listObject($object, $criteria = null, $authentication =null) { 
  62. if ($criteria != null) { 
  63. if (isset($criteria['max'])) { 
  64. $object->max = $criteria['max']; 
  65. if (isset($criteria['offset'])) { 
  66. $object->offset = $criteria['offset']; 
  67. if (isset($criteria['sorting'])) { 
  68. $object->sorting = $criteria['sorting']; 
  69. if (isset($criteria['filter'])) { 
  70. $object->filter = $criteria['filter']; 
  71.  
  72. $paymentsApi = new Simplify_PaymentsApi(); 
  73. $jsonObject = $paymentsApi->execute("list", $object, $authentication); 
  74.  
  75. $ret = new Simplify_ResourceList(); 
  76. if (array_key_exists('list', $jsonObject) & is_array($jsonObject['list'])) { 
  77. foreach ($jsonObject['list'] as $obj) { 
  78. array_push($ret->list, $paymentsApi->convertFromHashToObject($obj, $object->getClazz())); 
  79. $ret->total = $jsonObject['total']; 
  80.  
  81. return $ret; 
  82.  
  83. /** 
  84. * @ignore 
  85. */ 
  86. public function convertFromHashToObject($from, $toClazz) 
  87. $clazz = 'stdClass'; 
  88. $toClazz = "Simplify_" . $toClazz; 
  89. if ("stdClass" != $toClazz && class_exists("{$toClazz}", false)) { 
  90. $clazz = "{$toClazz}"; 
  91. $object = new $clazz(); 
  92.  
  93. foreach ($from as $key => $value) { 
  94. if (is_array($value) && count(array_keys($value))) { 
  95. $newClazz = "Simplify_" . ucfirst($key); 
  96. if (!class_exists($newClazz, false)) { 
  97. $newClazz = 'stdClass'; 
  98.  
  99. $object->$key = $this->convertFromHashToObject($value, $newClazz); 
  100. } else { 
  101. $object->$key = $value; 
  102.  
  103. return $object; 
  104.  
  105. /** 
  106. * @ignore 
  107. */ 
  108. public function getUrl($publicKey, $action, $object) 
  109. $url = $this->fixUrl(Simplify::$apiBaseSandboxUrl); 
  110. if ($this->isLiveKey($publicKey)) { 
  111. $url = $this->fixUrl(Simplify::$apiBaseLiveUrl); 
  112. $url = $this->fixUrl($url) . urlencode(lcfirst($object->getClazz())) . '/'; 
  113.  
  114. $queryParams = array(); 
  115. if ($action == "show") { 
  116. $url .= urlencode($object->id); 
  117. } elseif ($action == "list") { 
  118. $queryParams = array_merge($queryParams, array('max' => $object->max, 'offset' => $object->offset)); 
  119. if (is_array($object->filter) && count(array_keys($object->filter))) { 
  120. foreach ($object->filter as $key => $value) { 
  121. $queryParams["filter[$key]"] = $value; 
  122. if (is_array($object->sorting) && count(array_keys($object->sorting))) { 
  123. foreach ($object->sorting as $key => $value) { 
  124. $queryParams["sorting[$key]"] = $value; 
  125. $query = http_build_query($queryParams); 
  126. if ($query != '') { 
  127. if (strpos($url, '?', strlen($url)) === false) $url .= '?'; 
  128. $url .= $query; 
  129.  
  130. } elseif ($action == "delete") { 
  131. $url .= urlencode($object->id); 
  132. } elseif ($action == "update") { 
  133. $url .= urlencode($object->id); 
  134. } elseif ($action == "create") { 
  135. return $url; 
  136.  
  137. /** 
  138. * @ignore 
  139. */ 
  140. public function getMethod($action) 
  141. if (array_key_exists(strtolower($action), self::$methodMap)) { 
  142. return self::$methodMap[strtolower($action)]; 
  143. return 'GET'; 
  144.  
  145. /** 
  146. * @ignore 
  147. */ 
  148. private function execute($action, $object, $authentication) 
  149. $http = new Simplify_HTTP(); 
  150.  
  151. return $http->apiRequest($this->getUrl($authentication->publicKey, $action, $object), $this->getMethod($action),  
  152. $authentication, json_encode($object->getProperties())); 
  153.  
  154. /** 
  155. * @ignore 
  156. */ 
  157. public function jwsDecode($hash, $authentication) 
  158. $http = new Simplify_HTTP(); 
  159.  
  160. $data = $http->jwsDecode($authentication, $hash); 
  161.  
  162. return json_decode($data, true); 
  163.  
  164. /** 
  165. * @ignore 
  166. */ 
  167. private function fixUrl($url) 
  168. if ($this->endsWith($url, '/')) { 
  169. return $url; 
  170. return $url . '/'; 
  171.  
  172. /** 
  173. * @ignore 
  174. */ 
  175. private function isLiveKey($k) { 
  176. return strpos($k, "lvpb") === 0; 
  177.  
  178. /** 
  179. * @ignore 
  180. */ 
  181. private function endsWith($s, $c) 
  182. return substr($s, -strlen($c)) == $c; 
  183.  
  184. /** 
  185. * Helper function to build the Authentication object for backwards compatibility. 
  186. * An array of all the arguments passed to one of the API functions is checked against what 
  187. * we expect to received. If it's greater, then we're assuming that the user is using the older way of 
  188. * passing the keys. i.e as two separate strings. We take those two string and create the Authentication object 
  189. * @ignore 
  190. * @param $authentication 
  191. * @param $args 
  192. * @param $expectedArgCount 
  193. * @return Simplify_Authentication 
  194. */ 
  195. static function buildAuthenticationObject($authentication = null, $args, $expectedArgCount) { 
  196.  
  197. if(sizeof($args) > $expectedArgCount) { 
  198. $authentication = new Simplify_Authentication($args[$expectedArgCount-1], $args[$expectedArgCount]); 
  199.  
  200. if ($authentication == null) { 
  201. $authentication = new Simplify_Authentication(); 
  202.  
  203. // check that the keys have been set, if not use the global keys 
  204. if ( empty($authentication->publicKey)) { 
  205. $authentication->publicKey = Simplify::$publicKey; 
  206. if ( empty($authentication->privateKey)) { 
  207. $authentication->privateKey = Simplify::$privateKey; 
  208.  
  209. return $authentication; 
  210.