Completed
Push — 23 ( 2cbb94...e876bd )
by Harald
11:26 queued 05:21
created

MerchantAccountGateway::createForCurrency()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Braintree;
3
4
class MerchantAccountGateway
5
{
6
    private $_gateway;
7
    private $_config;
8
    private $_http;
9
10
    public function __construct($gateway)
11
    {
12
        $this->_gateway = $gateway;
13
        $this->_config = $gateway->config;
14
        $this->_config->assertHasAccessTokenOrKeys();
15
        $this->_http = new Http($gateway->config);
16
    }
17
18
    public function create($attribs)
19
    {
20
        Util::verifyKeys(self::detectSignature($attribs), $attribs);
21
        return $this->_doCreate('/merchant_accounts/create_via_api', ['merchant_account' => $attribs]);
22
    }
23
24 View Code Duplication
    public function find($merchant_account_id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
25
    {
26
        try {
27
            $path = $this->_config->merchantPath() . '/merchant_accounts/' . $merchant_account_id;
28
            $response = $this->_http->get($path);
29
            return MerchantAccount::factory($response['merchantAccount']);
30
        } catch (Exception\NotFound $e) {
31
            throw new Exception\NotFound('merchant account with id ' . $merchant_account_id . ' not found');
32
        }
33
    }
34
35
    public function update($merchant_account_id, $attributes)
36
    {
37
        Util::verifyKeys(self::updateSignature(), $attributes);
38
        return $this->_doUpdate('/merchant_accounts/' . $merchant_account_id . '/update_via_api', ['merchant_account' => $attributes]);
39
    }
40
41
    public static function detectSignature($attribs)
42
    {
43
        if (isset($attribs['applicantDetails'])) {
44
            trigger_error("DEPRECATED: Passing applicantDetails to create is deprecated. Please use individual, business, and funding", E_USER_NOTICE);
45
            return self::createDeprecatedSignature();
46
        } else {
47
            return self::createSignature();
48
        }
49
    }
50
51
    public static function updateSignature()
52
    {
53
        $signature = self::createSignature();
54
        unset($signature['tosAccepted']);
55
        return $signature;
56
    }
57
58
    public function createForCurrency($attribs)
59
    {
60
        $response = $this->_http->post($this->_config->merchantPath() . '/merchant_accounts/create_for_currency', ['merchant_account' => $attribs]);
61
        return $this->_verifyGatewayResponse($response);
62
    }
63
64
    public function all()
65
    {
66
        $pager = [
67
            'object' => $this,
68
            'method' => 'fetchMerchantAccounts',
69
        ];
70
        return new PaginatedCollection($pager);
71
    }
72
73
    public function fetchMerchantAccounts($page)
74
    {
75
        $response = $this->_http->get($this->_config->merchantPath() . '/merchant_accounts?page=' . $page);
76
        $body = $response['merchantAccounts'];
77
        $merchantAccounts = Util::extractattributeasarray($body, 'merchantAccount');
78
        $totalItems = $body['totalItems'][0];
79
        $pageSize = $body['pageSize'][0];
80
        return new PaginatedResult($totalItems, $pageSize, $merchantAccounts);
81
    }
82
83
    public static function createSignature()
84
    {
85
        $addressSignature = ['streetAddress', 'postalCode', 'locality', 'region'];
86
        $individualSignature = [
87
            'firstName',
88
            'lastName',
89
            'email',
90
            'phone',
91
            'dateOfBirth',
92
            'ssn',
93
            ['address' => $addressSignature]
94
        ];
95
96
        $businessSignature = [
97
            'dbaName',
98
            'legalName',
99
            'taxId',
100
            ['address' => $addressSignature]
101
        ];
102
103
        $fundingSignature = [
104
            'routingNumber',
105
            'accountNumber',
106
            'destination',
107
            'email',
108
            'mobilePhone',
109
            'descriptor',
110
        ];
111
112
        return [
113
            'id',
114
            'tosAccepted',
115
            'masterMerchantAccountId',
116
            ['individual' => $individualSignature],
117
            ['funding' => $fundingSignature],
118
            ['business' => $businessSignature]
119
        ];
120
    }
121
122
    public static function createDeprecatedSignature()
123
    {
124
        $applicantDetailsAddressSignature = ['streetAddress', 'postalCode', 'locality', 'region'];
125
        $applicantDetailsSignature = [
126
            'companyName',
127
            'firstName',
128
            'lastName',
129
            'email',
130
            'phone',
131
            'dateOfBirth',
132
            'ssn',
133
            'taxId',
134
            'routingNumber',
135
            'accountNumber',
136
            ['address' => $applicantDetailsAddressSignature]
137
        ];
138
139
        return [
140
            ['applicantDetails' =>  $applicantDetailsSignature],
141
            'id',
142
            'tosAccepted',
143
            'masterMerchantAccountId'
144
        ];
145
    }
146
147 View Code Duplication
    public function _doCreate($subPath, $params)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
148
    {
149
        $fullPath = $this->_config->merchantPath() . $subPath;
150
        $response = $this->_http->post($fullPath, $params);
151
152
        return $this->_verifyGatewayResponse($response);
153
    }
154
155 View Code Duplication
    private function _doUpdate($subPath, $params)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
156
    {
157
        $fullPath = $this->_config->merchantPath() . $subPath;
158
        $response = $this->_http->put($fullPath, $params);
159
160
        return $this->_verifyGatewayResponse($response);
161
    }
162
163
    private function _verifyGatewayResponse($response)
164
    {
165
        if (isset($response['response'])) {
166
            $response = $response['response'];
167
        }
168
        if (isset($response['merchantAccount'])) {
169
            // return a populated instance of merchantAccount
170
            return new Result\Successful(
171
                    MerchantAccount::factory($response['merchantAccount'])
0 ignored issues
show
Documentation introduced by
\Braintree\MerchantAccou...nse['merchantAccount']) is of type object<Braintree\MerchantAccount>, but the function expects a array|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
172
            );
173
        } else if (isset($response['apiErrorResponse'])) {
174
            return new Result\Error($response['apiErrorResponse']);
175
        } else {
176
            throw new Exception\Unexpected(
177
            "Expected merchant account or apiErrorResponse"
178
            );
179
        }
180
    }
181
}
182
class_alias('Braintree\MerchantAccountGateway', 'Braintree_MerchantAccountGateway');
183