PaymentRequestEvent: phương thức respondWith()
Khả dụng hạn chế
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Experimental: This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
Note: This feature is only available in Service Workers.
Phương thức respondWith() của giao diện PaymentRequestEvent ngăn chặn xử lý sự kiện mặc định và cho phép bạn tự cung cấp một Promise cho đối tượng phản hồi trình xử lý thanh toán.
Cú pháp
respondWith(promise)
Tham số
promise-
Một đối tượng phản hồi trình xử lý thanh toán hoặc một
Promiseđược giải quyết thành một. Đối tượng này nên chứa các thuộc tính sau:methodName-
Định danh phương thức thanh toán cho phương thức thanh toán mà người dùng đã chọn để hoàn thành giao dịch.
details-
Một đối tượng có thể JSON-serialize cung cấp thông điệp đặc thù của phương thức thanh toán được người bán sử dụng để xử lý giao dịch và xác định chuyển tiền thành công. Xem 8.1.2
detailsattribute để biết thêm chi tiết.
Giá trị trả về
Không có (undefined).
Ví dụ
Ví dụ dưới đây được lấy từ Open the payment handler window to display the web-based payment app frontend. Đọc bài viết để hiểu ngữ cảnh của mã.
self.addEventListener("paymentrequest", async (e) => {
// Retain a promise for future resolution
resolver = new PromiseResolver();
// Pass a promise that resolves when payment is done.
e.respondWith(resolver.promise);
// Open the checkout page.
try {
// Open the window and preserve the client
client = await e.openWindow(checkoutURL);
if (!client) {
// Reject if the window fails to open
throw new Error("Failed to open window");
}
} catch (err) {
// Reject the promise on failure
resolver.reject(err);
}
});
Thông số kỹ thuật
| Thông số kỹ thuật |
|---|
| Web-based Payment Handler API> # dom-paymentrequestevent-respondwith> |