Skip to content

chore: refactor dc events payload#243

Open
jakubjasinsky wants to merge 4 commits intomasterfrom
refactor-dc-events
Open

chore: refactor dc events payload#243
jakubjasinsky wants to merge 4 commits intomasterfrom
refactor-dc-events

Conversation

@jakubjasinsky
Copy link
Collaborator

Summary

Normalize all Dynamic Checkout event payloads to consistently include invoice_id, use snake_case keys, and flatten nested data structures. This ensures consumers receive a uniform event shape regardless of the event type, and adds missing events (tokenize_payment_success) to payment methods that were not dispatching them.

Changes

Event payload normalization (events.ts)

  • All dispatch methods now require invoiceId as a parameter and include invoice_id in every event payload
  • Renamed payload keys from camelCase to snake_case (invoiceIdinvoice_id, returnUrlreturn_url)
  • Flattened { details: { ... } } wrapping in dispatchPaymentSubmittedEvent, dispatchPaymentCancelledEvent, and dispatchPaymentPendingEvent — data is now passed directly as the event detail
  • Added reason field to payment error and pending event payloads
  • Added errorData normalization in dispatchPaymentErrorEvent to handle non-object errors (e.g., string) safely before spreading

Tokenize success event coverage

  • Added dispatchTokenizePaymentSuccessEvent to card.ts, apm.ts (both regular and save-for-future flows), and saved-apm.ts
  • Previously only apple-pay.ts and google-pay.ts dispatched this event

Pending callback signature update (processout.ts)

  • Changed makeCardPayment pending callback from (data: any) => void to (resourceId: string, reason: string | null) => void
  • Pending callback now passes data?.message as the reason

Native APM improvements (native-apm.ts)

  • Added onPaymentSubmit callback, invoked on PAYMENT_INIT, so Dynamic Checkout can dispatch payment_submitted for native APM methods

Apple Pay / Google Pay event fixes

  • Fixed dispatchApplePaySessionError which previously created the event but never included the error in the payload
  • Wrapped static method references with arrow functions to preserve this context for invoiceId

Example HTML cleanup

  • Removed native APM event listeners (no longer needed in the example)
  • Updated event names, added missing event listeners, standardized console log format

Impact

  • Breaking change for event consumers: All event payloads now use snake_case keys (invoice_id instead of invoiceId). The payment_submitted, payment_cancelled, and payment_pending events no longer nest data under e.detail.details — fields are now directly on e.detail.
  • Breaking change for makeCardPayment pending callback: Signature changed from (data) => void to (resourceId, reason) => void.

Testing Plan

  • Mount Dynamic Checkout and listen for all events — verify every event includes invoice_id in its payload
  • Complete a card payment — verify tokenize_payment_success fires with { token, invoice_id }
  • Complete an APM redirect payment — verify tokenize_payment_success fires after redirect
  • Complete an APM payment with save-for-future — verify tokenize_payment_success fires
  • Complete a saved APM redirect payment — verify tokenize_payment_success fires
  • Trigger a payment error — verify payment_error event includes invoice_id and reason
  • Trigger a pending payment — verify payment_pending event includes invoice_id, payment_method_name, and reason directly on e.detail (not nested under details)
  • Complete Apple Pay / Google Pay flows — verify events include invoice_id
  • Test native APM payment — verify payment_submitted event fires on init
  • Verify no e.detail.details nesting exists on any event

Additional Context

The previous event system had inconsistent shapes: some events included invoice_id, some didn't; some used camelCase, some snake_case; some wrapped data in { details }, others didn't. This made it difficult for consumers to rely on a consistent contract. This PR establishes a uniform pattern: every event includes invoice_id, all keys are snake_case, and no unnecessary nesting.

@jakubjasinsky jakubjasinsky requested review from a team and tomasz-pazdziurek-cko and removed request for a team March 4, 2026 08:24
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.

2 participants