Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/CLAUDE.md

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions backend/app-modules/ticketing/routes/ticketing-routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use Illuminate\Support\Facades\Route;
use PHPeste\Ticketing\Http\Controllers\AttendeeOrderController;
use PHPeste\Ticketing\Http\Controllers\AttendeeTicketController;
use PHPeste\Ticketing\Http\Controllers\CheckoutOrderController;
use PHPeste\Ticketing\Http\Controllers\CouponController;
Expand Down Expand Up @@ -88,3 +89,7 @@
Route::post('/{ticket}/transfer', [AttendeeTicketController::class, 'transfer'])->whereUuid('ticket')->middleware('throttle:6,1')->name('ticketing.attendee.tickets.transfer');
Route::post('/{ticket}/resend', [AttendeeTicketController::class, 'resend'])->whereUuid('ticket')->middleware('throttle:3,1')->name('ticketing.attendee.tickets.resend');
});

Route::apiV1PublishedEdition('attendee', 'orders', ['auth:sanctum'])
->get('/', [AttendeeOrderController::class, 'index'])
->name('ticketing.attendee.orders.index');
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace PHPeste\Ticketing\Http\Controllers;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use PHPeste\Community\Tenancy\CurrentEdition;
use PHPeste\Payments\Contracts\PaymentLedger;
use PHPeste\Ticketing\Http\Resources\AttendeeOrderResource;
use PHPeste\Ticketing\Models\Order;
use PHPeste\Ticketing\Tickets\TicketOwnership;

