Passed
Push — master ( c52187...71f110 )
by Sébastien
02:19
created

Transaction::cancel()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 17
ccs 10
cts 10
cp 1
rs 9.9666
cc 2
nc 1
nop 4
crap 2
1
<?php
2
3
namespace Sebdesign\VivaPayments;
4
5
use GuzzleHttp\RequestOptions;
6
use Sebdesign\VivaPayments\Requests\CreateRecurringTransaction;
7
8
class Transaction
9
{
10
    /**
11
     * Transaction types.
12
     */
13
14
    // A Capture event of a preAuthorized transaction
15
    const CAPTURE_FROM_PREAUTH = 0;
16
17
    // Authorization hold
18
    const PREAUTH = 1;
19
20
    // Refund transaction
21
    const REFUND_CARD = 4;
22
23
    // Card payment transaction
24
    const CHARGE_CARD = 5;
25
26
    // A card payment that will be done with installments
27
    const CHARGE_CARD_WITH_INSTALLMENTS = 6;
28
29
    // A payment cancelation
30
    const VOID = 7;
31
32
    // A Wallet Payment
33
    const WALLET_CHARGE = 9;
34
35
    // A Refund of a Wallet Payment
36
    const WALLET_REFUND = 11;
37
38
    // Refund transaction for a claimed transaction
39
    const CLAIM_REFUND = 13;
40
41
    // Payment made through the DIAS system
42
    const DIAS_PAYMENT = 15;
43
44
    // Cash Payments, through the Viva Payments Authorised Resellers Network
45
    const CASH_PAYMENT = 16;
46
47
    // A Refunded installment
48
    const REFUND_INSTALLMENTS = 18;
49
50
    // Clearance of a transactions batch
51
    const CLEARANCE = 19;
52
53
    // Bank Transfer command from the merchant's wallet to their IBAN
54
    const BANK_TRANSFER = 24;
55
56 5
    public function __construct(protected Client $client)
57
    {
58
    }
59
60
    /**
61
     * Retrieve transaction.
62
     *
63
     * @see https://developer.vivawallet.com/apis-for-payments/payment-api/#tag/Transactions/paths/~1checkout~1v2~1transactions~1{transactionId}/get
64
     *
65
     * @param  array<string,mixed>  $guzzleOptions  Additional parameters for the Guzzle client
66
     */
67 6
    public function retrieve(string $transactionId, array $guzzleOptions = []): Responses\Transaction
68
    {
69
        /** @phpstan-var TransactionArray */
70 6
        $response = $this->client->get(
71 6
            $this->client->getApiUrl()->withPath("/checkout/v2/transactions/{$transactionId}"),
72 6
            array_merge_recursive(
73 6
                $this->client->authenticateWithBearerToken(),
74
                $guzzleOptions,
75
            )
76
        );
77
78 3
        return Responses\Transaction::create($response);
79
    }
80
81
    /**
82
     * Create a recurring transaction.
83
     *
84
     * @see https://developer.vivawallet.com/apis-for-payments/payment-api/#tag/Transactions-(Deprecated)/paths/~1api~1transactions~1{transaction_id}/post
85
     *
86
     * @param  array<string,mixed>  $guzzleOptions  Additional parameters for the Guzzle client
87
     */
88 6
    public function createRecurring(
89
        string $transactionId,
90
        CreateRecurringTransaction $transaction,
91
        array $guzzleOptions = []
92
    ): Responses\RecurringTransaction {
93
        /** @phpstan-var RecurringTransactionArray */
94 6
        $response = $this->client->post(
95 6
            $this->client->getUrl()->withPath("/api/transactions/{$transactionId}"),
96 6
            array_merge_recursive(
97 2
                [RequestOptions::JSON => $transaction],
98 6
                $this->client->authenticateWithBasicAuth(),
99
                $guzzleOptions
100
            )
101
        );
102
103 3
        return Responses\RecurringTransaction::create($response);
104
    }
105
}
106