diff --git a/backend/app/DomainObjects/OrderDomainObject.php b/backend/app/DomainObjects/OrderDomainObject.php index 723e98b90..b86ab7fbf 100644 --- a/backend/app/DomainObjects/OrderDomainObject.php +++ b/backend/app/DomainObjects/OrderDomainObject.php @@ -2,6 +2,8 @@ namespace HiEvents\DomainObjects; +use Exception; +use HiEvents\DataTransferObjects\AddressDTO; use HiEvents\DomainObjects\Enums\PaymentProviders; use HiEvents\DomainObjects\Enums\ProductType; use HiEvents\DomainObjects\Interfaces\IsFilterable; @@ -286,4 +288,17 @@ public function isRefundable(): bool && $this->getPaymentProvider() === PaymentProviders::STRIPE->name && $this->getRefundStatus() !== OrderRefundStatus::REFUNDED->name; } + + public function getAddressDTO(): ?AddressDTO + { + if ($this->getAddress() === null) { + return null; + } + + try { + return AddressDTO::from($this->getAddress()); + } catch (Exception) { + return null; + } + } } diff --git a/backend/app/Services/Application/Handlers/Order/Payment/Stripe/CreatePaymentIntentHandler.php b/backend/app/Services/Application/Handlers/Order/Payment/Stripe/CreatePaymentIntentHandler.php index c4a6d2e63..47ccbde56 100644 --- a/backend/app/Services/Application/Handlers/Order/Payment/Stripe/CreatePaymentIntentHandler.php +++ b/backend/app/Services/Application/Handlers/Order/Payment/Stripe/CreatePaymentIntentHandler.php @@ -9,6 +9,7 @@ use HiEvents\DomainObjects\AccountConfigurationDomainObject; use HiEvents\DomainObjects\AccountStripePlatformDomainObject; use HiEvents\DomainObjects\AccountVatSettingDomainObject; +use HiEvents\DomainObjects\EventDomainObject; use HiEvents\DomainObjects\Generated\StripePaymentDomainObjectAbstract; use HiEvents\DomainObjects\OrderItemDomainObject; use HiEvents\DomainObjects\Status\OrderStatus; @@ -27,6 +28,7 @@ use HiEvents\Services\Infrastructure\Stripe\StripeClientFactory; use HiEvents\Services\Infrastructure\Stripe\StripeConfigurationService; use HiEvents\Values\MoneyValue; +use Illuminate\Support\Str; use Stripe\Exception\ApiErrorException; use Throwable; @@ -60,6 +62,7 @@ public function handle(string $orderShortId): CreatePaymentIntentResponseDTO $order = $this->orderRepository ->loadRelation(new Relationship(OrderItemDomainObject::class)) ->loadRelation(new Relationship(StripePaymentDomainObject::class, name: 'stripe_payment')) + ->loadRelation(new Relationship(EventDomainObject::class, name: 'event')) ->findByShortId($orderShortId); if (!$order || !$this->sessionIdentifierService->verifySession($order->getSessionId())) { @@ -110,6 +113,12 @@ public function handle(string $orderShortId): CreatePaymentIntentResponseDTO ); } + $description = __(':item_count item(s) for event: :event_name (Order :order_short_id)', [ + 'event_name' => Str::limit($order->getEvent()?->getTitle() ?? __('Event'), 75), + 'order_short_id' => $orderShortId, + 'item_count' => $order->getOrderItems()->sum(fn(OrderItemDomainObject $item) => $item->getQuantity()), + ]); + $paymentIntent = $this->stripePaymentService->createPaymentIntentWithClient( $stripeClient, CreatePaymentIntentRequestDTO::fromArray([ @@ -119,6 +128,7 @@ public function handle(string $orderShortId): CreatePaymentIntentResponseDTO 'order' => $order, 'stripeAccountId' => $stripeAccountId, 'vatSettings' => $account->getAccountVatSetting(), + 'description' => Str::limit($description, 997), ]) ); diff --git a/backend/app/Services/Domain/Payment/Stripe/DTOs/CreatePaymentIntentRequestDTO.php b/backend/app/Services/Domain/Payment/Stripe/DTOs/CreatePaymentIntentRequestDTO.php index de4ac4b32..d6fdb9ea3 100644 --- a/backend/app/Services/Domain/Payment/Stripe/DTOs/CreatePaymentIntentRequestDTO.php +++ b/backend/app/Services/Domain/Payment/Stripe/DTOs/CreatePaymentIntentRequestDTO.php @@ -17,6 +17,7 @@ public function __construct( public readonly OrderDomainObject $order, public readonly ?string $stripeAccountId = null, public readonly ?AccountVatSettingDomainObject $vatSettings = null, + public readonly ?string $description = null, ) { } diff --git a/backend/app/Services/Domain/Payment/Stripe/StripePaymentIntentCreationService.php b/backend/app/Services/Domain/Payment/Stripe/StripePaymentIntentCreationService.php index 90f835b14..74351891d 100644 --- a/backend/app/Services/Domain/Payment/Stripe/StripePaymentIntentCreationService.php +++ b/backend/app/Services/Domain/Payment/Stripe/StripePaymentIntentCreationService.php @@ -83,6 +83,7 @@ public function createPaymentIntentWithClient( 'automatic_payment_methods' => [ 'enabled' => true, ], + ...($paymentIntentDTO->description ? ['description' => $paymentIntentDTO->description] : []), ...($applicationFee && !$bypassApplicationFees ? ['application_fee_amount' => $applicationFee->grossApplicationFee->toMinorUnit()] : []), ], $this->getStripeAccountData($paymentIntentDTO)); @@ -157,11 +158,26 @@ private function upsertStripeCustomerWithClient( ]); if ($customer === null) { + $order = $paymentIntentDTO->order; + + $customerData = [ + 'email' => $order->getEmail(), + 'name' => $order->getFullName(), + ]; + + if (($address = $order->getAddressDTO()) && $address->address_line_1 && $address->country) { + $customerData['address'] = [ + 'line1' => $address->address_line_1, + 'line2' => $address->address_line_2 ?? '', + 'city' => $address->city ?? '', + 'state' => $address->state_or_region ?? '', + 'postal_code' => $address->zip_or_postal_code ?? '', + 'country' => $address->country, + ]; + } + $stripeCustomer = $stripeClient->customers->create( - params: [ - 'email' => $paymentIntentDTO->order->getEmail(), - 'name' => $paymentIntentDTO->order->getFullName(), - ], + params: $customerData, opts: $this->getStripeAccountData($paymentIntentDTO) );