Skip to content

fix: prefill APM fields by canonical key and parse E.164 phone numbers - #277

Draft
roshan-gorasia-cko wants to merge 1 commit into
masterfrom
fix/apm-initial-data-prefill
Draft

fix: prefill APM fields by canonical key and parse E.164 phone numbers#277
roshan-gorasia-cko wants to merge 1 commit into
masterfrom
fix/apm-initial-data-prefill

Conversation

@roshan-gorasia-cko

@roshan-gorasia-cko roshan-gorasia-cko commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Problem

Two issues with initialData prefilling on the APM form.

1. Prefill only matched the payment method's own parameter key. NextSteps looked up initialData[param.key], so the documented initialData.phone_number silently no-op'd whenever a payment method named the field something else (for example customerPhone). In practice a merchant had to discover each payment method's internal parameter names to prefill anything.

2. An E.164 phone string wasn't parsed. A phone_number passed as "+48123123123" went into the national-number input whole, with the dialing code set to the first entry in the available list. That selected the wrong country, left +CC inside the number box, and submitted a malformed value. The +-prefix splitting logic only existed in the phone field's paste/typing handler, so it never ran for seeded state.

Changes

  • resolvePrefilledValue (src/apm/utils.ts) resolves a parameter's prefill value: an exact parameter-key match wins, otherwise the canonical key for that parameter's type (email → type email, phone_number → type phone). Canonical keys now work on every payment method, and a specific parameter can still be targeted directly or used to override a canonical value.
  • normalizePhoneValue (src/apm/utils.ts) coerces a prefilled phone into the { dialing_code, value } shape the field renders. An E.164 string is split on a longest-prefix match against the available dialing codes (so +1242 beats +1), separators are ignored, and the browser-locale default is used when the country isn't offered. The object form is still accepted, keyed on dialing_code, including the number key the field emits on input.
  • InitialData (src/apm/types.ts) types phone_number as string | { dialing_code?, value? } and allows payment-method-specific keys.

Notes

The prefilling docs present email / phone_number as universal keys and document the phone value as a bare E.164 string. Both are now true of the SDK; the docs should be extended to cover the object form and to note that a specific parameter key can also be passed and takes precedence.

Tests

test/apm/prefill.test.ts — 15 cases covering key/type resolution precedence and E.164 splitting. yarn test, yarn build and yarn lint all pass.

@roshan-gorasia-cko
roshan-gorasia-cko force-pushed the fix/apm-initial-data-prefill branch 2 times, most recently from 282832c to 3cc9902 Compare August 6, 2026 10:47
Prefilling an APM form via `initialData` had two problems.

Values were matched only against the payment method's own parameter key, so
the documented `initialData.phone_number` silently did nothing whenever a
payment method named the field something else (for example `customerPhone`),
leaving merchants to discover each one's internal parameter names. `email`
and `phone_number` now also match on the parameter's type, so the canonical
keys prefill on every payment method. An exact parameter-key match still
wins, so a specific field can be targeted or a canonical value overridden.

A phone number passed as an E.164 string was dropped into the national
number input whole, with the dialing code set to the first entry in the
available list. That selected the wrong country, left `+CC` inside the
number box and submitted a malformed value. The string is now split on a
longest-prefix match against the available dialing codes, falling back to
the browser-locale default when the country isn't offered.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@roshan-gorasia-cko
roshan-gorasia-cko force-pushed the fix/apm-initial-data-prefill branch from 3cc9902 to 89b6b96 Compare August 6, 2026 10:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant