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