Welcome to the 2C2P Developer Zone

This is the resource where developers can find all the information needed to integrate the services offered by 2C2P with their e commerce system.

Google Pay

Introduction

Google Pay™ is a digital card wallet provided by Google. It enables your customer to checkout faster and simpler.

Prerequisites

To start accepting Google Pay in your checkout page you must signup with Google to get your Google Pay MerchantId
1) To get started, please visit Google Pay's Web Integration overview.
2) You need to complete Google Pay's Web Integration Checklist.
3) You need to request Production Access.

Accepting Google Pay

1) Implement a Google Pay button according to Google's Brand Guidelines.
1.1) Following parameters need to be submitted accordingly

  • allowedAuthMethods - PAN_ONLY
  • allowedCardNetworks - Card brands that you agreed with 2C2P to process the payment
  • paymentDataRequest.merchantInfo.merchantId - MerchantId you received from Google
  • paymentDataRequest.merchantInfo.merchantName - Merchant Name
  • paymentDataRequest.transactionInfo.currencyCode - Payment Currency
  • paymentDataRequest.transactionInfo.totalPrice - Payment Amount
  • tokenizationSpecification.parameters.gateway - 2c2p
  • tokenizationSpecification.parameters.gatewayMerchantId - MerchantId provided by 2C2P

