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\Goldenpay\PaymentKey; |
12
|
|
|
use Orkhanahmadov\Goldenpay\PaymentResult; |
13
|
|
|
use Orkhanahmadov\LaravelGoldenpay\Models\Payment; |
14
|
|
|
|
15
|
|
|
class Goldenpay |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var Application |
19
|
|
|
*/ |
20
|
|
|
private $application; |
21
|
|
|
/** |
22
|
|
|
* @var Library |
23
|
|
|
*/ |
24
|
|
|
private $goldenpay; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Goldenpay constructor. |
28
|
|
|
* |
29
|
|
|
* @param Application $application |
30
|
|
|
* @param Repository $config |
31
|
|
|
* @param PaymentInterface $goldenpay |
32
|
|
|
*/ |
33
|
|
|
public function __construct(Application $application, Repository $config, PaymentInterface $goldenpay) |
34
|
|
|
{ |
35
|
|
|
$this->application = $application; |
36
|
|
|
$this->goldenpay = $goldenpay; |
|
|
|
|
37
|
|
|
|
38
|
|
|
$this->goldenpay = $goldenpay->auth( |
|
|
|
|
39
|
|
|
$config->get('goldenpay.auth_key'), |
40
|
|
|
$config->get('goldenpay.merchant_name') |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param int $amount |
46
|
|
|
* @param CardType $cardType |
47
|
|
|
* @param string $description |
48
|
|
|
* @param Language|null $lang |
49
|
|
|
* |
50
|
|
|
* @throws \Orkhanahmadov\Goldenpay\Exceptions\GoldenpayPaymentKeyException |
51
|
|
|
* |
52
|
|
|
* @return PaymentKey |
53
|
|
|
*/ |
54
|
|
|
public function paymentKey(int $amount, CardType $cardType, string $description, ?Language $lang = null): PaymentKey |
55
|
|
|
{ |
56
|
|
|
$lang = $lang ?: $this->languageFromLocale(); |
57
|
|
|
|
58
|
|
|
$paymentKey = $this->goldenpay->paymentKey($amount, $cardType, $description, $lang); |
59
|
|
|
|
60
|
|
|
Payment::create([ |
61
|
|
|
'payment_key' => $paymentKey->getKey(), |
62
|
|
|
'amount' => $amount, |
63
|
|
|
'card_type' => $cardType->getValue(), |
64
|
|
|
'language' => $lang->getValue(), |
65
|
|
|
'description' => $description, |
66
|
|
|
]); |
67
|
|
|
|
68
|
|
|
return $paymentKey; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function paymentResult($paymentKey): PaymentResult |
72
|
|
|
{ |
73
|
|
|
return $this->goldenpay->paymentResult($paymentKey); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return Language |
78
|
|
|
*/ |
79
|
|
|
private function languageFromLocale(): Language |
80
|
|
|
{ |
81
|
|
|
$currentLocale = strtoupper($this->application->getLocale()); |
82
|
|
|
|
83
|
|
|
if (! in_array($currentLocale, ['EN', 'RU', 'AZ'])) { |
84
|
|
|
return Language::EN(); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return Language::{$currentLocale}(); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
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.