@economist/web-apple-pay 0.1.1-beta.0

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.
Files changed (55) hide show
  1. package/README.md +186 -0
  2. package/dist/index.d.ts +1 -0
  3. package/dist/index.js +1642 -0
  4. package/dist/src/apple-pay-button.d.ts +45 -0
  5. package/dist/src/apple-pay-button.js +1621 -0
  6. package/dist/src/apple-pay-button.stories.js +1934 -0
  7. package/dist/src/apple-pay-logo.d.ts +1 -0
  8. package/dist/src/apple-pay-logo.js +14 -0
  9. package/dist/src/apple-pay-modal.d.ts +12 -0
  10. package/dist/src/apple-pay-modal.js +442 -0
  11. package/dist/src/apple-pay-modal.stories.js +1778 -0
  12. package/dist/src/clients/payment-checkout/apple-session.d.ts +18 -0
  13. package/dist/src/clients/payment-checkout/apple-session.js +243 -0
  14. package/dist/src/clients/payment-checkout/billing-contact-details.d.ts +21 -0
  15. package/dist/src/clients/payment-checkout/billing-contact-details.js +109 -0
  16. package/dist/src/clients/payment-checkout/config.d.ts +9 -0
  17. package/dist/src/clients/payment-checkout/config.js +23 -0
  18. package/dist/src/clients/payment-checkout/contact-validation.d.ts +21 -0
  19. package/dist/src/clients/payment-checkout/contact-validation.js +50 -0
  20. package/dist/src/clients/payment-checkout/detects.d.ts +3 -0
  21. package/dist/src/clients/payment-checkout/detects.js +56 -0
  22. package/dist/src/clients/payment-checkout/index.d.ts +5 -0
  23. package/dist/src/clients/payment-checkout/index.js +83 -0
  24. package/dist/src/clients/payment-checkout/messages.d.ts +33 -0
  25. package/dist/src/clients/payment-checkout/messages.js +19 -0
  26. package/dist/src/clients/payment-checkout/payment-handler.d.ts +36 -0
  27. package/dist/src/clients/payment-checkout/payment-handler.js +389 -0
  28. package/dist/src/clients/payment-checkout/session.d.ts +2 -0
  29. package/dist/src/clients/payment-checkout/session.js +67 -0
  30. package/dist/src/clients/payment-checkout/shipping-contact-details.d.ts +10 -0
  31. package/dist/src/clients/payment-checkout/shipping-contact-details.js +83 -0
  32. package/dist/src/clients/payment-checkout/types.d.ts +118 -0
  33. package/dist/src/clients/payment-checkout/types.js +14 -0
  34. package/dist/src/index.d.ts +7 -0
  35. package/dist/src/index.js +1642 -0
  36. package/dist/src/stories/fixtures/offer.d.ts +2 -0
  37. package/dist/src/stories/fixtures/offer.js +28 -0
  38. package/dist/src/stories/utils/story-env.d.ts +11 -0
  39. package/dist/src/stories/utils/story-env.js +148 -0
  40. package/dist/src/types/html-template.d.js +0 -0
  41. package/dist/src/types/offer.types.d.ts +73 -0
  42. package/dist/src/types/offer.types.js +0 -0
  43. package/dist/src/utils/billing-countries.d.ts +19 -0
  44. package/dist/src/utils/billing-countries.js +263 -0
  45. package/dist/src/utils/define.d.ts +1 -0
  46. package/dist/src/utils/define.js +9 -0
  47. package/dist/src/utils/expressCheckoutRedirect.d.ts +55 -0
  48. package/dist/src/utils/expressCheckoutRedirect.js +46 -0
  49. package/dist/src/utils/expressCheckoutTracking.d.ts +64 -0
  50. package/dist/src/utils/expressCheckoutTracking.js +84 -0
  51. package/dist/src/utils/pricing.d.ts +1 -0
  52. package/dist/src/utils/pricing.js +60 -0
  53. package/dist/src/utils/recaptcha.d.ts +22 -0
  54. package/dist/src/utils/recaptcha.js +54 -0
  55. package/package.json +64 -0
