Skip to content

Commit

Permalink
refactor: combine paypal waiting and success urls
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristiaanScheermeijer committed Apr 10, 2024
1 parent 6dbc3ba commit c60630c
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 38 deletions.
13 changes: 1 addition & 12 deletions packages/common/src/controllers/CheckoutController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,11 @@ export default class CheckoutController {

paypalPayment = async ({
successUrl,
waitingUrl,
cancelUrl,
errorUrl,
couponCode = '',
}: {
successUrl: string;
waitingUrl: string;
cancelUrl: string;
errorUrl: string;
couponCode: string;
Expand All @@ -223,7 +221,6 @@ export default class CheckoutController {
const response = await this.checkoutService.paymentWithPayPal({
order: order,
successUrl,
waitingUrl,
cancelUrl,
errorUrl,
couponCode,
Expand Down Expand Up @@ -291,21 +288,13 @@ export default class CheckoutController {
return responseData;
};

updatePayPalPaymentMethod = async (
successUrl: string,
waitingUrl: string,
cancelUrl: string,
errorUrl: string,
paymentMethodId: number,
currentPaymentId?: number,
) => {
updatePayPalPaymentMethod = async (successUrl: string, cancelUrl: string, errorUrl: string, paymentMethodId: number, currentPaymentId?: number) => {
assertModuleMethod(this.checkoutService.updatePaymentMethodWithPayPal, 'updatePaymentMethodWithPayPal is not available in checkout service');
assertModuleMethod(this.checkoutService.deletePaymentMethod, 'deletePaymentMethod is not available in checkout service');

const response = await this.checkoutService.updatePaymentMethodWithPayPal({
paymentMethodId,
successUrl,
waitingUrl,
cancelUrl,
errorUrl,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export default class JWPCheckoutService extends CheckoutService {
paymentWithPayPal: PaymentWithPayPal = async (payload) => {
try {
const response = await InPlayer.Payment.getPayPalParams({
origin: payload.waitingUrl,
origin: payload.successUrl,
accessFeeId: payload.order.id,
paymentMethod: 2,
voucherCode: payload.couponCode,
Expand Down
1 change: 0 additions & 1 deletion packages/common/types/checkout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ export type PaymentWithPayPalPayload = {
successUrl: string;
cancelUrl: string;
errorUrl: string;
waitingUrl: string;
couponCode?: string;
};

Expand Down
6 changes: 1 addition & 5 deletions packages/hooks-react/src/useCheckout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,7 @@ const useCheckout = ({ onUpdateOrderSuccess, onSubmitPaymentWithoutDetailsSucces
},
});

const submitPaymentPaypal = useMutation<
{ redirectUrl: string },
Error,
{ successUrl: string; waitingUrl: string; cancelUrl: string; errorUrl: string; couponCode: string }
>({
const submitPaymentPaypal = useMutation<{ redirectUrl: string }, Error, { successUrl: string; cancelUrl: string; errorUrl: string; couponCode: string }>({
mutationKey: ['submitPaymentPaypal'],
mutationFn: checkoutController.paypalPayment,
onSuccess: onSubmitPaypalPaymentSuccess,
Expand Down
19 changes: 9 additions & 10 deletions packages/ui-react/src/containers/AccountModal/forms/Checkout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,12 @@ const Checkout = () => {
const absoluteCancelUrl = modalURLFromWindowLocation('payment-cancelled');
const absoluteWaitingUrl = modalURLFromWindowLocation('waiting-for-payment', { offerId: selectedOffer?.offerId });
const absoluteErrorUrl = modalURLFromWindowLocation('payment-error');
const absoluteSuccessUrl = offerType === 'svod' ? absoluteWaitingUrl : modalURLFromWindowLocation(null);
const absoluteCloseModalUrl = modalURLFromWindowLocation(null);

// the waiting for payment modal doesn't work for TVOD offers, so we close the modal instead
// @todo add support for different offers in the waiting for payment modal
const absoluteReturnUrl = offerType === 'svod' ? absoluteWaitingUrl : absoluteCloseModalUrl;
const returnUrl = offerType === 'svod' ? waitingUrl : closeModalUrl;
const referrer = window.location.href;

const paymentMethod = paymentMethods?.find((method) => method.id === parseInt(paymentMethodId));
Expand Down Expand Up @@ -138,26 +143,20 @@ const Checkout = () => {
{isStripePayment && (
<PaymentForm
onPaymentFormSubmit={async (cardPaymentPayload: PaymentFormData) =>
await submitPaymentStripe.mutateAsync({ cardPaymentPayload, referrer, returnUrl: absoluteWaitingUrl })
await submitPaymentStripe.mutateAsync({ cardPaymentPayload, referrer, returnUrl: absoluteReturnUrl })
}
/>
)}
{isAdyenPayment && (
<>
<AdyenInitialPayment
paymentSuccessUrl={offerType === 'svod' ? waitingUrl : closeModalUrl}
setUpdatingOrder={setAdyenUpdating}
orderId={order.id}
type="card"
/>
<AdyenInitialPayment paymentSuccessUrl={returnUrl} setUpdatingOrder={setAdyenUpdating} orderId={order.id} type="card" />
</>
)}
{isPayPalPayment && (
<PayPal
onSubmit={() =>
submitPaymentPaypal.mutate({
successUrl: absoluteSuccessUrl,
waitingUrl: absoluteWaitingUrl,
successUrl: absoluteReturnUrl,
cancelUrl: absoluteCancelUrl,
errorUrl: absoluteErrorUrl,
couponCode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,10 @@ const UpdatePaymentMethod = ({ onCloseButtonClick }: Props) => {
setPaymentError(undefined);

const successUrl = modalURLFromWindowLocation('payment-method-success');
const waitingUrl = modalURLFromWindowLocation('waiting-for-payment');
const cancelUrl = modalURLFromWindowLocation('payment-cancelled');
const errorUrl = modalURLFromWindowLocation('payment-error');

const response = await checkoutController.updatePayPalPaymentMethod(
successUrl,
waitingUrl,
cancelUrl,
errorUrl,
paymentMethodId as number,
currentPaymentId,
);
const response = await checkoutController.updatePayPalPaymentMethod(successUrl, cancelUrl, errorUrl, paymentMethodId as number, currentPaymentId);

if (response) {
window.location.href = response.redirectUrl;
Expand Down

0 comments on commit c60630c

Please sign in to comment.