Completed
Push — master ( 338305...709008 )
by Orkhan
03:44
created

Goldenpay::authenticate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
cc 3
nc 2
nop 0
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\Goldenpay as Library;
10
use Orkhanahmadov\Goldenpay\PaymentInterface;
11
use Orkhanahmadov\LaravelGoldenpay\Actions\PaymentEvent;
12
use Orkhanahmadov\LaravelGoldenpay\Models\Payment;
13
14
class Goldenpay
15
{
16
    /**
17
     * @var Application
18
     */
19
    private $application;
20
    /**
21
     * @var Repository
22
     */
23
    private $config;
24
    /**
25
     * @var PaymentEvent
26
     */
27
    private $event;
28
    /**
29
     * @var Library
30
     */
31
    private $goldenpay;
32
33
    /**
34
     * Goldenpay constructor.
35
     *
36
     * @param Application $application
37
     * @param Repository $config
38
     * @param PaymentEvent $event
39
     * @param PaymentInterface $goldenpay
40
     */
41
    public function __construct(
42
        Application $application,
43
        Repository $config,
44
        PaymentEvent $event,
45
        PaymentInterface $goldenpay
46
    ) {
47
        $this->application = $application;
48
        $this->config = $config;
49
        $this->event = $event;
50
        $this->goldenpay = $goldenpay;
0 ignored issues
show
Documentation Bug introduced by
$goldenpay is of type object<Orkhanahmadov\Goldenpay\PaymentInterface>, but the property $goldenpay was declared to be of type object<Orkhanahmadov\Goldenpay\Goldenpay>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof 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 given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
51
    }
52
53
    /**
54
     * Authenticates with Goldenpay using configured "auth_key" and "merchant_name".
55
     *
56
     * @return PaymentInterface
57
     */
58
    private function authenticate(): PaymentInterface
59
    {
60
        $authKey = $this->config->get('goldenpay.auth_key');
61
        $merchantName = $this->config->get('goldenpay.merchant_name');
62
63
        if (!$authKey || !$merchantName) {
64
            throw new \InvalidArgumentException(
65
                'Missing "auth_key" and/or "merchant_name" parameters. Make sure to set them in config or .env file.'
66
            );
67
        }
68
69
        return $this->goldenpay->authenticate($authKey, $merchantName);
70
    }
71
72
    /**
73
     * Creates new payment base on passed credentials.
74
     *
75
     * @param int $amount
76
     * @param CardType $cardType
77
     * @param string $description
78
     * @param Language|null $lang
79
     *
80
     * @throws \Orkhanahmadov\Goldenpay\Exceptions\GoldenpayPaymentKeyException
81
     *
82
     * @return Payment
83
     */
84
    public function payment(int $amount, CardType $cardType, string $description, ?Language $lang = null): Payment
85
    {
86
        $lang = $lang ?: $this->languageFromLocale();
87
88
        $paymentKey = $this->authenticate()->payment($amount, $cardType, $description, $lang);
89
90
        $payment = Payment::create([
91
            'payment_key' => $paymentKey->getPaymentKey(),
92
            'amount' => $amount,
93
            'card_type' => $cardType->getValue(),
94
            'language' => $lang->getValue(),
95
            'description' => $description,
96
        ]);
97
98
        $this->event->execute('goldenpay.payment_events.created', $payment);
99
100
        return $payment;
101
    }
102
103
    /**
104
     * Checks payment result with given payment_key or Payment instance.
105
     *
106
     * @param Payment|string $payment
107
     *
108
     * @return Payment
109
     */
110
    public function result($payment): Payment
111
    {
112
        if (! $payment instanceof Payment) {
113
            $payment = Payment::wherePaymentKey($payment)->firstOrFail();
114
        }
115
116
        $result = $this->authenticate()->result($payment->payment_key);
117
118
        $payment->status = $result->getCode();
119
        $payment->message = $result->getMessage();
120
        $payment->reference_number = $result->getReferenceNumber();
121
        $payment->card_number = $result->getCardNumber();
122
        $payment->payment_date = $result->getPaymentDate();
123
        $payment->checks = $result->getCheckCount();
124
        $payment->save();
125
126
        $this->event->execute('goldenpay.payment_events.checked', $payment);
127
        if ($payment->successful) {
128
            $this->event->execute('goldenpay.payment_events.successful', $payment);
129
        }
130
131
        return $payment;
132
    }
133
134
    /**
135
     * @return Language
136
     */
137
    private function languageFromLocale(): Language
138
    {
139
        $currentLocale = strtoupper($this->application->getLocale());
140
141
        if (! in_array($currentLocale, ['EN', 'RU', 'AZ'])) {
142
            return Language::EN();
143
        }
144
145
        return Language::{$currentLocale}();
146
    }
147
}
148