Skip to content

Commit 0827655

Browse files
authored
Merge pull request #454 from HiEventsDev/fix/widget-blocked-in-firefox
Fix ticket widget not working in Firefox and Safari
2 parents 009127d + dabbb16 commit 0827655

File tree

9 files changed

+84
-16
lines changed

9 files changed

+84
-16
lines changed

backend/app/DomainObjects/OrderDomainObject.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class OrderDomainObject extends Generated\OrderDomainObjectAbstract implements I
2929

3030
public ?EventDomainObject $event = null;
3131

32+
public ?string $sessionIdentifier = null;
33+
3234
public static function getAllowedFilterFields(): array
3335
{
3436
return [
@@ -253,4 +255,15 @@ public function getInvoices(): ?Collection
253255
{
254256
return $this->invoices;
255257
}
258+
259+
public function setSessionIdentifier(?string $sessionIdentifier): OrderDomainObject
260+
{
261+
$this->sessionIdentifier = $sessionIdentifier;
262+
return $this;
263+
}
264+
265+
public function getSessionIdentifier(): ?string
266+
{
267+
return $this->sessionIdentifier;
268+
}
256269
}

backend/app/Http/Actions/Orders/Public/CreateOrderActionPublic.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,12 @@ public function __invoke(CreateOrderRequest $request, int $eventId): JsonRespons
4848
])
4949
);
5050

51+
$order->setSessionIdentifier($sessionId);
52+
5153
$response = $this->resourceResponse(
5254
resource: OrderResourcePublic::class,
5355
data: $order,
54-
statusCode: ResponseCodes::HTTP_CREATED
56+
statusCode: ResponseCodes::HTTP_CREATED,
5557
);
5658

5759
return $response->withCookie(

backend/app/Http/Actions/Orders/Public/GetOrderActionPublic.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
use HiEvents\Resources\Order\OrderResourcePublic;
77
use HiEvents\Services\Application\Handlers\Order\DTO\GetOrderPublicDTO;
88
use HiEvents\Services\Application\Handlers\Order\GetOrderPublicHandler;
9+
use HiEvents\Services\Infrastructure\Session\CheckoutSessionManagementService;
910
use Illuminate\Http\JsonResponse;
1011
use Illuminate\Http\Request;
1112

1213
class GetOrderActionPublic extends BaseAction
1314
{
1415
public function __construct(
15-
private readonly GetOrderPublicHandler $getOrderPublicHandler
16+
private readonly GetOrderPublicHandler $getOrderPublicHandler,
17+
private readonly CheckoutSessionManagementService $sessionService,
1618
)
1719
{
1820
}
@@ -25,9 +27,17 @@ public function __invoke(int $eventId, string $orderShortId, Request $request):
2527
includeEventInResponse: $this->isIncludeRequested($request, 'event'),
2628
));
2729

28-
return $this->resourceResponse(
30+
$response = $this->resourceResponse(
2931
resource: OrderResourcePublic::class,
3032
data: $order,
3133
);
34+
35+
if ($request->query->has('session_identifier')) {
36+
$response->headers->setCookie(
37+
$this->sessionService->getSessionCookie()
38+
);
39+
}
40+
41+
return $response;
3242
}
3343
}

backend/app/Resources/Order/OrderResourcePublic.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use HiEvents\Resources\Attendee\AttendeeResourcePublic;
99
use HiEvents\Resources\BaseResource;
1010
use HiEvents\Resources\Event\EventResourcePublic;
11-
use HiEvents\Resources\Order\Invoice\InvoiceResource;
1211
use HiEvents\Resources\Order\Invoice\InvoiceResourcePublic;
1312
use Illuminate\Http\Request;
1413

@@ -64,6 +63,9 @@ public function toArray(Request $request): array
6463
!is_null($this->getAttendees()),
6564
fn() => AttendeeResourcePublic::collection($this->getAttendees())
6665
),
66+
$this->mergeWhen($this->getSessionIdentifier() !== null, fn() => [
67+
'session_identifier' => $this->getSessionIdentifier(),
68+
]),
6769
];
6870
}
6971
}

backend/app/Services/Infrastructure/Session/CheckoutSessionManagementService.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,17 @@ public function __construct(
2222
}
2323

