Skip to content

Commit 6198f67

Browse files
author
root
committed
feat: support itemized checkout payloads
1 parent 590e6f7 commit 6198f67

4 files changed

Lines changed: 30 additions & 3 deletions

File tree

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "kaninstein/multi-acquirer-checkout",
3+
"version": "1.0.16",
34
"description": "Multi-acquirer checkout package with DDD architecture for Laravel",
45
"keywords": [
56
"laravel",

src/Application/DTOs/PaymentRequest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
{
1212
/**
1313
* @param array<string, mixed> $metadata
14+
* @param array<int, array<string, mixed>> $items
1415
*/
1516
public function __construct(
1617
public Money $amount,
@@ -23,6 +24,7 @@ public function __construct(
2324
public bool $merchantAbsorbsFinancing = false,
2425
public string $feeResponsibility = 'buyer',
2526
public ?float $platformFeeRate = null,
27+
public array $items = [],
2628
) {}
2729

2830
public function isCardPayment(): bool

src/Infrastructure/Gateways/Pagarme/PagarmeGateway.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,15 +232,33 @@ private function configureFacade(): void
232232
*/
233233
private function buildOrderData(PaymentRequest $request): array
234234
{
235-
$order = [
236-
'items' => [
235+
$items = [];
236+
foreach ($request->items as $item) {
237+
$amountCents = (int) ($item['amount_cents'] ?? 0);
238+
if ($amountCents <= 0) {
239+
continue;
240+
}
241+
$items[] = [
242+
'amount' => $amountCents,
243+
'description' => (string) ($item['description'] ?? $item['name'] ?? $request->metadata['description'] ?? 'Checkout'),
244+
'quantity' => (int) ($item['quantity'] ?? 1),
245+
'code' => (string) ($item['code'] ?? $item['id'] ?? $request->metadata['item_code'] ?? 'ITEM-1'),
246+
];
247+
}
248+
249+
if ($items === []) {
250+
$items = [
237251
[
238252
'amount' => $request->amount->amountInCents,
239253
'description' => (string) ($request->metadata['description'] ?? 'Checkout'),
240254
'quantity' => 1,
241255
'code' => (string) ($request->metadata['item_code'] ?? 'ITEM-1'),
242256
],
243-
],
257+
];
258+
}
259+
260+
$order = [
261+
'items' => $items,
244262
'customer' => $this->buildCustomerData($request),
245263
'payments' => [
246264
$this->buildPaymentData($request),

src/Presentation/Http/Controllers/CheckoutController.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ public function process(Request $request): JsonResponse
3737
'card.cvv' => ['sometimes', 'string'],
3838
'card.token' => ['sometimes', 'string'],
3939
'metadata' => ['sometimes', 'array'],
40+
'items' => ['sometimes', 'array'],
41+
'items.*.amount_cents' => ['sometimes', 'integer', 'min:1'],
42+
'items.*.quantity' => ['sometimes', 'integer', 'min:1'],
43+
'items.*.code' => ['sometimes', 'string'],
44+
'items.*.description' => ['sometimes', 'string'],
4045
'gateway' => ['sometimes', 'string'],
4146
'platform_fee_rate' => ['sometimes', 'numeric', 'min:0', 'max:1'],
4247
'merchant_absorbs_financing' => ['sometimes', 'boolean'],
@@ -67,6 +72,7 @@ public function process(Request $request): JsonResponse
6772
merchantAbsorbsFinancing: (bool) ($validated['merchant_absorbs_financing'] ?? false),
6873
feeResponsibility: (string) ($validated['fee_responsibility'] ?? 'buyer'),
6974
platformFeeRate: array_key_exists('platform_fee_rate', $validated) ? (float) $validated['platform_fee_rate'] : null,
75+
items: (array) ($validated['items'] ?? []),
7076
));
7177

7278
return response()->json([

0 commit comments

Comments
 (0)