Completed
Push — master ( ae1c3d...eb4059 )
by Sébastien
10:48
created

NativeCheckout::chargeToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 26
rs 9.7998
cc 1
nc 1
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
namespace Sebdesign\VivaPayments;
4
5
use GuzzleHttp\RequestOptions;
6
7
class NativeCheckout
8
{
9
    /**
10
     * @var \Sebdesign\VivaPayments\Client
11
     */
12
    protected $client;
13
14
    /**
15
     * Constructor.
16
     */
17
    public function __construct(Client $client)
18
    {
19
        $this->client = $client;
20
    }
21
22
    /**
23
     * Generate one-time charge token using card details.
24
     *
25
     * @link https://developer.vivawallet.com/api-reference-guide/card-tokenization-api/#step-1-generate-one-time-charge-token-using-card-details
26
     */
27
    public function chargeToken(
28
        int $amount,
29
        string $name,
30
        string $cardNumber,
31
        int $cvc,
32
        int $month,
33
        int $year,
34
        string $url,
35
        array $guzzleOptions = []
36
    ): \stdClass {
37
        $parameters = [
38
            'amount' => $amount,
39
            'cvc' => $cvc,
40
            'number' => $this->normalizeNumber($cardNumber),
41
            'holderName' => $name,
42
            'expirationYear' => $year,
43
            'expirationMonth' => $month,
44
            'sessionRedirectUrl' => $url,
45
        ];
46
47
        return $this->client->post(
48
            $this->client->getApiUrl()->withPath('/nativecheckout/v2/chargetokens'),
49
            array_merge_recursive(
50
                [RequestOptions::JSON => $parameters],
51
                $this->client->authenticateWithBearerToken(),
52
                $guzzleOptions
53
            )
54
        );
55
    }
56
57
    /**
58
     * Generate card token using the charge token.
59
     *
60
     * You can save the card token for future transactions by using the following call.
61
     *
62
     * @see https://developer.vivawallet.com/api-reference-guide/card-tokenization-api/#step-2-generate-card-token-using-the-charge-token-optional
63
     */
64
    public function cardToken(string $chargeToken, array $guzzleOptions = []): string
65
    {
66
        $parameters = ['chargetoken' => $chargeToken];
67
68
        $response = $this->client->get(
69
            $this->client->getApiUrl()->withPath('/acquiring/v1/cards/tokens'),
70
            array_merge_recursive(
71
                [RequestOptions::QUERY => $parameters],
72
                $this->client->authenticateWithBearerToken(),
73
                $guzzleOptions
74
            )
75
        );
76
77
        return $response->token;
78
    }
79
80
    /**
81
     * Generate charge token using the card token.
82
     *
83
     * Each time you want to charge a card you would generate a new charge token from the card token.
84
     *
85
     * @see https://developer.vivawallet.com/api-reference-guide/card-tokenization-api/#step-3-generate-one-time-charge-token-using-card-token-optional
86
     */
87
    public function chargeTokenUsingCardToken(string $cardToken, array $guzzleOptions = []): string
88
    {
89
        $parameters = ['token' => $cardToken];
90
91
        $response = $this->client->get(
92
            $this->client->getApiUrl()->withPath('/acquiring/v1/cards/tokens'),
93
            array_merge_recursive(
94
                [RequestOptions::QUERY => $parameters],
95
                $this->client->authenticateWithBearerToken(),
96
                $guzzleOptions
97
            )
98
        );
99
100
        return $response->token;
101
    }
102
103
    /**
104
     * Create transaction.
105
     *
106
     * After a successful call to chargetokens, you need to create a new transaction.
107
     *
108
     * @see https://developer.vivawallet.com/api-reference-guide/native-checkout-v2-api/#create-transaction
109
     */
110
    public function createTransaction(array $parameters, array $guzzleOptions = []): string
111
    {
112
        $response = $this->client->post(
113
            $this->client->getApiUrl()->withPath(
114
                '/nativecheckout/v2/transactions'
115
            ),
116
            array_merge_recursive(
117
                [RequestOptions::JSON => $parameters],
118
                $this->client->authenticateWithBearerToken(),
119
                $guzzleOptions
120
            )
121
        );
122
123
        return $response->transactionId;
124
    }
125
126
    /**
127
     * Capture a pre-auth.
128
     *
129
     * For cases where a pre-authorization is created instead of a charge,
130
     * a separate call to the transaction endpoint will be required.
131
     * Pass the amount of the pre-authorization, or a smaller amount, to create the charge.
132
     *
133
     * @see https://developer.vivawallet.com/api-reference-guide/native-checkout-v2-api/#capture-a-pre-auth
134
     */
135
    public function capturePreAuthTransaction(
136
        string $preauthTransactionId,
137
        int $amount,
138
        array $guzzleOptions = []
139
    ): string {
140
        $parameters = ['amount' => $amount];
141
142
        $response = $this->client->get(
143
            $this->client->getApiUrl()->withPath(
144
                "/nativecheckout/v2/transactions/{$preauthTransactionId}"
145
            ),
146
            array_merge_recursive(
147
                [RequestOptions::JSON => $parameters],
148
                $this->client->authenticateWithBearerToken(),
149
                $guzzleOptions
150
            )
151
        );
152
153
        return $response->transactionId;
154
    }
155
156
    /**
157
     * Check for installments.
158
     *
159
     * Pass the number as an HTTP header to retrieve the maximum number of installments allowed on a card.
160
     *
161
     * @see https://developer.vivawallet.com/api-reference-guide/native-checkout-v2-api/#check-for-installments
162
     */
163
    public function installments(string $cardNumber, array $guzzleOptions = []): int
164
    {
165
        $parameters = ['cardNumber' => $this->normalizeNumber($cardNumber)];
166
167
        $response = $this->client->get(
168
            $this->client->getApiUrl()->withPath('/nativecheckout/v2/installments'),
169
            array_merge_recursive(
170
                [RequestOptions::HEADERS => $parameters],
171
                $this->client->authenticateWithBearerToken(),
172
                $guzzleOptions
173
            )
174
        );
175
176
        return $response->maxInstallments;
177
    }
178
179
    /**
180
     * Strip non-numeric characters.
181
     */
182
    protected function normalizeNumber(string $cardNumber): string
183
    {
184
        return preg_replace('/\D/', '', $cardNumber);
185
    }
186
}
187