Skip to content

Commit c2cfa78

Browse files
authored
Merge pull request #915 from evershopcommerce/paypal
Fix: Refactor the paypay payment status
2 parents 312ae8a + 34c7b16 commit c2cfa78

8 files changed

Lines changed: 66 additions & 9 deletions

File tree

packages/evershop/src/components/common/ui/Sonner.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Toaster as Sonner, type ToasterProps } from '@evershop/sonner';
12
import {
23
CircleCheckIcon,
34
InfoIcon,
@@ -6,7 +7,6 @@ import {
67
Loader2Icon
78
} from 'lucide-react';
89
import React from 'react';
9-
import { Toaster as Sonner, type ToasterProps } from '@evershop/sonner';
1010

1111
export { toast } from '@evershop/sonner';
1212

packages/evershop/src/modules/paypal/api/paypalAuthorizePayment/[bodyParser]authorize.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export default async (
4747
);
4848

4949
if (responseData.data.status === 'COMPLETED') {
50-
await updatePaymentStatus(order.order_id, 'authorized');
50+
await updatePaymentStatus(order.order_id, 'paypal_authorized');
5151
// Add transaction data to database
5252
await insert('payment_transaction')
5353
.given({

packages/evershop/src/modules/paypal/api/paypalCaptureAuthorizedPayment/[bodyParser]capture.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ export default async (
6666
`/v2/payments/authorizations/${transaction.transaction_id}`
6767
);
6868
if (transactionDetails.data.status === 'CAPTURED') {
69-
await updatePaymentStatus(order.order_id, 'paid');
69+
await updatePaymentStatus(
70+
order.order_id,
71+
'paypal_captured',
72+
connection
73+
);
7074
// Save order activities
7175
await addOrderActivityLog(
7276
order.order_id,
@@ -87,7 +91,11 @@ export default async (
8791
);
8892
if (responseData.data.status === 'COMPLETED') {
8993
// Update payment status
90-
await updatePaymentStatus(order.order_id, 'paid', connection);
94+
await updatePaymentStatus(
95+
order.order_id,
96+
'paypal_captured',
97+
connection
98+
);
9199
// Save order activities
92100
await addOrderActivityLog(
93101
order.order_id,

packages/evershop/src/modules/paypal/api/paypalCapturePayment/[bodyParser]capture.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ export default async (
5151

5252
if (responseData.data.status === 'COMPLETED') {
5353
// Update payment status
54-
await updatePaymentStatus(order.order_id, 'paid', connection);
54+
await updatePaymentStatus(
55+
order.order_id,
56+
'paypal_captured',
57+
connection
58+
);
5559
// Add transaction data to database
5660
await insert('payment_transaction')
5761
.given({

packages/evershop/src/modules/paypal/bootstrap.js renamed to packages/evershop/src/modules/paypal/bootstrap.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
1+
import config from 'config';
12
import { getConfig } from '../../lib/util/getConfig.js';
23
import { hookAfter } from '../../lib/util/hookable.js';
3-
import { addProcessor } from '../../lib/util/registry.js';
44
import { registerPaymentMethod } from '../checkout/services/getAvailablePaymentMethods.js';
55
import { getSetting } from '../setting/services/setting.js';
66
import { voidPaymentTransaction } from './services/voidPaymentTransaction.js';
77

88
export default async () => {
9+
const paypalPaymentStatus = {
10+
order: {
11+
paymentStatus: {
12+
paypal_authorized: {
13+
name: 'Authorized',
14+
badge: 'warning'
15+
},
16+
paypal_captured: {
17+
name: 'Captured',
18+
badge: 'success'
19+
}
20+
},
21+
psoMapping: {
22+
'paypal_authorized:*': 'processing',
23+
'paypal_captured:*': 'processing',
24+
'paypal_captured:delivered': 'completed'
25+
}
26+
}
27+
};
28+
config.util.setModuleDefaults('oms', paypalPaymentStatus);
29+
930
hookAfter('changePaymentStatus', async (order, orderID, status) => {
1031
if (status !== 'canceled') {
1132
return;
@@ -24,7 +45,7 @@ export default async () => {
2445
validator: async () => {
2546
const paypalConfig = getConfig('system.paypal', {});
2647
let paypalStatus;
27-
if (paypalConfig.status) {
48+
if (paypalConfig?.status) {
2849
paypalStatus = paypalConfig.status;
2950
} else {
3051
paypalStatus = await getSetting('paypalPaymentStatus', 0);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { execute } from '@evershop/postgres-query-builder';
2+
import type { PoolClient } from '@evershop/postgres-query-builder';
3+
4+
export default async (connection: PoolClient) => {
5+
// Migrate legacy Stripe payment statuses to the new prefixed values.
6+
// Only orders placed with payment_method = 'paypal' are affected.
7+
const statusMap: Record<string, string> = {
8+
authorized: 'paypal_authorized',
9+
failed: 'paypal_failed',
10+
refunded: 'paypal_refunded',
11+
partial_refunded: 'paypal_partial_refunded',
12+
paid: 'paypal_captured'
13+
};
14+
15+
for (const [oldStatus, newStatus] of Object.entries(statusMap)) {
16+
await execute(
17+
connection,
18+
`UPDATE "order"
19+
SET "payment_status" = '${newStatus}'
20+
WHERE "payment_method" = 'paypal'
21+
AND "payment_status" = '${oldStatus}'`
22+
);
23+
}
24+
};

packages/evershop/src/modules/paypal/pages/admin/orderEdit/PaypalCaptureButton.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export default function PaypalCaptureButton({
4343
return (
4444
<RenderIfTrue
4545
condition={
46-
paymentMethod === 'paypal' && paymentStatus.code === 'authorized'
46+
paymentMethod === 'paypal' && paymentStatus.code === 'paypal_authorized'
4747
}
4848
>
4949
<CardContent>

packages/evershop/src/modules/stripe/bootstrap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export default async () => {
1818
},
1919
stripe_captured: {
2020
name: 'Captured',
21-
isDefault: true,
21+
isDefault: false,
2222
isCancelable: false,
2323
badge: 'success'
2424
},

0 commit comments

Comments
 (0)