Skip to content

Commit 60c3f4c

Browse files
authored
Feature: Store Stripe platform fees (#869)
1 parent 6d81e93 commit 60c3f4c

15 files changed

+824
-21
lines changed
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
<?php
2+
3+
namespace HiEvents\DomainObjects\Generated;
4+
5+
/**
6+
* THIS FILE IS AUTOGENERATED - DO NOT EDIT IT DIRECTLY.
7+
* @package HiEvents\DomainObjects\Generated
8+
*/
9+
abstract class OrderPaymentPlatformFeeDomainObjectAbstract extends \HiEvents\DomainObjects\AbstractDomainObject
10+
{
11+
final public const SINGULAR_NAME = 'order_payment_platform_fee';
12+
final public const PLURAL_NAME = 'order_payment_platform_fees';
13+
final public const ID = 'id';
14+
final public const ORDER_ID = 'order_id';
15+
final public const PAYMENT_PLATFORM = 'payment_platform';
16+
final public const FEE_ROLLUP = 'fee_rollup';
17+
final public const PAYMENT_PLATFORM_FEE_AMOUNT = 'payment_platform_fee_amount';
18+
final public const APPLICATION_FEE_AMOUNT = 'application_fee_amount';
19+
final public const CURRENCY = 'currency';
20+
final public const TRANSACTION_ID = 'transaction_id';
21+
final public const PAID_AT = 'paid_at';
22+
final public const DELETED_AT = 'deleted_at';
23+
final public const CREATED_AT = 'created_at';
24+
final public const UPDATED_AT = 'updated_at';
25+
26+
protected int $id;
27+
protected int $order_id;
28+
protected string $payment_platform;
29+
protected array|string|null $fee_rollup = null;
30+
protected float $payment_platform_fee_amount;
31+
protected float $application_fee_amount = 0.0;
32+
protected string $currency = 'USD';
33+
protected ?string $transaction_id = null;
34+
protected ?string $paid_at = null;
35+
protected ?string $deleted_at = null;
36+
protected ?string $created_at = null;
37+
protected ?string $updated_at = null;
38+
39+
public function toArray(): array
40+
{
41+
return [
42+
'id' => $this->id ?? null,
43+
'order_id' => $this->order_id ?? null,
44+
'payment_platform' => $this->payment_platform ?? null,
45+
'fee_rollup' => $this->fee_rollup ?? null,
46+
'payment_platform_fee_amount' => $this->payment_platform_fee_amount ?? null,
47+
'application_fee_amount' => $this->application_fee_amount ?? null,
48+
'currency' => $this->currency ?? null,
49+
'transaction_id' => $this->transaction_id ?? null,
50+
'paid_at' => $this->paid_at ?? null,
51+
'deleted_at' => $this->deleted_at ?? null,
52+
'created_at' => $this->created_at ?? null,
53+
'updated_at' => $this->updated_at ?? null,
54+
];
55+
}
56+
57+
public function setId(int $id): self
58+
{
59+
$this->id = $id;
60+
return $this;
61+
}
62+
63+
public function getId(): int
64+
{
65+
return $this->id;
66+
}
67+
68+
public function setOrderId(int $order_id): self
69+
{
70+
$this->order_id = $order_id;
71+
return $this;
72+
}
73+
74+
public function getOrderId(): int
75+
{
76+
return $this->order_id;
77+
}
78+
79+
public function setPaymentPlatform(string $payment_platform): self
80+
{
81+
$this->payment_platform = $payment_platform;
82+
return $this;
83+
}
84+
85+
public function getPaymentPlatform(): string
86+
{
87+
return $this->payment_platform;
88+
}
89+
90+
public function setFeeRollup(array|string|null $fee_rollup): self
91+
{
92+
$this->fee_rollup = $fee_rollup;
93+
return $this;
94+
}
95+
96+
public function getFeeRollup(): array|string|null
97+
{
98+
return $this->fee_rollup;
99+
}
100+
101+
public function setPaymentPlatformFeeAmount(float $payment_platform_fee_amount): self
102+
{
103+
$this->payment_platform_fee_amount = $payment_platform_fee_amount;
104+
return $this;
105+
}
106+
107+
public function getPaymentPlatformFeeAmount(): float
108+
{
109+
return $this->payment_platform_fee_amount;
110+
}
111+
112+
public function setApplicationFeeAmount(float $application_fee_amount): self
113+
{
114+
$this->application_fee_amount = $application_fee_amount;
115+
return $this;
116+
}
117+
118+
public function getApplicationFeeAmount(): float
119+
{
120+
return $this->application_fee_amount;
121+
}
122+
123+
public function setCurrency(string $currency): self
124+
{
125+
$this->currency = $currency;
126+
return $this;
127+
}
128+
129+
public function getCurrency(): string
130+
{
131+
return $this->currency;
132+
}
133+
134+
public function setTransactionId(?string $transaction_id): self
135+
{
136+
$this->transaction_id = $transaction_id;
137+
return $this;
138+
}
139+
140+
public function getTransactionId(): ?string
141+
{
142+
return $this->transaction_id;
143+
}
144+
145+
public function setPaidAt(?string $paid_at): self
146+
{
147+
$this->paid_at = $paid_at;
148+
return $this;
149+
}
150+
151+
public function getPaidAt(): ?string
152+
{
153+
return $this->paid_at;
154+
}
155+
156+
public function setDeletedAt(?string $deleted_at): self
157+
{
158+
$this->deleted_at = $deleted_at;
159+
return $this;
160+
}
161+
162+
public function getDeletedAt(): ?string
163+
{
164+
return $this->deleted_at;
165+
}
166+
167+
public function setCreatedAt(?string $created_at): self
168+
{
169+
$this->created_at = $created_at;
170+
return $this;
171+
}
172+
173+
public function getCreatedAt(): ?string
174+
{
175+
return $this->created_at;
176+
}
177+
178+
public function setUpdatedAt(?string $updated_at): self
179+
{
180+
$this->updated_at = $updated_at;
181+
return $this;
182+
}
183+
184+
public function getUpdatedAt(): ?string
185+
{
186+
return $this->updated_at;
187+
}
188+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace HiEvents\DomainObjects;
4+
5+
class OrderPaymentPlatformFeeDomainObject extends Generated\OrderPaymentPlatformFeeDomainObjectAbstract
6+
{
7+
}

backend/app/Models/OrderItem.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,6 @@ protected function getCastMap(): array
3434
];
3535
}
3636

