@blocklet/payment-react 1.22.0 → 1.22.2

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.
@@ -83,11 +83,24 @@ const setUserFormValues = (userInfo, currentValues, setValue, options = {}) => {
83
83
  addressFields["billing_address.country"] = userInfo.metadata?.phone?.country || userInfo.address?.country;
84
84
  }
85
85
  const allFields = { ...addressFields, ...basicFields };
86
+ const updatedFields = {
87
+ ...currentValues,
88
+ billing_address: {
89
+ ...currentValues.billing_address
90
+ }
91
+ };
86
92
  Object.entries(allFields).forEach(([field, value]) => {
87
93
  if (!preferExisting || !currentValues[field.split(".")[0]]) {
88
94
  setValue(field, value, { shouldValidate });
95
+ if (field.startsWith("billing_address.")) {
96
+ const addressKey = field.replace("billing_address.", "");
97
+ updatedFields.billing_address[addressKey] = value;
98
+ } else {
99
+ updatedFields[field] = value;
100
+ }
89
101
  }
90
102
  });
103
+ return updatedFields;
91
104
  };
92
105
  export default function PaymentForm({
93
106
  checkoutSession,
@@ -196,10 +209,12 @@ export default function PaymentForm({
196
209
  userInfo = mergeUserInfo(customer || {}, userInfo);
197
210
  console.error(err);
198
211
  }
199
- setUserFormValues(userInfo, values, setValue, {
212
+ const formValues = setUserFormValues(userInfo, values, setValue, {
200
213
  preferExisting: false,
201
214
  showPhone: checkoutSession.phone_number_collection?.enabled
202
215
  });
216
+ const isValid = validateUserInfo(formValues);
217
+ setState({ showEditForm: !isValid });
203
218
  } else {
204
219
  setUserFormValues(
205
220
  {
@@ -246,97 +261,95 @@ export default function PaymentForm({
246
261
  const paymentCurrency = currencies.find((x) => x.id === paymentCurrencyId);
247
262
  const showStake = method.type === "arcblock" && !checkoutSession.subscription_data?.no_stake;
248
263
  const isDonationMode = checkoutSession?.submit_type === "donate" && isDonation;
249
- const validateUserInfo = () => {
250
- if (!session?.user) {
264
+ const validateUserInfo = (values) => {
265
+ if (!values) {
251
266
  return false;
252
267
  }
253
- const { fullName, name, email, phone, address } = session.user;
254
268
  const fieldValidation = checkoutSession.metadata?.page_info?.field_validation;
255
- const hasName = !!(fullName || name);
256
- if (!hasName) {
269
+ const customerName2 = values.customer_name;
270
+ if (!customerName2 || customerName2.trim() === "") {
257
271
  return false;
258
272
  }
259
- const hasValidEmail = email && isEmail(email);
260
- if (!hasValidEmail) {
273
+ const customerEmail2 = values.customer_email;
274
+ if (!customerEmail2 || !isEmail(customerEmail2)) {
261
275
  return false;
262
276
  }
263
277
  const nameValidation = getFieldValidation("customer_name", fieldValidation, locale);
264
- if (nameValidation.pattern && fullName) {
278
+ if (nameValidation.pattern) {
265
279
  const pattern = nameValidation.pattern.value;
266
- if (!pattern.test(fullName)) {
280
+ if (!pattern.test(customerName2)) {
267
281
  return false;
268
282
  }
269
283
  }
270
284
  const emailValidation = getFieldValidation("customer_email", fieldValidation, locale);
271
- if (emailValidation.pattern && email) {
285
+ if (emailValidation.pattern) {
272
286
  const pattern = emailValidation.pattern.value;
273
- if (!pattern.test(email)) {
287
+ if (!pattern.test(customerEmail2)) {
274
288
  return false;
275
289
  }
276
290
  }
277
- if (!address || !address.postalCode || !validatePostalCode(address.postalCode, address.country)) {
291
+ const billingAddress2 = values.billing_address || {};
292
+ const { postal_code: postalCode, country, state: stateValue, line1, city } = billingAddress2;
293
+ if (!postalCode || !validatePostalCode(postalCode, country)) {
278
294
  return false;
279
295
  }
280
296
  const postalCodeValidation = getFieldValidation("billing_address.postal_code", fieldValidation, locale);
281
297
  if (postalCodeValidation.pattern) {
282
298
  const pattern = postalCodeValidation.pattern.value;
283
- if (!pattern.test(address.postalCode)) {
299
+ if (!pattern.test(postalCode)) {
284
300
  return false;
285
301
  }
286
302
  }
287
- if (!address.province) {
303
+ if (!stateValue) {
288
304
  return false;
289
305
  }
290
306
  const stateValidation = getFieldValidation("billing_address.state", fieldValidation, locale);
291
307
  if (stateValidation.pattern) {
292
308
  const pattern = stateValidation.pattern.value;
293
- if (!pattern.test(address.province)) {
309
+ if (!pattern.test(stateValue)) {
294
310
  return false;
295
311
  }
296
312
  }
297
313
  if (checkoutSession.phone_number_collection?.enabled) {
298
- if (!phone || phone.trim() === "") {
314
+ const customerPhone2 = values.customer_phone;
315
+ if (!customerPhone2 || customerPhone2.trim() === "") {
299
316
  return false;
300
317
  }
301
318
  const phoneValidation = getFieldValidation("customer_phone", fieldValidation, locale);
302
319
  if (phoneValidation.pattern) {
303
320
  const pattern = phoneValidation.pattern.value;
304
- if (!pattern.test(phone)) {
321
+ if (!pattern.test(customerPhone2)) {
305
322
  return false;
306
323
  }
307
324
  }
308
325
  }
309
326
  const addressMode = checkoutSession.billing_address_collection;
310
327
  if (addressMode === "required") {
311
- if (!address?.country || !address?.province || !address?.line1 || !address?.city || !address?.postalCode) {
328
+ if (!country || !stateValue || !line1 || !city || !postalCode) {
312
329
  return false;
313
330
  }
314
331
  const line1Validation = getFieldValidation("billing_address.line1", fieldValidation, locale);
315
332
  if (line1Validation.pattern) {
316
333
  const pattern = line1Validation.pattern.value;
317
- if (!pattern.test(address.line1)) {
334
+ if (!pattern.test(line1)) {
318
335
  return false;
319
336
  }
320
337
  }
321
338
  const cityValidation = getFieldValidation("billing_address.city", fieldValidation, locale);
322
339
  if (cityValidation.pattern) {
323
340
  const pattern = cityValidation.pattern.value;
324
- if (!pattern.test(address.city)) {
341
+ if (!pattern.test(city)) {
325
342
  return false;
326
343
  }
327
344
  }
328
345
  }
329
346
  return true;
330
347
  };
331
- const showForm = useMemo(() => {
332
- if (!session?.user) {
333
- return false;
334
- }
335
- if (state.showEditForm) {
336
- return true;
337
- }
338
- return !validateUserInfo();
339
- }, [session?.user, method, state.showEditForm]);
348
+ const customerName = useWatch({ control, name: "customer_name" });
349
+ const customerEmail = useWatch({ control, name: "customer_email" });
350
+ const customerPhone = useWatch({ control, name: "customer_phone" });
351
+ const billingAddress = useWatch({ control, name: "billing_address" });
352
+ const showForm = session?.user ? state.showEditForm : false;
340
353
  const handleConnected = async () => {
341
354
  if (processingRef.current) {
342
355
  return;
@@ -375,11 +388,13 @@ export default function PaymentForm({
375
388
  if (profile) {
376
389
  const values = getValues();
377
390
  const userInfo = mergeUserInfo(profile, session?.user);
378
- setUserFormValues(userInfo, values, setValue, {
391
+ const formValues = setUserFormValues(userInfo, values, setValue, {
379
392
  preferExisting: false,
380
393
  showPhone: checkoutSession.phone_number_collection?.enabled,
381
394
  shouldValidate: true
382
395
  });
396
+ const isValid = validateUserInfo(formValues);
397
+ setState({ showEditForm: !isValid });
383
398
  await trigger();
384
399
  }
385
400
  };
@@ -857,19 +872,14 @@ export default function PaymentForm({
857
872
  )
858
873
  ] }),
859
874
  /* @__PURE__ */ jsxs(Stack, { spacing: 0.5, children: [
860
- /* @__PURE__ */ jsx(Typography, { variant: "body2", sx: { color: "text.primary", fontWeight: 600, fontSize: "0.9375rem" }, children: session.user.fullName || session.user.name }),
861
- /* @__PURE__ */ jsx(Typography, { variant: "body2", sx: { color: "text.secondary", fontSize: "0.8125rem" }, children: session.user.email }),
862
- checkoutSession.phone_number_collection?.enabled && session.user.phone && /* @__PURE__ */ jsx(Typography, { variant: "body2", sx: { color: "text.secondary", fontSize: "0.8125rem" }, children: session.user.phone }),
863
- session.user.address && /* @__PURE__ */ jsxs(Stack, { direction: "row", alignItems: "center", spacing: 0.75, children: [
864
- session.user.address.country && /* @__PURE__ */ jsx(FlagEmoji, { iso2: session.user.address.country.toLowerCase(), style: { width: 18, height: 14 } }),
875
+ /* @__PURE__ */ jsx(Typography, { variant: "body2", sx: { color: "text.primary", fontWeight: 600, fontSize: "0.9375rem" }, children: customerName }),
876
+ /* @__PURE__ */ jsx(Typography, { variant: "body2", sx: { color: "text.secondary", fontSize: "0.8125rem" }, children: customerEmail }),
877
+ checkoutSession.phone_number_collection?.enabled && customerPhone && /* @__PURE__ */ jsx(Typography, { variant: "body2", sx: { color: "text.secondary", fontSize: "0.8125rem" }, children: customerPhone }),
878
+ billingAddress && /* @__PURE__ */ jsxs(Stack, { direction: "row", alignItems: "center", spacing: 0.75, children: [
879
+ billingAddress.country && /* @__PURE__ */ jsx(FlagEmoji, { iso2: billingAddress.country.toLowerCase(), style: { width: 18, height: 14 } }),
865
880
  /* @__PURE__ */ jsxs(Typography, { variant: "body2", sx: { color: "text.secondary", fontSize: "0.8125rem" }, children: [
866
- [
867
- session.user.address.line1,
868
- session.user.address.city,
869
- session.user.address.province,
870
- session.user.address.country?.toUpperCase()
871
- ].filter(Boolean).join(", "),
872
- session.user.address.postalCode && ` [ ${t("payment.checkout.billing.postal_code")}: ${session.user.address.postalCode} ]`
881
+ checkoutSession.billing_address_collection === "required" ? [billingAddress.line1, billingAddress.city, billingAddress.state].filter(Boolean).join(", ") : billingAddress.state || "",
882
+ billingAddress.postal_code && ` [ ${t("payment.checkout.billing.postal_code")}: ${billingAddress.postal_code} ]`
873
883
  ] })
874
884
  ] })
875
885
  ] })
@@ -92,13 +92,26 @@ const setUserFormValues = (userInfo, currentValues, setValue, options = {}) => {
92
92
  ...addressFields,
93
93
  ...basicFields
94
94
  };
95
+ const updatedFields = {
96
+ ...currentValues,
97
+ billing_address: {
98
+ ...currentValues.billing_address
99
+ }
100
+ };
95
101
  Object.entries(allFields).forEach(([field, value]) => {
96
102
  if (!preferExisting || !currentValues[field.split(".")[0]]) {
97
103
  setValue(field, value, {
98
104
  shouldValidate
99
105
  });
106
+ if (field.startsWith("billing_address.")) {
107
+ const addressKey = field.replace("billing_address.", "");
108
+ updatedFields.billing_address[addressKey] = value;
109
+ } else {
110
+ updatedFields[field] = value;
111
+ }
100
112
  }
101
113
  });
114
+ return updatedFields;
102
115
  };
103
116
  function PaymentForm({
104
117
  checkoutSession,
@@ -223,10 +236,14 @@ function PaymentForm({
223
236
  userInfo = mergeUserInfo(customer || {}, userInfo);
224
237
  console.error(err);
225
238
  }
226
- setUserFormValues(userInfo, values, setValue, {
239
+ const formValues = setUserFormValues(userInfo, values, setValue, {
227
240
  preferExisting: false,
228
241
  showPhone: checkoutSession.phone_number_collection?.enabled
229
242
  });
243
+ const isValid = validateUserInfo(formValues);
244
+ setState({
245
+ showEditForm: !isValid
246
+ });
230
247
  } else {
231
248
  setUserFormValues({
232
249
  name: "",
@@ -279,103 +296,113 @@ function PaymentForm({
279
296
  const paymentCurrency = currencies.find(x => x.id === paymentCurrencyId);
280
297
  const showStake = method.type === "arcblock" && !checkoutSession.subscription_data?.no_stake;
281
298
  const isDonationMode = checkoutSession?.submit_type === "donate" && isDonation;
282
- const validateUserInfo = () => {
283
- if (!session?.user) {
299
+ const validateUserInfo = values => {
300
+ if (!values) {
284
301
  return false;
285
302
  }
286
- const {
287
- fullName,
288
- name,
289
- email,
290
- phone,
291
- address
292
- } = session.user;
293
303
  const fieldValidation = checkoutSession.metadata?.page_info?.field_validation;
294
- const hasName = !!(fullName || name);
295
- if (!hasName) {
304
+ const customerName2 = values.customer_name;
305
+ if (!customerName2 || customerName2.trim() === "") {
296
306
  return false;
297
307
  }
298
- const hasValidEmail = email && (0, _isEmail.default)(email);
299
- if (!hasValidEmail) {
308
+ const customerEmail2 = values.customer_email;
309
+ if (!customerEmail2 || !(0, _isEmail.default)(customerEmail2)) {
300
310
  return false;
301
311
  }
302
312
  const nameValidation = (0, _validator.getFieldValidation)("customer_name", fieldValidation, locale);
303
- if (nameValidation.pattern && fullName) {
313
+ if (nameValidation.pattern) {
304
314
  const pattern = nameValidation.pattern.value;
305
- if (!pattern.test(fullName)) {
315
+ if (!pattern.test(customerName2)) {
306
316
  return false;
307
317
  }
308
318
  }
309
319
  const emailValidation = (0, _validator.getFieldValidation)("customer_email", fieldValidation, locale);
310
- if (emailValidation.pattern && email) {
320
+ if (emailValidation.pattern) {
311
321
  const pattern = emailValidation.pattern.value;
312
- if (!pattern.test(email)) {
322
+ if (!pattern.test(customerEmail2)) {
313
323
  return false;
314
324
  }
315
325
  }
316
- if (!address || !address.postalCode || !(0, _validator.validatePostalCode)(address.postalCode, address.country)) {
326
+ const billingAddress2 = values.billing_address || {};
327
+ const {
328
+ postal_code: postalCode,
329
+ country,
330
+ state: stateValue,
331
+ line1,
332
+ city
333
+ } = billingAddress2;
334
+ if (!postalCode || !(0, _validator.validatePostalCode)(postalCode, country)) {
317
335
  return false;
318
336
  }
319
337
  const postalCodeValidation = (0, _validator.getFieldValidation)("billing_address.postal_code", fieldValidation, locale);
320
338
  if (postalCodeValidation.pattern) {
321
339
  const pattern = postalCodeValidation.pattern.value;
322
- if (!pattern.test(address.postalCode)) {
340
+ if (!pattern.test(postalCode)) {
323
341
  return false;
324
342
  }
325
343
  }
326
- if (!address.province) {
344
+ if (!stateValue) {
327
345
  return false;
328
346
  }
329
347
  const stateValidation = (0, _validator.getFieldValidation)("billing_address.state", fieldValidation, locale);
330
348
  if (stateValidation.pattern) {
331
349
  const pattern = stateValidation.pattern.value;
332
- if (!pattern.test(address.province)) {
350
+ if (!pattern.test(stateValue)) {
333
351
  return false;
334
352
  }
335
353
  }
336
354
  if (checkoutSession.phone_number_collection?.enabled) {
337
- if (!phone || phone.trim() === "") {
355
+ const customerPhone2 = values.customer_phone;
356
+ if (!customerPhone2 || customerPhone2.trim() === "") {
338
357
  return false;
339
358
  }
340
359
  const phoneValidation = (0, _validator.getFieldValidation)("customer_phone", fieldValidation, locale);
341
360
  if (phoneValidation.pattern) {
342
361
  const pattern = phoneValidation.pattern.value;
343
- if (!pattern.test(phone)) {
362
+ if (!pattern.test(customerPhone2)) {
344
363
  return false;
345
364
  }
346
365
  }
347
366
  }
348
367
  const addressMode = checkoutSession.billing_address_collection;
349
368
  if (addressMode === "required") {
350
- if (!address?.country || !address?.province || !address?.line1 || !address?.city || !address?.postalCode) {
369
+ if (!country || !stateValue || !line1 || !city || !postalCode) {
351
370
  return false;
352
371
  }
353
372
  const line1Validation = (0, _validator.getFieldValidation)("billing_address.line1", fieldValidation, locale);
354
373
  if (line1Validation.pattern) {
355
374
  const pattern = line1Validation.pattern.value;
356
- if (!pattern.test(address.line1)) {
375
+ if (!pattern.test(line1)) {
357
376
  return false;
358
377
  }
359
378
  }
360
379
  const cityValidation = (0, _validator.getFieldValidation)("billing_address.city", fieldValidation, locale);
361
380
  if (cityValidation.pattern) {
362
381
  const pattern = cityValidation.pattern.value;
363
- if (!pattern.test(address.city)) {
382
+ if (!pattern.test(city)) {
364
383
  return false;
365
384
  }
366
385
  }
367
386
  }
368
387
  return true;
369
388
  };
370
- const showForm = (0, _react.useMemo)(() => {
371
- if (!session?.user) {
372
- return false;
373
- }
374
- if (state.showEditForm) {
375
- return true;
376
- }
377
- return !validateUserInfo();
378
- }, [session?.user, method, state.showEditForm]);
389
+ const customerName = (0, _reactHookForm.useWatch)({
390
+ control,
391
+ name: "customer_name"
392
+ });
393
+ const customerEmail = (0, _reactHookForm.useWatch)({
394
+ control,
395
+ name: "customer_email"
396
+ });
397
+ const customerPhone = (0, _reactHookForm.useWatch)({
398
+ control,
399
+ name: "customer_phone"
400
+ });
401
+ const billingAddress = (0, _reactHookForm.useWatch)({
402
+ control,
403
+ name: "billing_address"
404
+ });
405
+ const showForm = session?.user ? state.showEditForm : false;
379
406
  const handleConnected = async () => {
380
407
  if (processingRef.current) {
381
408
  return;
@@ -425,11 +452,15 @@ function PaymentForm({
425
452
  if (profile) {
426
453
  const values = getValues();
427
454
  const userInfo = mergeUserInfo(profile, session?.user);
428
- setUserFormValues(userInfo, values, setValue, {
455
+ const formValues = setUserFormValues(userInfo, values, setValue, {
429
456
  preferExisting: false,
430
457
  showPhone: checkoutSession.phone_number_collection?.enabled,
431
458
  shouldValidate: true
432
459
  });
460
+ const isValid = validateUserInfo(formValues);
461
+ setState({
462
+ showEditForm: !isValid
463
+ });
433
464
  await trigger();
434
465
  }
435
466
  };
@@ -947,27 +978,27 @@ function PaymentForm({
947
978
  fontWeight: 600,
948
979
  fontSize: "0.9375rem"
949
980
  },
950
- children: session.user.fullName || session.user.name
981
+ children: customerName
951
982
  }), /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Typography, {
952
983
  variant: "body2",
953
984
  sx: {
954
985
  color: "text.secondary",
955
986
  fontSize: "0.8125rem"
956
987
  },
957
- children: session.user.email
958
- }), checkoutSession.phone_number_collection?.enabled && session.user.phone && /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Typography, {
988
+ children: customerEmail
989
+ }), checkoutSession.phone_number_collection?.enabled && customerPhone && /* @__PURE__ */(0, _jsxRuntime.jsx)(_material.Typography, {
959
990
  variant: "body2",
960
991
  sx: {
961
992
  color: "text.secondary",
962
993
  fontSize: "0.8125rem"
963
994
  },
964
- children: session.user.phone
965
- }), session.user.address && /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Stack, {
995
+ children: customerPhone
996
+ }), billingAddress && /* @__PURE__ */(0, _jsxRuntime.jsxs)(_material.Stack, {
966
997
  direction: "row",
967
998
  alignItems: "center",
968
999
  spacing: 0.75,
969
- children: [session.user.address.country && /* @__PURE__ */(0, _jsxRuntime.jsx)(_reactInternationalPhone.FlagEmoji, {
970
- iso2: session.user.address.country.toLowerCase(),
1000
+ children: [billingAddress.country && /* @__PURE__ */(0, _jsxRuntime.jsx)(_reactInternationalPhone.FlagEmoji, {
1001
+ iso2: billingAddress.country.toLowerCase(),
971
1002
  style: {
972
1003
  width: 18,
973
1004
  height: 14
@@ -978,7 +1009,7 @@ function PaymentForm({
978
1009
  color: "text.secondary",
979
1010
  fontSize: "0.8125rem"
980
1011
  },
981
- children: [[session.user.address.line1, session.user.address.city, session.user.address.province, session.user.address.country?.toUpperCase()].filter(Boolean).join(", "), session.user.address.postalCode && ` [ ${t("payment.checkout.billing.postal_code")}: ${session.user.address.postalCode} ]`]
1012
+ children: [checkoutSession.billing_address_collection === "required" ? [billingAddress.line1, billingAddress.city, billingAddress.state].filter(Boolean).join(", ") : billingAddress.state || "", billingAddress.postal_code && ` [ ${t("payment.checkout.billing.postal_code")}: ${billingAddress.postal_code} ]`]
982
1013
  })]
983
1014
  })]
984
1015
  })]
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blocklet/payment-react",
3
- "version": "1.22.0",
3
+ "version": "1.22.2",
4
4
  "description": "Reusable react components for payment kit v2",
5
5
  "keywords": [
6
6
  "react",
@@ -54,11 +54,11 @@
54
54
  }
55
55
  },
56
56
  "dependencies": {
57
- "@arcblock/did-connect-react": "^3.1.52",
58
- "@arcblock/ux": "^3.1.52",
57
+ "@arcblock/did-connect-react": "^3.1.53",
58
+ "@arcblock/ux": "^3.1.53",
59
59
  "@arcblock/ws": "^1.26.3",
60
- "@blocklet/theme": "^3.1.52",
61
- "@blocklet/ui-react": "^3.1.52",
60
+ "@blocklet/theme": "^3.1.53",
61
+ "@blocklet/ui-react": "^3.1.53",
62
62
  "@mui/icons-material": "^7.1.2",
63
63
  "@mui/lab": "7.0.0-beta.14",
64
64
  "@mui/material": "^7.1.2",
@@ -94,7 +94,7 @@
94
94
  "@babel/core": "^7.27.4",
95
95
  "@babel/preset-env": "^7.27.2",
96
96
  "@babel/preset-react": "^7.27.1",
97
- "@blocklet/payment-types": "1.22.0",
97
+ "@blocklet/payment-types": "1.22.2",
98
98
  "@storybook/addon-essentials": "^7.6.20",
99
99
  "@storybook/addon-interactions": "^7.6.20",
100
100
  "@storybook/addon-links": "^7.6.20",
@@ -125,5 +125,5 @@
125
125
  "vite-plugin-babel": "^1.3.1",
126
126
  "vite-plugin-node-polyfills": "^0.23.0"
127
127
  },
128
- "gitHead": "d69619c7d669bc6de8620273ff0f3f7b5d56383f"
128
+ "gitHead": "13ea906cf090d7069a970d97d9a3f06426b27092"
129
129
  }
@@ -155,11 +155,27 @@ const setUserFormValues = (
155
155
 
156
156
  const allFields = { ...addressFields, ...basicFields };
157
157
 
158
+ const updatedFields: Record<string, any> = {
159
+ ...currentValues,
160
+ billing_address: {
161
+ ...currentValues.billing_address,
162
+ },
163
+ };
164
+
158
165
  Object.entries(allFields).forEach(([field, value]) => {
159
166
  if (!preferExisting || !currentValues[field.split('.')[0]]) {
160
167
  setValue(field, value, { shouldValidate });
168
+
169
+ if (field.startsWith('billing_address.')) {
170
+ const addressKey = field.replace('billing_address.', '');
171
+ updatedFields.billing_address[addressKey] = value;
172
+ } else {
173
+ updatedFields[field] = value;
174
+ }
161
175
  }
162
176
  });
177
+
178
+ return updatedFields;
163
179
  };
164
180
 
165
181
  // FIXME: https://stripe.com/docs/elements/address-element
@@ -305,10 +321,13 @@ export default function PaymentForm({
305
321
  userInfo = mergeUserInfo(customer || {}, userInfo);
306
322
  console.error(err);
307
323
  }
308
- setUserFormValues(userInfo, values, setValue, {
324
+ const formValues = setUserFormValues(userInfo, values, setValue, {
309
325
  preferExisting: false,
310
326
  showPhone: checkoutSession.phone_number_collection?.enabled,
311
327
  });
328
+
329
+ const isValid = validateUserInfo(formValues);
330
+ setState({ showEditForm: !isValid });
312
331
  } else {
313
332
  setUserFormValues(
314
333
  {
@@ -372,71 +391,73 @@ export default function PaymentForm({
372
391
 
373
392
  const isDonationMode = checkoutSession?.submit_type === 'donate' && isDonation;
374
393
 
375
- const validateUserInfo = () => {
376
- if (!session?.user) {
394
+ const validateUserInfo = (values: any) => {
395
+ if (!values) {
377
396
  return false;
378
397
  }
379
398
 
380
- const { fullName, name, email, phone, address } = session.user;
381
399
  const fieldValidation = checkoutSession.metadata?.page_info?.field_validation;
382
400
 
383
- const hasName = !!(fullName || name);
384
- if (!hasName) {
401
+ const customerName = values.customer_name;
402
+ if (!customerName || customerName.trim() === '') {
385
403
  return false;
386
404
  }
387
405
 
388
- const hasValidEmail = email && isEmail(email);
389
- if (!hasValidEmail) {
406
+ const customerEmail = values.customer_email;
407
+ if (!customerEmail || !isEmail(customerEmail)) {
390
408
  return false;
391
409
  }
392
410
 
393
411
  const nameValidation = getFieldValidation('customer_name', fieldValidation, locale);
394
- if (nameValidation.pattern && fullName) {
412
+ if (nameValidation.pattern) {
395
413
  const pattern = nameValidation.pattern.value;
396
- if (!pattern.test(fullName)) {
414
+ if (!pattern.test(customerName)) {
397
415
  return false;
398
416
  }
399
417
  }
400
418
 
401
419
  const emailValidation = getFieldValidation('customer_email', fieldValidation, locale);
402
- if (emailValidation.pattern && email) {
420
+ if (emailValidation.pattern) {
403
421
  const pattern = emailValidation.pattern.value;
404
- if (!pattern.test(email)) {
422
+ if (!pattern.test(customerEmail)) {
405
423
  return false;
406
424
  }
407
425
  }
408
426
 
409
- if (!address || !address.postalCode || !validatePostalCode(address.postalCode, address.country)) {
427
+ const billingAddress = values.billing_address || {};
428
+ const { postal_code: postalCode, country, state: stateValue, line1, city } = billingAddress;
429
+ if (!postalCode || !validatePostalCode(postalCode, country)) {
410
430
  return false;
411
431
  }
412
432
 
413
433
  const postalCodeValidation = getFieldValidation('billing_address.postal_code', fieldValidation, locale);
414
434
  if (postalCodeValidation.pattern) {
415
435
  const pattern = postalCodeValidation.pattern.value;
416
- if (!pattern.test(address.postalCode)) {
436
+ if (!pattern.test(postalCode)) {
417
437
  return false;
418
438
  }
419
439
  }
420
440
 
421
- if (!address.province) {
441
+ if (!stateValue) {
422
442
  return false;
423
443
  }
424
444
  const stateValidation = getFieldValidation('billing_address.state', fieldValidation, locale);
425
445
  if (stateValidation.pattern) {
426
446
  const pattern = stateValidation.pattern.value;
427
- if (!pattern.test(address.province)) {
447
+ if (!pattern.test(stateValue)) {
428
448
  return false;
429
449
  }
430
450
  }
431
451
 
432
452
  if (checkoutSession.phone_number_collection?.enabled) {
433
- if (!phone || phone.trim() === '') {
453
+ const customerPhone = values.customer_phone;
454
+ if (!customerPhone || customerPhone.trim() === '') {
434
455
  return false;
435
456
  }
436
457
  const phoneValidation = getFieldValidation('customer_phone', fieldValidation, locale);
437
458
  if (phoneValidation.pattern) {
438
459
  const pattern = phoneValidation.pattern.value;
439
- if (!pattern.test(phone)) {
460
+ if (!pattern.test(customerPhone)) {
440
461
  return false;
441
462
  }
442
463
  }
@@ -444,14 +465,14 @@ export default function PaymentForm({
444
465
 
445
466
  const addressMode = checkoutSession.billing_address_collection;
446
467
  if (addressMode === 'required') {
447
- if (!address?.country || !address?.province || !address?.line1 || !address?.city || !address?.postalCode) {
468
+ if (!country || !stateValue || !line1 || !city || !postalCode) {
448
469
  return false;
449
470
  }
450
471
 
451
472
  const line1Validation = getFieldValidation('billing_address.line1', fieldValidation, locale);
452
473
  if (line1Validation.pattern) {
453
474
  const pattern = line1Validation.pattern.value;
454
- if (!pattern.test(address.line1)) {
475
+ if (!pattern.test(line1)) {
455
476
  return false;
456
477
  }
457
478
  }
@@ -459,7 +480,7 @@ export default function PaymentForm({
459
480
  const cityValidation = getFieldValidation('billing_address.city', fieldValidation, locale);
460
481
  if (cityValidation.pattern) {
461
482
  const pattern = cityValidation.pattern.value;
462
- if (!pattern.test(address.city)) {
483
+ if (!pattern.test(city)) {
463
484
  return false;
464
485
  }
465
486
  }
@@ -468,16 +489,12 @@ export default function PaymentForm({
468
489
  return true;
469
490
  };
470
491
 
471
- const showForm = useMemo(() => {
472
- if (!session?.user) {
473
- return false;
474
- }
475
- if (state.showEditForm) {
476
- return true;
477
- }
478
- return !validateUserInfo();
479
- // eslint-disable-next-line react-hooks/exhaustive-deps
480
- }, [session?.user, method, state.showEditForm]);
492
+ const customerName = useWatch({ control, name: 'customer_name' });
493
+ const customerEmail = useWatch({ control, name: 'customer_email' });
494
+ const customerPhone = useWatch({ control, name: 'customer_phone' });
495
+ const billingAddress = useWatch({ control, name: 'billing_address' });
496
+
497
+ const showForm = session?.user ? state.showEditForm : false;
481
498
 
482
499
  const handleConnected = async () => {
483
500
  if (processingRef.current) {
@@ -520,11 +537,14 @@ export default function PaymentForm({
520
537
  if (profile) {
521
538
  const values = getValues();
522
539
  const userInfo = mergeUserInfo(profile, session?.user);
523
- setUserFormValues(userInfo, values, setValue, {
540
+ const formValues = setUserFormValues(userInfo, values, setValue, {
524
541
  preferExisting: false,
525
542
  showPhone: checkoutSession.phone_number_collection?.enabled,
526
543
  shouldValidate: true,
527
544
  });
545
+
546
+ const isValid = validateUserInfo(formValues);
547
+ setState({ showEditForm: !isValid });
528
548
  await trigger();
529
549
  }
530
550
  };
@@ -1027,32 +1047,27 @@ export default function PaymentForm({
1027
1047
  </Stack>
1028
1048
  <Stack spacing={0.5}>
1029
1049
  <Typography variant="body2" sx={{ color: 'text.primary', fontWeight: 600, fontSize: '0.9375rem' }}>
1030
- {session.user.fullName || session.user.name}
1050
+ {customerName}
1031
1051
  </Typography>
1032
1052
  <Typography variant="body2" sx={{ color: 'text.secondary', fontSize: '0.8125rem' }}>
1033
- {session.user.email}
1053
+ {customerEmail}
1034
1054
  </Typography>
1035
- {checkoutSession.phone_number_collection?.enabled && session.user.phone && (
1055
+ {checkoutSession.phone_number_collection?.enabled && customerPhone && (
1036
1056
  <Typography variant="body2" sx={{ color: 'text.secondary', fontSize: '0.8125rem' }}>
1037
- {session.user.phone}
1057
+ {customerPhone}
1038
1058
  </Typography>
1039
1059
  )}
1040
- {session.user.address && (
1060
+ {billingAddress && (
1041
1061
  <Stack direction="row" alignItems="center" spacing={0.75}>
1042
- {session.user.address.country && (
1043
- <FlagEmoji iso2={session.user.address.country.toLowerCase()} style={{ width: 18, height: 14 }} />
1062
+ {billingAddress.country && (
1063
+ <FlagEmoji iso2={billingAddress.country.toLowerCase()} style={{ width: 18, height: 14 }} />
1044
1064
  )}
1045
1065
  <Typography variant="body2" sx={{ color: 'text.secondary', fontSize: '0.8125rem' }}>
1046
- {[
1047
- session.user.address.line1,
1048
- session.user.address.city,
1049
- session.user.address.province,
1050
- session.user.address.country?.toUpperCase(),
1051
- ]
1052
- .filter(Boolean)
1053
- .join(', ')}
1054
- {session.user.address.postalCode &&
1055
- ` [ ${t('payment.checkout.billing.postal_code')}: ${session.user.address.postalCode} ]`}
1066
+ {checkoutSession.billing_address_collection === 'required'
1067
+ ? [billingAddress.line1, billingAddress.city, billingAddress.state].filter(Boolean).join(', ')
1068
+ : billingAddress.state || ''}
1069
+ {billingAddress.postal_code &&
1070
+ ` [ ${t('payment.checkout.billing.postal_code')}: ${billingAddress.postal_code} ]`}
1056
1071
  </Typography>
1057
1072
  </Stack>
1058
1073
  )}