@easypayment/medusa-paypal-ui 1.0.50 → 1.0.51

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.
@@ -1,6 +1,6 @@
1
1
  "use client"
2
2
  import React, { useMemo, useState } from "react"
3
- import { PayPalButtons } from "@paypal/react-paypal-js"
3
+ import { PayPalButtons, usePayPalScriptReducer } from "@paypal/react-paypal-js"
4
4
  import { createPayPalStoreApi, markPaymentComplete } from "../client/paypal"
5
5
  import type { PayPalConfig } from "../client/types"
6
6
 
@@ -29,6 +29,7 @@ export function PayPalSmartButtons(props: {
29
29
 
30
30
  const [error, setError] = useState<string | null>(null)
31
31
  const [processing, setProcessing] = useState(false)
32
+ const [{ isPending, isResolved }] = usePayPalScriptReducer()
32
33
 
33
34
  if (!config.currency_supported) return null
34
35
 
@@ -75,7 +76,38 @@ export function PayPalSmartButtons(props: {
75
76
  </div>
76
77
  )}
77
78
 
78
- {config.environment === "sandbox" && (
79
+ {isPending && (
80
+ <div
81
+ style={{
82
+ display: "flex",
83
+ alignItems: "center",
84
+ justifyContent: "center",
85
+ gap: 10,
86
+ padding: "18px 16px",
87
+ background: "#f9fafb",
88
+ border: "1px solid #e5e7eb",
89
+ borderRadius: 8,
90
+ minHeight: 55,
91
+ }}
92
+ >
93
+ <div
94
+ style={{
95
+ width: 22,
96
+ height: 22,
97
+ borderRadius: "50%",
98
+ border: "2.5px solid #e5e7eb",
99
+ borderTopColor: "#0070ba",
100
+ animation: "_pp_spin .7s linear infinite",
101
+ flexShrink: 0,
102
+ }}
103
+ />
104
+ <div style={{ fontSize: 13, fontWeight: 500, color: "#6b7280" }}>
105
+ Loading PayPal…
106
+ </div>
107
+ </div>
108
+ )}
109
+
110
+ {config.environment === "sandbox" && isResolved && (
79
111
  <div
80
112
  style={{
81
113
  display: "flex",
@@ -111,47 +143,50 @@ export function PayPalSmartButtons(props: {
111
143
  </div>
112
144
  )}
113
145
 
114
- <PayPalButtons
115
- style={{
116
- layout: "vertical",
117
- color: config.button_color,
118
- shape: config.button_shape,
119
- label: config.button_label,
120
- height: config.button_height,
121
- }}
122
- createOrder={async () => {
123
- setError(null)
124
- const r = await api.createOrder(cartId)
125
- return r.id
126
- }}
127
- onApprove={async (data: { orderID?: string }) => {
128
- try {
129
- setProcessing(true)
146
+ {isResolved && (
147
+ <PayPalButtons
148
+ forceReRender={[config.currency, config.intent, cartId]}
149
+ style={{
150
+ layout: "vertical",
151
+ color: config.button_color,
152
+ shape: config.button_shape,
153
+ label: config.button_label,
154
+ height: config.button_height,
155
+ }}
156
+ createOrder={async () => {
130
157
  setError(null)
131
- const orderId = String(data?.orderID || "")
132
- const result = await api.captureOrder(cartId, orderId)
158
+ const r = await api.createOrder(cartId)
159
+ return r.id
160
+ }}
161
+ onApprove={async (data: { orderID?: string }) => {
162
+ try {
163
+ setProcessing(true)
164
+ setError(null)
165
+ const orderId = String(data?.orderID || "")
166
+ const result = await api.captureOrder(cartId, orderId)
133
167
 
134
- const completeResult = await markPaymentComplete(baseUrl, cartId, publishableApiKey)
168
+ const completeResult = await markPaymentComplete(baseUrl, cartId, publishableApiKey)
135
169
 
136
- onPaid?.({ ...result, ...completeResult })
137
- } catch (e: unknown) {
138
- const msg = e instanceof Error ? e.message : "Payment capture failed"
170
+ onPaid?.({ ...result, ...completeResult })
171
+ } catch (e: unknown) {
172
+ const msg = e instanceof Error ? e.message : "Payment capture failed"
173
+ setError(msg)
174
+ onError?.(msg)
175
+ } finally {
176
+ setProcessing(false)
177
+ }
178
+ }}
179
+ onCancel={() => {
180
+ setProcessing(false)
181
+ }}
182
+ onError={(err: Error | { message?: string }) => {
183
+ setProcessing(false)
184
+ const msg = err instanceof Error ? err.message : err?.message || "PayPal error"
139
185
  setError(msg)
140
186
  onError?.(msg)
141
- } finally {
142
- setProcessing(false)
143
- }
144
- }}
145
- onCancel={() => {
146
- setProcessing(false)
147
- }}
148
- onError={(err: Error | { message?: string }) => {
149
- setProcessing(false)
150
- const msg = err instanceof Error ? err.message : err?.message || "PayPal error"
151
- setError(msg)
152
- onError?.(msg)
153
- }}
154
- />
187
+ }}
188
+ />
189
+ )}
155
190
 
156
191
  {error ? (
157
192
  <div