@easypayment/medusa-payment-paypal 1.1.1 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.medusa/server/src/admin/index.js +7 -7
- package/.medusa/server/src/admin/index.mjs +7 -7
- package/.medusa/server/src/api/hooks/paypal/onboard-return/route.d.ts +16 -0
- package/.medusa/server/src/api/hooks/paypal/onboard-return/route.d.ts.map +1 -0
- package/.medusa/server/src/api/hooks/paypal/onboard-return/route.js +20 -0
- package/.medusa/server/src/api/hooks/paypal/onboard-return/route.js.map +1 -0
- package/.medusa/server/src/api/hooks/paypal/webhook/route.d.ts +18 -0
- package/.medusa/server/src/api/hooks/paypal/webhook/route.d.ts.map +1 -0
- package/.medusa/server/src/api/hooks/paypal/webhook/route.js +22 -0
- package/.medusa/server/src/api/hooks/paypal/webhook/route.js.map +1 -0
- package/.medusa/server/src/api/middlewares.d.ts.map +1 -1
- package/.medusa/server/src/api/middlewares.js +24 -1
- package/.medusa/server/src/api/middlewares.js.map +1 -1
- package/.medusa/server/src/api/store/paypal/capture-order/route.d.ts.map +1 -1
- package/.medusa/server/src/api/store/paypal/capture-order/route.js +66 -13
- package/.medusa/server/src/api/store/paypal/capture-order/route.js.map +1 -1
- package/.medusa/server/src/api/store/paypal/config/route.d.ts.map +1 -1
- package/.medusa/server/src/api/store/paypal/config/route.js +8 -0
- package/.medusa/server/src/api/store/paypal/config/route.js.map +1 -1
- package/.medusa/server/src/api/store/paypal/create-order/route.d.ts.map +1 -1
- package/.medusa/server/src/api/store/paypal/create-order/route.js +63 -9
- package/.medusa/server/src/api/store/paypal/create-order/route.js.map +1 -1
- package/.medusa/server/src/api/store/paypal-complete/route.js +1 -1
- package/.medusa/server/src/api/store/paypal-complete/route.js.map +1 -1
- package/.medusa/server/src/api/utils/rate-limit.d.ts +13 -1
- package/.medusa/server/src/api/utils/rate-limit.d.ts.map +1 -1
- package/.medusa/server/src/api/utils/rate-limit.js +18 -7
- package/.medusa/server/src/api/utils/rate-limit.js.map +1 -1
- package/.medusa/server/src/jobs/paypal-webhook-retry.d.ts.map +1 -1
- package/.medusa/server/src/jobs/paypal-webhook-retry.js +4 -0
- package/.medusa/server/src/jobs/paypal-webhook-retry.js.map +1 -1
- package/.medusa/server/src/modules/paypal/payment-provider/base-provider.d.ts +47 -3
- package/.medusa/server/src/modules/paypal/payment-provider/base-provider.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/payment-provider/base-provider.js +73 -0
- package/.medusa/server/src/modules/paypal/payment-provider/base-provider.js.map +1 -1
- package/.medusa/server/src/modules/paypal/payment-provider/card-service.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/payment-provider/card-service.js +415 -285
- package/.medusa/server/src/modules/paypal/payment-provider/card-service.js.map +1 -1
- package/.medusa/server/src/modules/paypal/payment-provider/service.d.ts +0 -4
- package/.medusa/server/src/modules/paypal/payment-provider/service.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/payment-provider/service.js +140 -151
- package/.medusa/server/src/modules/paypal/payment-provider/service.js.map +1 -1
- package/.medusa/server/src/modules/paypal/service.d.ts +7 -0
- package/.medusa/server/src/modules/paypal/service.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/service.js +135 -13
- package/.medusa/server/src/modules/paypal/service.js.map +1 -1
- package/.medusa/server/src/modules/paypal/types/config.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/types/config.js +13 -0
- package/.medusa/server/src/modules/paypal/types/config.js.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/amounts.d.ts +1 -1
- package/.medusa/server/src/modules/paypal/utils/credential-resolver.d.ts +10 -0
- package/.medusa/server/src/modules/paypal/utils/credential-resolver.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/credential-resolver.js +49 -34
- package/.medusa/server/src/modules/paypal/utils/credential-resolver.js.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/metrics.d.ts +18 -0
- package/.medusa/server/src/modules/paypal/utils/metrics.d.ts.map +1 -0
- package/.medusa/server/src/modules/paypal/utils/metrics.js +45 -0
- package/.medusa/server/src/modules/paypal/utils/metrics.js.map +1 -0
- package/.medusa/server/src/modules/paypal/utils/paypal-fetch.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/paypal-fetch.js +45 -28
- package/.medusa/server/src/modules/paypal/utils/paypal-fetch.js.map +1 -1
- package/.medusa/server/src/modules/paypal/webhook-processor.d.ts +27 -0
- package/.medusa/server/src/modules/paypal/webhook-processor.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/webhook-processor.js +170 -9
- package/.medusa/server/src/modules/paypal/webhook-processor.js.map +1 -1
- package/.medusa/server/src/subscribers/paypal-webhook-process.d.ts.map +1 -1
- package/.medusa/server/src/subscribers/paypal-webhook-process.js +4 -0
- package/.medusa/server/src/subscribers/paypal-webhook-process.js.map +1 -1
- package/CHANGELOG.md +208 -73
- package/README.md +175 -184
- package/package.json +91 -90
- package/src/api/hooks/paypal/onboard-return/route.ts +15 -0
- package/src/api/hooks/paypal/webhook/route.ts +17 -0
- package/src/api/middlewares.ts +24 -1
- package/src/api/store/paypal/capture-order/route.ts +83 -12
- package/src/api/store/paypal/config/route.ts +8 -0
- package/src/api/store/paypal/create-order/route.ts +73 -9
- package/src/api/store/paypal-complete/route.ts +1 -1
- package/src/api/utils/rate-limit.ts +31 -7
- package/src/jobs/paypal-webhook-retry.ts +4 -0
- package/src/modules/paypal/payment-provider/base-provider.ts +83 -0
- package/src/modules/paypal/payment-provider/card-service.ts +277 -136
- package/src/modules/paypal/payment-provider/service.ts +149 -186
- package/src/modules/paypal/service.ts +1703 -1554
- package/src/modules/paypal/types/config.ts +16 -0
- package/src/modules/paypal/utils/credential-resolver.ts +45 -36
- package/src/modules/paypal/utils/metrics.ts +48 -0
- package/src/modules/paypal/utils/paypal-fetch.ts +58 -29
- package/src/modules/paypal/webhook-processor.ts +195 -10
- package/src/subscribers/paypal-webhook-process.ts +4 -0
|
@@ -35,33 +35,23 @@ class PayPalAdvancedCardProvider extends base_provider_1.PayPalProviderBase {
|
|
|
35
35
|
currencyCode,
|
|
36
36
|
paypalCurrencyOverride: currencyOverride,
|
|
37
37
|
});
|
|
38
|
+
// Preserve the provider_id passthrough exactly like the wallet provider —
|
|
39
|
+
// dropping it here would make the two providers' session data diverge.
|
|
40
|
+
const providerId = input.data?.provider_id;
|
|
38
41
|
return {
|
|
39
42
|
data: {
|
|
40
43
|
...(input.data || {}),
|
|
44
|
+
...this.invalidateStaleOrder(input, currencyCode),
|
|
45
|
+
...(providerId ? { provider_id: providerId } : {}),
|
|
41
46
|
amount: input.amount,
|
|
42
47
|
currency_code: currencyCode,
|
|
43
48
|
},
|
|
44
49
|
};
|
|
45
50
|
}
|
|
46
51
|
async authorizePayment(_input) {
|
|
47
|
-
const { data
|
|
52
|
+
const { data } = await this.normalizePaymentData(_input);
|
|
48
53
|
const requestId = this.getIdempotencyKey(_input, "authorize");
|
|
49
|
-
|
|
50
|
-
const { additionalSettings, advancedCardSettings } = await this.resolveSettings();
|
|
51
|
-
const paymentActionRaw = typeof additionalSettings.paymentAction === "string"
|
|
52
|
-
? additionalSettings.paymentAction
|
|
53
|
-
: "capture";
|
|
54
|
-
const orderIntent = paymentActionRaw === "authorize" ? "AUTHORIZE" : "CAPTURE";
|
|
55
|
-
const returnStatus = paymentActionRaw === "authorize" ? "authorized" : "captured";
|
|
56
|
-
const timestampKey = paymentActionRaw === "authorize" ? "authorized_at" : "captured_at";
|
|
57
|
-
const threeDsRaw = typeof advancedCardSettings.threeDS === "string"
|
|
58
|
-
? advancedCardSettings.threeDS
|
|
59
|
-
: "when_required";
|
|
60
|
-
const threeDsMethod = threeDsRaw === "always"
|
|
61
|
-
? "SCA_ALWAYS"
|
|
62
|
-
: threeDsRaw === "when_required" || threeDsRaw === "sli"
|
|
63
|
-
? "SCA_WHEN_REQUIRED"
|
|
64
|
-
: null;
|
|
54
|
+
const { advancedCardSettings } = await this.resolveSettings();
|
|
65
55
|
const disabledCards = Array.isArray(advancedCardSettings.disabledCards)
|
|
66
56
|
? advancedCardSettings.disabledCards.map((card) => String(card).toLowerCase())
|
|
67
57
|
: [];
|
|
@@ -70,129 +60,204 @@ class PayPalAdvancedCardProvider extends base_provider_1.PayPalProviderBase {
|
|
|
70
60
|
throw new Error(`Card brand ${cardBrand} is disabled by admin settings.`);
|
|
71
61
|
}
|
|
72
62
|
const existingPayPal = (data.paypal || {});
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
63
|
+
// Session already carries a settled capture: trust it without a network
|
|
64
|
+
// round-trip, but only when the stored capture is actually COMPLETED — a
|
|
65
|
+
// PENDING/DECLINED capture must never be booked as captured money.
|
|
66
|
+
const storedCaptureStatus = (0, status_utils_1.extractCaptureStatus)(existingPayPal.capture);
|
|
67
|
+
if (storedCaptureStatus === "COMPLETED") {
|
|
77
68
|
return {
|
|
78
|
-
status:
|
|
69
|
+
status: "captured",
|
|
79
70
|
data: {
|
|
80
71
|
...(_input.data || {}),
|
|
81
|
-
|
|
72
|
+
captured_at: data.captured_at || new Date().toISOString(),
|
|
82
73
|
},
|
|
83
74
|
};
|
|
84
75
|
}
|
|
85
|
-
const
|
|
86
|
-
let orderId = String(existingPayPal.order_id || data.order_id || "");
|
|
87
|
-
let order = null;
|
|
88
|
-
let authorization = null;
|
|
76
|
+
const orderId = String(existingPayPal.order_id || data.order_id || "");
|
|
89
77
|
if (!orderId) {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
value,
|
|
100
|
-
},
|
|
101
|
-
},
|
|
102
|
-
],
|
|
103
|
-
custom_id: data.session_id || data.cart_id || data.payment_collection_id || undefined,
|
|
104
|
-
...(threeDsMethod
|
|
105
|
-
? {
|
|
106
|
-
payment_source: {
|
|
107
|
-
card: {
|
|
108
|
-
attributes: {
|
|
109
|
-
verification: {
|
|
110
|
-
method: threeDsMethod,
|
|
111
|
-
},
|
|
112
|
-
},
|
|
113
|
-
},
|
|
114
|
-
},
|
|
115
|
-
}
|
|
116
|
-
: {}),
|
|
117
|
-
};
|
|
118
|
-
const ppResp = await (0, paypal_fetch_1.paypalFetch)(`${base}/v2/checkout/orders`, {
|
|
119
|
-
method: "POST",
|
|
120
|
-
headers: {
|
|
121
|
-
Authorization: `Bearer ${accessToken}`,
|
|
122
|
-
"Content-Type": "application/json",
|
|
123
|
-
"PayPal-Request-Id": requestId,
|
|
124
|
-
"PayPal-Partner-Attribution-Id": partner_1.PAYPAL_PARTNER_ATTRIBUTION_ID,
|
|
125
|
-
},
|
|
126
|
-
body: JSON.stringify(orderPayload),
|
|
78
|
+
// Nothing to authorize: a PayPal order only exists after the buyer
|
|
79
|
+
// submitted their card via the storefront (create-order + card fields).
|
|
80
|
+
// The previous fallback created a fresh order here and immediately
|
|
81
|
+
// called /authorize on it, which PayPal always rejects (no payment
|
|
82
|
+
// source, and 422 UNSUPPORTED_INTENT for CAPTURE-intent orders).
|
|
83
|
+
await this.recordFailure("authorize_failed", {
|
|
84
|
+
cart_id: data.cart_id,
|
|
85
|
+
payment_collection_id: data.payment_collection_id,
|
|
86
|
+
message: "no PayPal order on card session",
|
|
127
87
|
});
|
|
128
|
-
|
|
129
|
-
debugId = ppResp.headers.get("paypal-debug-id");
|
|
130
|
-
if (!ppResp.ok) {
|
|
131
|
-
throw new Error(`PayPal create order error (${ppResp.status}): ${ppText}${debugId ? ` debug_id=${debugId}` : ""}`);
|
|
132
|
-
}
|
|
133
|
-
order = JSON.parse(ppText);
|
|
134
|
-
orderId = String(order.id || "");
|
|
88
|
+
throw new utils_1.MedusaError(utils_1.MedusaError.Types.NOT_ALLOWED, "No PayPal order was found for this payment session. The buyer must submit and approve their card payment before the cart can be completed.");
|
|
135
89
|
}
|
|
136
|
-
|
|
90
|
+
let order = null;
|
|
91
|
+
try {
|
|
137
92
|
order = (await this.getOrderDetails(orderId));
|
|
138
93
|
}
|
|
139
|
-
|
|
94
|
+
catch (e) {
|
|
95
|
+
console.warn("[PayPal] card authorizePayment: order lookup failed:", e?.message);
|
|
96
|
+
// Fall back to the session's own record on a transient lookup failure so
|
|
97
|
+
// checkout isn't blocked when a real authorization/capture exists.
|
|
98
|
+
if (data.captured_at) {
|
|
99
|
+
return {
|
|
100
|
+
status: "captured",
|
|
101
|
+
data: { ...(_input.data || {}), captured_at: data.captured_at },
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
if (existingPayPal.capture_id ||
|
|
105
|
+
existingPayPal.authorization_id ||
|
|
106
|
+
data.authorized_at) {
|
|
107
|
+
return {
|
|
108
|
+
status: "authorized",
|
|
109
|
+
data: {
|
|
110
|
+
...(_input.data || {}),
|
|
111
|
+
authorized_at: data.authorized_at || new Date().toISOString(),
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
throw e;
|
|
116
|
+
}
|
|
117
|
+
if (!order) {
|
|
140
118
|
throw new Error("Unable to resolve PayPal order details for authorization.");
|
|
141
119
|
}
|
|
142
|
-
const
|
|
143
|
-
const
|
|
144
|
-
|
|
145
|
-
|
|
120
|
+
const capture = order?.purchase_units?.[0]?.payments?.captures?.[0];
|
|
121
|
+
const authorization = order?.purchase_units?.[0]?.payments?.authorizations?.[0];
|
|
122
|
+
// Derive the status from what actually happened at PayPal, never from the
|
|
123
|
+
// mere presence of a capture/authorization or from the configured
|
|
124
|
+
// paymentAction: a PENDING (eCheck) capture is not settled money and a
|
|
125
|
+
// DENIED/DECLINED one must fail the authorization.
|
|
126
|
+
if (capture?.id) {
|
|
127
|
+
const captureStatus = this.mapCaptureStatus(capture?.status);
|
|
128
|
+
if (captureStatus === "captured") {
|
|
129
|
+
return {
|
|
130
|
+
status: "captured",
|
|
131
|
+
data: {
|
|
132
|
+
...(data || {}),
|
|
133
|
+
paypal: {
|
|
134
|
+
...existingPayPal,
|
|
135
|
+
order_id: orderId,
|
|
136
|
+
order,
|
|
137
|
+
capture_id: capture.id,
|
|
138
|
+
capture,
|
|
139
|
+
},
|
|
140
|
+
captured_at: new Date().toISOString(),
|
|
141
|
+
},
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
if (captureStatus === "pending") {
|
|
145
|
+
return {
|
|
146
|
+
status: "authorized",
|
|
147
|
+
data: {
|
|
148
|
+
...(data || {}),
|
|
149
|
+
paypal: {
|
|
150
|
+
...existingPayPal,
|
|
151
|
+
order_id: orderId,
|
|
152
|
+
order,
|
|
153
|
+
capture_id: capture.id,
|
|
154
|
+
},
|
|
155
|
+
authorized_at: new Date().toISOString(),
|
|
156
|
+
},
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
if (captureStatus === "error" || captureStatus === "canceled") {
|
|
160
|
+
return {
|
|
161
|
+
status: "error",
|
|
162
|
+
data: {
|
|
163
|
+
...(data || {}),
|
|
164
|
+
paypal: { ...existingPayPal, order_id: orderId, order },
|
|
165
|
+
},
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
if (authorization?.id) {
|
|
170
|
+
const authStatus = this.mapAuthorizationStatus(authorization?.status);
|
|
171
|
+
if (authStatus === "authorized") {
|
|
172
|
+
return {
|
|
173
|
+
status: "authorized",
|
|
174
|
+
data: {
|
|
175
|
+
...(data || {}),
|
|
176
|
+
paypal: {
|
|
177
|
+
...existingPayPal,
|
|
178
|
+
order_id: orderId,
|
|
179
|
+
order,
|
|
180
|
+
authorization_id: authorization.id,
|
|
181
|
+
authorizations: order?.purchase_units?.[0]?.payments?.authorizations || [],
|
|
182
|
+
},
|
|
183
|
+
authorized_at: new Date().toISOString(),
|
|
184
|
+
},
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
if (authStatus === "error" || authStatus === "canceled") {
|
|
188
|
+
return {
|
|
189
|
+
status: "error",
|
|
190
|
+
data: {
|
|
191
|
+
...(data || {}),
|
|
192
|
+
paypal: { ...existingPayPal, order_id: orderId, order },
|
|
193
|
+
},
|
|
194
|
+
};
|
|
195
|
+
}
|
|
146
196
|
}
|
|
147
|
-
|
|
197
|
+
const orderStatus = String(order?.status || "").toUpperCase();
|
|
198
|
+
const orderIntent = String(order?.intent || "").toUpperCase();
|
|
199
|
+
if (orderStatus === "APPROVED") {
|
|
200
|
+
if (orderIntent === "AUTHORIZE") {
|
|
201
|
+
// Approved AUTHORIZE-intent order with no authorization yet: create it.
|
|
202
|
+
const { accessToken, base } = await this.getPayPalAccessToken();
|
|
203
|
+
const authorizeResp = await (0, paypal_fetch_1.paypalFetch)(`${base}/v2/checkout/orders/${encodeURIComponent(orderId)}/authorize`, {
|
|
204
|
+
method: "POST",
|
|
205
|
+
headers: {
|
|
206
|
+
Authorization: `Bearer ${accessToken}`,
|
|
207
|
+
"Content-Type": "application/json",
|
|
208
|
+
"PayPal-Request-Id": `${requestId}-auth`,
|
|
209
|
+
"PayPal-Partner-Attribution-Id": partner_1.PAYPAL_PARTNER_ATTRIBUTION_ID,
|
|
210
|
+
},
|
|
211
|
+
});
|
|
212
|
+
const authorizeText = await authorizeResp.text();
|
|
213
|
+
const authorizeDebugId = authorizeResp.headers.get("paypal-debug-id");
|
|
214
|
+
if (!authorizeResp.ok) {
|
|
215
|
+
throw new Error(`PayPal authorize order error (${authorizeResp.status}): ${authorizeText}${authorizeDebugId ? ` debug_id=${authorizeDebugId}` : ""}`);
|
|
216
|
+
}
|
|
217
|
+
const authorized = JSON.parse(authorizeText);
|
|
218
|
+
const authorizationId = authorized?.purchase_units?.[0]?.payments?.authorizations?.[0]?.id;
|
|
219
|
+
return {
|
|
220
|
+
status: "authorized",
|
|
221
|
+
data: {
|
|
222
|
+
...(data || {}),
|
|
223
|
+
paypal: {
|
|
224
|
+
...existingPayPal,
|
|
225
|
+
order_id: orderId,
|
|
226
|
+
order: authorized || order,
|
|
227
|
+
authorization_id: authorizationId,
|
|
228
|
+
authorizations: authorized?.purchase_units?.[0]?.payments?.authorizations || [],
|
|
229
|
+
},
|
|
230
|
+
authorized_at: new Date().toISOString(),
|
|
231
|
+
},
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
// Approved CAPTURE-intent order: the buyer approved but the capture has
|
|
235
|
+
// not happened yet (e.g. the storefront capture call failed). Report
|
|
236
|
+
// "authorized" so the order can complete; capturePayment then routes the
|
|
237
|
+
// actual capture by intent. Calling /authorize here — as the previous
|
|
238
|
+
// code did for every intent — always fails with 422 UNSUPPORTED_INTENT
|
|
239
|
+
// and permanently stuck the checkout.
|
|
148
240
|
return {
|
|
149
|
-
status:
|
|
241
|
+
status: "authorized",
|
|
150
242
|
data: {
|
|
151
243
|
...(data || {}),
|
|
152
244
|
paypal: {
|
|
153
245
|
...existingPayPal,
|
|
154
246
|
order_id: orderId,
|
|
155
247
|
order,
|
|
156
|
-
capture_id: existingCapture.id,
|
|
157
|
-
capture: existingCapture,
|
|
158
248
|
},
|
|
159
|
-
|
|
249
|
+
authorized_at: new Date().toISOString(),
|
|
160
250
|
},
|
|
161
251
|
};
|
|
162
252
|
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
},
|
|
172
|
-
});
|
|
173
|
-
const authorizeText = await authorizeResp.text();
|
|
174
|
-
const authorizeDebugId = authorizeResp.headers.get("paypal-debug-id");
|
|
175
|
-
if (!authorizeResp.ok) {
|
|
176
|
-
throw new Error(`PayPal authorize order error (${authorizeResp.status}): ${authorizeText}${authorizeDebugId ? ` debug_id=${authorizeDebugId}` : ""}`);
|
|
177
|
-
}
|
|
178
|
-
authorization = JSON.parse(authorizeText);
|
|
179
|
-
}
|
|
180
|
-
const authorizationId = authorization?.purchase_units?.[0]?.payments?.authorizations?.[0]?.id ||
|
|
181
|
-
existingAuthorization?.id;
|
|
182
|
-
return {
|
|
183
|
-
status: "authorized",
|
|
184
|
-
data: {
|
|
185
|
-
...(data || {}),
|
|
186
|
-
paypal: {
|
|
187
|
-
...existingPayPal,
|
|
188
|
-
order_id: orderId,
|
|
189
|
-
order: order || authorization,
|
|
190
|
-
authorization_id: authorizationId,
|
|
191
|
-
authorizations: authorization?.purchase_units?.[0]?.payments?.authorizations || [],
|
|
192
|
-
},
|
|
193
|
-
authorized_at: new Date().toISOString(),
|
|
194
|
-
},
|
|
195
|
-
};
|
|
253
|
+
// CREATED / SAVED / PAYER_ACTION_REQUIRED etc.: the buyer never approved
|
|
254
|
+
// the payment — nothing is authorized at PayPal.
|
|
255
|
+
await this.recordFailure("authorize_failed", {
|
|
256
|
+
order_id: orderId,
|
|
257
|
+
order_status: orderStatus,
|
|
258
|
+
message: "PayPal order not approved by buyer",
|
|
259
|
+
});
|
|
260
|
+
throw new utils_1.MedusaError(utils_1.MedusaError.Types.NOT_ALLOWED, `PayPal order ${orderId} has not been approved by the buyer (status ${orderStatus || "UNKNOWN"}). The payment cannot be authorized.`);
|
|
196
261
|
}
|
|
197
262
|
async capturePayment(_input) {
|
|
198
263
|
const data = (_input.data || {});
|
|
@@ -215,133 +280,161 @@ class PayPalAdvancedCardProvider extends base_provider_1.PayPalProviderBase {
|
|
|
215
280
|
},
|
|
216
281
|
};
|
|
217
282
|
}
|
|
218
|
-
const { amount, currencyCode } = await this.normalizePaymentData(_input);
|
|
283
|
+
const { amount: sessionAmount, currencyCode } = await this.normalizePaymentData(_input);
|
|
284
|
+
// The requested (possibly partial) capture amount lives on the Medusa
|
|
285
|
+
// capture row, not in the provider input — resolve it so partial captures
|
|
286
|
+
// don't charge the full session total.
|
|
287
|
+
const amount = await this.resolveRequestedCaptureAmount(_input, sessionAmount);
|
|
219
288
|
// Include the amount in the idempotency suffix: PayPal deduplicates by
|
|
220
289
|
// PayPal-Request-Id, so two sequential partial captures of the same order
|
|
221
290
|
// that share an upstream idempotency_key would otherwise collide and the
|
|
222
291
|
// second capture would silently return the first one's result.
|
|
223
292
|
const requestId = this.getIdempotencyKey(_input, `capture-${orderId}-${amount}`);
|
|
224
293
|
let debugId = null;
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
294
|
+
try {
|
|
295
|
+
const { accessToken, base } = await this.getPayPalAccessToken();
|
|
296
|
+
const order = await this.getOrderDetails(orderId).catch(() => null);
|
|
297
|
+
const existingCapture = order?.purchase_units?.[0]?.payments?.captures?.[0];
|
|
298
|
+
if (existingCapture?.id && (0, status_utils_1.isCaptureCompleted)(existingCapture)) {
|
|
299
|
+
return {
|
|
300
|
+
data: {
|
|
301
|
+
...(data || {}),
|
|
302
|
+
paypal: {
|
|
303
|
+
...paypalData,
|
|
304
|
+
capture_id: existingCapture.id,
|
|
305
|
+
capture: existingCapture,
|
|
306
|
+
},
|
|
307
|
+
captured_at: new Date().toISOString(),
|
|
236
308
|
},
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
const resolvedIntent = String(order?.intent || paypalData.order?.intent || data.intent || "").toUpperCase();
|
|
312
|
+
if (!authorizationId && resolvedIntent === "AUTHORIZE") {
|
|
313
|
+
const authorizeResp = await (0, paypal_fetch_1.paypalFetch)(`${base}/v2/checkout/orders/${encodeURIComponent(orderId)}/authorize`, {
|
|
314
|
+
method: "POST",
|
|
315
|
+
headers: {
|
|
316
|
+
Authorization: `Bearer ${accessToken}`,
|
|
317
|
+
"Content-Type": "application/json",
|
|
318
|
+
"PayPal-Request-Id": `${requestId}-auth`,
|
|
319
|
+
"PayPal-Partner-Attribution-Id": partner_1.PAYPAL_PARTNER_ATTRIBUTION_ID,
|
|
320
|
+
},
|
|
321
|
+
});
|
|
322
|
+
const authorizeText = await authorizeResp.text();
|
|
323
|
+
debugId = authorizeResp.headers.get("paypal-debug-id");
|
|
324
|
+
if (!authorizeResp.ok) {
|
|
325
|
+
throw new Error(`PayPal authorize order error (${authorizeResp.status}): ${authorizeText}${debugId ? ` debug_id=${debugId}` : ""}`);
|
|
326
|
+
}
|
|
327
|
+
const authorization = JSON.parse(authorizeText);
|
|
328
|
+
authorizationId =
|
|
329
|
+
authorization?.purchase_units?.[0]?.payments?.authorizations?.[0]?.id;
|
|
330
|
+
}
|
|
331
|
+
const isFinalCapture = paypalData.is_final_capture ??
|
|
332
|
+
data.is_final_capture ??
|
|
333
|
+
data.final_capture ??
|
|
334
|
+
undefined;
|
|
335
|
+
const captureValue = amount > 0
|
|
336
|
+
? (0, amounts_1.formatAmountForPayPal)(amount, currencyCode || "EUR")
|
|
337
|
+
: null;
|
|
338
|
+
// `amount` and `is_final_capture` are only honored on the authorizations
|
|
339
|
+
// capture endpoint. The orders capture endpoint always captures the FULL
|
|
340
|
+
// order and silently ignores an `amount` body — so a partial amount there
|
|
341
|
+
// would over-capture while we record the smaller requested value. Route
|
|
342
|
+
// partial captures through the authorization, and fail closed if a partial
|
|
343
|
+
// capture is attempted against a capture-intent order.
|
|
344
|
+
let capturePayload;
|
|
345
|
+
let captureUrl;
|
|
346
|
+
if (authorizationId) {
|
|
347
|
+
captureUrl = `${base}/v2/payments/authorizations/${encodeURIComponent(authorizationId)}/capture`;
|
|
348
|
+
capturePayload = {
|
|
349
|
+
...(captureValue
|
|
350
|
+
? { amount: { currency_code: currencyCode || "EUR", value: captureValue } }
|
|
351
|
+
: {}),
|
|
352
|
+
...(typeof isFinalCapture === "boolean" ? { is_final_capture: isFinalCapture } : {}),
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
else {
|
|
356
|
+
captureUrl = `${base}/v2/checkout/orders/${encodeURIComponent(orderId)}/capture`;
|
|
357
|
+
capturePayload = {};
|
|
358
|
+
const orderTotal = order?.purchase_units?.[0]?.amount?.value;
|
|
359
|
+
if (captureValue && orderTotal && captureValue !== String(orderTotal)) {
|
|
360
|
+
throw new Error(`PayPal partial capture (${captureValue} ${currencyCode || "EUR"}) is not supported for ` +
|
|
361
|
+
`capture-intent orders (order total ${orderTotal}). Create the order with intent ` +
|
|
362
|
+
`AUTHORIZE to capture a partial amount.`);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
// Retry transient 5xx/429/timeout: the PayPal-Request-Id makes the capture
|
|
366
|
+
// idempotent, so a retry after a network blip re-uses the same capture
|
|
367
|
+
// instead of double-charging.
|
|
368
|
+
const ppResp = await (0, paypal_fetch_1.paypalFetchWithRetry)(captureUrl, {
|
|
244
369
|
method: "POST",
|
|
245
370
|
headers: {
|
|
246
371
|
Authorization: `Bearer ${accessToken}`,
|
|
247
372
|
"Content-Type": "application/json",
|
|
248
|
-
"PayPal-Request-Id":
|
|
373
|
+
"PayPal-Request-Id": requestId,
|
|
249
374
|
"PayPal-Partner-Attribution-Id": partner_1.PAYPAL_PARTNER_ATTRIBUTION_ID,
|
|
250
375
|
},
|
|
376
|
+
body: JSON.stringify(capturePayload),
|
|
251
377
|
});
|
|
252
|
-
const
|
|
253
|
-
debugId =
|
|
254
|
-
if (!
|
|
255
|
-
throw new Error(`PayPal
|
|
378
|
+
const ppText = await ppResp.text();
|
|
379
|
+
debugId = ppResp.headers.get("paypal-debug-id");
|
|
380
|
+
if (!ppResp.ok) {
|
|
381
|
+
throw new Error(`PayPal capture error (${ppResp.status}): ${ppText}${debugId ? ` debug_id=${debugId}` : ""}`);
|
|
256
382
|
}
|
|
257
|
-
const
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
? (0, amounts_1.formatAmountForPayPal)(amount, currencyCode || "EUR")
|
|
267
|
-
: null;
|
|
268
|
-
// `amount` and `is_final_capture` are only honored on the authorizations
|
|
269
|
-
// capture endpoint. The orders capture endpoint always captures the FULL
|
|
270
|
-
// order and silently ignores an `amount` body — so a partial amount there
|
|
271
|
-
// would over-capture while we record the smaller requested value. Route
|
|
272
|
-
// partial captures through the authorization, and fail closed if a partial
|
|
273
|
-
// capture is attempted against a capture-intent order.
|
|
274
|
-
let capturePayload;
|
|
275
|
-
let captureUrl;
|
|
276
|
-
if (authorizationId) {
|
|
277
|
-
captureUrl = `${base}/v2/payments/authorizations/${encodeURIComponent(authorizationId)}/capture`;
|
|
278
|
-
capturePayload = {
|
|
279
|
-
...(captureValue
|
|
280
|
-
? { amount: { currency_code: currencyCode || "EUR", value: captureValue } }
|
|
281
|
-
: {}),
|
|
282
|
-
...(typeof isFinalCapture === "boolean" ? { is_final_capture: isFinalCapture } : {}),
|
|
283
|
-
};
|
|
284
|
-
}
|
|
285
|
-
else {
|
|
286
|
-
captureUrl = `${base}/v2/checkout/orders/${encodeURIComponent(orderId)}/capture`;
|
|
287
|
-
capturePayload = {};
|
|
288
|
-
const orderTotal = order?.purchase_units?.[0]?.amount?.value;
|
|
289
|
-
if (captureValue && orderTotal && captureValue !== String(orderTotal)) {
|
|
290
|
-
throw new Error(`PayPal partial capture (${captureValue} ${currencyCode || "EUR"}) is not supported for ` +
|
|
291
|
-
`capture-intent orders (order total ${orderTotal}). Create the order with intent ` +
|
|
292
|
-
`AUTHORIZE to capture a partial amount.`);
|
|
383
|
+
const capture = JSON.parse(ppText);
|
|
384
|
+
// A 2xx response does NOT mean the funds were captured. PayPal returns 201
|
|
385
|
+
// for captures that are PENDING (pending review / eCheck), DECLINED, or
|
|
386
|
+
// FAILED. Recording any of these as "captured" books money that never
|
|
387
|
+
// settled, so only a COMPLETED capture is treated as success.
|
|
388
|
+
const captureStatus = (0, status_utils_1.extractCaptureStatus)(capture);
|
|
389
|
+
if (captureStatus !== "COMPLETED") {
|
|
390
|
+
throw new Error(`PayPal capture did not complete (status=${captureStatus || "UNKNOWN"}). ` +
|
|
391
|
+
`The payment was not captured.${debugId ? ` debug_id=${debugId}` : ""}`);
|
|
293
392
|
}
|
|
393
|
+
const captureId = capture?.id || capture?.purchase_units?.[0]?.payments?.captures?.[0]?.id;
|
|
394
|
+
const existingCaptures = Array.isArray(paypalData.captures) ? paypalData.captures : [];
|
|
395
|
+
const captureEntry = {
|
|
396
|
+
id: captureId,
|
|
397
|
+
status: capture?.status,
|
|
398
|
+
amount: capture?.amount,
|
|
399
|
+
raw: capture,
|
|
400
|
+
};
|
|
401
|
+
await this.recordSuccess("capture_success");
|
|
402
|
+
await this.recordPaymentEvent("capture", {
|
|
403
|
+
order_id: orderId,
|
|
404
|
+
capture_id: captureId,
|
|
405
|
+
authorization_id: authorizationId || undefined,
|
|
406
|
+
amount,
|
|
407
|
+
currency_code: currencyCode,
|
|
408
|
+
request_id: requestId,
|
|
409
|
+
});
|
|
410
|
+
return {
|
|
411
|
+
data: {
|
|
412
|
+
...(data || {}),
|
|
413
|
+
paypal: {
|
|
414
|
+
...paypalData,
|
|
415
|
+
order_id: orderId,
|
|
416
|
+
capture_id: captureId,
|
|
417
|
+
capture,
|
|
418
|
+
authorization_id: authorizationId || paypalData.authorization_id,
|
|
419
|
+
captures: [...existingCaptures, captureEntry],
|
|
420
|
+
},
|
|
421
|
+
captured_at: new Date().toISOString(),
|
|
422
|
+
},
|
|
423
|
+
};
|
|
294
424
|
}
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
});
|
|
308
|
-
const ppText = await ppResp.text();
|
|
309
|
-
debugId = ppResp.headers.get("paypal-debug-id");
|
|
310
|
-
if (!ppResp.ok) {
|
|
311
|
-
throw new Error(`PayPal capture error (${ppResp.status}): ${ppText}${debugId ? ` debug_id=${debugId}` : ""}`);
|
|
312
|
-
}
|
|
313
|
-
const capture = JSON.parse(ppText);
|
|
314
|
-
// A 2xx response does NOT mean the funds were captured. PayPal returns 201
|
|
315
|
-
// for captures that are PENDING (pending review / eCheck), DECLINED, or
|
|
316
|
-
// FAILED. Recording any of these as "captured" books money that never
|
|
317
|
-
// settled, so only a COMPLETED capture is treated as success.
|
|
318
|
-
const captureStatus = (0, status_utils_1.extractCaptureStatus)(capture);
|
|
319
|
-
if (captureStatus !== "COMPLETED") {
|
|
320
|
-
throw new Error(`PayPal capture did not complete (status=${captureStatus || "UNKNOWN"}). ` +
|
|
321
|
-
`The payment was not captured.${debugId ? ` debug_id=${debugId}` : ""}`);
|
|
425
|
+
catch (error) {
|
|
426
|
+
await this.recordFailure("capture_failed", {
|
|
427
|
+
order_id: orderId,
|
|
428
|
+
request_id: requestId,
|
|
429
|
+
debug_id: debugId,
|
|
430
|
+
message: error?.message,
|
|
431
|
+
});
|
|
432
|
+
// Surface the real reason: Medusa masks non-MedusaErrors as a generic
|
|
433
|
+
// "unknown error" in production, hiding the PayPal failure.
|
|
434
|
+
throw error instanceof utils_1.MedusaError
|
|
435
|
+
? error
|
|
436
|
+
: new utils_1.MedusaError(utils_1.MedusaError.Types.INVALID_DATA, error?.message || "PayPal capture failed.");
|
|
322
437
|
}
|
|
323
|
-
const captureId = capture?.id || capture?.purchase_units?.[0]?.payments?.captures?.[0]?.id;
|
|
324
|
-
const existingCaptures = Array.isArray(paypalData.captures) ? paypalData.captures : [];
|
|
325
|
-
const captureEntry = {
|
|
326
|
-
id: captureId,
|
|
327
|
-
status: capture?.status,
|
|
328
|
-
amount: capture?.amount,
|
|
329
|
-
raw: capture,
|
|
330
|
-
};
|
|
331
|
-
return {
|
|
332
|
-
data: {
|
|
333
|
-
...(data || {}),
|
|
334
|
-
paypal: {
|
|
335
|
-
...paypalData,
|
|
336
|
-
order_id: orderId,
|
|
337
|
-
capture_id: captureId,
|
|
338
|
-
capture,
|
|
339
|
-
authorization_id: authorizationId || paypalData.authorization_id,
|
|
340
|
-
captures: [...existingCaptures, captureEntry],
|
|
341
|
-
},
|
|
342
|
-
captured_at: new Date().toISOString(),
|
|
343
|
-
},
|
|
344
|
-
};
|
|
345
438
|
}
|
|
346
439
|
async cancelPayment(_input) {
|
|
347
440
|
const data = (_input.data || {});
|
|
@@ -349,54 +442,85 @@ class PayPalAdvancedCardProvider extends base_provider_1.PayPalProviderBase {
|
|
|
349
442
|
const orderId = String(paypalData.order_id || data.order_id || "");
|
|
350
443
|
const captureId = String(paypalData.capture_id || data.capture_id || "");
|
|
351
444
|
const storedAuthorizationId = String(paypalData.authorization_id || data.authorization_id || "");
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
445
|
+
try {
|
|
446
|
+
const order = orderId ? await this.getOrderDetails(orderId) : null;
|
|
447
|
+
const intent = String(order?.intent || "").toUpperCase();
|
|
448
|
+
const authorizationId = order?.purchase_units?.[0]?.payments?.authorizations?.[0]?.id ||
|
|
449
|
+
storedAuthorizationId;
|
|
450
|
+
if (intent === "AUTHORIZE" && authorizationId) {
|
|
451
|
+
const { accessToken, base } = await this.getPayPalAccessToken();
|
|
452
|
+
const requestId = this.getIdempotencyKey(_input, `void-${authorizationId}`);
|
|
453
|
+
const resp = await (0, paypal_fetch_1.paypalFetch)(`${base}/v2/payments/authorizations/${encodeURIComponent(authorizationId)}/void`, {
|
|
454
|
+
method: "POST",
|
|
455
|
+
headers: {
|
|
456
|
+
Authorization: `Bearer ${accessToken}`,
|
|
457
|
+
"Content-Type": "application/json",
|
|
458
|
+
"PayPal-Request-Id": requestId,
|
|
459
|
+
"PayPal-Partner-Attribution-Id": partner_1.PAYPAL_PARTNER_ATTRIBUTION_ID,
|
|
460
|
+
},
|
|
461
|
+
});
|
|
462
|
+
if (!resp.ok) {
|
|
463
|
+
const text = await resp.text();
|
|
464
|
+
const debugId = resp.headers.get("paypal-debug-id");
|
|
465
|
+
throw new Error(`PayPal void error (${resp.status}): ${text}${debugId ? ` debug_id=${debugId}` : ""}`);
|
|
466
|
+
}
|
|
467
|
+
await this.recordSuccess("void_success");
|
|
468
|
+
await this.recordPaymentEvent("void", {
|
|
469
|
+
order_id: orderId,
|
|
470
|
+
authorization_id: authorizationId,
|
|
471
|
+
});
|
|
372
472
|
}
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
473
|
+
else if (captureId) {
|
|
474
|
+
const { accessToken, base } = await this.getPayPalAccessToken();
|
|
475
|
+
const requestId = this.getIdempotencyKey(_input, `cancel-refund-${captureId}`);
|
|
476
|
+
const resp = await (0, paypal_fetch_1.paypalFetch)(`${base}/v2/payments/captures/${encodeURIComponent(captureId)}/refund`, {
|
|
477
|
+
method: "POST",
|
|
478
|
+
headers: {
|
|
479
|
+
Authorization: `Bearer ${accessToken}`,
|
|
480
|
+
"Content-Type": "application/json",
|
|
481
|
+
"PayPal-Request-Id": requestId,
|
|
482
|
+
"PayPal-Partner-Attribution-Id": partner_1.PAYPAL_PARTNER_ATTRIBUTION_ID,
|
|
483
|
+
},
|
|
484
|
+
body: JSON.stringify({}),
|
|
485
|
+
});
|
|
486
|
+
if (!resp.ok) {
|
|
487
|
+
const text = await resp.text();
|
|
488
|
+
const debugId = resp.headers.get("paypal-debug-id");
|
|
489
|
+
throw new Error(`PayPal refund error (${resp.status}): ${text}${debugId ? ` debug_id=${debugId}` : ""}`);
|
|
490
|
+
}
|
|
491
|
+
const refund = await resp.json().catch(() => ({}));
|
|
492
|
+
// As with refundPayment: a 2xx response does not guarantee the refund
|
|
493
|
+
// stuck — FAILED / CANCELLED / DENIED refunds also return 2xx. Booking
|
|
494
|
+
// `canceled_at` for a refund that never went through would record a
|
|
495
|
+
// cancellation while the merchant keeps the funds.
|
|
496
|
+
const cancelRefundStatus = String(refund?.status || "").toUpperCase();
|
|
497
|
+
if ((0, status_utils_1.isRefundFailureStatus)(cancelRefundStatus)) {
|
|
498
|
+
throw new Error(`PayPal cancel-refund did not succeed (status=${cancelRefundStatus}). The payment was not canceled.`);
|
|
499
|
+
}
|
|
500
|
+
const existingRefunds = Array.isArray(paypalData.refunds) ? paypalData.refunds : [];
|
|
501
|
+
const refundEntry = {
|
|
502
|
+
id: refund?.id,
|
|
503
|
+
status: refund?.status,
|
|
504
|
+
amount: refund?.amount,
|
|
505
|
+
raw: refund,
|
|
506
|
+
};
|
|
507
|
+
await this.recordSuccess("cancel_refund_success");
|
|
508
|
+
return {
|
|
509
|
+
data: {
|
|
510
|
+
...(data || {}),
|
|
511
|
+
paypal: {
|
|
512
|
+
...paypalData,
|
|
513
|
+
order: order || undefined,
|
|
514
|
+
authorization_id: authorizationId || storedAuthorizationId,
|
|
515
|
+
capture_id: captureId || paypalData.capture_id,
|
|
516
|
+
refund_id: refund?.id,
|
|
517
|
+
refund_status: refund?.status,
|
|
518
|
+
refunds: [...existingRefunds, refundEntry],
|
|
519
|
+
},
|
|
520
|
+
canceled_at: new Date().toISOString(),
|
|
521
|
+
},
|
|
522
|
+
};
|
|
391
523
|
}
|
|
392
|
-
const refund = await resp.json().catch(() => ({}));
|
|
393
|
-
const existingRefunds = Array.isArray(paypalData.refunds) ? paypalData.refunds : [];
|
|
394
|
-
const refundEntry = {
|
|
395
|
-
id: refund?.id,
|
|
396
|
-
status: refund?.status,
|
|
397
|
-
amount: refund?.amount,
|
|
398
|
-
raw: refund,
|
|
399
|
-
};
|
|
400
524
|
return {
|
|
401
525
|
data: {
|
|
402
526
|
...(data || {}),
|
|
@@ -405,26 +529,19 @@ class PayPalAdvancedCardProvider extends base_provider_1.PayPalProviderBase {
|
|
|
405
529
|
order: order || undefined,
|
|
406
530
|
authorization_id: authorizationId || storedAuthorizationId,
|
|
407
531
|
capture_id: captureId || paypalData.capture_id,
|
|
408
|
-
refund_id: refund?.id,
|
|
409
|
-
refund_status: refund?.status,
|
|
410
|
-
refunds: [...existingRefunds, refundEntry],
|
|
411
532
|
},
|
|
412
533
|
canceled_at: new Date().toISOString(),
|
|
413
534
|
},
|
|
414
535
|
};
|
|
415
536
|
}
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
},
|
|
425
|
-
canceled_at: new Date().toISOString(),
|
|
426
|
-
},
|
|
427
|
-
};
|
|
537
|
+
catch (error) {
|
|
538
|
+
await this.recordFailure("cancel_failed", {
|
|
539
|
+
order_id: orderId,
|
|
540
|
+
capture_id: captureId,
|
|
541
|
+
message: error?.message,
|
|
542
|
+
});
|
|
543
|
+
throw error;
|
|
544
|
+
}
|
|
428
545
|
}
|
|
429
546
|
async refundPayment(_input) {
|
|
430
547
|
const data = (_input.data || {});
|
|
@@ -488,6 +605,14 @@ class PayPalAdvancedCardProvider extends base_provider_1.PayPalProviderBase {
|
|
|
488
605
|
amount: refund?.amount,
|
|
489
606
|
raw: refund,
|
|
490
607
|
};
|
|
608
|
+
await this.recordSuccess("refund_success");
|
|
609
|
+
await this.recordPaymentEvent("refund", {
|
|
610
|
+
capture_id: captureId,
|
|
611
|
+
refund_id: refund?.id,
|
|
612
|
+
amount,
|
|
613
|
+
currency_code: currencyCode,
|
|
614
|
+
request_id: requestId,
|
|
615
|
+
});
|
|
491
616
|
return {
|
|
492
617
|
data: {
|
|
493
618
|
...(data || {}),
|
|
@@ -503,6 +628,11 @@ class PayPalAdvancedCardProvider extends base_provider_1.PayPalProviderBase {
|
|
|
503
628
|
};
|
|
504
629
|
}
|
|
505
630
|
catch (error) {
|
|
631
|
+
await this.recordFailure("refund_failed", {
|
|
632
|
+
capture_id: captureId,
|
|
633
|
+
request_id: requestId,
|
|
634
|
+
message: error?.message,
|
|
635
|
+
});
|
|
506
636
|
// Surface the real reason: Medusa masks any non-MedusaError as a generic
|
|
507
637
|
// "An unknown error occurred." in production, hiding the PayPal failure.
|
|
508
638
|
throw error instanceof utils_1.MedusaError
|