|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tzsk\Payu\Concerns; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Support\Facades\Validator; |
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
use Illuminate\Validation\ValidationException; |
|
9
|
|
|
use Tzsk\Payu\Contracts\HasFormParams; |
|
10
|
|
|
|
|
11
|
|
|
class Transaction implements HasFormParams |
|
12
|
|
|
{ |
|
13
|
|
|
public ?string $transactionId = null; |
|
|
|
|
|
|
14
|
|
|
public ?float $amount = null; |
|
15
|
|
|
public ?string $productInfo = null; |
|
16
|
|
|
public ?Customer $payee; |
|
17
|
|
|
public ?Attributes $params; |
|
18
|
|
|
public ?Model $model = null; |
|
19
|
|
|
|
|
20
|
|
|
public static function make(?string $transactionId = null): self |
|
21
|
|
|
{ |
|
22
|
|
|
return (new self())->id($transactionId ?? Str::random(10)); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public function id(string $transactionId): self |
|
26
|
|
|
{ |
|
27
|
|
|
$this->transactionId = $transactionId; |
|
28
|
|
|
|
|
29
|
|
|
return $this; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function charge(float $amount): self |
|
33
|
|
|
{ |
|
34
|
|
|
$this->amount = $amount; |
|
35
|
|
|
|
|
36
|
|
|
return $this; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function for(string $productInfo): self |
|
40
|
|
|
{ |
|
41
|
|
|
$this->productInfo = $productInfo; |
|
42
|
|
|
|
|
43
|
|
|
return $this; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function to(Customer $payee): self |
|
47
|
|
|
{ |
|
48
|
|
|
$this->payee = $payee; |
|
49
|
|
|
|
|
50
|
|
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function with(Attributes $params): self |
|
54
|
|
|
{ |
|
55
|
|
|
$this->params = $params; |
|
56
|
|
|
|
|
57
|
|
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function against(Model $model): self |
|
61
|
|
|
{ |
|
62
|
|
|
$this->model = $model; |
|
63
|
|
|
|
|
64
|
|
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function toArray(): array |
|
68
|
|
|
{ |
|
69
|
|
|
return [ |
|
70
|
|
|
'txnid' => $this->transactionId, |
|
71
|
|
|
'amount' => $this->amount, |
|
72
|
|
|
'productinfo' => $this->productInfo, |
|
73
|
|
|
]; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @throws ValidationException |
|
78
|
|
|
*/ |
|
79
|
|
|
public function validate(): array |
|
80
|
|
|
{ |
|
81
|
|
|
return Validator::make($this->toArray(), [ |
|
82
|
|
|
'txnid' => 'required|string', |
|
83
|
|
|
'amount' => 'required|numeric', |
|
84
|
|
|
'productinfo' => 'required|string', |
|
85
|
|
|
])->validate(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function fields(): array |
|
89
|
|
|
{ |
|
90
|
|
|
return collect($this->toArray()) |
|
91
|
|
|
->merge($this->payee->fields()) |
|
92
|
|
|
->merge($this->params->fields()) |
|
93
|
|
|
->all(); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|