Skip to content

Commit

Permalink
fix(payments-plugin): Fixed PayPal e2e tests and updated translations
Browse files Browse the repository at this point in the history
  • Loading branch information
sonntag-philipp committed Oct 16, 2024
1 parent 3d97fa9 commit 058e1e3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 49 deletions.
5 changes: 1 addition & 4 deletions packages/core/src/i18n/messages/en.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"error": {
"paypal-order-without-payments": "No payments were captured in this order",
"paypal-order-details-failed": "Failed to get PayPal order details",
"paypal-create-order-failed": "Failed to create PayPal order",
"paypal-refund-failed": "PayPal refund failed",
Expand Down Expand Up @@ -66,12 +67,8 @@
"unauthorized": "The credentials did not match. Please check and try again"
},
"errorResult": {
"PAYPAL_ORDER_WITHOUT_PAYMENTS": "The PayPal order does not have any payments",
"MULTIPLE_CAPTURES": "Multiple captures assigned to this order",
"PAYPAL_PAYMENT_NOT_CAPTURED": "Capture is not completed",
"PAYPAL_ORDER_STATUS_NOT_CREATED": "Payment authorization failed. Payment status must be 'CREATED'",
"PAYPAL_ORDER_WITHOUT_AUTHORIZATIONS": "The PayPal order does not have any authorizations",
"PAYPAL_ORDER_MATCH_INVALID": "The PayPal order does not match the internal order",
"ALREADY_LOGGED_IN_ERROR": "Cannot set a Customer for the Order when already logged in",
"ALREADY_REFUNDED_ERROR": "Cannot refund an OrderItem which has already been refunded",
"CANCEL_ACTIVE_ORDER_ERROR": "Cannot cancel OrderLines from an Order in the \"{ orderState }\" state",
Expand Down
56 changes: 24 additions & 32 deletions packages/payments-plugin/e2e/paypal-payment.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,9 @@ describe('PayPal payments', () => {
it('Should fail when no active order is set', async ({ expect }) => {
await shopClient.asUserWithCredentials(customers[0].emailAddress, 'test');
const createOrderPromise = shopClient.query(CREATE_PAYPAL_ORDER);
await expect(createOrderPromise).rejects.toThrowError('Session has no active order');
await expect(createOrderPromise).rejects.toThrowError(
'No active Order could be determined nor created',
);
});
it('Should fail when not in ArrangingPayment state', async ({ expect }) => {
await shopClient.asUserWithCredentials(customers[0].emailAddress, 'test');
Expand All @@ -208,7 +210,9 @@ describe('PayPal payments', () => {

await setShipping(shopClient);
const createOrderPromise = shopClient.query(CREATE_PAYPAL_ORDER);
await expect(createOrderPromise).rejects.toThrowError('Order must be in arranging payment state');
await expect(createOrderPromise).rejects.toThrowError(
'A Payment may only be added when Order is in "ArrangingPayment" state',
);
});
it('Should create an order with correct content', async ({ expect }) => {
let createOrderRequest: CreatePayPalOrderRequest | undefined;
Expand Down Expand Up @@ -356,20 +360,14 @@ describe('PayPal payments', () => {
it('Should print error message when orderId is not set', async ({ expect }) => {
await shopClient.asUserWithCredentials(customers[0].emailAddress, 'test');

const addPaymentToOrderResult = await shopClient.query(ADD_PAYMENT_TO_ORDER, {
input: {
method: methodCode,
metadata: {},
},
});
expect(addPaymentToOrderResult).toEqual({
addPaymentToOrder: {
message: 'The payment failed',
errorCode: 'PAYMENT_FAILED_ERROR',
paymentErrorMessage:
'"orderId" must be set in metadata. Call "createPayPalOrder" to get order id',
},
});
await expect(
shopClient.query(ADD_PAYMENT_TO_ORDER, {
input: {
method: methodCode,
metadata: {},
},
}),
).rejects.toThrow('The argument "orderId" is required');
});
it('Should return validation result if validation fails', async ({ expect }) => {
await shopClient.asUserWithCredentials(customers[0].emailAddress, 'test');
Expand Down Expand Up @@ -427,7 +425,7 @@ describe('PayPal payments', () => {
addPaymentToOrder: {
message: 'The payment failed',
errorCode: 'PAYMENT_FAILED_ERROR',
paymentErrorMessage: 'Payment authorization failed. Error while accessing PayPal.',
paymentErrorMessage: 'Payment authorization failed. Error while accessing PayPal',
},
});
});
Expand Down Expand Up @@ -492,12 +490,6 @@ describe('PayPal payments', () => {
},
{
id: 'T_3',
state: 'Error',
transactionId: null,
method: methodCode,
},
{
id: 'T_4',
state: 'Authorized',
transactionId: paypalOrderId,
method: methodCode,
Expand Down Expand Up @@ -559,14 +551,14 @@ describe('PayPal payments', () => {

// We use the last payment of the previous describe block that was successful.
const { settlePayment } = await adminClient.query(SETTLE_PAYMENT, {
id: 'T_4',
id: 'T_3',
});

expect(settlePayment).toEqual({
errorCode: 'SETTLE_PAYMENT_ERROR',
message: 'Settling the payment failed',
__typename: 'SettlePaymentError',
paymentErrorMessage: 'No authorizations found in order details.',
paymentErrorMessage: 'No authorizations found in order details',
});
});
it('Should capture order and return success', async ({ expect }) => {
Expand Down Expand Up @@ -631,7 +623,7 @@ describe('PayPal payments', () => {
});

expect(settlePayment).toEqual({
id: 'T_5',
id: 'T_4',
transactionId: paypalOrderId,
amount: 120990,
method: methodCode,
Expand Down Expand Up @@ -721,7 +713,7 @@ describe('PayPal payments', () => {

expect(reauthorizeRequestCount).toBe(1);
expect(settlePayment).toEqual({
id: 'T_6',
id: 'T_5',
transactionId: paypalOrderId,
amount: 120990,
method: methodCode,
Expand Down Expand Up @@ -749,7 +741,7 @@ describe('PayPal payments', () => {
],
shipping: 1000,
adjustment: 119900,
paymentId: 'T_5',
paymentId: 'T_4',
reason: 'Test refund',
},
}),
Expand Down Expand Up @@ -788,7 +780,7 @@ describe('PayPal payments', () => {
],
shipping: 1000,
adjustment: 119900,
paymentId: 'T_5',
paymentId: 'T_4',
reason: 'Test refund',
},
}),
Expand Down Expand Up @@ -824,7 +816,7 @@ describe('PayPal payments', () => {
amount: 120990,
shipping: 0,
adjustment: 0,
paymentId: 'T_5',
paymentId: 'T_4',
reason: 'Test refund',
},
});
Expand Down Expand Up @@ -872,7 +864,7 @@ describe('PayPal payments', () => {
amount: 120000,
shipping: 0,
adjustment: 0,
paymentId: 'T_6',
paymentId: 'T_5',
reason: 'Test refund',
},
});
Expand All @@ -883,7 +875,7 @@ describe('PayPal payments', () => {
amount: 990,
shipping: 0,
adjustment: 0,
paymentId: 'T_6',
paymentId: 'T_5',
reason: 'Test refund',
},
});
Expand Down
11 changes: 6 additions & 5 deletions packages/payments-plugin/src/paypal/paypal-order.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ export class PayPalOrderService extends PayPalBaseService {
return {
state: 'Error' as const,
amount: 0,
errorMessage: 'errorResult.PAYPAL_ORDER_MATCH_INVALID',
errorMessage:
'PayPal order must have exactly one purchase_unit assigned. Please use "createPayPalOrder" to create a new order',
};
}

Expand All @@ -140,15 +141,15 @@ export class PayPalOrderService extends PayPalBaseService {
return {
state: 'Error' as const,
amount: 0,
errorMessage: 'errorResult.PAYPAL_ORDER_MATCH_INVALID',
errorMessage: `Currency mismatch. Expected ${internalOrder.currencyCode}, got ${purchaseUnit.amount.currency_code}`,
};
}

if (purchaseUnit.reference_id !== internalOrder.code) {
return {
state: 'Error' as const,
amount: 0,
errorMessage: 'errorResult.PAYPAL_ORDER_MATCH_INVALID',
errorMessage: `Reference ID mismatch. Expected ${internalOrder.code}, got ${purchaseUnit.reference_id ?? 'no reference id'}`,
};
}

Expand All @@ -157,15 +158,15 @@ export class PayPalOrderService extends PayPalBaseService {
return {
state: 'Error' as const,
amount: 0,
errorMessage: 'errorResult.PAYPAL_ORDER_MATCH_INVALID',
errorMessage: `Amount mismatch. Expected ${orderTotal}, got ${purchaseUnit.amount.value}`,
};
}

if (purchaseUnit.payee?.merchant_id !== merchantId) {
return {
state: 'Error' as const,
amount: 0,
errorMessage: 'errorResult.PAYPAL_ORDER_MATCH_INVALID',
errorMessage: 'Payee mismatch',
};
}
}
Expand Down
16 changes: 8 additions & 8 deletions packages/payments-plugin/src/paypal/paypal.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,23 @@ export const paypalPaymentMethodHandler = new PaymentMethodHandler({
return {
state: 'Error' as const,
amount: 0,
errorMessage: error.message,
errorMessage: 'Payment authorization failed. Error while accessing PayPal',
};
}

if (!authorizedOrderResponse.purchase_units[0].payments) {
return {
state: 'Error' as const,
amount: 0,
errorMessage: 'errorResult.PAYPAL_ORDER_WITHOUT_PAYMENTS',
errorMessage: 'Payment authorization failed. No payments found',
};
}

if (!authorizedOrderResponse.purchase_units[0].payments.authorizations?.length) {
return {
state: 'Error' as const,
amount: 0,
errorMessage: 'errorResult.PAYPAL_ORDER_WITHOUT_AUTHORIZATIONS',
errorMessage: 'Payment authorization failed. No authorizations found',
};
}

Expand All @@ -89,7 +89,7 @@ export const paypalPaymentMethodHandler = new PaymentMethodHandler({
return {
state: 'Error' as const,
amount: 0,
errorMessage: 'errorResult.PAYPAL_ORDER_STATUS_NOT_CREATED',
errorMessage: 'Payment authorization failed. Payment status must be "CREATED"',
};
}

Expand All @@ -107,7 +107,7 @@ export const paypalPaymentMethodHandler = new PaymentMethodHandler({
if (payment.state !== 'Authorized') {
return {
success: false,
errorMessage: 'errorResult.PAYPAL_ORDER_WITHOUT_AUTHORIZATIONS',
errorMessage: 'Payment is not authorized. Call "createPayment" to authorize payment',
state: 'Error' as const,
};
}
Expand All @@ -121,7 +121,7 @@ export const paypalPaymentMethodHandler = new PaymentMethodHandler({
if (!authorizations?.length) {
return {
success: false,
errorMessage: 'errorResult.PAYPAL_ORDER_WITHOUT_AUTHORIZATIONS',
errorMessage: 'No authorizations found in order details',
state: 'Error' as const,
};
}
Expand Down Expand Up @@ -153,11 +153,11 @@ export const paypalPaymentMethodHandler = new PaymentMethodHandler({
const captures = payments?.captures;

if (!captures || !captures.length) {
throw new PayPalError('errorResult.PAYPAL_ORDER_WITHOUT_PAYMENTS');
throw new PayPalError('No payments were captured in this order');
}

if (captures.length !== 1) {
throw new PayPalError('errorResult.MULTIPLE_CAPTURES');
throw new PayPalError('Multiple captures assigned to this order');
}

const capture = captures[0];
Expand Down

0 comments on commit 058e1e3

Please sign in to comment.