Skip to content

Commit 067e52c

Browse files
committed
add order signature to meta in response after checkout
fix test
1 parent 5a409fb commit 067e52c

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

packages/api/src/Domain/Carts/Http/Controllers/CheckoutCartController.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public function checkout(
4343
'product_lines',
4444
'product_lines.purchasable',
4545
])
46+
->withMeta([
47+
'order_signature' => $order->getSelfLinkSignature(),
48+
])
4649
->didCreate();
4750
}
4851
}

packages/api/src/Domain/Orders/Concerns/InteractsWithDystoreApi.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,34 @@
88
use Illuminate\Database\Eloquent\Relations\HasMany;
99
use Illuminate\Database\Eloquent\Relations\HasOne;
1010
use Illuminate\Support\Facades\Config;
11+
use Illuminate\Support\Facades\URL;
1112
use Lunar\Base\Casts\Price;
1213
use Lunar\Models\Transaction;
1314

1415
trait InteractsWithDystoreApi
1516
{
1617
use HashesRouteKey;
1718

19+
public function getSelfLinkSignature(): ?string
20+
{
21+
$signedUrl = URL::signedRoute(
22+
name: 'v1.orders.show',
23+
parameters: ['order' => $this->getRouteKey()],
24+
absolute: false,
25+
);
26+
27+
$signature = null;
28+
29+
$query = (string) parse_url($signedUrl, PHP_URL_QUERY);
30+
if ($query !== '') {
31+
/** @var array<string, mixed> $params */
32+
parse_str($query, $params);
33+
$signature = is_string($params['signature'] ?? null) ? $params['signature'] : null;
34+
}
35+
36+
return $signature;
37+
}
38+
1839
/**
1940
* Attribute activity log blacklist.
2041
*

tests/api/Feature/Domain/Cart/JsonApi/V1/CheckoutCartTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,3 +430,50 @@
430430
]
431431
);
432432
});
433+
434+
it('returns order signature in meta after checkout', function () {
435+
/** @var TestCase $this */
436+
Config::set('dystore.general.checkout.checkout_protection_strategy', CheckoutProtectionStrategy::SIGNATURE);
437+
438+
/** @var CartFactory $factory */
439+
$factory = Cart::factory();
440+
441+
/** @var Cart $cart */
442+
$cart = $factory
443+
->withAddresses()
444+
->withLines()
445+
->create();
446+
447+
/** @var CartSessionManager $cartSession */
448+
$cartSession = App::make(CartSessionInterface::class);
449+
$cartSession->use($cart);
450+
451+
$response = $this
452+
->jsonApi()
453+
->expects('orders')
454+
->withData([
455+
'type' => 'carts',
456+
'attributes' => [
457+
'agree' => true,
458+
'create_user' => false,
459+
],
460+
])
461+
->post(serverUrl('/carts/-actions/checkout'));
462+
463+
$orderId = $response->json('data.id');
464+
465+
ray($response->json('meta.order_signature'));
466+
467+
$response
468+
->assertSuccessful()
469+
->assertCreatedWithServerId(
470+
serverUrl('/orders', true),
471+
[
472+
'type' => 'orders',
473+
'id' => (string) $response->json('data.id'),
474+
]
475+
)
476+
->assertMeta([
477+
'order_signature' => $response->json('meta.order_signature'),
478+
]);
479+
});

0 commit comments

Comments
 (0)