Javascript Google Pay UPI Payment Integration Button Example in Browser Using HTML5 CSS3

Javascript Google Pay UPI Payment Integration Button Example in Browser Using HTML5 CSS3

<!DOCTYPE html>
<!--
Copyright 2020 Google LLC
 
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
 
      http://www.apache.org/licenses/LICENSE-2.0
 
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
 
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>Customize your button</title>
    <script
      async
      src="https://unpkg.com/@google-pay/button-element@3.x/dist/index.umd.min.js"
    ></script>
    <script async src="./script.js"></script>
    <style>
      body,
      html {
        font: 400 16px/24px Roboto, Noto Sans, Noto Sans JP, Noto Sans KR,
          Noto Naskh Arabic, Noto Sans Thai, Noto Sans Hebrew, Noto Sans Bengali,
          sans-serif;
      }
 
      form {
        display: inline-block;
      }
 
      form.top-bottom {
        margin: -4px -5px;
      }
 
      form.top-bottom label {
        margin-top: 10px;
      }
 
      form label {
        display: block;
      }
 
      form label.control {
        display: inline-block;
        width: 200px;
        margin: 4px 5px;
      }
 
      form label > span {
        display: inline-block;
        margin-right: 5px;
      }
 
      form label.control > span {
        display: block;
      }
 
      form label .value {
        font-size: 80%;
        color: #666;
      }
 
      form select {
        -moz-appearance: none;
        -webkit-appearance: none;
        border: 1px solid #dadce0;
        border-radius: 4px;
        box-shadow: none;
        color: #202124;
        cursor: pointer;
        display: inline-block;
        font: 500 14px/36px Google Sans, Noto Sans, Noto Sans JP, Noto Sans KR,
          Noto Naskh Arabic, Noto Sans Thai, Noto Sans Hebrew, Noto Sans Bengali,
          sans-serif;
        height: 36px;
        line-height: 34px;
        outline: 0;
        padding: 0 27px 0 15px;
        text-align: left;
        text-indent: 0.01px;
        text-overflow: ellipsis;
        transition: background-color 0.2s;
        vertical-align: middle;
        white-space: nowrap;
        background: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="20" height="4" viewBox="0 0 20 4"><path d="M0,0l4,4l4-4H0z" fill="%23212121"/></svg>')
          #fff no-repeat 100%;
      }
 
      form.top-bottom select {
        width: 100%;
      }
 
      form option {
        font-weight: normal;
        display: block;
        white-space: pre;
        min-height: 1.2em;
        padding: 0 2px 1px;
      }
 
      form input[type="range"] {
        width: 100%;
      }
 
      .demo {
        margin-top: 20px;
        margin-bottom: 30px;
        min-height: 40px;
      }
 
      .note {
        font: 400 16px/24px Roboto, Noto Sans, Noto Sans JP, Noto Sans KR,
          Noto Naskh Arabic, Noto Sans Thai, Noto Sans Hebrew, Noto Sans Bengali,
          sans-serif;
      }
 
      google-pay-button {
        transition: width 0.2s, height 0.2s;
      }
    </style>
  </head>
 
  <body>
    <form class="top-bottom">
      <label class="control">
        <span>Button color</span>
        <select id="button-color">
          <option value="default" selected>default</option>
          <option value="black">black</option>
          <option value="white">white</option>
        </select>
      </label>
      <label class="control">
        <span>Button type</span>
        <select id="button-type">
          <option value="buy" selected>buy</option>
          <option value="plain">plain</option>
          <option value="donate">donate</option>
        </select>
      </label>
      <label>
        <input id="button-custom" type="checkbox" />
        <span>Custom button size</span>
      </label>
      <label class="control">
        <span>Button width <span class="value"></span></span>
        <input
          id="button-width"
          type="range"
          min="160"
          max="800"
          value="240"
          disabled
        />
      </label>
      <label class="control">
        <span>Button height <span class="value"></span></span>
        <input
          id="button-height"
          type="range"
          min="40"
          max="100"
          value="40"
          disabled
        />
      </label>
    </form>
 
    <div class="demo">
      <google-pay-button
        environment="TEST"
        button-color="default"
        button-type="buy"
      ></google-pay-button>
    </div>
 
    <div class="note">
      <p>
        Note: This page may need to open in a new window for it to function
        correctly.
      </p>
      <p><a href="/" target="_blank">Open in new window</a>.</p>
    </div>
  </body>
</html>

const button = document.querySelector("google-pay-button");
 
button.paymentRequest = {
  apiVersion: 2,
  apiVersionMinor: 0,
  allowedPaymentMethods: [
    {
      type: "CARD",
      parameters: {
        allowedAuthMethods: ["PAN_ONLY", "CRYPTOGRAM_3DS"],
        allowedCardNetworks: ["MASTERCARD", "VISA"],
      },
      tokenizationSpecification: {
        type: "PAYMENT_GATEWAY",
        parameters: {
          gateway: "example",
          gatewayMerchantId: "exampleGatewayMerchantId",
        },
      },
    },
  ],
  merchantInfo: {
    merchantId: "12345678901234567890",
    merchantName: "Demo Merchant",
  },
  transactionInfo: {
    totalPriceStatus: "FINAL",
    totalPriceLabel: "Total",
    totalPrice: "100.00",
    currencyCode: "USD",
    countryCode: "US",
  },
};
 
button.addEventListener("loadpaymentdata", (event) => {
  console.log("load payment data", event.detail);
});
 
// form control behaviors
 
/** @type {HTMLSelectElement} */
const buttonColor = document.getElementById("button-color");
/** @type {HTMLSelectElement} */
const buttonType = document.getElementById("button-type");
/** @type {HTMLInputElement} */
const buttonCustom = document.getElementById("button-custom");
/** @type {HTMLInputElement} */
const buttonWidth = document.getElementById("button-width");
/** @type {HTMLInputElement} */
const buttonHeight = document.getElementById("button-height");
/** @type {HTMLSelectElement} */
const buttonLibrary = document.getElementById("button-library");
 
buttonColor.addEventListener("change", (event) => {
  button.buttonColor = buttonColor.value;
});
 
buttonType.addEventListener("change", (event) => {
  button.buttonType = buttonType.value;
});
 
buttonCustom.addEventListener("change", (event) => {
  button.buttonSizeMode = buttonCustom.checked ? "fill" : "static";
 
  buttonWidth.disabled = !buttonCustom.checked;
  buttonHeight.disabled = !buttonCustom.checked;
 
  button.style.width = `${buttonWidth.value}px`;
  button.style.height = `${buttonHeight.value}px`;
});
 
buttonWidth.addEventListener("change", (event) => {
  button.style.width = `${buttonWidth.value}px`;
});
 
buttonHeight.addEventListener("change", (event) => {
  button.style.height = `${buttonHeight.value}px`;
});
 
function handleSliderInput(event) {
  const element = event.target.parentElement.querySelector(".value");
  element.textContent = `(${event.target.value}px)`;
}
 
buttonWidth.addEventListener("input", handleSliderInput);
buttonHeight.addEventListener("input", handleSliderInput);
 
document.querySelector(".note").style.display =
  window === window.top ? "none" : "";

Share on:

Hi, I'm Ranjith a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Comment