OnlinePayment::update()   C
last analyzed

Complexity

Conditions 7
Paths 14

Size

Total Lines 35
Code Lines 20

Duplication

Lines 8
Ratio 22.86 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
eloc 20
nc 14
nop 1
dl 8
loc 35
rs 6.7272
c 0
b 0
f 0
ccs 0
cts 22
cp 0
crap 56
1
<?php
2
/**
3
 * Online payment
4
 *
5
 * Online payment is able to add payments to a debtor.
6
 *
7
 * @package Intraface_OnlinePayment
8
 */
9
class OnlinePayment extends Intraface_Standard
10
{
11
    public $id;
12
    public $kernel;
13
    protected $dbquery;
14
15
    protected $currency;
16
17
    /**
18
     * Standard transactions statuses from providers. Based on QuickPay
19
     * @var array
20
     */
21
    public $transaction_status_types = array(
22
        '' => 'Ingen kontakt til udbyder - mangler $eval',
23
        '000' => '', // Betalingsoplysninger godkendt
24
        '001' => 'Afvist af PBS',
25
        '002' => 'Kommunikationsfejl',
26
        '003' => 'Kort udløbet',
27
        '004' => 'Status er forkert (Ikke autoriseret)',
28
        '005' => 'Autorisation er forældet',
29
        '006' => 'Fejl hos PBS',
30
        '007' => 'Fejl hos udbyder',
31
        '008' => 'Fejl i parameter sendt til udbyder'
32
    );
33
34
    public $transaction_status_authorized = "000";
35
36
    public function __construct($kernel, $id = 0)
37
    {
38
        $this->kernel = $kernel;
39
        $this->id = $id;
40
        $this->error = new Intraface_Error;
0 ignored issues
show
Bug introduced by
The property error does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
41
42
        // @todo is this the proper place to get the provider key?
43
        $this->provider_key = $kernel->getSetting()->get('intranet', 'onlinepayment.provider_key');
0 ignored issues
show
Bug introduced by
The property provider_key does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
44
        $this->dbquery = $this->getDBQuery();
45
46
        if ($this->id > 0) {
47
            $this->load();
48
        } else {
49
            $this->value['id'] = 0;
50
        }
51
    }
52
53
    public static function factory($kernel, $type = 'settings', $value = 0)
54
    {
55
        $gateway = new Intraface_modules_onlinepayment_OnlinePaymentGateway($kernel);
56
57
        switch ($type) {
58
            case 'settings':
59
                return $gateway->findBySettings();
60
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
61
            case 'id':
62
                return $gateway->findById($value);
63
            case 'provider':
64
                return $gateway->findByProvider($value);
65
            case 'transactionnumber':
66
                return $gateway->findByTransactionNumber($value);
67
            default:
68
                throw new Exception('Ikke gyldig type i Onlinebetaling');
69
                break;
0 ignored issues
show
Unused Code introduced by
break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
70
        }
71
    }
72
73
    function load()
74
    {
75
        $db = new DB_Sql;
76
        $db->query("SELECT id, date_created, date_authorized, date_captured, date_reversed, belong_to_key, belong_to_id, text, status_key, amount, original_amount, transaction_number, transaction_status, pbs_status, currency_id,
77
                captured_in_currency_payment_exchange_rate_id,
78
                DATE_FORMAT(date_created, '%d-%m-%Y') AS dk_date_created,
79
                DATE_FORMAT(date_authorized, '%d-%m-%Y') AS dk_date_authorized,
80
                DATE_FORMAT(date_captured, '%d-%m-%Y') AS dk_date_captured,
81
                DATE_FORMAT(date_reversed, '%d-%m-%Y') AS dk_date_reversed
82
            FROM onlinepayment WHERE intranet_id = ".$this->kernel->intranet->get('id')." AND id = ".$this->id);
83
        if ($db->nextRecord()) {
84
            $this->value['id'] = $db->f('id');
85
            $this->value['dk_date_created'] = $db->f('dk_date_created');
86
            $this->value['date_created'] = $db->f('date_created');
87
88
            $this->value['dk_date_authorized'] = $db->f('dk_date_authorized');
89
            $this->value['date_authorized'] = $db->f('date_authorized');
90
91
            $this->value['dk_date_captured'] = $db->f('dk_date_captured');
92
            $this->value['date_captured'] = $db->f('date_captured');
93
94
            $this->value['dk_date_reversed'] = $db->f('dk_date_reversed');
95
            $this->value['date_reversed'] = $db->f('date_reversed');
96
97
            $this->value['belong_to_key'] = $db->f('belong_to_key');
98
            $belong_to_types = $this->getBelongToTypes();
99
            $this->value['belong_to'] = $belong_to_types[$db->f('belong_to_key')];
100
            $this->value['belong_to_id'] = $db->f('belong_to_id');
101
            $this->value['text'] = $db->f('text');
102
            $this->value['status_key'] = $db->f('status_key');
103
            $status_types = OnlinePayment::getStatusTypes();
104
            $this->value['status'] = $status_types[$db->f('status_key')];
105
            $this->value['amount'] = $db->f('amount');
106
            $this->value['dk_amount'] = number_format($db->f('amount'), 2, ",", ".");
107
            $this->value['currency_id'] = $db->f('currency_id');
108
            $this->value['captured_in_currency_payment_exchange_rate_id'] = $db->f('captured_in_currency_payment_exchange_rate_id');
109
110
            $this->value['original_amount'] = $db->f('original_amount');
111
            $this->value['dk_original_amount'] = number_format($db->f('original_amount'), 2, ",", ".");
112
113
            $this->value['transaction_number'] = $db->f('transaction_number');
114
            $this->value['transaction_status'] = $db->f('transaction_status');
115
            $this->value['pbs_status'] = $db->f('pbs_status');
116
            $this->value['transaction_status_translated'] = $this->transaction_status_types[$db->f('transaction_status')];
117
            if ($db->f('transaction_status') != $this->transaction_status_authorized) {
118
                $this->value['user_transaction_status_translated'] = $this->transaction_status_types[$db->f('transaction_status')];
119
            } else {
120
                $this->value['user_transaction_status_translated'] = "";
121
            }
122
            return $this->id;
123
        } else {
124
            $this->id = 0;
125
            $this->value['id'] = 0;
126
            return 0;
127
        }
128
    }
129
130
    /**
131
     * Saves online payment through the xmlrpc webservice
132
     *
133
     * @param array $input (belong_to, belong_to_id, transaction_number, transaction_status, amount)
134
     *
135
     * @return integer
136
      */
137
    public function save($input)
138
    {
139
        $input = safeToDb($input);
140
141
        if (!isset($input['belong_to'])) {
142
            $input['belong_to'] = 0;
143
        }
144
145
        if (!isset($input['belong_to_id'])) {
146
            $input['belong_to_id'] = 0;
147
        }
148
149
        if (!isset($input['transaction_number'])) {
150
            $input['transaction_number'] = 0;
151
        }
152
153
        if (!isset($input['transaction_status'])) {
154
            $input['transaction_status'] = '';
155
        }
156
157
        if (!isset($input['pbs_status'])) {
158
            $input['pbs_status'] = '';
159
        }
160
161
        if (!isset($input['text'])) {
162
            $input['text'] = '';
163
        }
164
165
        if ($input['transaction_status'] == $this->transaction_status_authorized) {
166
             $status_key = 2;
167
        } else {
168
            $status_key = 1;
169
        }
170
171
        if (!isset($input['amount'])) {
172
            $input['amount'] = 0;
173
        }
174
175
        $currency_id = 0;
176
        if (isset($input['currency']) && is_object($input['currency'])) {
177
            $currency_id = $input['currency']->getId();
178
        }
179
180
        $validator = new Intraface_Validator($this->error);
181
182
        $belong_to_key = array_search($input['belong_to'], $this->getBelongToTypes());
183
        if ($input['belong_to'] == '' || $belong_to_key === false) {
184
            $this->error->set("Ugyldig belong_to");
185
        }
186
187
        $validator->isNumeric($input['belong_to_id'], 'belong_to_id er ikke et tal');
188
        $validator->isNumeric($input['transaction_number'], 'transaction_number er ikke gyldig');
189
190
        $validator->isString($input['transaction_status'], 'transaction_status er ikke udfyldt');
191
192
        if (!isset($this->transaction_status_types[$input['transaction_status']])) {
193
            $this->error->set("transaction_status '".$input['transaction_status']."' er ikke en gyldig status");
194
        }
195
        $validator->isString($input['pbs_status'], 'pbs status er ikke udfyldt', '', 'allow_empty');
196
197
        $validator->isString($input['text'], 'text er ikke en gyldig streng', '', 'allow_empty');
198
199 View Code Duplication
        if ($validator->isDouble($input['amount'], 'amount er ikke et gyldigt beløb')) {
200
            $amount = new Intraface_Amount($input['amount']);
201
            if ($amount->convert2db()) {
202
                $input['amount'] = $amount->get();
203
            } else {
204
                $this->error->set("Kunne ikke konvertere amount til databasen!");
205
            }
206
        }
207
208
        if ($this->error->isError()) {
209
            return 0;
210
        }
211
212
        $sql = "date_changed = NOW(),
213
            status_key = ".$status_key.",
214
            belong_to_key = ".$belong_to_key.",
215
            belong_to_id = ".$input['belong_to_id'].",
216
            text = \"".$input['text']."\",
217
            transaction_number = ".$input['transaction_number'].",
218
            transaction_status = \"".$input['transaction_status']."\",
219
            pbs_status = \"".$input['pbs_status']."\",
220
            amount = ".$input['amount'].",
221
            provider_key = ".$this->provider_key.",
222
            original_amount = ".$input['amount'].",
223
            currency_id = ".$currency_id;
224
225
        $db = new DB_Sql;
226
227 View Code Duplication
        if ($this->id > 0) {
228
            $db->query("UPDATE onlinepayment SET ".$sql." WHERE intranet_id = ".$this->kernel->intranet->get('id')." AND id = ".$this->id);
229
        } else {
230
            $db->query("INSERT INTO onlinepayment SET ".$sql.",
231
                intranet_id = ".$this->kernel->intranet->get('id').",
232
                date_created = NOW()");
233
            $this->id = $db->insertedId();
234
        }
235
        $this->load();
236
237
        return $this->id;
238
    }
239
240
    /**
241
     * Creates an onlinepayment to be processed
242
     *
243
     * @return integer payment_id
244
     */
245
    public function create()
246
    {
247
        $provider_key = $this->kernel->getSetting()->get('intranet', 'onlinepayment.provider_key');
248
        $db = new DB_Sql;
249
250
        $db->query("INSERT INTO onlinepayment SET
251
            status_key = 1,
252
            intranet_id = ".$this->kernel->intranet->get('id').",
253
            date_created = NOW(),
254
            provider_key = ".$provider_key);
255
        return $db->insertedId();
256
    }
257
258
    /**
259
     * Updates payment from intraface
260
     *
261
     * @param array $input
262
     *
263
     * return integer
264
     */
265
    function update($input)
266
    {
267
        if ($this->id == 0) {
268
            throw new Exception("OnlinePayment->update kan kun køres på en allerede oprettet betaling");
269
        }
270
271
        if ($this->getStatus() != 'authorized') {
272
            throw new Exception("OnlinePayment->update kan kun køres på betaling der er authorized");
273
        }
274
275
        $input = safeToDb($input);
276
277
        $validator = new Intraface_Validator($this->error);
278
279 View Code Duplication
        if ($validator->isDouble($input['dk_amount'], 'Beløb er ikke et gyldigt beløb', 'greater_than_zero')) {
280
            $amount = new Intraface_Amount($input['dk_amount']);
281
            if ($amount->convert2db()) {
282
                $input['amount'] = $amount->get();
283
            } else {
284
                $this->error->set("Kunne ikke konvertere amount til databasen!");
285
            }
286
        }
287
288
        if ($input['amount'] > $this->get('original_amount')) {
289
            $this->error->set("Du kan ikke sætte beløbet højere end hvad kunden har godkendt: ".$this->get('dk_original_amount'));
290
        }
291
292
        if ($this->error->isError()) {
293
            return 0;
294
        }
295
296
        $db = new DB_Sql;
297
        $db->query("UPDATE onlinepayment SET amount = ".$input['amount'].", date_changed = NOW() WHERE intranet_id = ".$this->kernel->intranet->get('id')." AND id = ".$this->id);
298
        return $this->id;
299
    }
300
301
    function changeBelongTo($belong_to, $belong_to_id)
302
    {
303
        if ($this->id == 0) {
304
            throw new Exception("OnlinePayment->setBelongTo kan kun ændre eksisterende betalinger");
305
        }
306
307
        $belong_to = safeToDb($belong_to);
308
309
        $belong_to_key = array_search($belong_to, $this->getBelongToTypes());
310
        if ($belong_to == '' || $belong_to_key === false) {
311
            throw new Exception("Ugyldig belong_to i OnlinePayment->changeBelongTo()");
312
        }
313
314
        if (!is_int($belong_to_id)) {
315
            throw new Exception("Belong_to_id er ikke et tal i OnlinePayment->changeBelongTo()");
316
        }
317
318
        $db = new DB_Sql;
319
        $db->query("UPDATE onlinepayment SET belong_to_key = ".$belong_to_key.", belong_to_id = ".$belong_to_id." WHERE intranet_id = ".$this->kernel->intranet->get('id')." AND id = ".$this->id);
320
        return $this->id;
321
    }
322
323
    function setStatus($status)
324
    {
325
        if ($this->id == 0) {
326
            throw new Exception("OnlinePayment->setStatus kan kun ændre eksisterende betalinger");
327
        }
328
        $status = safeToDb($status);
329
330
331
        $status_key = array_search($status, OnlinePayment::getStatusTypes());
332
        if ($status == "" || $status_key === false) {
333
            throw new Exception("Ugyldig status i OnlinePayment->setStatus()");
334
        }
335
336
        if ($status_key <= $this->get('status_key')) {
337
            throw new Exception("Kan ikke skifte til lavere eller samme status i OnlinePayment->setStatus()");
338
        }
339
340
        switch ($status) {
341
            case "authorized":
342
                $date_field = "date_authorized";
343
                break;
344
            case "captured":
345
                $date_field = "date_captured";
346
                break;
347
            case "reversed":
348
                $date_field = "date_reversed";
349
                break;
350
            case "cancelled":
351
                $date_field = "date_cancelled";
352
                break;
353
        }
354
355
        $db = new DB_Sql;
356
357
        $db->query("UPDATE onlinepayment SET status_key = ".$status_key.", ".$date_field." = NOW() WHERE intranet_id = ".$this->kernel->intranet->get('id')." AND id = ".$this->id);
0 ignored issues
show
Bug introduced by
The variable $date_field does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
358
359
        $this->value['status_key'] = $status_key;
360
361
        return true;
362
    }
363
364
    /**
365
     * Adds onlinepayment to invoice
366
     */
367
    function addAsPayment()
368
    {
369
        if ($this->get('status') != 'authorized') {
370
            $this->error->set("Der kan kun udføres handlinger på betalinger der er godkendt");
371
            return false;
372
        }
373
374
        if ($this->get('belong_to') != 'invoice') {
375
            $this->error->set("Der kan kun udføres handlinger på betalinger der er tilknyttet en faktura");
376
            return false;
377
        }
378
379
        if (!$this->kernel->intranet->hasModuleAccess('invoice')) {
380
            return false;
381
        }
382
383
        $invoice_module = $this->kernel->getModule('debtor', true); // true: tjekker kun intranet adgang
0 ignored issues
show
Unused Code introduced by
$invoice_module is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
384
385
        $invoice = Debtor::factory($this->kernel, (int)$this->get('belong_to_id'));
386
387
        if ($invoice->get('id') == 0) {
388
            $this->error->set("Ugyldig faktura");
389
            return false;
390
        }
391
392
        $payment = new Payment($invoice);
393
394
        $input = array(
395
            "payment_date" => date("d-m-Y"),
396
            "amount" => $this->getAmountInSystemCurrency()->getAsLocal('da_dk', 2),
0 ignored issues
show
Deprecated Code introduced by
The method Ilib_Variable_Float::getAsLocal() has been deprecated with message: Use getAsLocale() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
397
            "description" => "Transaction ".$this->get('transaction_number'),
398
            "type" => 2 // credit card
399
        );
400
401
        if ($payment->update($input)) {
0 ignored issues
show
Documentation introduced by
$input is of type array<string,*,{"payment...ing","type":"integer"}>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
402
            $this->value['create_payment_id'] = $payment->get('id');
403
            if ($this->getCurrency()) {
404
                $db = new DB_Sql;
405
                $db->query("UPDATE onlinepayment SET captured_in_currency_payment_exchange_rate_id = ".$this->getCurrency()->getPaymentExchangeRate()->getId()." WHERE intranet_id = ".$this->kernel->intranet->get('id')." AND id = ".$this->id);
406
            }
407
            return true;
408
        } else {
409
            $this->error->merge($payment->error->getMessage());
410
            return false;
411
        }
412
    }
413
414
    /**
415
     * Returns the possible actions to perform on an onlinepayment.
416
     * These are defined individually to all providers. The actual action is executed in OnlinePayment->transactionAction()
417
     *
418
     * Nb. the action 'capture' is not shown in debtor (view.php) before it is an sent invoice.
419
     *
420
     * @todo better description of this, what is it used for. I think that the label
421
     *       has to go by the way.
422
     *
423
     * @return array    with actions to perform on onlinepayment.
424
     */
425
    function getTransactionActions()
426
    {
427
        return array(
428
            0 => array(
429
                'action' => 'capture',
430
                'label' => 'Hæv')
431
        );
432
    }
433
434
    function transactionAction($action)
435
    {
436
        return false;
437
    }
438
439
    function getList()
440
    {
441
        if ($this->getDBQuery()->getFilter('belong_to') != '') {
442
            if ($this->getDBQuery()->getFilter('belong_to_id') == 0) {
443
                throw new Exception("belong_to_id er nul i OnlinePayment->getList()");
444
            }
445
            $belong_to_key = array_search($this->dbquery->getFilter('belong_to'), $this->getBelongToTypes());
446
            if ($this->getDBQuery()->getFilter('belong_to') == '' || $belong_to_key === false) {
447
                throw new Exception("belong_to_key er ikke gyldig i OnlinePayment->getList()");
448
            }
449
            $this->getDBQuery()->setCondition("belong_to_key = ".$belong_to_key." AND belong_to_id = ".$this->dbquery->getFilter('belong_to_id'));
450
        }
451
452
        if ($this->getDBQuery()->getFilter('status') > 0) {
453
            $this->getDBQuery()->setCondition("status_key = ".intval($this->getDBQuery()->getFilter('status')));
454
        }
455
456 View Code Duplication
        if ($this->getDBQuery()->getFilter('text') != "") {
457
            $this->getDBQuery()->setCondition("transaction_number LIKE \"%".$this->getDBQuery()->getFilter('text')."%\" OR text LIKE \"%".$this->dbquery->getFilter('text')."%\"");
458
        }
459
460 View Code Duplication
        if ($this->getDBQuery()->checkFilter("from_date")) {
461
            $date = new Intraface_Date($this->getDBQuery()->getFilter("from_date"));
462
            if ($date->convert2db()) {
463
                $this->getDBQuery()->setCondition("date_created >= \"".$date->get()."\"");
464
            } else {
465
                $this->error->set("Fra dato er ikke gyldig");
466
            }
467
        }
468
469
        // Poster med fakturadato før slutdato.
470 View Code Duplication
        if ($this->getDBQuery()->checkFilter("to_date")) {
471
            $date = new Intraface_Date($this->getDBQuery()->getFilter("to_date"));
472
            if ($date->convert2db()) {
473
                $this->getDBQuery()->setCondition("date_created <= \"".$date->get()."\"");
474
            } else {
475
                $this->error->set("Til dato er ikke gyldig");
476
            }
477
        }
478
479
        $doctrine = Doctrine_Manager::connection(DB_DSN);
480
        $doctrine->setCharset('utf8');
481
482
        // @todo this does not work
483
        $currency_gateway = new Intraface_modules_currency_Currency_Gateway($doctrine);
484
485
        $this->getDBQuery()->setSorting("date_created DESC");
486
        $db = $this->getDBQuery()->getRecordset("id, date_created, belong_to_key, belong_to_id, text, status_key, amount, provider_key, transaction_number, transaction_status, pbs_status, currency_id, DATE_FORMAT(date_created, '%d-%m-%Y %H:%i') AS dk_date_created", "", false);
487
        $i = 0;
488
        $list = array();
489
490
        while ($db->nextRecord()) {
491
            $list[$i]['id'] = $db->f('id');
492
            $list[$i]['dk_date_created'] = $db->f('dk_date_created');
493
            $list[$i]['date_created'] = $db->f('date_created');
494
            $list[$i]['belong_to_key'] = $db->f('belong_to_key');
495
            $belong_to_types = $this->getBelongToTypes();
496
            $list[$i]['belong_to'] = $belong_to_types[$db->f('belong_to_key')];
497
            $list[$i]['belong_to_id'] = $db->f('belong_to_id');
498
            $list[$i]['text'] = $db->f('text');
499
            $list[$i]['status_key'] = $db->f('status_key');
500
            $status_types = OnlinePayment::getStatusTypes();
501
            $list[$i]['status'] = $status_types[$db->f('status_key')];
502
            $list[$i]['amount'] = $db->f('amount');
503
            $list[$i]['provider_key'] = $db->f('provider_key');
504
            $list[$i]['dk_amount'] = number_format($db->f('amount'), 2, ",", ".");
505
            $list[$i]['transaction_number'] = $db->f('transaction_number');
506
            $list[$i]['pbs_status'] = $db->f('pbs_status');
507
            $list[$i]['transaction_status'] = $db->f('transaction_status');
508
            if (array_key_exists($list[$i]['transaction_status'], $this->transaction_status_types)) {
509
                $list[$i]['transaction_status_translated'] = $this->transaction_status_types[$db->f('transaction_status')];
510
            } else {
511
                $list[$i]['transaction_status_translated'] = 'invalid status';
512
            }
513
514
            if ($db->f('currency_id') != 0) {
515
                $list[$i]['currency'] = $currency_gateway->findById($db->f('currency_id'));
516
            } else {
517
                $list[$i]['currency'] = false;
518
            }
519
520
            // @todo What is this for?
521
            if (array_key_exists($list[$i]['transaction_status'], $this->transaction_status_types) && $db->f('transaction_status') != $this->transaction_status_authorized) {
522
                $list[$i]['user_transaction_status_translated'] = $this->transaction_status_types[$db->f('transaction_status')];
523
            } else {
524
                $list[$i]['user_transaction_status_translated'] = "";
525
            }
526
527
            $i++;
528
        }
529
        return $list;
530
    }
531
532
    /**
533
     * Returns the currency if set, otherwise false
534
     */
535 View Code Duplication
    public function getCurrency()
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...
536
    {
537
        if ($this->get('currency_id') == 0) {
538
            return false;
539
        }
540
541
        if (!$this->currency) {
542
            $doctrine = Doctrine_Manager::connection(DB_DSN);
543
            $gateway = new Intraface_modules_currency_Currency_Gateway($doctrine);
544
            $this->currency = $gateway->findById($this->get('currency_id'));
545
        }
546
547
        return $this->currency;
548
    }
549
550
    /**
551
     * Returns the amount in the given currency
552
     */
553
    public function getAmount()
554
    {
555
        return new Ilib_Variable_Float($this->get('amount'));
556
    }
557
558
    /**
559
     * Returns an approximate amount in the
560
     */
561
    public function getAmountInSystemCurrency()
562
    {
563
        if ($this->getCurrency()) {
564
            if ($this->get('status') == 'captured') {
565
                return new Ilib_Variable_Float(
566
                    $this->getCurrency()
567
                    ->getPaymentExchangeRate(
568
                        $this->get('captured_in_currency_payment_exchange_rate_id')
569
                    )
570
                    ->convertAmountFromCurrency($this->getAmount())->getAsIso(2)
571
                );
572
            } else {
573
                return new Ilib_Variable_Float($this->getCurrency()->getPaymentExchangeRate()->convertAmountFromCurrency($this->getAmount())->getAsIso(2));
574
            }
575
        }
576
        return $this->getAmount();
577
    }
578
579
580
    /**
581
     * returns the possible status types
582
     *
583
     * @return array with status types
584
     */
585
    static function getStatusTypes()
586
    {
587
        return array(
588
            0 => '',
589
            1 => 'created',
590
            2 => 'authorized',
591
            3 => 'captured',
592
            4 => 'reversed',
593
            5 => 'cancelled');
594
    }
595
596
    /**
597
     * returns possible belong to types
598
     *
599
     * @return array with belong to types
600
     */
601
    private function getBelongToTypes()
602
    {
603
        return array(
604
            0 => '',
605
            1 => 'order',
606
            2 => 'invoice');
607
    }
608
609
    /**
610
     * returns the implemented providers
611
     *
612
     * @return array with providers
613
     */
614
    static function getImplementedProviders()
615
    {
616
        return array(
617
            0 => '_invalid_',
618
            1 => 'default', // reserved for custom provider, where everything runs outside the system
619
            2 => 'quickpay',
620
            3 => 'dandomain'
621
        );
622
    }
623
624
    function getDBQuery()
625
    {
626
        if ($this->dbquery) {
627
            return $this->dbquery;
628
        }
629
630
        $this->dbquery = new Intraface_DBQuery($this->kernel, "onlinepayment", "intranet_id = ".$this->kernel->intranet->get("id"));
631
        $this->dbquery->useErrorObject($this->error);
632
633
        return $this->dbquery;
634
    }
635
636
    function isFilledIn()
637
    {
638
        return true; // No settings for the online payment
639
    }
640
641
    function isSettingsSet()
642
    {
643
        return true;
644
    }
645
646
    function isProviderSet()
647
    {
648
        return $this->kernel->getSetting()->get('intranet', 'onlinepayment.provider_key');
649
    }
650
651
    function setProvider($input)
652
    {
653
        // @todo check whether all payments has been dealt with when changing provider
654
        $this->kernel->getSetting()->set('intranet', 'onlinepayment.provider_key', $input['provider_key']);
655
        return true;
656
    }
657
658
    function getProvider()
659
    {
660
        return array('provider_key' => $this->kernel->getSetting()->get('intranet', 'onlinepayment.provider_key'));
661
    }
662
663
    function getStatus()
664
    {
665
        $status =  $this->getStatusTypes();
666
        return $status[$this->value['status_key']];
667
    }
668
}
669