DEV Community

Ethern Myth
Ethern Myth

Posted on

3 3 3 2 3

Payment Request API

This is a submission for DEV Challenge v24.03.20, One Byte Explainer: Browser API or Feature.

Explainer

Payment Request API simplifies online payments by offering a standard browser interface for collecting payment details. It smoothens checkout processes, reduces manual input, and supports various payment methods, enhancing user experience and speeding up web transactions.

Additional Context

Usage example:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Payment Request Example</title>
</head>
<body>
  <button id="buyButton">Buy Now</button>

  <script>
    // Check if Payment Request API is supported
    if (window.PaymentRequest) {
      const supportedPaymentMethods = [
        {
          supportedMethods: 'basic-card',
          data: {
            supportedNetworks: ['visa', 'mastercard'],
            supportedTypes: ['debit', 'credit']
          }
        }
      ];

      const paymentDetails = {
        total: {
          label: 'Total',
          amount: {
            currency: 'USD',
            value: '10.00'
          }
        }
      };

      const options = {};

      // Create Payment Request object
      const paymentRequest = new PaymentRequest(supportedPaymentMethods, paymentDetails, options);

      // Handle buy button click event
      document.getElementById('buyButton').addEventListener('click', async () => {
        try {
          // Show payment request
          const paymentResponse = await paymentRequest.show();

          // Process payment response
          console.log('Payment method:', paymentResponse.methodName);
          console.log('Payment details:', paymentResponse.details);

          // Complete payment
          await paymentResponse.complete('success');
        } catch (error) {
          console.error('Payment failed:', error);
        }
      });
    } else {
      console.error('Payment Request API is not supported.');
    }
  </script>
</body>
</html>

Enter fullscreen mode Exit fullscreen mode

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more