Google Pay 即時示範模式
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
本頁面提供的 Google Pay 即時示範模式可讓您在 JavaScript 或 JSFiddle 中編輯。
基本示例
以下是 Google Pay 按鈕的基本示例,可讓您瞭解按鈕的實際運作方式。如要瞭解如何建立按鈕,請參閱鄰近的程式碼。
以下說明如何調整 Google Pay 按鈕的大小。呼叫 buttonSizeMode
屬性設為 fill
的 createButton
,然後為 #container div
設定大小。
授權付款示例
以下示例說明如何先「授權付款」,再處理付款。
採取先授權付款再進行處理這種做法,是因為如果授權失敗,使用者還有機會可以選擇其他付款方式。
處理付款授權要求的操作說明如下:
-
在
PaymentOptions
中註冊 onPaymentAuthorized()
回呼。
- 使用
PAYMENT_AUTHORIZATION
回呼意圖呼叫 loadPaymentData()
函式。
- 實作
onPaymentAuthorized()
回呼處理常式。
按一下在 JSFiddle 中編輯連結即可編輯並親自試用。
動態價格更新示例
「動態價格更新」功能可讓您動態調整總價,以反映運送地址和運送選項的變更。
請按照下列步驟設定動態價格更新功能:
- 在
PaymentOptions
中註冊 onPaymentAuthorized
和 onPaymentDataChanged
回呼。
- 使用回呼意圖呼叫
loadPaymentData()
函式。詳情請參閱相對應的示例。
- 實作
onPaymentAuthorized
和 onPaymentDataChanged
。
按一下在 JSFiddle 中編輯連結即可編輯並親自試用。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2024-10-16 (世界標準時間)。
[null,null,["上次更新時間:2024-10-16 (世界標準時間)。"],[[["\u003cp\u003eThis page provides live, editable Google Pay demos using JavaScript or JSFiddle.\u003c/p\u003e\n"],["\u003cp\u003eYou can explore examples demonstrating basic Google Pay button integration, button resizing, and payment authorization.\u003c/p\u003e\n"],["\u003cp\u003eThe demos also showcase how to dynamically update prices based on shipping changes using Dynamic Price Updates.\u003c/p\u003e\n"],["\u003cp\u003eThese demos are interactive and can be edited in JSFiddle for experimentation and learning.\u003c/p\u003e\n"]]],["The content showcases live Google Pay demos editable in JavaScript or JSFiddle. Key actions include creating and resizing the Google Pay button using `createButton` with `buttonSizeMode`. It demonstrates authorizing payments before processing by registering `onPaymentAuthorized()`, calling `loadPaymentData()`, and implementing the callback handler. Dynamic price updates are enabled by registering `onPaymentAuthorized` and `onPaymentDataChanged`, and implementing both functions after calling `loadPaymentData`. Note that authorize payments and Dynamic price updates do not support liability shift.\n"],null,["# Live Google Pay demos\n\nThis page contains live Google Pay demos that you can edit in JavaScript or JSFiddle.\n| **Note**: The demos on this page will only show a button if your browser supports Google Pay.\n\nBasic example\n-------------\n\nThe following demonstrates a basic example of the Google Pay button in action. Refer to the\nadjacent code for an example on how it's constructed. \n\nButton resize example\n---------------------\n\nThe following demonstrates how you can resize the Google Pay button. Call\n`createButton` with the `buttonSizeMode` property set to `fill`,\nand then set a size on the `#container div`. \n\nAuthorize Payments example\n--------------------------\n\nThe following is an example of how you can *authorize a payment* before processing it. \n\nThe reason you might want to authorize a payment before processing it is to give the user an\nopportunity to choose a different payment method if the authorization fails.\n\nYou can handle the payment authorization request by following these instructions:\n\n1. Register an [`onPaymentAuthorized()`](/pay/api/web/reference/client#onPaymentAuthorized) callback in [`PaymentOptions`](/pay/api/web/reference/request-objects#PaymentOptions).\n2. Call the [`loadPaymentData()`](/pay/api/web/reference/client#loadPaymentData) function with the `PAYMENT_AUTHORIZATION` callback intent.\n3. Implement the [`onPaymentAuthorized()`](/pay/api/web/reference/client#onPaymentAuthorized) callback handler.\n\nClick the [Edit in JSFiddle](https://jsfiddle.net/34wxopb2/)\nlink to edit and try it out for yourself.\n\nDynamic Price Update example\n----------------------------\n\nDynamic Price Updates allows you to dynamically adjust the total price to reflect changes in shipping address and shipping option changes.\n\nUse the following steps to set up Dynamic Price Updates:\n\n1. Register both of the [`onPaymentAuthorized`](/pay/api/web/reference/client#onPaymentAuthorized) and [`onPaymentDataChanged`](/pay/api/web/reference/client#onPaymentDataChanged) callbacks in [`PaymentOptions`](/pay/api/web/reference/request-objects#PaymentOptions).\n2. Call the [`loadPaymentData()`](/pay/api/web/reference/client#loadPaymentData) function with callback intents. For details, see the corresponding [example](/pay/api/web/guides/tutorial#load).\n3. Implement [`onPaymentAuthorized`](/pay/api/web/reference/client#onPaymentAuthorized) and [`onPaymentDataChanged`](/pay/api/web/reference/client#onPaymentDataChanged).\n\nClick the [Edit in JSFiddle](https://jsfiddle.net/ynt8p152/) link to edit and try it out for yourself."]]