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\Models\Payment; |
12
|
|
|
|
13
|
|
|
class Goldenpay |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var Application |
17
|
|
|
*/ |
18
|
|
|
private $application; |
19
|
|
|
/** |
20
|
|
|
* @var Library |
21
|
|
|
*/ |
22
|
|
|
private $goldenpay; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Goldenpay constructor. |
26
|
|
|
* |
27
|
|
|
* @param Application $application |
28
|
|
|
* @param Repository $config |
29
|
|
|
* @param PaymentInterface $goldenpay |
30
|
|
|
*/ |
31
|
|
|
public function __construct(Application $application, Repository $config, PaymentInterface $goldenpay) |
32
|
|
|
{ |
33
|
|
|
$this->application = $application; |
34
|
|
|
$this->goldenpay = $goldenpay; |
|
|
|
|
35
|
|
|
|
36
|
|
|
$this->goldenpay = $goldenpay->auth( |
|
|
|
|
37
|
|
|
$config->get('goldenpay.auth_key'), |
38
|
|
|
$config->get('goldenpay.merchant_name') |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param int $amount |
44
|
|
|
* @param CardType $cardType |
45
|
|
|
* @param string $description |
46
|
|
|
* @param Language|null $lang |
47
|
|
|
* |
48
|
|
|
* @throws \Orkhanahmadov\Goldenpay\Exceptions\GoldenpayPaymentKeyException |
49
|
|
|
* |
50
|
|
|
* @return Payment |
51
|
|
|
*/ |
52
|
|
|
public function payment(int $amount, CardType $cardType, string $description, ?Language $lang = null): Payment |
53
|
|
|
{ |
54
|
|
|
$lang = $lang ?: $this->languageFromLocale(); |
55
|
|
|
|
56
|
|
|
$paymentKey = $this->goldenpay->payment($amount, $cardType, $description, $lang); |
57
|
|
|
|
58
|
|
|
return Payment::create([ |
59
|
|
|
'payment_key' => $paymentKey->getPaymentKey(), |
60
|
|
|
'amount' => $amount, |
61
|
|
|
'card_type' => $cardType->getValue(), |
62
|
|
|
'language' => $lang->getValue(), |
63
|
|
|
'description' => $description, |
64
|
|
|
]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param Payment|string $payment |
69
|
|
|
* |
70
|
|
|
* @return Payment |
71
|
|
|
*/ |
72
|
|
|
public function result($payment): Payment |
73
|
|
|
{ |
74
|
|
|
if (! $payment instanceof Payment) { |
75
|
|
|
$payment = Payment::wherePaymentKey($payment)->firstOrFail(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$result = $this->goldenpay->result($payment->payment_key); |
79
|
|
|
|
80
|
|
|
$payment->status = $result->getCode(); |
81
|
|
|
$payment->message = $result->getMessage(); |
82
|
|
|
$payment->reference_number = $result->getReferenceNumber(); |
83
|
|
|
$payment->card_number = $result->getCardNumber(); |
84
|
|
|
$payment->payment_date = $result->getPaymentDate(); |
85
|
|
|
$payment->checks = $result->getCheckCount(); |
86
|
|
|
$payment->save(); |
87
|
|
|
|
88
|
|
|
return $payment; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return Language |
93
|
|
|
*/ |
94
|
|
|
private function languageFromLocale(): Language |
95
|
|
|
{ |
96
|
|
|
$currentLocale = strtoupper($this->application->getLocale()); |
97
|
|
|
|
98
|
|
|
if (! in_array($currentLocale, ['EN', 'RU', 'AZ'])) { |
99
|
|
|
return Language::EN(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return Language::{$currentLocale}(); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
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.