Completed
Push — master ( 8fc956...1ff290 )
by Andrii
02:29
created

CompletePurchaseResponse::getTransactionStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * Paxum plugin for PHP merchant library
5
 *
6
 * @link      https://github.com/hiqdev/omnipay-paxum
7
 * @package   omnipay-paxum
8
 * @license   MIT
9
 * @copyright Copyright (c) 2015, HiQDev (http://hiqdev.com/)
10
 */
11
12
namespace Omnipay\Paxum\Message;
13
14
use Omnipay\Common\Exception\InvalidResponseException;
15
use Omnipay\Common\Message\AbstractResponse;
16
use Omnipay\Common\Message\RequestInterface;
17
18
/**
19
 * Paxum Complete Purchase Response.
20
 */
21
class CompletePurchaseResponse extends AbstractResponse
22
{
23
    /**
24
     * @param RequestInterface $request
25
     * @param array $data
26
     */
27
    public function __construct(RequestInterface $request, $data)
28
    {
29
        $this->request = $request;
30
        $this->data    = $data;
31
32
        if ($this->getTransactionStatus() !== 'done') {
33
            throw new InvalidResponseException('Transaction not done');
34
        }
35
36
        if ($this->getHash() !== $this->calculateHash()) {
37
            throw new InvalidResponseException('Invalid hash');
38
        }
39
40
        if ($this->request->getTestMode() !== $this->getTestMode()) {
41
            throw new InvalidResponseException('Invalid test mode');
42
        }
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function isSuccessful()
49
    {
50
        return true;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     * @return string
56
     */
57
    public function getTransactionId()
58
    {
59
        return $this->data['item_id'];
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     * @return string
65
     */
66
    public function getTransactionReference()
67
    {
68
        return $this->data['transaction_id'];
69
    }
70
71
    public function getTransactionStatus()
72
    {
73
        return $this->data['transaction_status'];
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     * @return string
79
     */
80
    public function getAmount()
81
    {
82
        return $this->data['transaction_amount'];
83
    }
84
85
    /**
86
     * Get payment time.
87
     *
88
     * @return string
89
     */
90
    public function getTime()
91
    {
92
        return Helper::isotime($this->data['transaction_date'] . ' EST');
93
    }
94
95
    /**
96
     * Get test mode.
97
     *
98
     * @return string
99
     */
100
    public function getTestMode()
101
    {
102
        return $this->data['sandbox'] === 'ON';
103
    }
104
105
    /**
106
     * Get payer info - name, username and id.
107
     *
108
     * @return string
109
     */
110
    public function getPayer()
111
    {
112
        return $this->data['buyer_name'] . '/' . $this->data['buyer_username'] . '/' . $this->data['buyer_id'];
113
    }
114
115
    /**
116
     * Get hash from request.
117
     *
118
     * @return string
119
     */
120
    public function getHash()
121
    {
122
        return $this->data['key'];
123
    }
124
125
    /**
126
     * Calculate hash to validate incoming IPN notifications.
127
     *
128
     * @return string
129
     */
130
    public function calculateHash()
131
    {
132
        // raw POST request
133
        $raw = file_get_contents('php://input');
134
        // removing trailing '&key=...'
135
        $fields = substr($raw, 0, strpos($raw, '&key='));
136
        // this is the documentation way
137
        $supposed_hash = md5($fields . $this->request->getSecret());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Omnipay\Common\Message\RequestInterface as the method getSecret() does only exist in the following implementations of said interface: Omnipay\Paxum\Message\AbstractRequest, Omnipay\Paxum\Message\CompletePurchaseRequest, Omnipay\Paxum\Message\PurchaseRequest.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
Unused Code introduced by
$supposed_hash 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...
138
139
        // this is how they actually get it
140
        $kvs = '';
141
        foreach ($this->data as $k=>$v) {
142
            if ($k !== 'key' && $k !== 'username') {
143
                $kvs  .= ($kvs ? '&' : '') . "$k=$v";
144
            }
145
        }
146
        $hash = md5($kvs);
147
148
        /* Testing facility
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
149
        throw new \Exception(
150
            var_export([
151
                'key'    => $this->getHash(),
152
                'fields' => $fields,
153
                'secret' => $this->request->getSecret(),
154
                'hash'   => $hash,
155
                'h2'     => md5($fields),
156
                'h3'     => md5($fields . $this->request->getSecret()),
157
                'kvs'    => $kvs,
158
                'kh3'    => md5($kvs),
159
                'kh4'    => md5($kvs . $this->request->getSecret()),
160
            ], true)
161
        );*/
162
163
        return $hash;
164
    }
165
}
166