Test Failed
Branch master (3f2e80)
by Gabriel
03:43 queued 01:39
created

CompletePurchaseRequest::isValidTransaction()   A

Complexity

Conditions 5
Paths 20

Size

Total Lines 23
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
c 1
b 0
f 0
dl 0
loc 23
rs 9.4222
cc 5
nc 20
nop 0
1
<?php
2
3
namespace ByTIC\Omnipay\Paylike\Message;
4
5
use ByTIC\Omnipay\Common\Message\Traits\GatewayNotificationRequestTrait;
6
use ByTIC\Omnipay\Paylike\Traits\HasApiTrait;
7
8
/**
9
 * Class CompletePurchaseRequest
10
 * @package ByTIC\Omnipay\Paylike\Message
11
 */
12
class CompletePurchaseRequest extends AbstractRequest
13
{
14
    use GatewayNotificationRequestTrait;
0 ignored issues
show
introduced by
The trait ByTIC\Omnipay\Common\Mes...otificationRequestTrait requires some properties which are not provided by ByTIC\Omnipay\Paylike\Me...CompletePurchaseRequest: $query, $request
Loading history...
15
    use HasApiTrait;
16
17
    /**
18
     * @return mixed
19
     */
20
    protected function isValidNotification()
21
    {
22
        $this->validate('privateKey');
23
24
        return $this->hasGet('pTransactionId') && $this->isValidTransaction();
25
    }
26
27
    /**
28
     * @return bool|mixed
29
     */
30
    protected function parseNotification()
31
    {
32
        $transaction = $this->getDataItem('transaction');
33
34
        if ($transaction['pendingAmount'] > 0) {
35
            $transaction = $this->makeCaptureRequest(
36
                [
37
                    'amount' => $transaction['pendingAmount'] / 100,
38
                    'currency' => $transaction['currency'],
39
                ]
40
            );
41
        }
42
43
        return $transaction;
44
    }
45
46
    /**
47
     * @return bool
48
     */
49
    protected function isValidTransaction()
50
    {
51
        $transactionId = $this->httpRequest->query->get('pTransactionId');
52
        $this->setTransactionId($transactionId);
53
54
        $isValid = false;
55
        try {
56
            $transactions = $this->getApi()->transactions();
57
            $transaction = $transactions->fetch($transactionId);
58
            if (is_array($transaction)) {
0 ignored issues
show
introduced by
The condition is_array($transaction) is always true.
Loading history...
59
                $this->setDataItem('transaction', $transaction);
60
                $isValid = true;
61
            }
62
        } catch (\Paylike\Exception\NotFound $e) {
63
            $this->setDataItem('message', "The transaction was not found");
64
        } catch (\Paylike\Exception\ApiException $e) {
65
            $this->setDataItem('message', "Api Error:" . $e->getMessage());
66
        }
67
68
        if ($isValid) {
69
            $this->setDataItem('success', true);
70
        }
71
        return $isValid;
72
    }
73
74
    /**
75
     * @param array $parameters
76
     * @return array
77
     */
78
    protected function makeCaptureRequest(array $parameters)
79
    {
80
        $request = new CaptureRequest($this->httpClient, $this->httpRequest);
81
        $request->initialize(array_replace($this->getParameters(), $parameters));
82
        $response = $request->send();
83
        return $response->getDataProperty('transaction');
84
    }
85
}
86