PaymentResponse: shippingAddress 属性
PaymentRequest 接口的只读属性 shippingAddress 返回一个 PaymentAddress 对象,其中包含用户提供的送货地址。
值
一个 PaymentAddress 对象,提供包含用户提供的送货地址的详细信息。
示例
通常,用户代理会为您填充 shippingAddress 属性。您可以通过在调用 PaymentRequest 构造函数时将 options.requestShipping 设置为 true 来触发此操作。
在下面的示例中,运费因地区而异。当触发并捕获 shippingaddresschange 事件时,会调用 updateDetails() 来更新 PaymentRequest 的详细信息,使用 shippingAddress 来设置正确的运费。
js
// Initialization of PaymentRequest arguments are excerpted for brevity.
const payment = new PaymentRequest(supportedInstruments, details, options);
request.addEventListener("shippingaddresschange", (evt) => {
evt.updateWith(
new Promise((resolve) => {
updateDetails(details, request.shippingAddress, resolve);
}),
);
});
payment
.show()
.then((paymentResponse) => {
// Processing of paymentResponse excerpted for the same of brevity.
})
.catch((err) => {
console.error("Uh oh, something bad happened", err.message);
});
function updateDetails(details, shippingAddress, resolve) {
if (shippingAddress.country === "US") {
const shippingOption = {
id: "",
label: "",
amount: { currency: "USD", value: "0.00" },
selected: true,
};
if (shippingAddress.region === "MO") {
shippingOption.id = "mo";
shippingOption.label = "Free shipping in Missouri";
details.total.amount.value = "55.00";
} else {
shippingOption.id = "us";
shippingOption.label = "Standard shipping in US";
shippingOption.amount.value = "5.00";
details.total.amount.value = "60.00";
}
details.displayItems.splice(2, 1, shippingOption);
details.shippingOptions = [shippingOption];
} else {
delete details.shippingOptions;
}
resolve(details);
}
规范
| 规范 |
|---|
| Payment Request API # shippingaddress-attribute |
浏览器兼容性
加载中…