Skip to content

Commit

Permalink
The "return_route" parameter has been added to the gateway configurat…
Browse files Browse the repository at this point in the history
…ion, allowing you to specify the route name and set parameters, including dynamic ones.
  • Loading branch information
arhitov committed Apr 22, 2024
1 parent 688aecf commit c44d866
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
15 changes: 15 additions & 0 deletions config/billing.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,21 @@
],
'return_url' => 'https://www.example.com/pay',
'capture' => false,
],
'yookassa-use-route-name' => [
'omnipay_class' => 'YooKassa',
'omnipay_initialize' => [
'shop_id' => 54401,
'secret' => 'test_Fh8hUAVVBGUGbjmlzba6TB0iyUbos_lueTHE-axOwM0',
],
'return_route' => [
'name' => 'yookassa-return-url',
/**
* If you specify "operation_uuid" with a null value, the value will be substituted automatically.
* Other parameters will be added to the address bar.
*/
'parameters' => ['operation_uuid' => null, 'order' => 123],
],
]
],
'payment' => [
Expand Down
5 changes: 3 additions & 2 deletions src/Models/Traits/ModelOwnerExpandTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,21 +378,22 @@ final public function createPayment(
$omnipayGateway = new OmnipayGateway($gatewayName);

// Creating a balance increase record
$operationUuid = Str::orderedUuid()->toString();
$increase = new Increase(
$balance,
$amount,
gateway: $omnipayGateway->getGatewayName(),
description: $description,
operation_identifier: 'payment',
operation_uuid: Str::orderedUuid()->toString(),
operation_uuid: $operationUuid,
);
$increase->createOrFail();
$operation = $increase->getOperation();

$response = $omnipayGateway->getGateway()->purchase(array_filter([
'amount' => $amount,
'currency' => $balance->currency->value,
'returnUrl' => $omnipayGateway->getReturnUrl(),
'returnUrl' => $omnipayGateway->getReturnUrl(['operation_uuid' => $operationUuid]),
'transactionId' => $operation->operation_uuid,
'description' => $operation->description,
'capture' => $omnipayGateway->getCapture(),
Expand Down
24 changes: 22 additions & 2 deletions src/OmnipayGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,31 @@ public function getGatewayName(): string
}

/**
* @param array $parameters using only for return_route
* @return string|null
*/
public function getReturnUrl(): ?string
public function getReturnUrl(array $parameters = []): ?string
{
return $this->gatewayConfig['return_url'] ?? null;
return match (true) {
! empty($this->gatewayConfig['return_url']) => $this->gatewayConfig['return_url'],
! empty($this->gatewayConfig['return_route']) => (function() use ($parameters) {
if (is_array($this->gatewayConfig['return_route'])) {
$parametersConfig = $this->gatewayConfig['return_route']['parameters'] ?? [];
array_walk(
$parametersConfig,
fn(&$value, $key) => $value = (is_null($value) && array_key_exists($key,
$parameters)) ? $parameters[$key] : $value,
);
return route(
$this->gatewayConfig['return_route']['name'],
$parametersConfig,
);
} else {
return route($this->gatewayConfig['return_route']);
}
})(),
default => null
};
}

/**
Expand Down
37 changes: 37 additions & 0 deletions tests/Feature/CreatePaymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@

class CreatePaymentTest extends FeatureTestCase
{
public function setUp(): void
{
parent::setUp();

/** @var \Illuminate\Routing\Router $router */
$router = $this->app['router'];
$router->get('yookassa/return-url')->name('yookassa-return-url');

/** @var \Illuminate\Routing\UrlGenerator $url */
$url = $this->app['url'];
$url->forceScheme('https');
}

/**
* @return void
Expand Down Expand Up @@ -100,4 +112,29 @@ public function testCreateByYooKassa()
$this->assertNotEmpty($payment->getResponse()->getTransactionReference());
$this->assertNotEmpty($payment->getResponse()->getRedirectUrl());
}

/**
* @depends testCreateByYooKassa
* @return void
*/
public function testCreateUseRouteName()
{
$balanceAmount = 456;
$owner = $this->createOwner();
$owner->getBalanceOrCreate();

$payment = $owner->createPayment(
$balanceAmount,
'Test payment',
gatewayName: 'yookassa-use-route-name',
);

$returnUrl = $payment->getResponse()->getRequest()->getReturnUrl();

$this->assertNotEmpty($returnUrl);
$this->assertEquals(
route('yookassa-return-url', ['operation_uuid' => $payment->getIncrease()->getOperation()->operation_uuid, 'order' => 123]),
$returnUrl,
);
}
}

0 comments on commit c44d866

Please sign in to comment.