Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: missing event tickets amount for PayPal one-time donations #7403

Merged
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,14 @@ import {PayPalSubscriber} from './types';

formData.append('give_payment_mode', 'paypal-commerce');

formData.append('give-amount', getAmount());
const eventTicketsTotalAmount = eventTickets ? getEventTicketsTotalAmount(JSON.parse(eventTickets)) : 0;
if (subscriptionPeriod === 'one-time') {
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 +303,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
Loading