( config: CheckoutPlaygroundConfig )
| 115 | } |
| 116 | |
| 117 | export async function fetchCheckoutSessionParams( |
| 118 | config: CheckoutPlaygroundConfig |
| 119 | ): Promise<CheckoutSessionResponse> { |
| 120 | if (normalizePaymentMethodTypes(config.paymentMethodTypes).length === 0) { |
| 121 | throw new Error('Select at least one payment method.'); |
| 122 | } |
| 123 | |
| 124 | const response = await fetch(hostedCheckoutEndpoint, { |
| 125 | method: 'POST', |
| 126 | headers: { |
| 127 | 'Content-Type': 'application/json', |
| 128 | }, |
| 129 | body: JSON.stringify(buildCheckoutSessionRequestBody(config)), |
| 130 | }); |
| 131 | |
| 132 | const responseBody = await parseCheckoutSessionResponseBody(response); |
| 133 | |
| 134 | if (!response.ok) { |
| 135 | throw new Error( |
| 136 | getCheckoutSessionErrorMessage(responseBody) || |
| 137 | 'Failed to create checkout session.' |
| 138 | ); |
| 139 | } |
| 140 | |
| 141 | if (!isCheckoutSessionResponse(responseBody)) { |
| 142 | throw new Error('Checkout session response was missing required fields.'); |
| 143 | } |
| 144 | |
| 145 | return { |
| 146 | publishableKey: responseBody.publishableKey, |
| 147 | checkoutSessionClientSecret: responseBody.checkoutSessionClientSecret, |
| 148 | }; |
| 149 | } |
no test coverage detected
searching dependent graphs…