|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Goodoneuz\PayUz; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\View; |
|
6
|
|
|
use Goodoneuz\PayUz\Models\Transaction; |
|
7
|
|
|
use Goodoneuz\PayUz\Models\PaymentSystem; |
|
8
|
|
|
use Goodoneuz\PayUz\Http\Classes\Payme\Payme; |
|
9
|
|
|
use Goodoneuz\PayUz\Http\Classes\Click\Click; |
|
10
|
|
|
use Goodoneuz\PayUz\Http\Classes\Paynet\Paynet; |
|
11
|
|
|
use Goodoneuz\PayUz\Http\Classes\Stripe\Stripe; |
|
12
|
|
|
use Goodoneuz\PayUz\Http\Classes\PaymentException; |
|
13
|
|
|
|
|
14
|
|
|
class PayUz |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
protected $driverClass = null; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* PayUz constructor. |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct() |
|
23
|
|
|
{ |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Select payment driver |
|
29
|
|
|
* @param null $driver |
|
30
|
|
|
* @return $this |
|
31
|
|
|
*/ |
|
32
|
|
|
public function driver($driver = null){ |
|
33
|
|
|
switch ($driver){ |
|
34
|
|
|
case PaymentSystem::PAYME: |
|
35
|
|
|
$this->driverClass = new Payme; |
|
36
|
|
|
break; |
|
37
|
|
|
case PaymentSystem::CLICK: |
|
38
|
|
|
$this->driverClass = new Click; |
|
39
|
|
|
break; |
|
40
|
|
|
case PaymentSystem::PAYNET: |
|
41
|
|
|
$this->driverClass = new Paynet; |
|
42
|
|
|
break; |
|
43
|
|
|
case PaymentSystem::STRIPE: |
|
44
|
|
|
$this->driverClass = new Stripe; |
|
45
|
|
|
break; |
|
46
|
|
|
} |
|
47
|
|
|
return $this; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Redirect to payment system |
|
52
|
|
|
* @param $model |
|
53
|
|
|
* @param $amount |
|
54
|
|
|
* @param int $currency_code |
|
55
|
|
|
* @return PayUz |
|
56
|
|
|
* @throws \Exception |
|
57
|
|
|
*/ |
|
58
|
|
|
public function redirect($model, $amount, $currency_code = Transaction::CURRENCY_CODE_UZS, $url = null){ |
|
59
|
|
|
$this->validateDriver(); |
|
60
|
|
|
$driver = $this->driverClass; |
|
61
|
|
|
$params = $driver->getRedirectParams($model, $amount, $currency_code, $url); |
|
|
|
|
|
|
62
|
|
|
$view = 'pay-uz::merchant.index'; |
|
63
|
|
|
if (!empty($driver::CUSTOM_FORM)) |
|
64
|
|
|
$view = $driver::CUSTOM_FORM; |
|
65
|
|
|
echo view($view, compact('params')); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return $this |
|
70
|
|
|
* @throws \Exception |
|
71
|
|
|
*/ |
|
72
|
|
|
public function handle(){ |
|
73
|
|
|
$this->validateDriver(); |
|
74
|
|
|
try{ |
|
75
|
|
|
return $this->driverClass->run(); |
|
76
|
|
|
}catch(PaymentException $e){ |
|
77
|
|
|
return $e->response(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $this; |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param $model |
|
85
|
|
|
* @param $amount |
|
86
|
|
|
* @param $currency_code |
|
87
|
|
|
* @throws \Exception |
|
88
|
|
|
*/ |
|
89
|
|
|
public function validateModel($model, $amount, $currency_code){ |
|
90
|
|
|
if (is_null($model)) |
|
91
|
|
|
throw new \Exception('Modal can\'t be null'); |
|
92
|
|
|
if (is_null($amount) || $amount == 0) |
|
93
|
|
|
throw new \Exception('Amount can\'t be null or 0'); |
|
94
|
|
|
if (is_null($currency_code)) |
|
95
|
|
|
throw new \Exception('Currency code can\'t be null'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @throws \Exception |
|
100
|
|
|
*/ |
|
101
|
|
|
public function validateDriver(){ |
|
102
|
|
|
if (is_null($this->driverClass)) |
|
103
|
|
|
throw new \Exception('Driver not selected'); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: