Goldenpay::result()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
nc 4
nop 1
dl 0
loc 22
c 0
b 0
f 0
cc 3
rs 9.7998
1
<?php
2
3
namespace Orkhanahmadov\LaravelGoldenpay;
4
5
use Illuminate\Contracts\Config\Repository;
6
use Illuminate\Contracts\Foundation\Application;
7
use Orkhanahmadov\Goldenpay\Enums\CardType;
8
use Orkhanahmadov\Goldenpay\Enums\Language;
9
use Orkhanahmadov\Goldenpay\PaymentInterface;
10
use Orkhanahmadov\LaravelGoldenpay\Actions\PaymentEvent;
11
use Orkhanahmadov\LaravelGoldenpay\Models\Payment;
12
13
class Goldenpay
14
{
15
    /**
16
     * @var Application
17
     */
18
    private $application;
19
    /**
20
     * @var Repository
21
     */
22
    private $config;
23
    /**
24
     * @var PaymentEvent
25
     */
26
    private $event;
27
    /**
28
     * @var PaymentInterface
29
     */
30
    private $goldenpay;
31
32
    /**
33
     * Goldenpay constructor.
34
     *
35
     * @param Application $application
36
     * @param Repository $config
37
     * @param PaymentEvent $event
38
     * @param PaymentInterface $goldenpay
39
     */
40
    public function __construct(
41
        Application $application,
42
        Repository $config,
43
        PaymentEvent $event,
44
        PaymentInterface $goldenpay
45
    ) {
46
        $this->application = $application;
47
        $this->config = $config;
48
        $this->event = $event;
49
        $this->goldenpay = $goldenpay;
50
    }
51
52
    /**
53
     * Authenticates with Goldenpay using configured "auth_key" and "merchant_name".
54
     *
55
     * @return PaymentInterface
56
     */
57
    private function authenticate(): PaymentInterface
58
    {
59
        $authKey = $this->config->get('goldenpay.auth_key');
60
        $merchantName = $this->config->get('goldenpay.merchant_name');
61
62
        if (! $authKey || ! $merchantName) {
63
            throw new \InvalidArgumentException(
64
                'Missing "auth_key" and/or "merchant_name" parameters. Make sure to set them in config or .env file.'
65
            );
66
        }
67
68
        return $this->goldenpay->authenticate($authKey, $merchantName);
69
    }
70
71
    /**
72
     * Creates new payment base on passed credentials.
73
     *
74
     * @param int $amount
75
     * @param CardType $cardType
76
     * @param string $description
77
     * @param Language|null $lang
78
     *
79
     * @throws \Orkhanahmadov\Goldenpay\Exceptions\GoldenpayPaymentKeyException
80
     *
81
     * @return Payment
82
     */
83
    public function payment(int $amount, CardType $cardType, string $description, ?Language $lang = null): Payment
84
    {
85
        $lang = $lang ?: $this->languageFromLocale();
86
87
        $paymentKey = $this->authenticate()->payment($amount, $cardType, $description, $lang);
88
89
        $payment = Payment::create([
90
            'payment_key' => $paymentKey->getPaymentKey(),
91
            'amount' => $amount,
92
            'card_type' => $cardType->getValue(),
93
            'language' => $lang->getValue(),
94
            'description' => $description,
95
        ]);
96
97
        $this->event->execute('goldenpay.payment_events.created', $payment);
98
99
        return $payment;
100
    }
101
102
    /**
103
     * Checks payment result with given payment_key or Payment instance.
104
     *
105
     * @param Payment|string $payment
106
     *
107
     * @return Payment
108
     */
109
    public function result($payment): Payment
110
    {
111
        if (! $payment instanceof Payment) {
112
            $payment = Payment::where('payment_key', $payment)->firstOrFail();
113
        }
114
115
        $result = $this->authenticate()->result($payment->payment_key);
116
117
        $payment->status = $result->getCode();
118
        $payment->message = $result->getMessage();
119
        $payment->reference_number = $result->getReferenceNumber();
120
        $payment->card_number = $result->getCardNumber();
121
        $payment->payment_date = $result->getPaymentDate();
0 ignored issues
show
Documentation Bug introduced by
It seems like $result->getPaymentDate() can also be of type DateTimeImmutable. However, the property $payment_date is declared as type Carbon\Carbon. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
122
        $payment->checks = $result->getCheckCount();
123
        $payment->save();
124
125
        $this->event->execute('goldenpay.payment_events.checked', $payment);
126
        if ($payment->successful) {
127
            $this->event->execute('goldenpay.payment_events.successful', $payment);
128
        }
129
130
        return $payment;
131
    }
132
133
    /**
134
     * @return Language
135
     */
136
    private function languageFromLocale(): Language
137
    {
138
        $currentLocale = strtoupper($this->application->getLocale());
139
140
        if (! in_array($currentLocale, ['EN', 'RU', 'AZ'])) {
141
            return Language::EN();
142
        }
143
144
        return Language::{$currentLocale}();
145
    }
146
}
147