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

Transaction::getBySourceCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 17
rs 9.9332
cc 1
nc 1
nop 3
1
<?php
2
3
namespace Sebdesign\VivaPayments;
4
5
use GuzzleHttp\RequestOptions;
6
7
class Transaction
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
     * @see https://developer.vivawallet.com/online-checkouts/simple-checkout/#step-2-make-the-charge
109
     *
110
     * @param  array  $parameters
111
     * @param  array  $guzzleOptions Additional parameters for the Guzzle client
112
     * @return \stdClass
113
     */
114
    public function create(array $parameters, array $guzzleOptions = [])
115
    {
116
        return $this->client->post(
117
            $this->client->getUrl()->withPath('/api/transactions'),
118
            array_merge_recursive(
119
                [RequestOptions::JSON => $parameters],
120
                $this->client->authenticateWithBasicAuth(),
121
                $guzzleOptions
122
            )
123
        );
124
    }
125
126
    /**
127
     * Create a recurring transaction.
128
     *
129
     * This API call allows you to make a new payment by either committing
130
     * an already authorized transaction or by making a recurring payment.
131
     * The latter is only permitted if the following two conditions are met:
132
     *
133
     * - The cardholder has already been charged successfully in the past
134
     * - The cardholder has agreed to allow recurring payments on their card
135
     *
136
     * @see https://developer.vivawallet.com/api-reference-guide/payment-api/#tag/Transactions/paths/~1api~1transactions~1{Id}/post
137
     *
138
     * @param  string   $id            The transaction's unique ID
139
     * @param  int      $amount        The amount requested in the currency's smallest unit of measurement
140
     * @param  array    $parameters    Transaction parameters
141
     * @param  array    $guzzleOptions Additional parameters for the Guzzle client
142
     * @return \stdClass
143
     */
144
    public function createRecurring(
145
        string $id,
146
        int $amount,
147
        array $parameters = [],
148
        array $guzzleOptions = []
149
    ) {
150
        $parameters = array_merge(['amount' => $amount], $parameters);
151
152
        return $this->client->post(
153
            $this->client->getUrl()->withPath("/api/transactions/{$id}"),
154
            array_merge_recursive(
155
                [RequestOptions::JSON => $parameters],
156
                $this->client->authenticateWithBasicAuth(),
157
                $guzzleOptions
158
            )
159
        );
160
    }
161
162
    /**
163
     * Get details for a single transaction, identified by its transactionId.
164
     *
165
     * @see https://developer.vivawallet.com/api-reference-guide/payment-api/#tag/Transactions/paths/~1api~1transactions~1{Id}/get
166
     *
167
     * @param  string $id
168
     * @param  array  $guzzleOptions Additional parameters for the Guzzle client
169
     * @return array
170
     */
171
    public function get(string $id, array $guzzleOptions = []): array
172
    {
173
        $response = $this->client->get(
174
            $this->client->getUrl()->withPath("/api/transactions/{$id}"),
175
            array_merge_recursive(
176
                $this->client->authenticateWithBasicAuth(),
177
                $guzzleOptions
178
            )
179
        );
180
181
        return $response->Transactions;
182
    }
183
184
    /**
185
     * Get details for all transactions for a given payment order.
186
     *
187
     * @see https://developer.vivawallet.com/api-reference-guide/payment-api/#tag/Transactions/paths/~1api~1transactions~1{Id}/get
188
     *
189
     * @param  int   $ordercode
190
     * @param  array $guzzleOptions Additional parameters for the Guzzle client
191
     * @return array
192
     */
193
    public function getByOrder($ordercode, array $guzzleOptions = []): array
194
    {
195
        $parameters = ['ordercode' => $ordercode];
196
197
        $response = $this->client->get(
198
            $this->client->getUrl()->withPath('/api/transactions'),
199
            array_merge_recursive(
200
                [RequestOptions::QUERY => $parameters],
201
                $this->client->authenticateWithBasicAuth(),
202
                $guzzleOptions
203
            )
204
        );
205
206
        return $response->Transactions;
207
    }
208
209
    /**
210
     * List of all transactions that occurred on a given date.
211
     *
212
     * @see https://developer.vivawallet.com/api-reference-guide/payment-api/#tag/Transactions/paths/~1api~1transactions~1{Id}/get
213
     *
214
     * @param  \DateTimeInterface|string $date
215
     * @param  array                     $guzzleOptions Additional parameters for the Guzzle client
216
     * @return array
217
     */
