ChecksumHelper::generateChecksum1()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 4
crap 1
1
<?php
2
3
namespace Loevgaard\Dandomain\Pay\Helper;
4
5
use Loevgaard\Dandomain\Pay\Model\Payment;
6
use Money\Money;
7
8
/**
9
 * The checksum helper will assist in creating checksums and also verifying checksums based on a payment
10
 */
11
class ChecksumHelper
12
{
13
    /**
14
     * @var string
15
     */
16
    protected $sharedKey1;
17
18
    /**
19
     * @var string
20
     */
21
    protected $sharedKey2;
22
23
    /**
24
     * @var string
25
     */
26
    protected $checksum1;
27
28
    /**
29
     * @var string
30
     */
31
    protected $checksum2;
32
33
    /**
34
     * @var Payment
35
     */
36
    protected $payment;
37
38 6
    public function __construct(Payment $payment, string $sharedKey1, string $sharedKey2)
39
    {
40 6
        $this->payment = $payment;
41 6
        $this->sharedKey1 = $sharedKey1;
42 6
        $this->sharedKey2 = $sharedKey2;
43 6
    }
44
45
    /**
46
     * Returns true if the checksum given from Dandomain matches the checksum we can compute.
47
     *
48
     * @return bool
49
     */
50 3
    public function checksumMatches(): bool
51
    {
52 3
        return $this->payment->getApiKey() === $this->getChecksum1();
53
    }
54
55
    /**
56
     * @return string
57
     */
58 3
    public function getChecksum1()
59
    {
60 3
        if (!$this->checksum1) {
61 3
            $this->checksum1 = static::generateChecksum1(
62 3
                $this->payment->getOrderId(),
63 3
                $this->payment->getTotalAmount(),
64 3
                $this->sharedKey1,
65 3
                $this->payment->getPaymentGatewayCurrencyCode()
66
            );
67
        }
68
69 3
        return $this->checksum1;
70
    }
71
72
    /**
73
     * @return string
74
     */
75 3
    public function getChecksum2()
76
    {
77 3
        if (!$this->checksum2) {
78 3
            $this->checksum2 = static::generateChecksum2(
79 3
                $this->payment->getOrderId(),
80 3
                $this->sharedKey2,
81 3
                $this->payment->getPaymentGatewayCurrencyCode()
82
            );
83
        }
84
85 3
        return $this->checksum2;
86
    }
87
88
    /**
89
     * @param int    $orderId
90
     * @param Money  $amount
91
     * @param string $sharedKey
92
     * @param int    $currency
93
     *
94
     * @return string
95
     */
96 6
    public static function generateChecksum1(int $orderId, Money $amount, string $sharedKey, int $currency): string
97
    {
98 6
        $amountAsFloat = (float)$amount->getAmount() / pow(10, 2);
99
100
        // the amount needs to be formatted as a danish number, so we convert the float
101 6
        $amount = number_format($amountAsFloat, 2, ',', '');
102
103 6
        return strtolower(md5($orderId.'+'.$amount.'+'.$sharedKey.'+'.$currency));
104
    }
105
106
    /**
107
     * Dandomain has a bug in their payment implementation where they don't
108
     * include amount in checksum on their complete/success page.
109
     * That is why we have a second method for computing that checksum.
110
     *
111
     * @param int    $orderId
112
     * @param string $sharedKey
113
     * @param int    $currency
114
     *
115
     * @return string
116
     */
117 3
    public static function generateChecksum2(int $orderId, string $sharedKey, int $currency): string
118
    {
119 3
        return strtolower(md5($orderId.'+'.$sharedKey.'+'.$currency));
120
    }
121
}
122