1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Orkhanahmadov\LaravelGoldenpay\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Config\Repository; |
6
|
|
|
use Illuminate\Contracts\Events\Dispatcher; |
7
|
|
|
use Illuminate\Contracts\Foundation\Application; |
8
|
|
|
use Orkhanahmadov\LaravelGoldenpay\Events\PaymentSuccessfulEvent; |
9
|
|
|
use Orkhanahmadov\LaravelGoldenpay\Goldenpay; |
10
|
|
|
use Orkhanahmadov\LaravelGoldenpay\Http\Requests\Request; |
11
|
|
|
use Orkhanahmadov\LaravelGoldenpay\Models\Payment; |
12
|
|
|
|
13
|
|
|
abstract class GoldenpayController |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var Repository |
17
|
|
|
*/ |
18
|
|
|
protected $config; |
19
|
|
|
/** |
20
|
|
|
* @var Dispatcher |
21
|
|
|
*/ |
22
|
|
|
protected $dispatcher; |
23
|
|
|
/** |
24
|
|
|
* @var Goldenpay |
25
|
|
|
*/ |
26
|
|
|
protected $goldenpay; |
27
|
|
|
/** |
28
|
|
|
* @var Payment |
29
|
|
|
*/ |
30
|
|
|
protected $payment; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Controller constructor. |
34
|
|
|
* |
35
|
|
|
* @param Repository $config |
36
|
|
|
* @param Request $request |
37
|
|
|
* @param Dispatcher $dispatcher |
38
|
|
|
* @param Goldenpay $goldenpay |
39
|
|
|
*/ |
40
|
|
|
public function __construct( |
41
|
|
|
Repository $config, |
42
|
|
|
Request $request, |
43
|
|
|
Dispatcher $dispatcher, |
44
|
|
|
Goldenpay $goldenpay |
45
|
|
|
) { |
46
|
|
|
$this->config = $config; |
47
|
|
|
$this->dispatcher = $dispatcher; |
48
|
|
|
$this->goldenpay = $goldenpay; |
49
|
|
|
|
50
|
|
|
$this->checkPaymentResult($request); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Checks payment result with "payment_key" query parameter. |
55
|
|
|
* |
56
|
|
|
* @param Request $request |
57
|
|
|
*/ |
58
|
|
|
final protected function checkPaymentResult(Request $request): void |
59
|
|
|
{ |
60
|
|
|
$this->payment = $this->goldenpay->result($request->query('payment_key')); |
|
|
|
|
61
|
|
|
|
62
|
|
|
$this->fireEvent(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Fires event based on payment status. |
67
|
|
|
*/ |
68
|
|
|
private function fireEvent(): void |
69
|
|
|
{ |
70
|
|
|
if ($this->paymentSuccessful()) { |
71
|
|
|
$event = $this->config->get('goldenpay.events.payment_successful'); |
72
|
|
|
} else { |
73
|
|
|
$event = $this->config->get('goldenpay.events.payment_failed'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$this->dispatcher->dispatch(new $event($this->payment)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns state if payment was successful. |
81
|
|
|
* |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
protected function paymentSuccessful(): bool |
85
|
|
|
{ |
86
|
|
|
return $this->payment->successful; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.