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 Illuminate\Http\Request; |
9
|
|
|
use Orkhanahmadov\LaravelGoldenpay\Goldenpay; |
10
|
|
|
use Orkhanahmadov\LaravelGoldenpay\Models\Payment; |
11
|
|
|
|
12
|
|
|
abstract class GoldenpayController |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var Application |
16
|
|
|
*/ |
17
|
|
|
protected $application; |
18
|
|
|
/** |
19
|
|
|
* @var Goldenpay |
20
|
|
|
*/ |
21
|
|
|
protected $goldenpay; |
22
|
|
|
/** |
23
|
|
|
* @var Dispatcher |
24
|
|
|
*/ |
25
|
|
|
protected $dispatcher; |
26
|
|
|
/** |
27
|
|
|
* @var Repository |
28
|
|
|
*/ |
29
|
|
|
protected $config; |
30
|
|
|
/** |
31
|
|
|
* @var Payment |
32
|
|
|
*/ |
33
|
|
|
protected $payment; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Controller constructor. |
37
|
|
|
* |
38
|
|
|
* @param Application $application |
39
|
|
|
* @param Repository $config |
40
|
|
|
* @param Request $request |
41
|
|
|
* @param Goldenpay $goldenpay |
42
|
|
|
* @param Dispatcher $dispatcher |
43
|
|
|
*/ |
44
|
|
|
public function __construct( |
45
|
|
|
Application $application, |
46
|
|
|
Repository $config, |
47
|
|
|
Request $request, |
48
|
|
|
Goldenpay $goldenpay, |
49
|
|
|
Dispatcher $dispatcher |
50
|
|
|
) { |
51
|
|
|
$this->application = $application; |
52
|
|
|
$this->config = $config; |
53
|
|
|
$this->goldenpay = $goldenpay; |
54
|
|
|
$this->dispatcher = $dispatcher; |
55
|
|
|
|
56
|
|
|
$this->checkPaymentResult($request); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Checks payment result with "payment_key" query parameter. |
61
|
|
|
* |
62
|
|
|
* @param Request $request |
63
|
|
|
*/ |
64
|
|
|
final protected function checkPaymentResult(Request $request): void |
65
|
|
|
{ |
66
|
|
|
$this->payment = $this->goldenpay->paymentResult($request->query('payment_key')); |
|
|
|
|
67
|
|
|
|
68
|
|
|
$this->fireEvent($this->payment); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Fires event based on payment status. |
73
|
|
|
* |
74
|
|
|
* @param Payment $payment |
75
|
|
|
*/ |
76
|
|
|
private function fireEvent(Payment $payment): void |
77
|
|
|
{ |
78
|
|
|
if ($payment->status === 1) { |
79
|
|
|
$event = $this->application->make( |
80
|
|
|
$this->config->get('goldenpay.events.payment_successful'), |
81
|
|
|
[$payment] |
82
|
|
|
); |
83
|
|
|
} else { |
84
|
|
|
$event = $this->application->make( |
85
|
|
|
$this->config->get('goldenpay.events.payment_failed'), |
86
|
|
|
[$payment] |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$this->dispatcher->dispatch($event); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
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.