@easypayment/medusa-payment-paypal 1.3.7 → 1.3.9
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 +172 -76
- package/.medusa/server/src/admin/index.mjs +107 -13
- package/.medusa/server/src/api/admin/paypal/logs/route.d.ts.map +1 -1
- package/.medusa/server/src/api/admin/paypal/logs/route.js.map +1 -1
- package/.medusa/server/src/api/admin/paypal/merchant-status/route.d.ts +11 -0
- package/.medusa/server/src/api/admin/paypal/merchant-status/route.d.ts.map +1 -0
- package/.medusa/server/src/api/admin/paypal/merchant-status/route.js +32 -0
- package/.medusa/server/src/api/admin/paypal/merchant-status/route.js.map +1 -0
- package/.medusa/server/src/api/store/paypal/create-order/route.d.ts +4 -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 +101 -102
- package/.medusa/server/src/api/store/paypal/create-order/route.js.map +1 -1
- package/.medusa/server/src/modules/paypal/service.d.ts +34 -0
- package/.medusa/server/src/modules/paypal/service.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/service.js +161 -34
- package/.medusa/server/src/modules/paypal/service.js.map +1 -1
- package/.medusa/server/src/modules/paypal/types/config.d.ts +12 -0
- package/.medusa/server/src/modules/paypal/types/config.d.ts.map +1 -1
- package/.medusa/server/src/modules/paypal/types/config.js +25 -1
- package/.medusa/server/src/modules/paypal/types/config.js.map +1 -1
- package/.medusa/server/src/modules/paypal/utils/merchant-status.d.ts +50 -0
- package/.medusa/server/src/modules/paypal/utils/merchant-status.d.ts.map +1 -0
- package/.medusa/server/src/modules/paypal/utils/merchant-status.js +106 -0
- package/.medusa/server/src/modules/paypal/utils/merchant-status.js.map +1 -0
- package/.medusa/server/src/modules/paypal/utils/order-context.d.ts +61 -0
- package/.medusa/server/src/modules/paypal/utils/order-context.d.ts.map +1 -0
- package/.medusa/server/src/modules/paypal/utils/order-context.js +127 -0
- package/.medusa/server/src/modules/paypal/utils/order-context.js.map +1 -0
- package/CHANGELOG.md +78 -0
- package/README.md +108 -61
- package/package.json +2 -2
- package/src/admin/routes/settings/paypal/_components/MerchantStatusNotices.tsx +116 -0
- package/src/admin/routes/settings/paypal/additional-settings/page.tsx +32 -6
- package/src/admin/routes/settings/paypal/advanced-card-payments/page.tsx +2 -0
- package/src/admin/routes/settings/paypal/connection/page.tsx +7 -0
- package/src/api/admin/paypal/logs/route.ts +1 -4
- package/src/api/admin/paypal/merchant-status/route.ts +36 -0
- package/src/api/store/paypal/create-order/route.ts +176 -157
- package/src/modules/paypal/service.ts +222 -43
- package/src/modules/paypal/types/config.ts +35 -2
- package/src/modules/paypal/utils/merchant-status.ts +185 -0
- package/src/modules/paypal/utils/order-context.ts +163 -0
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import { createHash } from "crypto"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Pieces of the Create Order payload (`POST /v2/checkout/orders`) that are
|
|
5
|
+
* worth testing on their own: the PayPal landing page and the shipping
|
|
6
|
+
* address sent with `shipping_preference: SET_PROVIDED_ADDRESS`.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export type PayPalLandingPage = "LOGIN" | "GUEST_CHECKOUT"
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* `payment_source.paypal.experience_context.landing_page` for the admin
|
|
13
|
+
* "Landing Page" setting.
|
|
14
|
+
*
|
|
15
|
+
* `LOGIN` is the default PayPal's partner review asks for. A stored
|
|
16
|
+
* "no_preference" (the admin default before 1.3.9) is treated as `LOGIN` too.
|
|
17
|
+
* "billing" selects PayPal's guest (card) checkout page, which
|
|
18
|
+
* `experience_context` calls `GUEST_CHECKOUT` — `BILLING` was its name in the
|
|
19
|
+
* deprecated `application_context`.
|
|
20
|
+
*/
|
|
21
|
+
export function resolveLandingPage(setting: unknown): PayPalLandingPage {
|
|
22
|
+
const value = String(setting ?? "")
|
|
23
|
+
.trim()
|
|
24
|
+
.toLowerCase()
|
|
25
|
+
return value === "billing" || value === "guest_checkout"
|
|
26
|
+
? "GUEST_CHECKOUT"
|
|
27
|
+
: "LOGIN"
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export type CartShippingAddress = {
|
|
31
|
+
first_name?: string | null
|
|
32
|
+
last_name?: string | null
|
|
33
|
+
address_1?: string | null
|
|
34
|
+
address_2?: string | null
|
|
35
|
+
city?: string | null
|
|
36
|
+
province?: string | null
|
|
37
|
+
postal_code?: string | null
|
|
38
|
+
country_code?: string | null
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export type PayPalShipping = {
|
|
42
|
+
name?: { full_name: string }
|
|
43
|
+
address: {
|
|
44
|
+
address_line_1: string
|
|
45
|
+
address_line_2?: string
|
|
46
|
+
admin_area_2?: string
|
|
47
|
+
admin_area_1?: string
|
|
48
|
+
postal_code: string
|
|
49
|
+
country_code: string
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function text(value: unknown): string {
|
|
54
|
+
return String(value ?? "").trim()
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Medusa stores a province either as the buyer typed it or as an ISO 3166-2
|
|
59
|
+
* code ("us-ca"). PayPal wants the subdivision without the country prefix
|
|
60
|
+
* ("CA").
|
|
61
|
+
*/
|
|
62
|
+
function normalizeProvince(province: string, country: string): string {
|
|
63
|
+
const match = province.match(/^([a-z]{2})-([a-z0-9]{1,3})$/i)
|
|
64
|
+
if (match && match[1].toUpperCase() === country) {
|
|
65
|
+
return match[2].toUpperCase()
|
|
66
|
+
}
|
|
67
|
+
return province
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The cart's shipping address as a PayPal `purchase_units[].shipping` object,
|
|
72
|
+
* or null when the cart has no address PayPal would accept (it rejects an
|
|
73
|
+
* address without a street line, postal code or two-letter country).
|
|
74
|
+
*/
|
|
75
|
+
export function buildPayPalShipping(
|
|
76
|
+
addr: CartShippingAddress | null | undefined
|
|
77
|
+
): PayPalShipping | null {
|
|
78
|
+
if (!addr) return null
|
|
79
|
+
const line1 = text(addr.address_1)
|
|
80
|
+
const postal = text(addr.postal_code)
|
|
81
|
+
const country = text(addr.country_code).toUpperCase()
|
|
82
|
+
if (!line1 || !postal || country.length !== 2) return null
|
|
83
|
+
|
|
84
|
+
const fullName = [addr.first_name, addr.last_name]
|
|
85
|
+
.map(text)
|
|
86
|
+
.filter(Boolean)
|
|
87
|
+
.join(" ")
|
|
88
|
+
const line2 = text(addr.address_2)
|
|
89
|
+
const city = text(addr.city)
|
|
90
|
+
const province = normalizeProvince(text(addr.province), country)
|
|
91
|
+
|
|
92
|
+
return {
|
|
93
|
+
...(fullName ? { name: { full_name: fullName.slice(0, 300) } } : {}),
|
|
94
|
+
address: {
|
|
95
|
+
address_line_1: line1.slice(0, 300),
|
|
96
|
+
...(line2 ? { address_line_2: line2.slice(0, 300) } : {}),
|
|
97
|
+
...(city ? { admin_area_2: city.slice(0, 120) } : {}),
|
|
98
|
+
...(province ? { admin_area_1: province.slice(0, 300) } : {}),
|
|
99
|
+
postal_code: postal.slice(0, 60),
|
|
100
|
+
country_code: country,
|
|
101
|
+
},
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function comparable(shipping: unknown): string {
|
|
106
|
+
const s = (shipping && typeof shipping === "object" ? shipping : {}) as any
|
|
107
|
+
const a = (s.address && typeof s.address === "object" ? s.address : {}) as any
|
|
108
|
+
return [
|
|
109
|
+
s.name?.full_name,
|
|
110
|
+
a.address_line_1,
|
|
111
|
+
a.address_line_2,
|
|
112
|
+
a.admin_area_2,
|
|
113
|
+
a.admin_area_1,
|
|
114
|
+
a.postal_code,
|
|
115
|
+
a.country_code,
|
|
116
|
+
]
|
|
117
|
+
.map((part) => text(part).toLowerCase().replace(/\s+/g, " "))
|
|
118
|
+
.join("|")
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** True when two PayPal shipping objects describe the same recipient/address. */
|
|
122
|
+
export function sameShipping(a: unknown, b: unknown): boolean {
|
|
123
|
+
return comparable(a) === comparable(b)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Short stable fingerprint of a shipping object, for the PayPal-Request-Id:
|
|
128
|
+
* PayPal replays the stored response for a repeated request id, so an order
|
|
129
|
+
* for the same cart and amount but a different address needs a different id.
|
|
130
|
+
*/
|
|
131
|
+
export function shippingFingerprint(shipping: unknown): string {
|
|
132
|
+
return createHash("sha256")
|
|
133
|
+
.update(comparable(shipping))
|
|
134
|
+
.digest("hex")
|
|
135
|
+
.slice(0, 8)
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Whether PayPal rejected a Create Order request because of the shipping
|
|
140
|
+
* address we sent (so the order can be retried without it instead of failing
|
|
141
|
+
* the checkout).
|
|
142
|
+
*/
|
|
143
|
+
export function isShippingAddressRejection(
|
|
144
|
+
status: number,
|
|
145
|
+
responseText: string
|
|
146
|
+
): boolean {
|
|
147
|
+
if (status !== 400 && status !== 422) return false
|
|
148
|
+
let json: any
|
|
149
|
+
try {
|
|
150
|
+
json = JSON.parse(responseText)
|
|
151
|
+
} catch {
|
|
152
|
+
return false
|
|
153
|
+
}
|
|
154
|
+
const details = Array.isArray(json?.details) ? json.details : []
|
|
155
|
+
return details.some((detail: any) => {
|
|
156
|
+
const field = String(detail?.field || "")
|
|
157
|
+
const issue = String(detail?.issue || "")
|
|
158
|
+
return (
|
|
159
|
+
/\/shipping(\/|$)/i.test(field) ||
|
|
160
|
+
/SHIPPING_ADDRESS|MISSING_SHIPPING/i.test(issue)
|
|
161
|
+
)
|
|
162
|
+
})
|
|
163
|
+
}
|