TransactionLineItemGateway.php
1.84 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
<?php
namespace Braintree;
use InvalidArgumentException;
/**
 * Braintree TransactionLineItemGateway processor
 * Creates and manages transaction line items
 *
 * @package    Braintree
 * @category   Resources
 */
class TransactionLineItemGateway
{
    private $_gateway;
    private $_config;
    private $_http;
    public function __construct($gateway)
    {
        $this->_gateway = $gateway;
        $this->_config = $gateway->config;
        $this->_config->assertHasAccessTokenOrKeys();
        $this->_http = new Http($gateway->config);
    }
    /**
     * @access public
     * @param string id
     * @return Transaction
     */
    public function findAll($id)
    {
        $this->_validateId($id);
        try {
            $path = $this->_config->merchantPath() . '/transactions/' . $id . '/line_items';
            $response = $this->_http->get($path);
            $lineItems = [];
            if (isset($response['lineItems'])) {
                foreach ($response['lineItems'] AS $lineItem) {
                    $lineItems[] = new TransactionLineItem($lineItem);
                }
            }
            return $lineItems;
        } catch (Exception\NotFound $e) {
            throw new Exception\NotFound('transaction line items with id ' . $id . ' not found');
        }
    }
    /**
     * verifies that a valid transaction id is being used
     * @ignore
     * @param string transaction id
     * @throws InvalidArgumentException
     */
    private function _validateId($id = null) {
        if (empty($id)) {
           throw new InvalidArgumentException('expected transaction id to be set');
        }
        if (!preg_match('/^[0-9a-z]+$/', $id)) {
            throw new InvalidArgumentException($id . ' is an invalid transaction id.');
        }
    }
}
class_alias('Braintree\TransactionLineItemGateway', 'Braintree_TransactionLineItemGateway');