AMP
  • websites

Payments in AMP

Introduction

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.

Setup

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:

  1. Sets the allowpaymentrequest attribute, which gives the iframe source permission to call PaymentRequest.show().
  2. 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>

we add a placeholder element to the amp-iframe to 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.

However, since AMP does not allow javascript, the iframe source must also handle the case where PaymentRequest is not available. While currently out of scope for this example, potential options are to:

  1. Swap the "Buy Now" button for an "Add to cart" button.
  2. Redirect to a standard checkout form.
  3. 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