createOrder.tsx
24 lines
| 1 | import type {PayPalCommerceGateway} from '../../types'; |
| 2 | |
| 3 | /** |
| 4 | * @since 4.0.0 |
| 5 | */ |
| 6 | export default async function createOrder(url: string, gateway: PayPalCommerceGateway, formData: FormData) { |
| 7 | const response = await fetch(url, { |
| 8 | method: 'POST', |
| 9 | body: formData, |
| 10 | }); |
| 11 | |
| 12 | const responseJson = await response.json(); |
| 13 | |
| 14 | if (!responseJson.success) { |
| 15 | throw responseJson.data.error; |
| 16 | } |
| 17 | |
| 18 | const orderId = responseJson.data.id; |
| 19 | |
| 20 | gateway.payPalOrderId = orderId; |
| 21 | |
| 22 | return orderId; |
| 23 | } |
| 24 |