package/README.md ADDED
@@ -0,0 +1,186 @@
1
+ # @economist/web-apple-pay
2
+
3
+ Framework-agnostic Apple Pay UI components implemented as Web Components.
4
+
5
+ This package provides:
6
+
7
+ - `teg-apple-pay-button`
8
+ - `teg-apple-pay-modal`
9
+
10
+ It is intentionally UI-focused. Consumer applications own backend payment orchestration, merchant validation, and secret handling.
11
+
12
+ ## Requirements
13
+
14
+ - Node.js `20.x` or later
15
+ - npm `10.x` or later
16
+
17
+ ## Installation
18
+
19
+ ```sh
20
+ npm install --save @economist/web-apple-pay
21
+ ```
22
+
23
+ ## Quick start
24
+
25
+ ### 1) Configure runtime values
26
+
27
+ Call `configureApplePay(...)` once during app startup, before rendering Apple Pay UI:
28
+
29
+ ```ts
30
+ import { configureApplePay, defineApplePayElements } from '@economist/web-apple-pay';
31
+
32
+ configureApplePay({
33
+ merchantId: 'merchant.com.economist.your-app',
34
+ debugBypassEnabled: false,
35
+ // required: wallet API base URL for session and checkout calls
36
+ walletApiBaseUrl: 'https://bff-test.economist.com',
37
+ });
38
+
39
+ defineApplePayElements();
40
+ ```
41
+
42
+ ### 2) Create and mount a button
43
+
44
+ ```ts
45
+ import type { Offer, ApplePayButton } from '@economist/web-apple-pay';
46
+
47
+ const button = document.createElement('teg-apple-pay-button') as ApplePayButton;
48
+ button.offer = offer as Offer;
49
+ button.country = 'US';
50
+ button.dataset.loggedIn = 'true';
51
+
52
+ container.appendChild(button);
53
+ ```
54
+
55
+ When clicked, the button creates and opens `teg-apple-pay-modal` automatically.
56
+
57
+ ## Runtime configuration
58
+
59
+ ### `configureApplePay(config)`
60
+
61
+ Sets package-level runtime configuration used by Apple Pay availability checks.
62
+ Call this once during app startup.
63
+
64
+ `config` shape:
65
+
66
+ - `merchantId` (`string`, required): Apple Pay merchant identifier used for active-card checks.
67
+ - `debugBypassEnabled` (`boolean`, optional): allows `?BYPASS_APPLE_PAY_CHECKS=1` query param to bypass capability checks in non-production-style debugging flows.
68
+ - `walletApiBaseUrl` (`string`, required): base URL for wallet API requests.
69
+
70
+ If `merchantId` is not configured, Apple Pay availability checks return `false` and the button hides itself.
71
+
72
+ ## Component behavior
73
+
74
+ ### `ApplePayButton`
75
+
76
+ - Requires `offer` and `country` before click flow is meaningful.
77
+ - Checks Apple Pay availability on connect; if unavailable, it hides.
78
+ - Skips rendering for excluded variants (`bundle`, `insider_print`).
79
+ - Forwards `data-logged-in` from button to modal (`modal.dataset.loggedIn`).
80
+
81
+ #### Awaiting render completion — `whenReady()`
82
+
83
+ Use `whenReady()` to await the component's render lifecycle after appending it
84
+ to the DOM. This is the supported, stable way to know whether Apple Pay is
85
+ available without touching lifecycle internals.
86
+
87
+ ```ts
88
+ container.appendChild(btn); // spec-compliant — browser fires connectedCallback naturally
89
+ await btn.whenReady(); // resolves when rendering is complete
90
+ if (btn.style.display !== 'none') {
91
+ // Apple Pay rendered successfully
92
+ } else {
93
+ btn.remove(); // unavailable or suppressed
94
+ }
95
+ ```
96
+
97
+ > **Important:** calling `whenReady()` _before_ `appendChild` resolves
98
+ > immediately to `undefined` — it does not wait for a future connection.
99
+ > Always call it after appending the element.
100
+
101
+ ### `ApplePayModal`
102
+
103
+ - Opened by `ApplePayButton` click.
104
+ - Emits `apple-pay-confirmed` when CTA is accepted.
105
+ - `Escape` closes the modal and prevents propagation to other global handlers.
106
+
107
+ ## Events
108
+
109
+ ### `apple-pay-confirmed`
110
+
111
+ Dispatched from `ApplePayModal` as a `CustomEvent` with:
112
+
113
+ - `detail.skuId` (`string`)
114
+ - `detail.country` (`string`)
115
+
116
+ Consumers should listen for this event and continue payment orchestration.
117
+
118
+ ## Public API
119
+
120
+ - `defineApplePayElements()`: registers both custom elements.
121
+ - `configureApplePay(config)`: sets required runtime config (merchant ID and wallet API base URL, with optional debug bypass).
122
+ - `ApplePayButton`: button component class.
123
+ - `whenReady(): Promise<void>` — resolves when the current render lifecycle completes (available or hidden). Resolves immediately if called before the element is connected.
124
+ - `ApplePayModal`: modal component class.
125
+ - `ensureApplePayLogoSymbol()`: logo symbol injection helper.
126
+ - `Offer`: offer contract type used by component props.
127
+
128
+ ## Troubleshooting
129
+
130
+ ### Button does not appear
131
+
132
+ Check:
133
+
134
+ - `configureApplePay(...)` was called before rendering (with a valid `merchantId` and `walletApiBaseUrl`)
135
+ - device/browser supports Apple Pay checks (Safari/Apple device logic)
136
+ - query string/feature flag gating for your page is satisfied
137
+ - offer variant is not excluded (`bundle`, `insider_print`)
138
+
139
+ ### Logged-in checkbox copy looks wrong
140
+
141
+ Set `button.dataset.loggedIn = 'true'` before appending the button. The button forwards this flag to the modal automatically.
142
+
143
+ ## Security and responsibilities
144
+
145
+ Merchant setup must be handled by the consuming application:
146
+
147
+ - inject merchant config at runtime
148
+ - perform merchant validation on trusted backend services
149
+ - keep certificates/secrets outside this package
150
+
151
+ ## Local development
152
+
153
+ From `shared-packages`:
154
+
155
+ ```sh
156
+ cd packages/web-apple-pay
157
+ npm run build
158
+ yalc publish --sig
159
+ ```
160
+
161
+ Then in a consuming app:
162
+
163
+ ```sh
164
+ yalc add @economist/web-apple-pay
165
+ ```
166
+
167
+ ## Storybook
168
+
169
+ Run Storybook locally:
170
+
171
+ ```sh
172
+ npm run storybook --workspace @economist/web-apple-pay
173
+ ```
174
+
175
+ Build static Storybook output:
176
+
177
+ ```sh
178
+ npm run build-storybook --workspace @economist/web-apple-pay
179
+ ```
180
+
181
+ Story coverage matrix:
182
+
183
+ - `ApplePayButton` docs page shows the stable default example.
184
+ - `ApplePayButton` behavior and state variants are available as individual canvas stories in Storybook.
185
+ - `ApplePayModal` docs page shows the stable default example.
186
+ - `ApplePayModal` behavior and state variants are available as individual canvas stories in Storybook.
@@ -0,0 +1 @@
1
+ export * from './src/index';