DisputeGateway.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. namespace Braintree;
  3. use InvalidArgumentException;
  4. /**
  5. * Braintree DisputeGateway module
  6. * PHP Version 5
  7. * Creates and manages Braintree Disputes
  8. *
  9. * @package Braintree
  10. */
  11. class DisputeGateway
  12. {
  13. /**
  14. * @var Gateway
  15. */
  16. private $_gateway;
  17. /**
  18. * @var Configuration
  19. */
  20. private $_config;
  21. /**
  22. * @var Http
  23. */
  24. private $_http;
  25. /**
  26. * @param Gateway $gateway
  27. */
  28. public function __construct($gateway)
  29. {
  30. $this->_gateway = $gateway;
  31. $this->_config = $gateway->config;
  32. $this->_config->assertHasAccessTokenOrKeys();
  33. $this->_http = new Http($gateway->config);
  34. }
  35. /* public class methods */
  36. /**
  37. * Accepts a dispute, given a dispute ID
  38. *
  39. * @param string $id
  40. */
  41. public function accept($id)
  42. {
  43. try {
  44. if (trim($id) == "") {
  45. throw new Exception\NotFound();
  46. }
  47. $path = $this->_config->merchantPath() . '/disputes/' . $id . '/accept';
  48. $response = $this->_http->put($path);
  49. if (isset($response['apiErrorResponse'])) {
  50. return new Result\Error($response['apiErrorResponse']);
  51. }
  52. return new Result\Successful();
  53. } catch (Exception\NotFound $e) {
  54. throw new Exception\NotFound('dispute with id "' . $id . '" not found');
  55. }
  56. }
  57. /**
  58. * Adds file evidence to a dispute, given a dispute ID and a document ID
  59. *
  60. * @param string $disputeId
  61. * @param string $documentIdOrRequest
  62. */
  63. public function addFileEvidence($disputeId, $documentIdOrRequest)
  64. {
  65. $request = is_array($documentIdOrRequest) ? $documentIdOrRequest : ['documentId' => $documentIdOrRequest];
  66. if (trim($disputeId) == "") {
  67. throw new Exception\NotFound('dispute with id "' . $disputeId . '" not found');
  68. }
  69. if (trim($request['documentId']) == "") {
  70. throw new Exception\NotFound('document with id "' . $request['documentId'] . '" not found');
  71. }
  72. try {
  73. if (array_key_exists('category', $request)) {
  74. if (trim($request['category']) == "") {
  75. throw new InvalidArgumentException('category cannot be blank');
  76. }
  77. }
  78. $request['document_upload_id'] = $request['documentId'];
  79. unset($request['documentId']);
  80. $path = $this->_config->merchantPath() . '/disputes/' . $disputeId . '/evidence';
  81. $response = $this->_http->post($path, ['evidence' => $request]);
  82. if (isset($response['apiErrorResponse'])) {
  83. return new Result\Error($response['apiErrorResponse']);
  84. }
  85. if (isset($response['evidence'])) {
  86. $evidence = new Dispute\EvidenceDetails($response['evidence']);
  87. return new Result\Successful($evidence);
  88. }
  89. } catch (Exception\NotFound $e) {
  90. throw new Exception\NotFound('dispute with id "' . $disputeId . '" not found');
  91. }
  92. }
  93. /**
  94. * Adds text evidence to a dispute, given a dispute ID and content
  95. *
  96. * @param string $id
  97. * @param string $content
  98. */
  99. public function addTextEvidence($id, $contentOrRequest)
  100. {
  101. $request = is_array($contentOrRequest) ? $contentOrRequest : ['content' => $contentOrRequest];
  102. if (trim($request['content']) == "") {
  103. throw new InvalidArgumentException('content cannot be blank');
  104. }
  105. try {
  106. $evidence = [
  107. 'comments' => $request['content'],
  108. ];
  109. if (trim($id) == "") {
  110. throw new Exception\NotFound();
  111. }
  112. if (array_key_exists('tag', $request)) {
  113. $evidence['category'] = $request['tag'];
  114. }
  115. if (array_key_exists('category', $request)) {
  116. if (trim($request['category']) == "") {
  117. throw new InvalidArgumentException('category cannot be blank');
  118. }
  119. $evidence['category'] = $request['category'];
  120. }
  121. if (array_key_exists('sequenceNumber', $request)) {
  122. if (trim($request['sequenceNumber']) == "") {
  123. throw new InvalidArgumentException('sequenceNumber cannot be blank');
  124. } else if ((string)(int)($request['sequenceNumber']) != $request['sequenceNumber']) {
  125. throw new InvalidArgumentException('sequenceNumber must be an integer');
  126. }
  127. $evidence['sequenceNumber'] = (int)$request['sequenceNumber'];
  128. }
  129. $path = $this->_config->merchantPath() . '/disputes/' . $id . '/evidence';
  130. $response = $this->_http->post($path, [
  131. 'evidence' => $evidence
  132. ]);
  133. if (isset($response['apiErrorResponse'])) {
  134. return new Result\Error($response['apiErrorResponse']);
  135. }
  136. if (isset($response['evidence'])) {
  137. $evidence = new Dispute\EvidenceDetails($response['evidence']);
  138. return new Result\Successful($evidence);
  139. }
  140. } catch (Exception\NotFound $e) {
  141. throw new Exception\NotFound('dispute with id "' . $id . '" not found');
  142. }
  143. }
  144. /**
  145. * Finalize a dispute, given a dispute ID
  146. *
  147. * @param string $id
  148. */
  149. public function finalize($id)
  150. {
  151. try {
  152. if (trim($id) == "") {
  153. throw new Exception\NotFound();
  154. }
  155. $path = $this->_config->merchantPath() . '/disputes/' . $id . '/finalize';
  156. $response = $this->_http->put($path);
  157. if (isset($response['apiErrorResponse'])) {
  158. return new Result\Error($response['apiErrorResponse']);
  159. }
  160. return new Result\Successful();
  161. } catch (Exception\NotFound $e) {
  162. throw new Exception\NotFound('dispute with id "' . $id . '" not found');
  163. }
  164. }
  165. /**
  166. * Find a dispute, given a dispute ID
  167. *
  168. * @param string $id
  169. */
  170. public function find($id)
  171. {
  172. if (trim($id) == "") {
  173. throw new Exception\NotFound('dispute with id "' . $id . '" not found');
  174. }
  175. try {
  176. $path = $this->_config->merchantPath() . '/disputes/' . $id;
  177. $response = $this->_http->get($path);
  178. return Dispute::factory($response['dispute']);
  179. } catch (Exception\NotFound $e) {
  180. throw new Exception\NotFound('dispute with id "' . $id . '" not found');
  181. }
  182. }
  183. /**
  184. * Remove evidence from a dispute, given a dispute ID and evidence ID
  185. *
  186. * @param string $disputeId
  187. * @param string $evidenceId
  188. */
  189. public function removeEvidence($disputeId, $evidenceId)
  190. {
  191. try {
  192. if (trim($disputeId) == "" || trim($evidenceId) == "") {
  193. throw new Exception\NotFound();
  194. }
  195. $path = $this->_config->merchantPath() . '/disputes/' . $disputeId . '/evidence/' . $evidenceId;
  196. $response = $this->_http->delete($path);
  197. if (isset($response['apiErrorResponse'])) {
  198. return new Result\Error($response['apiErrorResponse']);
  199. }
  200. return new Result\Successful();
  201. } catch (Exception\NotFound $e) {
  202. throw new Exception\NotFound('evidence with id "' . $evidenceId . '" for dispute with id "' . $disputeId . '" not found');
  203. }
  204. }
  205. /**
  206. * Search for Disputes, given a DisputeSearch query
  207. *
  208. * @param DisputeSearch $query
  209. */
  210. public function search($query)
  211. {
  212. $criteria = [];
  213. foreach ($query as $term) {
  214. $criteria[$term->name] = $term->toparam();
  215. }
  216. $pager = [
  217. 'object' => $this,
  218. 'method' => 'fetchDisputes',
  219. 'query' => $criteria
  220. ];
  221. return new PaginatedCollection($pager);
  222. }
  223. public function fetchDisputes($query, $page)
  224. {
  225. $response = $this->_http->post($this->_config->merchantPath() . '/disputes/advanced_search?page=' . $page, [
  226. 'search' => $query
  227. ]);
  228. $body = $response['disputes'];
  229. $disputes = Util::extractattributeasarray($body, 'dispute');
  230. $totalItems = $body['totalItems'][0];
  231. $pageSize = $body['pageSize'][0];
  232. return new PaginatedResult($totalItems, $pageSize, $disputes);
  233. }
  234. }
  235. class_alias('Braintree\DisputeGateway', 'Braintree_DisputeGateway');