class AttendeeOrderController
{
public function __construct(
private readonly CurrentEdition $currentEdition,
private readonly TicketOwnership $ownership,
private readonly PaymentLedger $ledger,
) {}

public function index(Request $request): JsonResponse
{
$user = $request->user();
abort_unless($user instanceof Authenticatable, 401);

$editionId = (string) $this->currentEdition->id();
$id = (string) $user->getAuthIdentifier();
$email = $this->ownership->verifiedEmail($user);

$orders = Order::query()->forEdition($editionId)
->where(fn (Builder $query) => $query
->where('user_id', $id)
->when($email !== null, fn (Builder $byEmail) => $byEmail->orWhere('buyer_email', $email)))
->with('items')
->latest()
->orderByDesc('number')
->get();

return response()->json([
'data' => $orders->map(function (Order $order) use ($request, $editionId): array {
$resource = new AttendeeOrderResource($order);
$resource->refunds = $this->ledger->refunds($editionId, $order->id);

return $resource->resolve($request);
})->all(),
'meta' => ['email_verified' => $email !== null],
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace PHPeste\Ticketing\Http\Resources;

use App\Http\Resources\ApiResource;
use Illuminate\Http\Request;
use PHPeste\Payments\Data\RefundRecord;
use PHPeste\Ticketing\Models\Order;
use PHPeste\Ticketing\Models\OrderItem;

class AttendeeOrderResource extends ApiResource
{
/**
* @var list<RefundRecord>
*/
public array $refunds = [];

/**
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
$order = $this->resource;

if (!$order instanceof Order) {
return [];
}

return [
'id' => $order->id,
'number' => $order->number,
'status' => $order->status->value,
'status_label' => $order->status->label(),
'subtotal' => $this->money($order->subtotal_cents),
'discount' => $this->money($order->discount_cents),
'fee' => $this->money($order->fee_cents),
'total' => $this->money($order->total_cents),
'refunded' => $this->money($order->refunded_cents),
'coupon_code' => $order->coupon_code,
'created_at' => $this->isoDate($order->created_at),
'paid_at' => $this->isoDate($order->paid_at),
'expires_at' => $this->isoDate($order->expires_at),
'items' => $order->items->map(fn (OrderItem $item): array => [
'id' => $item->id,
'ticket_type_name' => $item->ticket_type_name,
'batch_name' => $item->batch_name,
'attendee_name' => $item->attendee_name,
'total' => $this->money($item->total_cents),
'cancelled_at' => $this->isoDate($item->cancelled_at),
])->all(),
'refunds' => array_map(fn (RefundRecord $refund): array => [
'id' => $refund->id,
'amount' => $this->money($refund->amountCents),
'status' => $refund->status->value,
'status_label' => $refund->status->label(),
'reason_label' => $refund->reason->label(),
'created_at' => $this->isoDate($refund->createdAt),
'completed_at' => $this->isoDate($refund->completedAt),
], $this->refunds),
];
}
}
2 changes: 1 addition & 1 deletion backend/app-modules/ticketing/src/Tickets/TicketMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function previewDataReminder(string $editionId, string $email, string $na

private function ticketsUrl(string $editionId): string
{
return mb_rtrim($this->sites->url($editionId), '/').'/ingressos';
return mb_rtrim($this->sites->url($editionId), '/').'/conta/ingressos';
}

/**
Expand Down
83 changes: 83 additions & 0 deletions backend/app-modules/ticketing/tests/Feature/AttendeeOrdersTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

use Laravel\Sanctum\Sanctum;
use PHPeste\Community\Enums\EditionRole;
use PHPeste\Community\Enums\EditionStatus;
use PHPeste\Community\Models\Edition;
use PHPeste\Community\Models\EditionMember;
use PHPeste\Identity\Models\User;
use PHPeste\Payments\Drivers\FakeGateway;
use PHPeste\Payments\Models\PaymentAccount;
use PHPeste\Payments\Models\PaymentCharge;
use PHPeste\Ticketing\Models\TicketBatch;
use PHPeste\Ticketing\Models\TicketType;

beforeEach(function (): void {
FakeGateway::reset();
$this->travelTo('2027-06-10 12:00:00');
$this->edition = Edition::factory()->status(EditionStatus::SalesOpen)->create(['name' => 'PHPeste 2027']);
$this->type = TicketType::create(['edition_id' => $this->edition->id, 'name' => 'Geral']);
TicketBatch::create(['edition_id' => $this->edition->id, 'ticket_type_id' => $this->type->id, 'name' => '1º lote', 'price_cents' => 15_000, 'quantity' => 10]);
PaymentAccount::create(['edition_id' => $this->edition->id, 'driver' => 'fake', 'environment' => 'sandbox', 'credentials' => ['api_key' => 'fake_key_1234', 'webhook_secret' => 'segredo-webhook'], 'credentials_hint' => '1234', 'holder_type' => 'individual', 'holder_name' => 'Fulana', 'holder_document_masked' => '***.***.247-**', 'methods' => ['pix', 'card'], 'max_installments' => 3, 'webhook_token' => str_repeat('a', 48), 'verified_at' => now(), 'webhook_verified_at' => now()]);

$this->checkout = "/api/v1/checkout/editions/{$this->edition->id}/orders";
$order = $this->postJson($this->checkout, [
'buyer' => ['name' => 'Ana Souza', 'email' => 'Ana@Exemplo.com', 'document' => '529.982.247-25'],
'items' => [['ticket_type_id' => $this->type->id, 'attendees' => [['name' => 'Ana Souza', 'email' => 'ana@exemplo.com'], ['name' => 'Bia Lima', 'email' => 'bia@exemplo.com']]]],
], ['Idempotency-Key' => 'pedido-1'])->assertCreated()->json('data');
$headers = ['X-Order-Token' => $order['access_token']];
$this->postJson("{$this->checkout}/{$order['id']}/payment", ['method' => 'pix'], $headers)->assertOk();
FakeGateway::pay(PaymentCharge::query()->forEdition($this->edition->id)->sole()->gateway_charge_id);
$this->travel(6)->seconds();
$this->getJson("{$this->checkout}/{$order['id']}/payment", $headers)->assertJsonPath('data.order_status', 'paid');

$this->orderId = $order['id'];
$this->mine = "/api/v1/attendee/editions/{$this->edition->id}/orders";
});

it('quem comprou com o e-mail verificado vê o pedido, os itens e os reembolsos, sem CPF nem dados internos', function (): void {
$organizer = User::factory()->create();
EditionMember::factory()->for($this->edition)->create(['user_id' => $organizer->id, 'role' => EditionRole::Organizer]);
Sanctum::actingAs($organizer);
$this->postJson("/api/v1/editions/{$this->edition->id}/ticketing/orders/{$this->orderId}/refunds", ['reason' => 'buyer_request', 'note' => 'Pediu pelo Instagram', 'amount_cents' => 15_000])->assertCreated();

Sanctum::actingAs(User::factory()->create(['email' => 'ana@exemplo.com']));
$response = $this->getJson($this->mine)->assertOk()
->assertJsonPath('meta.email_verified', true)
->assertJsonCount(1, 'data')
->assertJsonPath('data.0.id', $this->orderId)
->assertJsonPath('data.0.status', 'paid')
->assertJsonPath('data.0.total.amount', 30_000)
->assertJsonPath('data.0.refunded.amount', 15_000)
->assertJsonPath('data.0.items.1.attendee_name', 'Bia Lima')
->assertJsonPath('data.0.refunds.0.amount.amount', 15_000)
->assertJsonPath('data.0.refunds.0.status', 'completed')
->assertJsonPath('data.0.refunds.0.reason_label', 'Pedido de quem comprou');

expect($response->json('data.0'))->not->toHaveKeys(['buyer', 'gateway_fee', 'payment_account_id', 'access_token'])
->and($response->json('data.0.refunds.0'))->not->toHaveKeys(['note', 'requested_by']);
});

it('pedido feito logado aparece mesmo sem o e-mail verificado, e o de outra pessoa nunca', function (): void {
$caio = User::factory()->unverified()->create(['email' => 'caio@exemplo.com']);
Sanctum::actingAs($caio);
$this->postJson($this->checkout, [
'buyer' => ['name' => 'Caio', 'email' => 'caio@exemplo.com', 'document' => '529.982.247-25'],
'items' => [['ticket_type_id' => $this->type->id, 'attendees' => [['name' => 'Caio', 'email' => 'caio@exemplo.com']]]],
], ['Idempotency-Key' => 'pedido-2'])->assertCreated();

$this->getJson($this->mine)->assertOk()
->assertJsonPath('meta.email_verified', false)
->assertJsonCount(1, 'data')
->assertJsonPath('data.0.status', 'created')
->assertJsonPath('data.0.refunds', []);

Sanctum::actingAs(User::factory()->unverified()->create(['email' => 'ana@exemplo.com']));
$this->getJson($this->mine)->assertOk()->assertJsonCount(0, 'data');
});

it('sem sessão não lista pedidos', function (): void {
$this->getJson($this->mine)->assertUnauthorized();
});
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ function emails(string $editionId, EmailTemplate $template): array
->and($tickets->pluck('holder_email')->all())->toBe(['ana@exemplo.com', 'bia@exemplo.com'])
->and($tickets->first()->code)->toMatch('/^[2-9A-HJ-NP-Z]{4}-[2-9A-HJ-NP-Z]{4}$/')
->and(emails($this->edition->id, EmailTemplate::OrderConfirmed))->toBe(['ana@exemplo.com', 'bia@exemplo.com'])
->and(EmailDelivery::query()->forEdition($this->edition->id)->where('template', EmailTemplate::OrderConfirmed->value)->first()->action_url)->toEndWith('/ingressos');
->and(EmailDelivery::query()->forEdition($this->edition->id)->where('template', EmailTemplate::OrderConfirmed->value)->first()->action_url)->toEndWith('/conta/ingressos');

expect(resolve(IssueTickets::class)->handle($this->order))->toBe([])
->and(Ticket::query()->forEdition($this->edition->id)->count())->toBe(2);
Expand Down
Binary file modified docs/arquitetura/PHPeste_Arquitetura.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/arquitetura/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
| 19 | Telas: organização (patrocinadores, fornecedores, finanças, tarefas, comunicação; cresce a cada tela do Marco 2) |
| 20 | Onde estamos e o que falta |

Retrato de 26/11/2026, com a P05 (checkout: pagamento por Pix e cartão).
Retrato de 27/11/2026, com a P06 (conta do participante: ingressos, crachá, transferência e pedidos).

## Como atualizar

Expand Down
Loading
Loading