From d714de47d62b3db9c4ec8c18bffe700cbd820c4c Mon Sep 17 00:00:00 2001 From: Glauber Silva Date: Tue, 11 Jun 2024 13:20:00 -0300 Subject: [PATCH] Fix: missing event tickets amount for PayPal one-time donations (#7403) --- .../ConvertEventTicketsBlockToFieldsApi.php | 7 ++-- .../PayPalCommerce/payPalCommerceGateway.tsx | 34 +++++++++++++++++-- 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/EventTickets/Actions/ConvertEventTicketsBlockToFieldsApi.php b/src/EventTickets/Actions/ConvertEventTicketsBlockToFieldsApi.php index 492713d9d4..e702b1c766 100644 --- a/src/EventTickets/Actions/ConvertEventTicketsBlockToFieldsApi.php +++ b/src/EventTickets/Actions/ConvertEventTicketsBlockToFieldsApi.php @@ -3,11 +3,9 @@ namespace Give\EventTickets\Actions; use Give\Donations\Models\Donation; -use Give\EventTickets\DataTransferObjects\TicketPurchaseData; use Give\EventTickets\DataTransferObjects\EventTicketTypeData; +use Give\EventTickets\DataTransferObjects\TicketPurchaseData; use Give\EventTickets\Fields\EventTickets; -use Give\EventTickets\Models\EventTicket; -use Give\EventTickets\Models\EventTicketType; use Give\EventTickets\Repositories\EventRepository; use Give\Framework\Blocks\BlockModel; use Give\Framework\FieldsAPI\Exceptions\EmptyNameException; @@ -16,13 +14,14 @@ class ConvertEventTicketsBlockToFieldsApi { /** + * @unreleased Remove event ID from field name * @since 3.6.0 * * @throws EmptyNameException */ public function __invoke(BlockModel $block, int $formId) { - return EventTickets::make($block->getShortName() . '-' . $block->getAttribute('eventId')) + return EventTickets::make($block->getShortName()) ->tap(function (EventTickets $eventTicketsField) use ($block, $formId) { $eventId = $block->getAttribute('eventId'); $event = give(EventRepository::class)->getById($eventId); diff --git a/src/PaymentGateways/Gateways/PayPalCommerce/payPalCommerceGateway.tsx b/src/PaymentGateways/Gateways/PayPalCommerce/payPalCommerceGateway.tsx index be9b07f005..980acec0f8 100644 --- a/src/PaymentGateways/Gateways/PayPalCommerce/payPalCommerceGateway.tsx +++ b/src/PaymentGateways/Gateways/PayPalCommerce/payPalCommerceGateway.tsx @@ -50,6 +50,26 @@ import {PayPalSubscriber} from './types'; let currency; + let eventTickets; + + /** + * @unreleased + */ + const getEventTicketsTotalAmount = ( + eventTickets: Array<{ + ticketId: number; + quantity: number; + amount: number; + }> + ) => { + const totalAmount = eventTickets.reduce((accumulator, eventTicket) => accumulator + eventTicket.amount, 0); + if (totalAmount > 0) { + return totalAmount / 100; + } else { + return 0; + } + }; + const buttonsStyle = { color: 'gold' as 'gold' | 'blue' | 'silver' | 'white' | 'black', label: 'paypal' as 'paypal' | 'checkout' | 'buynow' | 'pay' | 'installment' | 'subscribe' | 'donate', @@ -122,7 +142,15 @@ import {PayPalSubscriber} from './types'; formData.append('give_payment_mode', 'paypal-commerce'); - formData.append('give-amount', getAmount()); + const eventTicketsTotalAmount = eventTickets ? getEventTicketsTotalAmount(JSON.parse(eventTickets)) : 0; + const isSubscription = subscriptionPeriod ? subscriptionPeriod !== 'one-time' : false; + if (!isSubscription) { + formData.append('give-amount', getAmount() + eventTicketsTotalAmount); + } else { + formData.append('give-amount', getAmount()); // We don't want to charge the event tickets for each subscription renewal + } + + formData.append('give-event-tickets-total-amount', String(eventTicketsTotalAmount)); formData.append('give-recurring-period', subscriptionPeriod); formData.append('period', subscriptionPeriod); @@ -276,11 +304,13 @@ import {PayPalSubscriber} from './types'; currency = useWatch({name: 'currency'}); + eventTickets = useWatch({name: 'event-tickets'}); + useEffect(() => { if (orderCreated) { updateOrderAmount = true; } - }, [amount]); + }, [amount, eventTickets]); return children; };