1.2) GooglePay API's returned value paymentData.paymentMethodData.tokenizationData.token is to be submitted to 2C2P's Securepay API in base64 encoded format.


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>2C2P Google Pay Demo (3DS)</title>
</head>
<body>
    <div id="container"></div>
    <div id="result"></div>
    <!-- Mandatory Web Form Attributes:
         Form 'id': Used to identify the form that will be submitted to the backend.
         Form 'action': The address of the backend code that will submit payment request to 2c2p Payment Gateway.
         Form 'method': Should be set to 'POST' method
    -->
    <form id="2c2p-payment-form" action="payment_3ds.aspx" method="post">
        <!-- Optional Section Start -->
        <!--This extended section is optional, it can be fully customizable if neccessary -->
        <input type="hidden" id="mobilePaymentData" name="mobilePaymentData" /><br />
        <!-- Optional Section End -->
    </form>

    <!--Importing JSLibrary-->
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js" defer></script>
    <script type="text/javascript">
        const baseRequest = {
            apiVersion: 2,
            apiVersionMinor: 0
        };

        const allowedCardNetworks = ["AMEX", "DISCOVER", "JCB", "MASTERCARD", "VISA"]; // Enable cards accepted

        const allowedCardAuthMethods = ["PAN_ONLY"]; // CRYPTOGRAM_3DS

        const billingAddressParameters = { phoneNumberRequired: true };

        const tokenizationSpecification = {
            type: 'PAYMENT_GATEWAY',
            parameters: {
                "gateway": "2c2p", 
                "gatewayMerchantId": "JT01",
            }
        };


        const baseCardPaymentMethod = {
            type: 'CARD',
            parameters: {
                allowedAuthMethods: allowedCardAuthMethods,
                allowedCardNetworks: allowedCardNetworks,
                billingAddressRequired: true,
                billingAddressParameters: billingAddressParameters
            }
        };

        const isRTPCardPaymentMethod = {
            type: 'CARD',
            parameters: {
                allowedAuthMethods: allowedCardAuthMethods,
                allowedCardNetworks: allowedCardNetworks,
                billingAddressRequired: true,
                billingAddressParameters: billingAddressParameters,
            }
        };

        const cardPaymentMethod = Object.assign(
            {},
            baseCardPaymentMethod,
            {
                tokenizationSpecification: tokenizationSpecification
            }
        );


        let paymentsClient = null;

        /**
         * Configure your site's support for payment methods supported by the Google Pay
         * API.
         */
        function getGoogleIsReadyToPayRequest() {
            return Object.assign(
                {},
                baseRequest,
                {
                    allowedPaymentMethods: [isRTPCardPaymentMethod],
                    existingPaymentMethodRequired: true
                }
            );
        }

        /**
         * Configure support for the Google Pay API
         */
        function getGooglePaymentDataRequest() {
            const paymentDataRequest = Object.assign({}, baseRequest);
            paymentDataRequest.allowedPaymentMethods = [cardPaymentMethod];
            paymentDataRequest.transactionInfo = getGoogleTransactionInfo();
            paymentDataRequest.merchantInfo = {
                // @todo a merchant ID is available for a production environment after approval by Google
                // See {@link https://developers.google.com/pay/api/web/guides/test-and-deploy/integration-checklist|Integration checklist}
                merchantId: '<sign up for Merchant ID from Google Pay>',
                merchantName: '2C2P Test Merchant'
            };
            return paymentDataRequest;
        }

        /**
         * Return an active PaymentsClient or initialize
         */
        function getGooglePaymentsClient() {
            if (paymentsClient === null) {
                paymentsClient = new google.payments.api.PaymentsClient({ environment: 'TEST' });
            }
            return paymentsClient;
        }

        /**
         * Initialize Google PaymentsClient after Google-hosted JavaScript has loaded
         */
        function onGooglePayLoaded() {
            const paymentsClient = getGooglePaymentsClient();
            paymentsClient.isReadyToPay(getGoogleIsReadyToPayRequest())
                .then(function (response) {
                    if (response.result) {
                        addGooglePayButton();
                        // @todo prefetch payment data to improve performance after confirming site functionality
                        prefetchGooglePaymentData();
                    } else {
                        console.log("isRTP false");
                    }
                })
                .catch(function (err) {
                    // show error in developer console for debugging
                    console.error(err);
                });
        }

        /**
         * Add a Google Pay purchase button alongside an existing checkout button
         */
        function addGooglePayButton() {
            const paymentsClient = getGooglePaymentsClient();
            const button =
                paymentsClient.createButton({ onClick: onGooglePaymentButtonClicked });
            document.getElementById('container').appendChild(button);
        }

        /**
         * Provide Google Pay API with a payment amount, currency, and amount status
         */
        function getGoogleTransactionInfo() {
            return {
                currencyCode: 'SGD',
                totalPriceStatus: 'FINAL',
                // set to cart total
                totalPrice: '10.00'
            };
        }

        /**
         * Prefetch payment data to improve performance
         */
        function prefetchGooglePaymentData() {
            const paymentDataRequest = getGooglePaymentDataRequest();
            // transactionInfo must be set but does not affect cache
            paymentDataRequest.transactionInfo = {
                totalPriceStatus: 'NOT_CURRENTLY_KNOWN',
                currencyCode: 'SGD'
            };
            const paymentsClient = getGooglePaymentsClient();
            paymentsClient.prefetchPaymentData(paymentDataRequest);
        }

        /**
         * Show Google Pay payment sheet when Google Pay payment button is clicked
         */
        function onGooglePaymentButtonClicked() {
            const paymentDataRequest = getGooglePaymentDataRequest();
            paymentDataRequest.transactionInfo = getGoogleTransactionInfo();

            const paymentsClient = getGooglePaymentsClient();
            paymentsClient.loadPaymentData(paymentDataRequest)
                .then(function (paymentData) {
                    // handle the response
                    processPayment(paymentData);
                })
                .catch(function (err) {
                    // show error in developer console for debugging
                    console.error(err);
                    //alert(JSON.stringify(err));
                });
        }



        /**
         * Process payment data returned by the Google Pay API
         */
        function processPayment(paymentData) {
            $("#mobilePaymentData").val(JSON.stringify(JSON.parse(paymentData.paymentMethodData.tokenizationData.token)));
            // @todo pass payment data response to gateway to process payment
            document.forms[0].submit();
        }

    </script>

    <script async src="https://pay.google.com/gp/p/js/pay.js" onload="onGooglePayLoaded()"></script>
</body>
</html>

2) Accept Google Pay Token from frontend and Submit to SecurePayAPI
2.1) paymentChannel - GOOGLEPAY
2.2) mobilePaymentData - base64 encoded value of paymentData.paymentMethodData.tokenizationData.token

