SettlementBatchSummaryGateway.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace Braintree;
  3. class SettlementBatchSummaryGateway
  4. {
  5. /**
  6. *
  7. * @var Gateway
  8. */
  9. private $_gateway;
  10. /**
  11. *
  12. * @var Configuration
  13. */
  14. private $_config;
  15. /**
  16. *
  17. * @var Http
  18. */
  19. private $_http;
  20. /**
  21. *
  22. * @param Gateway $gateway
  23. */
  24. public function __construct($gateway)
  25. {
  26. $this->_gateway = $gateway;
  27. $this->_config = $gateway->config;
  28. $this->_config->assertHasAccessTokenOrKeys();
  29. $this->_http = new Http($gateway->config);
  30. }
  31. /**
  32. *
  33. * @param string $settlement_date
  34. * @param string $groupByCustomField
  35. * @return SettlementBatchSummary|Result\Error
  36. */
  37. public function generate($settlement_date, $groupByCustomField = NULL)
  38. {
  39. $criteria = ['settlement_date' => $settlement_date];
  40. if (isset($groupByCustomField))
  41. {
  42. $criteria['group_by_custom_field'] = $groupByCustomField;
  43. }
  44. $params = ['settlement_batch_summary' => $criteria];
  45. $path = $this->_config->merchantPath() . '/settlement_batch_summary';
  46. $response = $this->_http->post($path, $params);
  47. if (isset($groupByCustomField))
  48. {
  49. $response['settlementBatchSummary']['records'] = $this->_underscoreCustomField(
  50. $groupByCustomField,
  51. $response['settlementBatchSummary']['records']
  52. );
  53. }
  54. return $this->_verifyGatewayResponse($response);
  55. }
  56. /**
  57. *
  58. * @param string $groupByCustomField
  59. * @param array $records
  60. * @return array
  61. */
  62. private function _underscoreCustomField($groupByCustomField, $records)
  63. {
  64. $updatedRecords = [];
  65. foreach ($records as $record)
  66. {
  67. $camelized = Util::delimiterToCamelCase($groupByCustomField);
  68. $record[$groupByCustomField] = $record[$camelized];
  69. unset($record[$camelized]);
  70. $updatedRecords[] = $record;
  71. }
  72. return $updatedRecords;
  73. }
  74. /**
  75. *
  76. * @param array $response
  77. * @return Result\Successful|Result\Error
  78. * @throws Exception\Unexpected
  79. */
  80. private function _verifyGatewayResponse($response)
  81. {
  82. if (isset($response['settlementBatchSummary'])) {
  83. return new Result\Successful(
  84. SettlementBatchSummary::factory($response['settlementBatchSummary'])
  85. );
  86. } else if (isset($response['apiErrorResponse'])) {
  87. return new Result\Error($response['apiErrorResponse']);
  88. } else {
  89. throw new Exception\Unexpected(
  90. "Expected settlementBatchSummary or apiErrorResponse"
  91. );
  92. }
  93. }
  94. }
  95. class_alias('Braintree\SettlementBatchSummaryGateway', 'Braintree_SettlementBatchSummaryGateway');