Repository navigation
Expand file tree
/
Copy pathInteractsWithOneclick.php
More file actions
146 lines (124 loc) · 3.56 KB
/
Copy pathInteractsWithOneclick.php
File metadata and controls
146 lines (124 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
namespace Laragear\Transbank\Livewire;
use Laragear\Transbank\Services\Oneclick;
use Laragear\Transbank\Services\Transactions\Transaction;
use Livewire\Attributes\Url;
use Throwable;
trait InteractsWithOneclick
{
/**
* The Oneclick Token for transactions.
*
* @var string|null
*/
#[Url]
public ?string $TBK_TOKEN = null;
/**
* The Buy Order for failed transactions (returned as fallback for aborted/failed transactions)
*
* @var string
*/
#[Url]
public ?string $TBK_ORDEN_COMPRA = null;
/**
* Determines if the transaction was successful or not.
*
* @var bool|null
*/
public ?bool $isSuccessful = null;
/**
* Handles the incoming Webpay request.
*/
public function mountInteractsWithOneclick(Oneclick $oneclick): void
{
if ($this->isOneclickReturn()) {
$this->beforeHandledRegistration();
try {
$transaction = $this->handleOneclickRegistration($oneclick);
} catch (Throwable $exception) {
$result = $this->handleOneclickException($exception);
// If the result is falsy (null, false, etc.), just return and do nothing else.
if (!$result) {
return;
}
throw $result instanceof Throwable ? $result : $exception;
}
$this->afterRegistrationReceived($transaction);
if ($this->isSuccessful = $this->handleRegistrationStatus($transaction)) {
$this->handleSuccessfulRegistration($transaction);
} else {
$this->handleFailedRegistration($transaction);
}
$this->afterHandledRegistration($transaction);
} else {
$this->handleNonOneclickResponse();
}
}
/**
* Determine if the current request is byproduct of a Oneclick transaction.
*/
protected function isOneclickReturn(): bool
{
return (bool) $this->TBK_TOKEN;
}
/**
* Run something before handling the Oneclick Transaction.
*/
protected function beforeHandledRegistration(): void
{
//
}
/**
* Retrieve the Oneclick Transaction.
*/
protected function handleOneclickRegistration(Oneclick $oneclick): Transaction
{
return $oneclick->confirm($this->TBK_TOKEN);
}
/**
* Handle the transaction after being received.
*/
protected function afterRegistrationReceived(Transaction $transaction): void
{
// ...
}
/**
* Determine if the transaction is successful or not.
*/
protected function handleRegistrationStatus(Transaction $transaction): bool
{
return $transaction->isSuccessful();
}
/**
* Handle the exception given by Oneclick.
*/
protected function handleOneclickException(Throwable $exception): mixed
{
return $exception;
}
/**
* Handle a successful transaction.
*/
abstract protected function handleSuccessfulRegistration(Transaction $transaction): void;
/**
* Handle a failed transaction
*/
protected function handleFailedRegistration(Transaction $transaction): void
{
//
}
/**
* Execute something after the transaction has been handled.
*/
protected function afterHandledRegistration(Transaction $transaction): void
{
//
}
/**
* Handle a response that does not come from Oneclick.
*/
protected function handleNonOneclickResponse(): void
{
//
}
}