Passed
Push — master ( 73f8f7...36e07c )
by Sébastien
15:06
created

Transaction::getKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Sebdesign\VivaPayments;
4
5
class Transaction
6
{
7
    const ENDPOINT = '/api/transactions/';
8
9
    /**
10
     * Transaction types.
11
     */
12
13
    // A Capture event of a preAuthorized transaction
14
    const CAPTURE_FROM_PREAUTH = 0;
15
16
    // Authorization hold
17
    const PREAUTH = 1;
18
19
    // Refund transaction
20
    const REFUND_CARD = 4;
21
22
    // Card payment transaction
23
    const CHARGE_CARD = 5;
24
25
    // A card payment that will be done with installments
26
    const CHARGE_CARD_WITH_INSTALLMENTS = 6;
27
28
    // A payment cancelation
29
    const VOID = 7;
30
31
    // A Wallet Payment
32
    const WALLET_CHARGE = 9;
33
34
    // A Refund of a Wallet Payment
35
    const WALLET_REFUND = 11;
36
37
    // Refund transaction for a claimed transaction
38
    const CLAIM_REFUND = 13;
39
40
    // Payment made through the DIAS system
41
    const DIAS_PAYMENT = 15;
42
43
    // Cash Payments, through the Viva Payments Authorised Resellers Network
44
    const CASH_PAYMENT = 16;
45
46
    // A Refunded installment
47
    const REFUND_INSTALLMENTS = 18;
48
49
    // Clearance of a transactions batch
50
    const CLEARANCE = 19;
51
52
    // Bank Transfer command from the merchant's wallet to their IBAN
53
    const BANK_TRANSFER = 24;
54
55
    /**
56
     * Transaction statuses.
57
     */
58
59
    // The transaction was not completed because of an error
60
    const ERROR = 'E';
61
62
    // The transaction is in progress
63
    const PROGRESS = 'A';
64
65
    // The cardholder has disputed the transaction with the issuing Bank
66
    const DISPUTED = 'M';
67
68
    // Dispute Awaiting Response
69
    const DISPUTE_AWAITING = 'MA';
70
71
    // Dispute in Progress
72
    const DISPUTE_IN_PROGRESS = 'MI';
73
74
    // A disputed transaction has been refunded (Dispute Lost)
75
    const DISPUTE_REFUNDED = 'ML';
76
77
    // Dispute Won
78
    const DISPUTE_WON = 'MW';
79
80
    // Suspected Dispute
81
    const DISPUTE_SUSPECTED = 'MS';
82
83
    // The transaction was cancelled by the merchant
84
    const CANCELED = 'X';
85
86
    // The transaction has been fully or partially refunded
87
    const REFUNDED = 'R';
88
89
    // The transaction has been completed successfully
90
    const COMPLETED = 'F';
91
92
    /**
93
     * @var \Sebdesign\VivaPayments\Client
94
     */
95
    protected $client;
96
97
    /**
98
     * Constructor.
99
     */
100
    public function __construct(Client $client)
101
    {
102
        $this->client = $client;
103
    }
104
105
    /**
106
     * Create a new transaction.
107
     *
108
     * @param  array  $parameters
109
     * @param  array  $guzzleOptions Additional parameters for the Guzzle client
110
     * @return \stdClass
111
     */
112
    public function create(array $parameters, array $guzzleOptions = [])
113
    {
114
        return $this->client->post(self::ENDPOINT, array_merge([
115
            \GuzzleHttp\RequestOptions::JSON => $parameters,
116
            \GuzzleHttp\RequestOptions::QUERY => [
117
                'key' => $this->client->getKey(),
118
            ],
119
        ], $guzzleOptions));
120
    }
121
122
    /**
123
     * Create a recurring transaction.
124
     *
125
     * @param  string   $id
126
     * @param  int      $amount
127
     * @param  array    $parameters
128
     * @param  array    $guzzleOptions Additional parameters for the Guzzle client
129
     * @return \stdClass
130
     */
131
    public function createRecurring(
132
        string $id,
133
        int $amount,
134
        array $parameters = [],
135
        array $guzzleOptions = []
136
    ) {
137
        $parameters = array_merge(['amount' => $amount], $parameters);
138
139
        return $this->client->post(self::ENDPOINT.$id, array_merge([
140
            \GuzzleHttp\RequestOptions::JSON => $parameters,
141
        ], $guzzleOptions));
142
    }
143
144
    /**
145
     * Get the transactions for an id.
146
     *
147
     * @param  string $id
148
     * @param  array  $guzzleOptions Additional parameters for the Guzzle client
149
     * @return array
150
     */
151
    public function get(string $id, array $guzzleOptions = []): array
152
    {
153
        $response = $this->client->get(self::ENDPOINT.$id, $guzzleOptions);
154
155
        return $response->Transactions;
156
    }
157
158
    /**
159
     * Get the transactions for an order code.
160
     *
161
     * @param  int   $ordercode
162
     * @param  array $guzzleOptions Additional parameters for the Guzzle client
163
     * @return array
164
     */
165
    public function getByOrder($ordercode, array $guzzleOptions = []): array
166
    {
167
        $parameters = ['ordercode' => $ordercode];
168
169
        $response = $this->client->get(self::ENDPOINT, array_merge([
170
            \GuzzleHttp\RequestOptions::QUERY => $parameters,
171
        ], $guzzleOptions));
172
173
        return $response->Transactions;
174
    }
175
176
    /**
177
     * Get the transactions that occured on a given date.
178
     *
179
     * @param  \DateTimeInterface|string $date
180
     * @param  array                     $guzzleOptions Additional parameters for the Guzzle client
181
     * @return array
182
     */
183
    public function getByDate($date, array $guzzleOptions = []): array
184
    {
185
        $parameters = ['date' => $this->formatDate($date)];
186
187
        $response = $this->client->get(self::ENDPOINT, array_merge([
188
            \GuzzleHttp\RequestOptions::QUERY => $parameters,
189
        ], $guzzleOptions));
190
191
        return $response->Transactions;
192
    }
193
194
    /**
195
     * Get the transactions that were cleared on a given date.
196
     *
197
     * @param  \DateTimeInterface|string $clearancedate
198
     * @param  array                     $guzzleOptions Additional parameters for the Guzzle client
199
     * @return array
200
     */
201
    public function getByClearanceDate($clearancedate, array $guzzleOptions = []): array
202
    {
203
        $parameters = ['clearancedate' => $this->formatDate($clearancedate)];
204
205
        $response = $this->client->get(self::ENDPOINT, array_merge([
206
            \GuzzleHttp\RequestOptions::QUERY => $parameters,
207
        ], $guzzleOptions));
208
209
        return $response->Transactions;
210
    }
211
212
    /**
213
     * Format a date object to string.
214
     *
215
     * @param  \DateTimeInterface|string $date
216
     * @return string
217
     */
218
    protected function formatDate($date): string
219
    {
220
        if ($date instanceof \DateTimeInterface) {
221
            return $date->format('Y-m-d');
222
        }
223
224
        return $date;
225
    }
226
227
    /**
228
     * Cancel or refund a payment.
229
     *
230
     * @param  string       $id
231
     * @param  int          $amount
232
     * @param  string|null  $actionUser
233
     * @param  array        $guzzleOptions Additional parameters for the Guzzle client
234
     * @return \stdClass
235
     */
236
    public function cancel(
237
        string $id,
238
        int $amount,
239
        $actionUser = null,
240
        array $guzzleOptions = []
241
    ) {
242
        $parameters = array_merge(
243
            ['amount' => $amount],
244
            $actionUser ? ['actionUser' => $actionUser] : []
245
        );
246
247
        return $this->client->delete(self::ENDPOINT.$id, array_merge([
248
            \GuzzleHttp\RequestOptions::QUERY => $parameters,
249
        ], $guzzleOptions));
250
    }
251
}
252