37-
protected function getFillableFields(): array
38-
{
39-
return [];
40-
}
41-
4237
public function product_price(): HasOne
4338
{
4439
return $this->hasOne(ProductPrice::class);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace HiEvents\Models;
4+
5+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
6+
use Illuminate\Database\Eloquent\SoftDeletes;
7+
8+
class OrderPaymentPlatformFee extends BaseModel
9+
{
10+
use SoftDeletes;
11+
12+
protected function getCastMap(): array
13+
{
14+
return [
15+
'fee_rollup' => 'array',
16+
];
17+
}
18+
19+
public function order(): BelongsTo
20+
{
21+
return $this->belongsTo(Order::class);
22+
}
23+
}

backend/app/Providers/RepositoryServiceProvider.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use HiEvents\Repository\Eloquent\MessageRepository;
2424
use HiEvents\Repository\Eloquent\OrderApplicationFeeRepository;
2525
use HiEvents\Repository\Eloquent\OrderItemRepository;
26+
use HiEvents\Repository\Eloquent\OrderPaymentPlatformFeeRepository;
2627
use HiEvents\Repository\Eloquent\OrderRefundRepository;
2728
use HiEvents\Repository\Eloquent\OrderRepository;
2829
use HiEvents\Repository\Eloquent\OrganizerRepository;
@@ -62,6 +63,7 @@
6263
use HiEvents\Repository\Interfaces\MessageRepositoryInterface;
6364
use HiEvents\Repository\Interfaces\OrderApplicationFeeRepositoryInterface;
6465
use HiEvents\Repository\Interfaces\OrderItemRepositoryInterface;
66+
use HiEvents\Repository\Interfaces\OrderPaymentPlatformFeeRepositoryInterface;
6567
use HiEvents\Repository\Interfaces\OrderRefundRepositoryInterface;
6668
use HiEvents\Repository\Interfaces\OrderRepositoryInterface;
6769
use HiEvents\Repository\Interfaces\OrganizerRepositoryInterface;
@@ -123,6 +125,7 @@ class RepositoryServiceProvider extends ServiceProvider
123125
WebhookRepositoryInterface::class => WebhookRepository::class,
124126
WebhookLogRepositoryInterface::class => WebhookLogRepository::class,
125127
OrderApplicationFeeRepositoryInterface::class => OrderApplicationFeeRepository::class,
128+
OrderPaymentPlatformFeeRepositoryInterface::class => OrderPaymentPlatformFeeRepository::class,
126129
AccountConfigurationRepositoryInterface::class => AccountConfigurationRepository::class,
127130
QuestionAndAnswerViewRepositoryInterface::class => QuestionAndAnswerViewRepository::class,
128131
OutgoingMessageRepositoryInterface::class => OutgoingMessageRepository::class,
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace HiEvents\Repository\Eloquent;
4+
5+
use HiEvents\DomainObjects\OrderPaymentPlatformFeeDomainObject;
6+
use HiEvents\Models\OrderPaymentPlatformFee;
7+
use HiEvents\Repository\Interfaces\OrderPaymentPlatformFeeRepositoryInterface;
8+
9+
class OrderPaymentPlatformFeeRepository extends BaseRepository implements OrderPaymentPlatformFeeRepositoryInterface
10+
{
11+
protected function getModel(): string
12+
{
13+
return OrderPaymentPlatformFee::class;
14+
}
15+
16+
public function getDomainObject(): string
17+
{
18+
return OrderPaymentPlatformFeeDomainObject::class;
19+
}
20+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace HiEvents\Repository\Interfaces;
4+
5+
use HiEvents\DomainObjects\OrderPaymentPlatformFeeDomainObject;
6+
use HiEvents\Repository\Eloquent\BaseRepository;
7+
8+
/**
9+
* @extends BaseRepository<OrderPaymentPlatformFeeDomainObject>
10+
*/
11+
interface OrderPaymentPlatformFeeRepositoryInterface extends RepositoryInterface
12+
{
13+
14+
}

backend/app/Services/Application/Handlers/Order/Payment/Stripe/IncomingWebhookHandler.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use HiEvents\Services\Application\Handlers\Order\Payment\Stripe\DTO\StripeWebhookDTO;
77
use HiEvents\Services\Domain\Payment\Stripe\EventHandlers\AccountUpdateHandler;
88
use HiEvents\Services\Domain\Payment\Stripe\EventHandlers\ChargeRefundUpdatedHandler;
9+
use HiEvents\Services\Domain\Payment\Stripe\EventHandlers\ChargeSucceededHandler;
910
use HiEvents\Services\Domain\Payment\Stripe\EventHandlers\PaymentIntentFailedHandler;
1011
use HiEvents\Services\Domain\Payment\Stripe\EventHandlers\PaymentIntentSucceededHandler;
1112
use Illuminate\Cache\Repository;
@@ -25,10 +26,13 @@ class IncomingWebhookHandler
2526
Event::PAYMENT_INTENT_PAYMENT_FAILED,
2627
Event::ACCOUNT_UPDATED,
2728
Event::REFUND_UPDATED,
29+
Event::CHARGE_SUCCEEDED,
30+
Event::CHARGE_UPDATED,
2831
];
2932

3033
public function __construct(
3134
private readonly ChargeRefundUpdatedHandler $refundEventHandlerService,
35+
private readonly ChargeSucceededHandler $chargeSucceededHandler,
3236
private readonly PaymentIntentSucceededHandler $paymentIntentSucceededHandler,
3337
private readonly PaymentIntentFailedHandler $paymentIntentFailedHandler,
3438
private readonly AccountUpdateHandler $accountUpdateHandler,
@@ -70,7 +74,7 @@ public function handle(StripeWebhookDTO $webhookDTO): void
7074
return;
7175
}
7276

73-
$this->logger->debug('Stripe event received', $event->data->object->toArray());
77+
$this->logger->debug('Stripe event received: ' . $event->type, $event->data->object->toArray());
7478

7579
switch ($event->type) {
7680
case Event::PAYMENT_INTENT_SUCCEEDED:
@@ -79,6 +83,10 @@ public function handle(StripeWebhookDTO $webhookDTO): void
7983
case Event::PAYMENT_INTENT_PAYMENT_FAILED:
8084
$this->paymentIntentFailedHandler->handleEvent($event->data->object);
8185
break;
86+
case Event::CHARGE_SUCCEEDED:
87+
case Event::CHARGE_UPDATED:
88+
$this->chargeSucceededHandler->handleEvent($event->data->object);
89+
break;
8290
case Event::REFUND_UPDATED:
8391
$this->refundEventHandlerService->handleEvent($event->data->object);
8492
break;

backend/app/Services/Domain/EventStatistics/EventStatisticsIncrementService.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ public function incrementForOrder(OrderDomainObject $order): void
4848
->findById($order->getId());
4949

5050
$this->retrier->retry(
51-
callableAction: function (int $attempt) use ($order): void {
52-
$this->databaseManager->transaction(function () use ($order, $attempt): void {
53-
$this->incrementAggregateStatistics($order, $attempt);
54-
$this->incrementDailyStatistics($order, $attempt);
51+
callableAction: function () use ($order): void {
52+
$this->databaseManager->transaction(function () use ($order): void {
53+
$this->incrementAggregateStatistics($order);
54+
$this->incrementDailyStatistics($order);
5555
$this->incrementPromoCodeUsage($order);
5656
$this->incrementProductStatistics($order);
5757
});
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace HiEvents\Services\Domain\Order;
4+
5+
use HiEvents\DomainObjects\Generated\OrderPaymentPlatformFeeDomainObjectAbstract;
6+
use HiEvents\Helper\Currency;
7+
use HiEvents\Repository\Interfaces\OrderPaymentPlatformFeeRepositoryInterface;
8+
9+
class OrderPaymentPlatformFeeService
10+
{
11+
public function __construct(
12+
private readonly OrderPaymentPlatformFeeRepositoryInterface $orderPaymentPlatformFeeRepository,
13+
)
14+
{
15+
}
16+
17+
public function createOrderPaymentPlatformFee(
18+
int $orderId,
19+
string $paymentPlatform,
20+
?array $feeRollup,
21+
int $paymentPlatformFeeAmountMinorUnit,
22+
int $applicationFeeAmountMinorUnit,
23+
string $currency,
24+
?string $transactionId = null,
25+
): void
26+
{
27+
$isZeroDecimalCurrency = Currency::isZeroDecimalCurrency($currency);
28+
29+
$paymentPlatformFeeAmount = $isZeroDecimalCurrency
30+
? $paymentPlatformFeeAmountMinorUnit
31+
: $paymentPlatformFeeAmountMinorUnit / 100;
32+
33+
$applicationFeeAmount = $isZeroDecimalCurrency
34+
? $applicationFeeAmountMinorUnit
35+
: $applicationFeeAmountMinorUnit / 100;
36+
37+
$this->orderPaymentPlatformFeeRepository->create([
38+
OrderPaymentPlatformFeeDomainObjectAbstract::ORDER_ID => $orderId,
39+
OrderPaymentPlatformFeeDomainObjectAbstract::PAYMENT_PLATFORM => $paymentPlatform,
40+
OrderPaymentPlatformFeeDomainObjectAbstract::FEE_ROLLUP => $feeRollup,
41+
OrderPaymentPlatformFeeDomainObjectAbstract::PAYMENT_PLATFORM_FEE_AMOUNT => $paymentPlatformFeeAmount,
42+
OrderPaymentPlatformFeeDomainObjectAbstract::APPLICATION_FEE_AMOUNT => $applicationFeeAmount,
43+
OrderPaymentPlatformFeeDomainObjectAbstract::CURRENCY => strtoupper($currency),
44+
OrderPaymentPlatformFeeDomainObjectAbstract::TRANSACTION_ID => $transactionId,
45+
OrderPaymentPlatformFeeDomainObjectAbstract::PAID_AT => now()->toDateTimeString(),
46+
]);
47+
}
48+
}

0 commit comments

Comments
 (0)