TransactionLineItemGateway.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace Braintree;
  3. use InvalidArgumentException;
  4. /**
  5. * Braintree TransactionLineItemGateway processor
  6. * Creates and manages transaction line items
  7. *
  8. * @package Braintree
  9. * @category Resources
  10. */
  11. class TransactionLineItemGateway
  12. {
  13. private $_gateway;
  14. private $_config;
  15. private $_http;
  16. public function __construct($gateway)
  17. {
  18. $this->_gateway = $gateway;
  19. $this->_config = $gateway->config;
  20. $this->_config->assertHasAccessTokenOrKeys();
  21. $this->_http = new Http($gateway->config);
  22. }
  23. /**
  24. * @access public
  25. * @param string id
  26. * @return Transaction
  27. */
  28. public function findAll($id)
  29. {
  30. $this->_validateId($id);
  31. try {
  32. $path = $this->_config->merchantPath() . '/transactions/' . $id . '/line_items';
  33. $response = $this->_http->get($path);
  34. $lineItems = [];
  35. if (isset($response['lineItems'])) {
  36. foreach ($response['lineItems'] AS $lineItem) {
  37. $lineItems[] = new TransactionLineItem($lineItem);
  38. }
  39. }
  40. return $lineItems;
  41. } catch (Exception\NotFound $e) {
  42. throw new Exception\NotFound('transaction line items with id ' . $id . ' not found');
  43. }
  44. }
  45. /**
  46. * verifies that a valid transaction id is being used
  47. * @ignore
  48. * @param string transaction id
  49. * @throws InvalidArgumentException
  50. */
  51. private function _validateId($id = null) {
  52. if (empty($id)) {
  53. throw new InvalidArgumentException('expected transaction id to be set');
  54. }
  55. if (!preg_match('/^[0-9a-z]+$/', $id)) {
  56. throw new InvalidArgumentException($id . ' is an invalid transaction id.');
  57. }
  58. }
  59. }
  60. class_alias('Braintree\TransactionLineItemGateway', 'Braintree_TransactionLineItemGateway');