Completed
Push — master ( 084316...8aa23e )
by Sébastien
07:36
created

Transaction::formatDate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
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
     * @param \Sebdesign\VivaPayments\Client $client
101
     */
102
    public function __construct(Client $client)
103
    {
104
        $this->client = $client;
105
    }
106
107
    /**
108
     * Create a new transaction.
109
     *
110
     * @param  array   $parameters
111
     * @return object
112
     */
113
    public function create(array $parameters)
114
    {
115
        return $this->client->post(self::ENDPOINT, [
116
            \GuzzleHttp\RequestOptions::FORM_PARAMS => $parameters,
117
            \GuzzleHttp\RequestOptions::QUERY => [
118
                'key' => $this->getKey(),
119
            ],
120
        ]);
121
    }
122
123
    /**
124
     * Create a recurring transaction.
125
     *
126
     * @param  string   $id
127
     * @param  int      $amount
128
     * @param  array    $parameters
129
     * @return object
130
     */
131
    public function createRecurring($id, $amount, array $parameters = [])
132
    {
133
        return $this->client->post(self::ENDPOINT.$id, [
134
            \GuzzleHttp\RequestOptions::FORM_PARAMS => array_merge(['Amount' => $amount], $parameters),
135
        ]);
136
    }
137
138
    /**
139
     * Get the transactions for an order code, a transaction id, or a date.
140
     *
141
     * @param  mixed $id
142
     * @return array
143
     */
144
    public function get($id)
145
    {
146
        $response = $this->client->get(self::ENDPOINT.$id);
147
148
        return $response->Transactions;
149
    }
150
151
    /**
152
     * Get the transactions for an order code.
153
     *
154
     * @param  int $ordercode
155
     * @return array
156
     */
157 View Code Duplication
    public function getByOrder($ordercode)
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...
158
    {
159
        $response = $this->client->get(self::ENDPOINT, [
160
            \GuzzleHttp\RequestOptions::QUERY => compact('ordercode'),
161
        ]);
162
163
        return $response->Transactions;
164
    }
165
166
    /**
167
     * Get the transactions that occured on a given date.
168
     *
169
     * @param  \DateTimeInterface|string $date
170
     * @return array
171
     */
172 View Code Duplication
    public function getByDate($date)
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...
173
    {
174
        $date = $this->formatDate($date);
175
176
        $response = $this->client->get(self::ENDPOINT, [
177
            \GuzzleHttp\RequestOptions::QUERY => compact('date'),
178
        ]);
179
180
        return $response->Transactions;
181
    }
182
183
    /**
184
     * Get the transactions that were cleared on a given date.
185
     *
186
     * @param  \DateTimeInterface|string $date
0 ignored issues
show
Bug introduced by
There is no parameter named $date. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
187
     * @return array
188
     */
189 View Code Duplication
    public function getByClearanceDate($clearancedate)
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...
190
    {
191
        $clearancedate = $this->formatDate($clearancedate);
192
193
        $response = $this->client->get(self::ENDPOINT, [
194
            \GuzzleHttp\RequestOptions::QUERY => compact('clearancedate'),
195
        ]);
196
197
        return $response->Transactions;
198
    }
199
200
    /**
201
     * Format a date object to string.
202
     *
203
     * @param  \DateTimeInterface|string $date
204
     * @return string
205
     */
206
    protected function formatDate($date)
207
    {
208
        if ($date instanceof \DateTimeInterface) {
209
            return $date->format('Y-m-d');
210
        }
211
212
        return $date;
213
    }
214
215
    /**
216
     * Cancel or refund a payment.
217
     *
218
     * @param  string       $id
219
     * @param  int          $amount
220
     * @param  string|null  $actionUser
221
     * @return object
222
     */
223
    public function cancel($id, $amount, $actionUser = null)
224
    {
225
        $query = ['Amount' => $amount];
226
        $actionUser = $actionUser ? ['ActionUser' => $actionUser] : [];
227
228
        return $this->client->delete(self::ENDPOINT.$id, [
229
            \GuzzleHttp\RequestOptions::QUERY => array_merge($query, $actionUser),
230
        ]);
231
    }
232
233
    /**
234
     * Get the public key as query string.
235
     *
236
     * @return string
237
     */
238
    protected function getKey()
239
    {
240
        return config('services.viva.public_key');
241
    }
242
}
243