PaymentRequest: shippingOption 属性
已弃用:此特性不再推荐。虽然某些浏览器可能仍然支持它,但它可能已经从相关的网络标准中删除,可能正在删除过程中,或者可能仅为兼容性目的而保留。请避免使用它,如果可能,请更新现有代码;请参阅本页底部的兼容性表格以指导您的决策。请注意,此特性可能随时停止工作。
非标准:此特性未标准化。我们不建议在生产环境中使用非标准特性,因为它们浏览器支持有限,并且可能会更改或被移除。但是,在没有标准选项的特定情况下,它们可以是合适的替代方案。
PaymentRequest 接口的只读属性 shippingOption 返回已选中的配送选项的 id、null(如果未选中任何配送选项)或用户选择的配送选项。当没有提供“已选中”的配送选项时,它最初是 null。
仅当构造函数使用设置为 true 的 requestShipping 标志调用时,此属性才会被填充。如果 requestShipping 为 false(或缺失),则 shippingOption 返回 null,即使开发者提供了选中的配送选项。
值
对象或 null。
示例
在下面的示例中,会触发 shippingaddresschange 和 shippingoptionchange 事件。每次调用 updateDetails() 都会执行,一次使用 Promise,另一次使用纯 JS 对象。这演示了支付工作表的同步和异步更新。
js
const request = new PaymentRequest(methodData, details, options);
// Async update to details
request.onshippingaddresschange = (ev) => {
ev.updateWith(checkShipping(request));
};
// Sync update to the total
request.onshippingoptionchange = (ev) => {
const shippingOption = shippingOptions.find(
(option) => option.id === request.id,
);
const newTotal = {
currency: "USD",
label: "Total due",
value: calculateNewTotal(shippingOption),
};
ev.updateWith({ ...details, total: newTotal });
};
async function checkShipping(request) {
try {
const json = request.shippingAddress.toJSON();
await ensureCanShipTo(json);
const { shippingOptions, total } = await calculateShipping(json);
return { ...details, shippingOptions, total };
} catch (err) {
return { ...details, error: `Sorry! we can't ship to your address.` };
}
}
浏览器兼容性
加载中…