Skip to content

Commit

Permalink
Fix defered listeners and payment adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
FlamesONE committed Apr 10, 2024
1 parent dee727f commit ef2682e
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 20 deletions.
8 changes: 8 additions & 0 deletions app/Core/Admin/Http/Controllers/Api/ModulesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public function enable(FluteRequest $request, string $key): Response
$this->actions->activateModule($mopd);
user()->log('events.module_activated', $key);

cache()->delete('flute.deferred_listeners');

return $this->success();
} catch (\Exception $e) {
return $this->error($e->getMessage());
Expand All @@ -77,6 +79,8 @@ public function disable(FluteRequest $request, string $key): Response
$this->actions->disableModule($mopd);
user()->log('events.module_disabled', $key);

cache()->delete('flute.deferred_listeners');

return $this->success();
} catch (\Exception $e) {
return $this->error($e->getMessage());
Expand All @@ -91,6 +95,8 @@ public function install(FluteRequest $request, string $key): Response
if (!$this->actions->installModule($mopd))
return $this->error(__('def.unknown_error'));

cache()->delete('flute.deferred_listeners');

user()->log('events.module_installed', $key);

return $this->success();
Expand Down Expand Up @@ -203,6 +209,7 @@ protected function processZipFile(UploadedFile $file): Response
cache()->delete('flute.modules.alldb');
cache()->delete('flute.modules.array');
cache()->delete('flute.modules.json');
cache()->delete('flute.deferred_listeners');

user()->log('events.module_uploaded', $folderName);

Expand Down Expand Up @@ -264,6 +271,7 @@ public function delete(FluteRequest $request, string $key): Response
cache()->delete('flute.modules.alldb');
cache()->delete('flute.modules.array');
cache()->delete('flute.modules.json');
cache()->delete('flute.deferred_listeners');

user()->log('events.module_deleted', $key);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ private function getGatewayParameters($gatewayInstance)
{
$parameters = [];
$reflectionClass = new \ReflectionClass($gatewayInstance);
$except = ['currency', 'amount', 'transactionId'];

foreach ($reflectionClass->getMethods() as $method) {
if (strpos($method->name, 'get') === 0) {
Expand All @@ -205,7 +206,8 @@ private function getGatewayParameters($gatewayInstance)
$source = implode("\n", array_slice(explode("\n", $methodBody), $startLine, $length));

if (preg_match('/->getParameter\([\'"]([^\'"]+)[\'"]\)/', $source, $matches)) {
$parameters[] = $matches[1];
if (!in_array($matches[1], $except))
$parameters[] = $matches[1];
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions app/Core/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ public function run(): void
$router = $this->get(RouteDispatcher::class);
$res = $this->responseEvent($router->handle($this->get(FluteRequest::class)));

events()->saveDeferredListenersToCache();

// Ставим новый Response из ивента в возвращаемый объект.
// Вдруг кто-то там что-то поменял, и нам надо вернуть новый объект
if (is_debug() && !(strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'))
Expand Down
8 changes: 4 additions & 4 deletions app/Core/Payments/PaymentProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function handlePayment(string $gatewayName)
throw new \Exception('Gateway is not exists');

$gatewayEntity = rep(PaymentGateway::class)
->findOne(['enabled' => true, 'name' => $gatewayName]);
->findOne(['enabled' => true, 'adapter' => $gatewayName]);

if (!$gatewayEntity)
throw new \Exception("Gateway wasn't found");
Expand Down Expand Up @@ -237,8 +237,8 @@ public function processPayment(PaymentInvoice $invoice, PaymentGateway $gatewayE
$paymentData = array_merge([
'amount' => $invoice->originalAmount,
'transactionId' => $invoice->transactionId,
'cancelUrl' => url('/lk/fail'),
'returnUrl' => url('/lk/success'),
'cancelUrl' => url('/lk/fail')->get(),
'returnUrl' => url('/lk/success')->get(),
], (array) $additional);

if (!isset($paymentData['currency']))
Expand All @@ -251,7 +251,7 @@ public function processPayment(PaymentInvoice $invoice, PaymentGateway $gatewayE
$gatewayEntity = $event->getPaymentGateway();
$invoice = $event->getInvoice();

$paymentData['notifyUrl'] = url('/api/lk/handle/' . $gateway->getShortName());
$paymentData['notifyUrl'] = url('/api/lk/handle/' . $gateway->getShortName())->get();

$response = $gateway->purchase($paymentData)->send();

Expand Down
41 changes: 26 additions & 15 deletions app/Core/Support/FluteEventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class FluteEventDispatcher extends EventDispatcher
{
private $cache;
private $deferredListenersKey = 'flute.deferred_listeners';
private $deferredListeners = [];

public function __construct(CacheInterface $cache)
{
Expand All @@ -19,40 +20,50 @@ public function __construct(CacheInterface $cache)

public function addDeferredListener($eventName, $listener, $priority = 0)
{
$deferredListeners = $this->cache->get($this->deferredListenersKey, function () {
return [];
});
$listenerId = $this->getListenerId($listener);

if (!is_array($deferredListeners)) {
$deferredListeners = [];
if (!isset($this->deferredListeners[$eventName])) {
$this->deferredListeners[$eventName] = [];
}

$this->deferredListeners[$eventName][$listenerId] = ['listener' => $listener, 'priority' => $priority];

$this->addListener($eventName, $listener, $priority);
}

public function removeDeferredListener($eventName, $listener)
{
$listenerId = $this->getListenerId($listener);

if (!isset($deferredListeners[$eventName])) {
$deferredListeners[$eventName] = [];
}
if (isset($this->deferredListeners[$eventName][$listenerId])) {
unset($this->deferredListeners[$eventName][$listenerId]);

if (!isset($deferredListeners[$eventName][$listenerId])) {
$deferredListeners[$eventName][$listenerId] = ['listener' => $listener, 'priority' => $priority];
$this->cache->set($this->deferredListenersKey, $deferredListeners);
if (empty($this->deferredListeners[$eventName])) {
unset($this->deferredListeners[$eventName]);
}

$this->cache->set($this->deferredListenersKey, $this->deferredListeners);
}

$this->addListener($eventName, $listener, $priority);
$this->removeListener($eventName, $listener);
}

public function saveDeferredListenersToCache()
{
$this->cache->set($this->deferredListenersKey, $this->deferredListeners);
}

private function initializeDeferredListeners()
{
$deferredListeners = $this->cache->get($this->deferredListenersKey, function () {
return [];
});
$deferredListeners = $this->cache->get($this->deferredListenersKey, []);

foreach ($deferredListeners as $eventName => $listeners) {
foreach ($listeners as $listenerData) {
$this->addListener($eventName, $listenerData['listener'], $listenerData['priority']);
}
}

$this->deferredListeners = $deferredListeners;
}

private function getListenerId($listener)
Expand Down

0 comments on commit ef2682e

Please sign in to comment.