Skip to content

Commit

Permalink
Fix: missing event tickets amount for PayPal one-time donations (#7403)
Browse files Browse the repository at this point in the history
  • Loading branch information
glaubersilva authored Jun 11, 2024
1 parent 194dd53 commit d714de4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
};
Expand Down

0 comments on commit d714de4

Please sign in to comment.