218
    public function getByDate($date, array $guzzleOptions = []): array
219
    {
220
        $parameters = ['date' => $this->formatDate($date)];
221
222
        $response = $this->client->get(
223
            $this->client->getUrl()->withPath('/api/transactions'),
224
            array_merge_recursive(
225
                [RequestOptions::QUERY => $parameters],
226
                $this->client->authenticateWithBasicAuth(),
227
                $guzzleOptions
228
            )
229
        );
230
231
        return $response->Transactions;
232
    }
233
234
    /**
235
     * List of all transactions that occurred on a specific clearance date.
236
     *
237
     * @see https://developer.vivawallet.com/api-reference-guide/payment-api/#tag/Transactions/paths/~1api~1transactions~1{Id}/get
238
     *
239
     * @param  \DateTimeInterface|string $clearancedate
240
     * @param  array                     $guzzleOptions Additional parameters for the Guzzle client
241
     * @return array
242
     */
243
    public function getByClearanceDate($clearancedate, array $guzzleOptions = []): array
244
    {
245
        $parameters = ['clearancedate' => $this->formatDate($clearancedate)];
246
247
        $response = $this->client->get(
248
            $this->client->getUrl()->withPath('/api/transactions'),
249
            array_merge_recursive(
250
                [RequestOptions::QUERY => $parameters],
251
                $this->client->authenticateWithBasicAuth(),
252
                $guzzleOptions
253
            )
254
        );
255
256
        return $response->Transactions;
257
    }
258
259
    /**
260
     * List of all transactions for a given Source Code for a specific date.
261
     *
262
     * @see https://developer.vivawallet.com/api-reference-guide/payment-api/#tag/Transactions/paths/~1api~1transactions~1{Id}/get
263
     *
264
     * @param  string                    $sourcecode
265
     * @param  \DateTimeInterface|string $date
266
     * @param  array                     $guzzleOptions Additional parameters for the Guzzle client
267
     * @return array
268
     */
269
    public function getBySourceCode($sourcecode, $date, array $guzzleOptions = []): array
270
    {
271
        $parameters = [
272
            'sourcecode' => $sourcecode,
273
            'date' => $this->formatDate($date),
274
        ];
275
276
        $response = $this->client->get(
277
            $this->client->getUrl()->withPath('/api/transactions'),
278
            array_merge_recursive(
279
                [RequestOptions::QUERY => $parameters],
280
                $this->client->authenticateWithBasicAuth(),
281
                $guzzleOptions
282
            )
283
        );
284
285
        return $response->Transactions;
286
    }
287
288
    /**
289
     * Cancel or refund a payment.
290
     *
291
     * @see https://developer.vivawallet.com/api-reference-guide/payment-api/#tag/Transactions/paths/~1api~1transactions~1{Id}/delete
292
     *
293
     * @param  string       $id            The transaction's unique ID
294
     * @param  int          $amount        The amount that will be refunded in the currency's smallest denomination (e.g amount in pounds x 100)
295
     * @param  string|null  $sourceCode    The source from which the funds will be withdrawn. Each source is linked to a wallet. If no sourceCode is set then the funds will be withdrawn from the primary wallet.
296
     * @param  array        $guzzleOptions Additional parameters for the Guzzle client
297
     * @return \stdClass
298
     */
299
    public function cancel(
300
        string $id,
301
        int $amount,
302
        $sourceCode = null,
303
        array $guzzleOptions = []
304
    ) {
305
        $parameters = array_merge(
306
            ['amount' => $amount],
307
            $sourceCode ? ['sourceCode' => $sourceCode] : []
308
        );
309
310
        return $this->client->delete(
311
            $this->client->getUrl()->withPath("/api/transactions/{$id}"),
312
            array_merge_recursive(
313
                [RequestOptions::QUERY => $parameters],
314
                $this->client->authenticateWithBasicAuth(),
315
                $guzzleOptions
316
            )
317
        );
318
    }
319
320
    /**
321
     * Format a date object to string.
322
     *
323
     * @param  \DateTimeInterface|string $date
324
     * @return string
325
     */
326
    protected function formatDate($date): string
327
    {
328
        if ($date instanceof \DateTimeInterface) {
329
            return $date->format('Y-m-d');
330
        }
331
332
        return $date;
333
    }
334
}
335