Issues (3)

src/Message/AbstractRequest.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Omnipay\Worldline\Message;
4
5
use DateTime;
6
use DateTimeZone;
7
use Money\Currency;
8
use Money\Number;
9
use Money\Parser\DecimalMoneyParser;
10
use Omnipay\Common\Message\AbstractRequest as BaseAbstractRequest;
11
use Omnipay\Common\Exception\InvalidRequestException;
12
13
/**
14
 * Base request for Worldline
15
 *
16
 * @see https://docs.direct.worldline-solutions.com/en/integration/api-developer-guide/authentication
17
 */
18
abstract class AbstractRequest extends BaseAbstractRequest
19
{
20
    /** @var string */
21
    protected $liveEndpoint = 'https://payment.direct.worldline-solutions.com';
22
    /** @var string */
23
    protected $testEndpoint = 'https://payment.preprod.direct.worldline-solutions.com';
24
    /** @var string HTTP verb used to make the request */
25
    protected $requestMethod = 'GET';
26
27
    public function getApiKey()
28
    {
29
        return $this->getParameter('apiKey');
30
    }
31
32
    public function setApiKey($value)
33
    {
34
        return $this->setParameter('apiKey', $value);
35
    }
36
37
    public function getApiSecret()
38
    {
39
        return $this->getParameter('apiSecret');
40
    }
41
42
    public function setApiSecret($value)
43
    {
44
        return $this->setParameter('apiSecret', $value);
45
    }
46
47
    public function getMerchantId()
48
    {
49
        return $this->getParameter('merchantId');
50
    }
51
52
    public function setMerchantId($value)
53
    {
54
        return $this->setParameter('merchantId', $value);
55
    }
56
57
    public function getCreateToken()
58
    {
59
        return $this->getParameter('createToken');
60
    }
61
62
    public function setCreateToken($value)
63
    {
64
        return $this->setParameter('createToken', $value);
65
    }
66
67
    public function getEndpoint()
68
    {
69
        return ($this->getTestMode() ? $this->testEndpoint : $this->liveEndpoint).$this->getAction();
70
    }
71
72
    public function sendData($data)
73
    {
74
        $contentType = $this->requestMethod == 'POST' ? 'application/json; charset=utf-8' : '';
75
        $now = new DateTime('now', new DateTimeZone('GMT'));
76
        $dateTime = $now->format("D, d M Y H:i:s T");
77
        $endpointAction = $this->getAction();
78
79
        $message = $this->requestMethod."\n".$contentType."\n".$dateTime."\n".$endpointAction."\n";
80
        $signature = $this->createSignature($message, $this->getApiSecret());
81
82
        $headers = [
83
            'Content-Type' => $contentType,
84
            'Authorization' => 'GCS v1HMAC:'.$this->getApiKey().':'.$signature,
85
            'Date' => $dateTime,
86
        ];
87
88
        $body = json_encode($data);
89
90
        $httpResponse = $this->httpClient->request(
91
            $this->requestMethod,
92
            $this->getEndpoint(),
93
            $headers,
94
            $body
95
        );
96
97
        return $this->createResponse($httpResponse->getBody()->getContents());
98
    }
99
100
    /**
101
     * @param mixed $data
102
     * @return AbstractResponse
0 ignored issues
show
The type Omnipay\Worldline\Message\AbstractResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
103
     */
104
    abstract protected function createResponse($data);
105
106
    /**
107
     * @return string
108
     */
109
    abstract protected function getAction();
110
111
    /**
112
     * Create signature hash used to verify messages
113
     *
114
     * @param string $message  The message to encrypt
115
     * @param string $key      The base64-encoded key used to encrypt the message
116
     *
117
     * @return string Generated signature
118
     */
119
    protected function createSignature($message, $key)
120
    {
121
        return base64_encode(hash_hmac('sha256', $message, $key, true));
122
    }
123
124
    /**
125
     * Get integer version (sallest unit) of item price
126
     *
127
     * Copied from {@see BaseAbstractRequest::getAmountInteger()} & {@see BaseAbstractRequest::getMoney()}
128
     */
129
    protected function getItemPriceInteger($item)
130
    {
131
        $currencyCode = $this->getCurrency() ?: 'USD';
132
        $currency = new Currency($currencyCode);
133
        $amount = $item->getPrice();
134
135
        $moneyParser = new DecimalMoneyParser($this->getCurrencies());
136
        $number = Number::fromString($amount);
137
        // Check for rounding that may occur if too many significant decimal digits are supplied.
138
        $decimal_count = strlen($number->getFractionalPart());
139
        $subunit = $this->getCurrencies()->subunitFor($currency);
140
        if ($decimal_count > $subunit) {
141
            throw new InvalidRequestException('Amount precision is too high for currency.');
142
        }
143
        $money = $moneyParser->parse((string) $number, $currency);
144
145
        return (int) $money->getAmount();
146
    }
147
}
148