| 1 |
import type {PayPalCommerceGateway} from '../../types'; |
| 2 |
import {__} from '@wordpress/i18n'; |
| 3 |
|
| 4 |
/** |
| 5 |
* @since 4.16.7.1 Throw an Error carrying the server's message instead of a bare string, so the card |
| 6 |
* fields error handler and the form can display it. |
| 7 |
* @since 4.0.0 |
| 8 |
*/ |
| 9 |
export default async function createOrder(url: string, gateway: PayPalCommerceGateway, formData: FormData) { |
| 10 |
const response = await fetch(url, { |
| 11 |
method: 'POST', |
| 12 |
body: formData, |
| 13 |
}); |
| 14 |
|
| 15 |
const responseJson = await response.json(); |
| 16 |
|
| 17 |
if (!responseJson.success) { |
| 18 |
const error = responseJson.data?.error; |
| 19 |
|
| 20 |
// The server answers with either a plain message or PayPal's own error object. |
| 21 |
throw new Error( |
| 22 |
typeof error === 'string' ? error : error?.message ?? __('Unable to create the PayPal order.', 'give') |
| 23 |
); |
| 24 |
} |
| 25 |
|
| 26 |
const orderId = responseJson.data.id; |
| 27 |
|
| 28 |
gateway.payPalOrderId = orderId; |
| 29 |
|
| 30 |
return orderId; |
| 31 |
} |
| 32 |
|