Payments in AMP
Via amp-iframe and the
allowpaymentrequest attribute, AMP pages can support requesting payment information directly from the browser. The PaymentRequest API is an open W3C spec with support from most major browsers. This example won't go into detail on the API, but you can get more information in this deep dive article.
This example shows how to embed an iframe that contains only a "buy now" button. All of the actual payment logic is contained within the iframe src itself.
The only extra component needed for this example is amp-iframe.
<script async custom-element="amp-iframe" src="https://cdn.ampproject.org/v0/amp-iframe-0.1.js"></script>
Configuring the Iframe
In the basic case, the AMP page hands off all PaymentRequest logic to the iframe to complete purchase of a single item.
Crtically, the iframe does two things:
- Sets the
allowpaymentrequestattribute, which gives the iframe source permission to call PaymentRequest.show().
- Passes a relevant product identifier via query string, allowing the iframe to retrive appropriate product information for this product.
<amp-iframe title="Buy Now button to retrieve product information" width="130" height="42" sandbox="allow-scripts allow-same-origin allow-top-navigation" allowpaymentrequest frameborder="0" noloading src="/static/samples/files/iframe/payments.html?productId=1"> <button placeholder disabled>Buy Now</button> </amp-iframe>
placeholderelement to the
amp-iframeto ensure that it can be loaded in the top viewport. The placeholder shows a disabled button until the iframe is loaded. We also disable the default loading animation using CSS.
- Swap the "Buy Now" button for an "Add to cart" button.
- Redirect to a standard checkout form.
- Hide the "Buy Now" button altogether, and have a separate AMP-based Add to Cart button (see the Product Page demo)
如果此页面上的说明未能涵盖您的所有问题，欢迎与其他 AMP 用户取得联系，讨论您的具体用例。前往 Stack Overflow 一项无法解释的功能？
AMP 项目强烈鼓励您参与并做出贡献！我们希望您能成为我们开放源代码社区的持续参与者，但我们也欢迎您对所热衷问题做出一次性贡献。编辑 GitHub 上的示例
Written by @sebastianbenz