Passed
Push — master ( 30757a...03f32c )
by Gabriel
11:44
created

CompletePurchaseRequest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 10
eloc 33
dl 0
loc 76
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A makeCaptureRequest() 0 6 1
A isValidNotification() 0 3 1
A isValidTransaction() 0 23 5
A parseNotification() 0 20 3
1
<?php
2
3
namespace Paytic\Omnipay\Paylike\Message;
4
5
use Paytic\Omnipay\Common\Message\Traits\GatewayNotificationRequestTrait;
6
use Paytic\Omnipay\Paylike\Traits\HasApiTrait;
7
8
/**
9
 * Class CompletePurchaseRequest
10
 * @package Paytic\Omnipay\Paylike\Message
11
 *
12
 * @method CompletePurchaseResponse send()
13
 */
14
class CompletePurchaseRequest extends AbstractRequest
15
{
16
    use GatewayNotificationRequestTrait;
0 ignored issues
show
introduced by
The trait Paytic\Omnipay\Common\Me...otificationRequestTrait requires some properties which are not provided by Paytic\Omnipay\Paylike\M...CompletePurchaseRequest: $query, $request
Loading history...
17
    use HasApiTrait;
18
19
    /**
20
     * @return mixed
21
     */
22
    public function isValidNotification()
23
    {
24
        return $this->hasGET('pTransactionId');
25
    }
26
27
    /**
28
     * @return bool|mixed
29
     */
30
    protected function parseNotification()
31
    {
32
        $this->validate('privateKey');
33
34
        if ($this->isValidTransaction() == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
35
            return false;
36
        }
37
38
        $transaction = $this->getDataItem('transaction');
39
40
        if ($transaction['pendingAmount'] > 0) {
41
            $transaction = $this->makeCaptureRequest(
42
                [
43
                    'amount' => $transaction['pendingAmount'] / 100,
44
                    'currency' => $transaction['currency'],
45
                ]
46
            );
47
        }
48
49
        return $transaction;
50
    }
51
52
    /**
53
     * @return bool
54
     */
55
    protected function isValidTransaction()
56
    {
57
        $transactionId = $this->httpRequest->query->get('pTransactionId');
58
        $this->setTransactionId($transactionId);
59
60
        $isValid = false;
61
        try {
62
            $transactions = $this->getApi()->transactions();
63
            $transaction = $transactions->fetch($transactionId);
64
            if (is_array($transaction)) {
0 ignored issues
show
introduced by
The condition is_array($transaction) is always true.
Loading history...
65
                $this->setDataItem('transaction', $transaction);
66
                $isValid = true;
67
            }
68
        } catch (\Paylike\Exception\NotFound $e) {
69
            $this->setDataItem('message', "The transaction was not found");
70
        } catch (\Paylike\Exception\ApiException $e) {
71
            $this->setDataItem('message', "Api Error:" . $e->getMessage());
72
        }
73
74
        if ($isValid) {
75
            $this->setDataItem('success', true);
76
        }
77
        return $isValid;
78
    }
79
80
    /**
81
     * @param array $parameters
82
     * @return array
83
     */
84
    protected function makeCaptureRequest(array $parameters)
85
    {
86
        $request = new CaptureRequest($this->httpClient, $this->httpRequest);
87
        $request->initialize(array_replace($this->getParameters(), $parameters));
88
        $response = $request->send();
89
        return $response->getDataProperty('transaction');
90
    }
91
}
92