From cd06fa1a8be5bce543ece82e83535cf0c4a61248 Mon Sep 17 00:00:00 2001 From: Glauber Silva Date: Wed, 5 Jun 2024 12:49:38 -0300 Subject: [PATCH 1/9] Fix: missing event tickets amount for PayPal one-time donations --- .../PayPalCommerce/payPalCommerceGateway.tsx | 46 ++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/src/PaymentGateways/Gateways/PayPalCommerce/payPalCommerceGateway.tsx b/src/PaymentGateways/Gateways/PayPalCommerce/payPalCommerceGateway.tsx index be9b07f005..d710c1c0d9 100644 --- a/src/PaymentGateways/Gateways/PayPalCommerce/payPalCommerceGateway.tsx +++ b/src/PaymentGateways/Gateways/PayPalCommerce/payPalCommerceGateway.tsx @@ -50,6 +50,29 @@ import {PayPalSubscriber} from './types'; let currency; + let eventTicketsName; // eventTickets-1 + let eventTickets; // [{"ticketId":1,"quantity":1,"amount":2000},{"ticketId":2,"quantity":2,"amount":2000}] + let eventTicketsTotalAmount = 0; // 60 + + window.givewpDonationFormExports.form.nodes.forEach((section) => { + const eventField = section.nodes.filter((field) => field.type === 'eventTickets'); + + if (eventField.length > 0) { + eventTicketsName = eventField[0].name; + console.log('eventTicketsName: ', eventTicketsName); + } + }); + + const setEventTicketsTotalAmount = (eventsTicket: Array) => { + const totalAmount = eventsTicket.reduce((accumulator, currentValue) => accumulator + currentValue.amount, 0); + if (totalAmount > 0) { + eventTicketsTotalAmount = totalAmount / 100; + } else { + eventTicketsTotalAmount = 0; + } + console.log('EventsTicketTotalAmount: ', eventTicketsTotalAmount); + }; + const buttonsStyle = { color: 'gold' as 'gold' | 'blue' | 'silver' | 'white' | 'black', label: 'paypal' as 'paypal' | 'checkout' | 'buynow' | 'pay' | 'installment' | 'subscribe' | 'donate', @@ -107,11 +130,19 @@ import {PayPalSubscriber} from './types'; * @return {number} Amount with fee. */ const getAmount = () => { + /*const {useWatch, useFormState} = window.givewp.form.hooks; + const donationType = useWatch({name: 'donationType'}); + const isSubscription = donationType === 'subscription';*/ + const feeAmount = feeRecovery ? feeRecovery : 0; let amountWithFee = amount + feeAmount; amountWithFee = Math.round(amountWithFee * 100) / 100; - return amountWithFee; + /*if (isSubscription) { + return amountWithFee + }*/ + + return amountWithFee + eventTicketsTotalAmount; }; const getFormData = () => { @@ -282,6 +313,19 @@ import {PayPalSubscriber} from './types'; } }, [amount]); + //eventTickets-1 + if (eventTicketsName) { + console.log('eventTicketsName: ', eventTicketsName); + eventTickets = useWatch({name: eventTicketsName}); + + useEffect(() => { + console.log('eventTickets: ', eventTickets); + if (eventTickets) { + setEventTicketsTotalAmount(JSON.parse(eventTickets)); + } + }, [eventTickets]); + } + return children; }; From 8ffa3ea4258035223d6dbbd0da638d205040d2e9 Mon Sep 17 00:00:00 2001 From: Glauber Silva Date: Wed, 5 Jun 2024 14:53:14 -0300 Subject: [PATCH 2/9] refactor: update full amount only for one-time donations --- .../PayPalCommerce/payPalCommerceGateway.tsx | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/PaymentGateways/Gateways/PayPalCommerce/payPalCommerceGateway.tsx b/src/PaymentGateways/Gateways/PayPalCommerce/payPalCommerceGateway.tsx index d710c1c0d9..d2a68aa6f7 100644 --- a/src/PaymentGateways/Gateways/PayPalCommerce/payPalCommerceGateway.tsx +++ b/src/PaymentGateways/Gateways/PayPalCommerce/payPalCommerceGateway.tsx @@ -130,19 +130,11 @@ import {PayPalSubscriber} from './types'; * @return {number} Amount with fee. */ const getAmount = () => { - /*const {useWatch, useFormState} = window.givewp.form.hooks; - const donationType = useWatch({name: 'donationType'}); - const isSubscription = donationType === 'subscription';*/ - const feeAmount = feeRecovery ? feeRecovery : 0; let amountWithFee = amount + feeAmount; amountWithFee = Math.round(amountWithFee * 100) / 100; - /*if (isSubscription) { - return amountWithFee - }*/ - - return amountWithFee + eventTicketsTotalAmount; + return amountWithFee; }; const getFormData = () => { @@ -153,7 +145,13 @@ import {PayPalSubscriber} from './types'; formData.append('give_payment_mode', 'paypal-commerce'); - formData.append('give-amount', getAmount()); + if (subscriptionPeriod === 'one-time') { + formData.append('give-amount', getAmount() + eventTicketsTotalAmount); + } else { + formData.append('give-amount', getAmount()); + } + + formData.append('give-event-tickets-total-amount', String(eventTicketsTotalAmount)); formData.append('give-recurring-period', subscriptionPeriod); formData.append('period', subscriptionPeriod); @@ -307,14 +305,22 @@ import {PayPalSubscriber} from './types'; currency = useWatch({name: 'currency'}); + eventTickets = useWatch({name: eventTicketsName}); //eventTickets-1 + useEffect(() => { + console.log('amount: ', amount); + if (eventTicketsName) { + console.log('eventTickets: ', eventTickets); + setEventTicketsTotalAmount(JSON.parse(eventTickets)); + } + if (orderCreated) { updateOrderAmount = true; } - }, [amount]); + }, [amount, eventTickets]); //eventTickets-1 - if (eventTicketsName) { + /*if (eventTicketsName) { console.log('eventTicketsName: ', eventTicketsName); eventTickets = useWatch({name: eventTicketsName}); @@ -324,7 +330,7 @@ import {PayPalSubscriber} from './types'; setEventTicketsTotalAmount(JSON.parse(eventTickets)); } }, [eventTickets]); - } + }*/ return children; }; From 8dd21dec8011e11f0c6fcb8d3e1eefb004bd01c1 Mon Sep 17 00:00:00 2001 From: Glauber Silva Date: Wed, 5 Jun 2024 16:57:32 -0300 Subject: [PATCH 3/9] refactor: change condition and polishments --- .../PayPalCommerce/payPalCommerceGateway.tsx | 32 ++++++------------- 1 file changed, 9 insertions(+), 23 deletions(-) diff --git a/src/PaymentGateways/Gateways/PayPalCommerce/payPalCommerceGateway.tsx b/src/PaymentGateways/Gateways/PayPalCommerce/payPalCommerceGateway.tsx index d2a68aa6f7..db3098c79b 100644 --- a/src/PaymentGateways/Gateways/PayPalCommerce/payPalCommerceGateway.tsx +++ b/src/PaymentGateways/Gateways/PayPalCommerce/payPalCommerceGateway.tsx @@ -50,19 +50,21 @@ import {PayPalSubscriber} from './types'; let currency; - let eventTicketsName; // eventTickets-1 + let eventTicketsFieldName; // eventTickets-1 let eventTickets; // [{"ticketId":1,"quantity":1,"amount":2000},{"ticketId":2,"quantity":2,"amount":2000}] let eventTicketsTotalAmount = 0; // 60 window.givewpDonationFormExports.form.nodes.forEach((section) => { const eventField = section.nodes.filter((field) => field.type === 'eventTickets'); - if (eventField.length > 0) { - eventTicketsName = eventField[0].name; - console.log('eventTicketsName: ', eventTicketsName); + if (eventField.length === 1) { + eventTicketsFieldName = eventField[0].name; } }); + /** + * @unreleased + */ const setEventTicketsTotalAmount = (eventsTicket: Array) => { const totalAmount = eventsTicket.reduce((accumulator, currentValue) => accumulator + currentValue.amount, 0); if (totalAmount > 0) { @@ -70,7 +72,6 @@ import {PayPalSubscriber} from './types'; } else { eventTicketsTotalAmount = 0; } - console.log('EventsTicketTotalAmount: ', eventTicketsTotalAmount); }; const buttonsStyle = { @@ -148,7 +149,7 @@ import {PayPalSubscriber} from './types'; if (subscriptionPeriod === 'one-time') { formData.append('give-amount', getAmount() + eventTicketsTotalAmount); } else { - formData.append('give-amount', getAmount()); + 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)); @@ -305,12 +306,10 @@ import {PayPalSubscriber} from './types'; currency = useWatch({name: 'currency'}); - eventTickets = useWatch({name: eventTicketsName}); //eventTickets-1 + eventTickets = useWatch({name: eventTicketsFieldName}); useEffect(() => { - console.log('amount: ', amount); - if (eventTicketsName) { - console.log('eventTickets: ', eventTickets); + if (eventTicketsFieldName) { setEventTicketsTotalAmount(JSON.parse(eventTickets)); } @@ -319,19 +318,6 @@ import {PayPalSubscriber} from './types'; } }, [amount, eventTickets]); - //eventTickets-1 - /*if (eventTicketsName) { - console.log('eventTicketsName: ', eventTicketsName); - eventTickets = useWatch({name: eventTicketsName}); - - useEffect(() => { - console.log('eventTickets: ', eventTickets); - if (eventTickets) { - setEventTicketsTotalAmount(JSON.parse(eventTickets)); - } - }, [eventTickets]); - }*/ - return children; }; From ee1aede74eb452468e19b6a3437bf9b9340c8400 Mon Sep 17 00:00:00 2001 From: Glauber Silva Date: Wed, 5 Jun 2024 17:42:20 -0300 Subject: [PATCH 4/9] doc: change comment --- .../Gateways/PayPalCommerce/payPalCommerceGateway.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PaymentGateways/Gateways/PayPalCommerce/payPalCommerceGateway.tsx b/src/PaymentGateways/Gateways/PayPalCommerce/payPalCommerceGateway.tsx index db3098c79b..7104d0aa4a 100644 --- a/src/PaymentGateways/Gateways/PayPalCommerce/payPalCommerceGateway.tsx +++ b/src/PaymentGateways/Gateways/PayPalCommerce/payPalCommerceGateway.tsx @@ -50,7 +50,7 @@ import {PayPalSubscriber} from './types'; let currency; - let eventTicketsFieldName; // eventTickets-1 + let eventTicketsFieldName; // event-tickets-1 let eventTickets; // [{"ticketId":1,"quantity":1,"amount":2000},{"ticketId":2,"quantity":2,"amount":2000}] let eventTicketsTotalAmount = 0; // 60 From 7c44bc3234ba2ef3a0876cc61b09c2197f78f3f4 Mon Sep 17 00:00:00 2001 From: Glauber Silva Date: Thu, 6 Jun 2024 09:57:59 -0300 Subject: [PATCH 5/9] refactor: add type to function argument --- .../Gateways/PayPalCommerce/payPalCommerceGateway.tsx | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/PaymentGateways/Gateways/PayPalCommerce/payPalCommerceGateway.tsx b/src/PaymentGateways/Gateways/PayPalCommerce/payPalCommerceGateway.tsx index 7104d0aa4a..7fd6c28f74 100644 --- a/src/PaymentGateways/Gateways/PayPalCommerce/payPalCommerceGateway.tsx +++ b/src/PaymentGateways/Gateways/PayPalCommerce/payPalCommerceGateway.tsx @@ -65,8 +65,14 @@ import {PayPalSubscriber} from './types'; /** * @unreleased */ - const setEventTicketsTotalAmount = (eventsTicket: Array) => { - const totalAmount = eventsTicket.reduce((accumulator, currentValue) => accumulator + currentValue.amount, 0); + const setEventTicketsTotalAmount = ( + eventTickets: Array<{ + ticketId: number; + quantity: number; + amount: number; + }> + ) => { + const totalAmount = eventTickets.reduce((accumulator, eventTicket) => accumulator + eventTicket.amount, 0); if (totalAmount > 0) { eventTicketsTotalAmount = totalAmount / 100; } else { From f2da4037c77b0c95dda84077892e553d057ae249 Mon Sep 17 00:00:00 2001 From: Glauber Silva Date: Fri, 7 Jun 2024 10:08:46 -0300 Subject: [PATCH 6/9] refactor: remove event id from field name --- .../Actions/ConvertEventTicketsBlockToFieldsApi.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/EventTickets/Actions/ConvertEventTicketsBlockToFieldsApi.php b/src/EventTickets/Actions/ConvertEventTicketsBlockToFieldsApi.php index 492713d9d4..2677ce99f1 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; @@ -22,7 +20,7 @@ class ConvertEventTicketsBlockToFieldsApi */ 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); From 4c8937b17a529096de1a99a7a4a8f21388004232 Mon Sep 17 00:00:00 2001 From: Glauber Silva Date: Fri, 7 Jun 2024 10:10:56 -0300 Subject: [PATCH 7/9] refactor: remove code to retrieve field name and simplify logic --- .../PayPalCommerce/payPalCommerceGateway.tsx | 25 +++++-------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/src/PaymentGateways/Gateways/PayPalCommerce/payPalCommerceGateway.tsx b/src/PaymentGateways/Gateways/PayPalCommerce/payPalCommerceGateway.tsx index 7fd6c28f74..870b8bcaad 100644 --- a/src/PaymentGateways/Gateways/PayPalCommerce/payPalCommerceGateway.tsx +++ b/src/PaymentGateways/Gateways/PayPalCommerce/payPalCommerceGateway.tsx @@ -50,22 +50,12 @@ import {PayPalSubscriber} from './types'; let currency; - let eventTicketsFieldName; // event-tickets-1 - let eventTickets; // [{"ticketId":1,"quantity":1,"amount":2000},{"ticketId":2,"quantity":2,"amount":2000}] - let eventTicketsTotalAmount = 0; // 60 - - window.givewpDonationFormExports.form.nodes.forEach((section) => { - const eventField = section.nodes.filter((field) => field.type === 'eventTickets'); - - if (eventField.length === 1) { - eventTicketsFieldName = eventField[0].name; - } - }); + let eventTickets; /** * @unreleased */ - const setEventTicketsTotalAmount = ( + const getEventTicketsTotalAmount = ( eventTickets: Array<{ ticketId: number; quantity: number; @@ -74,9 +64,9 @@ import {PayPalSubscriber} from './types'; ) => { const totalAmount = eventTickets.reduce((accumulator, eventTicket) => accumulator + eventTicket.amount, 0); if (totalAmount > 0) { - eventTicketsTotalAmount = totalAmount / 100; + return totalAmount / 100; } else { - eventTicketsTotalAmount = 0; + return 0; } }; @@ -152,6 +142,7 @@ import {PayPalSubscriber} from './types'; formData.append('give_payment_mode', 'paypal-commerce'); + const eventTicketsTotalAmount = eventTickets ? getEventTicketsTotalAmount(JSON.parse(eventTickets)) : 0; if (subscriptionPeriod === 'one-time') { formData.append('give-amount', getAmount() + eventTicketsTotalAmount); } else { @@ -312,13 +303,9 @@ import {PayPalSubscriber} from './types'; currency = useWatch({name: 'currency'}); - eventTickets = useWatch({name: eventTicketsFieldName}); + eventTickets = useWatch({name: 'event-tickets'}); useEffect(() => { - if (eventTicketsFieldName) { - setEventTicketsTotalAmount(JSON.parse(eventTickets)); - } - if (orderCreated) { updateOrderAmount = true; } From 10d66668fb332d75c6b4d28abb0dc92dcaf15875 Mon Sep 17 00:00:00 2001 From: Glauber Silva Date: Fri, 7 Jun 2024 10:11:58 -0300 Subject: [PATCH 8/9] doc: add unreleased tag --- src/EventTickets/Actions/ConvertEventTicketsBlockToFieldsApi.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/EventTickets/Actions/ConvertEventTicketsBlockToFieldsApi.php b/src/EventTickets/Actions/ConvertEventTicketsBlockToFieldsApi.php index 2677ce99f1..e702b1c766 100644 --- a/src/EventTickets/Actions/ConvertEventTicketsBlockToFieldsApi.php +++ b/src/EventTickets/Actions/ConvertEventTicketsBlockToFieldsApi.php @@ -14,6 +14,7 @@ class ConvertEventTicketsBlockToFieldsApi { /** + * @unreleased Remove event ID from field name * @since 3.6.0 * * @throws EmptyNameException From 3b1fad9919fe3b9f46c8428c8520ee4bdfbe5e35 Mon Sep 17 00:00:00 2001 From: Glauber Silva Date: Mon, 10 Jun 2024 16:38:11 -0300 Subject: [PATCH 9/9] fix: change how we check if it's a subscription or not --- .../Gateways/PayPalCommerce/payPalCommerceGateway.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/PaymentGateways/Gateways/PayPalCommerce/payPalCommerceGateway.tsx b/src/PaymentGateways/Gateways/PayPalCommerce/payPalCommerceGateway.tsx index 870b8bcaad..980acec0f8 100644 --- a/src/PaymentGateways/Gateways/PayPalCommerce/payPalCommerceGateway.tsx +++ b/src/PaymentGateways/Gateways/PayPalCommerce/payPalCommerceGateway.tsx @@ -143,7 +143,8 @@ import {PayPalSubscriber} from './types'; formData.append('give_payment_mode', 'paypal-commerce'); const eventTicketsTotalAmount = eventTickets ? getEventTicketsTotalAmount(JSON.parse(eventTickets)) : 0; - if (subscriptionPeriod === 'one-time') { + 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