> For the complete documentation index, see [llms.txt](https://docs.solapayments.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.solapayments.com/mobile-wallets/click-to-pay-hosted-checkout/click-to-pay-sample-code.md).

# Click-To-Pay Sample Code

1. **Let's define a helper object with all necessary components:**

   ```javascript
   const click2payRequest = {
       paymentPrefill: function(){
           const result = {
               merchantRequestId: "Merchant defined request ID",
               currencyCode: "USD",
               description: "...corp Product",
               orderId: "Merchant defined order ID",
               promoCode: "Merchant defined promo code",
               subtotal: roundTo(getAmount(), 2),
               shippingHandling: "2.00",
               tax: "2.00",
               discount: "1.00",
               giftWrap: "2.00",
               misc: "1.00",
               setTotal:  function() {
                   this.total = roundTo(
                       roundToNumber(this.subtotal, 2)
                       + roundToNumber(this.shippingHandling, 2)
                       + roundToNumber(this.tax, 2)
                       + roundToNumber(this.giftWrap, 2)
                       + roundToNumber(this.misc, 2)
                       - roundToNumber(this.discount, 2)
                   , 2);
                   delete this.setTotal;
                   return this;
               },
           }.setTotal();
           logDebug({
               label: "paymentPrefill",
               data: result
           });
           return result;
       },
       authorize: function(payload) {
           return new Promise(function (resolve, reject) {
               var xhr = new XMLHttpRequest();
               xhr.open("POST", "https://<your domain>/<path to handle authorization>");
               xhr.onload = function () {
                   if (this.status >= 200 && this.status < 300) {
                       resolve(xhr.response);
                   } else {
                       reject({
                           status: this.status,
                           statusText: xhr.statusText
                       });
                   }
               };
               xhr.onerror = function () {
                   reject({
                       status: this.status,
                       statusText: xhr.statusText
                   });
               };
               xhr.setRequestHeader("Content-Type", "application/json");
               xhr.send(JSON.stringify(payload));
           });
       },
       paymentSuccess: async function(clickToPayResponse) {
           return new Promise((resolve, reject) => {
               try {
                   const response = await this.authorize(clickToPayResponse);
                   console.log(response);
                   const resp = JSON.parse(response);
                   if (!resp)
                       throw "Invalid response: "+ response;
                   if (resp.xError) {
                       throw resp;
                   }
                   resolve(response);
               } catch (err) {
                   console.error("paymentSuccess error.", JSON.stringify(err));
                   reject(err);
               }
           });
       },a
       paymentCallback: function (clickToPayResponse) {
           click2payRequest.setPayload(clickToPayResponse);
       },
       cpButtonLoaded: function(resp) {
           if (!resp) return;
           if (resp.status === iStatus.success) {
               showHide("click2payContainer", true);
           } else if (resp.reason) {
               console.log(resp.reason);
           }
       },
       setPayload: function (value) {
           document.getElementById('c2p-payload').value = JSON.stringify(value, null, 2);
           showHide("divC2PPayload", value);
       }
   }
   ```
2. **Let’s enable Click-To-Pay for the website:**

   ```javascript
   document.addEventListener("DOMContentLoaded", function(event) { 
       .....
       ckClick2Pay.enableClickToPay({
         environment: c2pEnvironment.sandbox, 
         externalClientId: "<Your externalClientId>",
         click2payContainer: "click2payContainer", 
         onPaymentPrefill: click2payRequest.paymentPrefill,
         onPaymentSuccess: click2payRequest.paymentSuccess,
         onPaymentError: click2payRequest.paymentCallback,
         onPaymentCancel: click2payRequest.paymentCallback,
         onButtonLoaded: click2payRequest.cpButtonLoaded
     });
   .....
   }
   ```
3. To see the full solution please click [**here**](https://docs.solapayments.com/mobile-wallets/click-to-pay-hosted-checkout/click-to-pay-ifields-integration.md)**.**


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.solapayments.com/mobile-wallets/click-to-pay-hosted-checkout/click-to-pay-sample-code.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