protected void Page_Load(object sender, EventArgs e)
{
  if (!Page.IsPostBack)
  {
    if (base.Request.HttpMethod == "POST")
    {
      #region ALL Fields

        string cardholderEmail = "", payCategoryID = "", userDefined1 = "", userDefined2 = "", userDefined3 = "", userDefined4 = "", userDefined5 = "", storeCard = "",
      ippTransaction = "", installmentPeriod = "", interestType = "", recurring = "", invoicePrefix = "", recurringAmount = "", allowAccumulate = "", maxAccumulateAmt = "",
      recurringInterval = "", recurringCount = "", chargeNextDate = "", promotion = "", request3DS = "", statementDescriptor = "", agentCode = "", channelCode = "", paymentExpiry = "", mobileNo = "", tokenizeWithoutAuthorization = "",
      paymentChannel = "", storeCardUniqueID = "", panBank = "";

      #endregion

        //Merchant Account Information
        string merchantID = "JT01";       //Get MerchantID from 2c2p PGW Dashboard
      string secretKey = "7jYcp4FxFdf0";  //Get SecretKey from 2c2p PGW Dashboard

      //Product Information
      string uniqueTransactionCode = "Invoice" + DateTime.Now.ToString("yyMMddHHmmss");
      string desc = "Test Payment";
      string amt = "000000001000";       //12 digit format
      string currencyCode = "702";       //Ref: http://en.wikipedia.org/wiki/ISO_4217

      //Customer Information
      string cardholderName = "John Doe";
      string country = "SG";

      //Request Information
      string timeStamp = DateTime.Now.ToString("ddMMyyHHmmss");
      string apiVersion = "9.9";

      string mobilePaymentData = Request.Form["mobilePaymentData"]; //Retrieve encrypted credit card data

      mobilePaymentData = base64Encode(mobilePaymentData);

      //Construct payment request message
      string payloadXml = "<PaymentRequest>" +
        "<timeStamp>" + timeStamp + "</timeStamp>" +
        "<merchantID>" + merchantID + "</merchantID>" +
        "<paymentChannel>GPAY</paymentChannel>" +
        "<uniqueTransactionCode>" + uniqueTransactionCode + "</uniqueTransactionCode>" +
        "<desc>" + HttpUtility.HtmlEncode(desc) + "</desc>" +
        "<amt>" + amt + "</amt>" +
        "<currencyCode>" + currencyCode + "</currencyCode>" +
        "<panCountry>" + country + "</panCountry>" +
        "<cardholderName>" + cardholderName + "</cardholderName>" +
        "<mobilePaymentData>" + mobilePaymentData + "</mobilePaymentData>" + 
        "</PaymentRequest>";
      
      string payload = base64Encode(payloadXml);       //Convert payload to base64  
      string hash = getHMAC2(payload, secretKey);                         //Calculate Hash Value

      string xml = "<PaymentRequest>" +
        "<version>" + apiVersion + "</version>" + //request version (9.9)
        "<payload>" + payload + "</payload>" + //request payload
        "<signature>" + hash + "</signature>" + //signature
        "</PaymentRequest>";

      string data = base64Encode(xml);    //Convert payload to base64                    
      string url = "https://demo2.2c2p.com/2C2PFrontEnd/SecurePayment/PaymentAuth.aspx";
      Response.Write(FrontendPost(url, data));
    }
  }
}


private string FrontendPost(string url, string request)
{
  try
  {
    string strPost = "";
    strPost += "<form name=\"ResultForm\" id=\"ResultForm\" action=\"" + url + "\" method=\"post\" >";
    strPost += "<input type=\"hidden\" name=\"paymentRequest\" id=\"paymentRequest\" value=\"" + request + "\">";
    strPost += "<h3>Please wait while submitting payment request . . .</h3>";
    strPost += "<script language=\"JavaScript\">document.ResultForm.submit();</script>";
    strPost += "</form>";
    return strPost;
  }
  catch (Exception exception)
  {
    return "";
  };
}

private string base64Encode(string plainText)
{
  var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
  return Convert.ToBase64String(plainTextBytes);
}

private string getHMAC2(string signatureString, string secretKey)
{
  System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
  byte[] keyByte = encoding.GetBytes(secretKey);
  HMACSHA256 hmac = new HMACSHA256(keyByte);
  byte[] messageBytes = encoding.GetBytes(signatureString);
  byte[] hashmessage = hmac.ComputeHash(messageBytes);
  return ByteArrayToHexString(hashmessage);
}

private string ByteArrayToHexString(byte[] Bytes)
{
  StringBuilder Result = new StringBuilder();
  string HexAlphabet = "0123456789ABCDEF";
  foreach (byte B in Bytes)
  {
    Result.Append(HexAlphabet[(int)(B >> 4)]);
    Result.Append(HexAlphabet[(int)(B & 0xF)]);
  }
  return Result.ToString().ToUpper();
}

Updated 2 months ago

Google Pay


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.