2424
/**
25-
* Get the session ID from the cookie, or generate a new one if it doesn't exist.
25+
* Get the session ID from query param, cookie, or generate a new one.
2626
*/
2727
public function getSessionId(): string
2828
{
2929
if ($this->sessionId) {
3030
return $this->sessionId;
3131
}
3232

33-
$this->sessionId = $this->request->cookie(self::SESSION_IDENTIFIER) ?? $this->createSessionId();
33+
$this->sessionId = $this->request->query(self::SESSION_IDENTIFIER)
34+
?? $this->request->cookie(self::SESSION_IDENTIFIER)
35+
?? $this->createSessionId();
3436

3537
return $this->sessionId;
3638
}

frontend/src/api/order.client.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,24 @@ export const orderClientPublic = {
116116
return response.data;
117117
},
118118

119-
findByShortId: async (eventId: number, orderShortId: string, includes: string[] = []) => {
120-
const response = await publicApi.get<GenericDataResponse<Order>>(`events/${eventId}/order/${orderShortId}?include=${includes.join(',')}`);
119+
findByShortId: async (
120+
eventId: number,
121+
orderShortId: string,
122+
includes: string[] = [],
123+
sessionIdentifier?: string
124+
) => {
125+
const query = new URLSearchParams();
126+
if (includes.length > 0) {
127+
query.append("include", includes.join(","));
128+
}
129+
if (sessionIdentifier) {
130+
query.append("session_identifier", sessionIdentifier);
131+
}
132+
133+
const response = await publicApi.get<GenericDataResponse<Order>>(
134+
`events/${eventId}/order/${orderShortId}?${query.toString()}`
135+
);
136+
121137
return response.data;
122138
},
123139

frontend/src/components/routes/product-widget/SelectProducts/index.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ const SelectProducts = (props: SelectProductsProps) => {
103103
.then(() => {
104104
const url = '/checkout/' + eventId + '/' + data.data.short_id + '/details';
105105
if (props.widgetMode === 'embedded') {
106-
window.open(url, '_blank');
106+
window.open(
107+
url + '?session_identifier=' + data.data.session_identifier + '&utm_source=embedded_widget',
108+
'_blank'
109+
);
107110
setOrderInProcessOverlayVisible(true);
108111
return;
109112
}
Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,44 @@
11
import {useQuery} from "@tanstack/react-query";
22
import {orderClientPublic} from "../api/order.client.ts";
33
import {IdParam, Order} from "../types.ts";
4+
import {useMemo} from "react";
5+
import {isSsr} from "../utilites/helpers.ts";
46

5-
export const GET_ORDER_PUBLIC_QUERY_KEY = 'getOrderPublic';
7+
export const GET_ORDER_PUBLIC_QUERY_KEY = "getOrderPublic";
68

7-
export const useGetOrderPublic = (eventId: IdParam, orderShortId: IdParam, includes: string[] = []) => {
8-
return useQuery<Order>({
9-
queryKey: [GET_ORDER_PUBLIC_QUERY_KEY, eventId, orderShortId],
9+
const getSessionIdentifierFromUrl = (): string | null => {
10+
if (isSsr()) return null;
11+
12+
const url = new URL(window.location.href);
13+
return url.searchParams.get("session_identifier");
14+
};
15+
16+
export const useGetOrderPublic = (
17+
eventId: IdParam,
18+
orderShortId: IdParam,
19+
includes: string[] = []
20+
) => {
21+
const sessionIdentifier = useMemo(getSessionIdentifierFromUrl, []);
1022

23+
return useQuery<Order>({
24+
queryKey: [
25+
GET_ORDER_PUBLIC_QUERY_KEY,
26+
eventId,
27+
orderShortId,
28+
sessionIdentifier,
29+
],
1130
queryFn: async () => {
1231
const {data} = await orderClientPublic.findByShortId(
1332
Number(eventId),
1433
String(orderShortId),
1534
includes,
35+
sessionIdentifier ?? undefined
1636
);
1737
return data;
1838
},
19-
2039
refetchOnWindowFocus: false,
2140
staleTime: 500,
2241
retryOnMount: false,
23-
retry: false
42+
retry: false,
2443
});
25-
}
44+
};

frontend/src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,7 @@ export interface Order {
475475
question_answers?: QuestionAnswer[];
476476
event?: Event;
477477
latest_invoice?: Invoice;
478+
session_identifier?: string;
478479
}
479480

480481
export interface Invoice {

0 commit comments

Comments
 (0)