How to accept Cash App payments on your Node.js web server without Cash App Pay!
Tags: node, webdev, javascript
Published: 2024-06-26 00:35:20

Hello there! Welcome to this concise Node.js tutorial where I will guide you through integrating Cash App payments into your Node.js website without relying on Stripe, Cash App Pay, or any other payment processing platforms. The best part? No SSN or ID verification is required to handle the payments!

**To begin accepting Cash App payments, start by launching Cash App on your device and locating the profile button in the top-right corner of the screen. Proceed by scrolling down until you spot the Notifications tab, and ensure that email notifications are enabled. This step holds significant importance since we will utilize the IMAP library to monitor email notifications that are sent by Cash App.**

Now, let’s move on to the coding part. Create a new file and name it cashapp.js
![Directory file tree](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ifmb2exarv3wy962n0qk.png)
Next, we’ll need to get our mail credentials. In this tutorial, I’ll be using Gmail since that’s the email I use for my Cash App account. If you have 2FA (two-factor authentication) enabled on your Google account, you will need to generate an app password to proceed. Now, let’s import the required libraries and open our inbox.
```js
const Imap = require('imap'); //Include IMAP to check emails
const simpleParser = require('mailparser').simpleParser; //Include mail parser to parse email bodies

// IMAP configuration
const imapConfig = {
  user: process.env.email, //My email (make sure to replace it)
  password: process.env.password, //My password (make sure to replace it)
  host: 'imap.gmail.com', //The IMAP host (gmail)
  port: 993, //IMAP port
  tls: true, //Enable TLS
 tlsOptions: { rejectUnauthorized: false }, //Allow authentication without unauthorization via cert
};

const imap = new Imap(imapConfig); //Authenticate with our credentials

//Open up the inbox to prepare for reading the emails
function openInbox(cb) {
  imap.openBox('Inbox', true, cb);
}
```

After you include your email and password as an environment variable, it’s time to search through all the emails sent by Cash App (cash@square.com). In this tutorial, we will verify if the transaction sent by the user is the one we’re looking for by requiring our customers to enter a special code in the “For” field when making payments on Cash App.
![Secret code example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ngb5l9akz1zsq4wnt29k.png)
We can replicate the following by adding this line of code to cashapp.js:
```js
const verifyPayment = (cashTag, specialCode, amount) => {
  return new Promise((resolve, reject) => {
    imap.search([['FROM', 'cash@square.com'], ['SUBJECT', `sent you ${'$' + amount} for ${specialCode}`], ['TEXT', `Payment from ${cashTag}`]], (err, results) => {
      if (err) {
        reject(err);
        return;
      }
      if (results.length === 0) {
        resolve(false);
        return;
      }

      const latestEmail = results[0];
      const f = imap.fetch(latestEmail, { bodies: '' });
      f.on('message', (msg) => {
        msg.on('body', (stream, info) => {
          let buffer = '';

          stream.on('data', (chunk) => {
            buffer += chunk.toString('utf8');
          });

          stream.on('end', () => {
            simpleParser(buffer, (err, parsedEmail) => {
              if (err) {
                reject(err);
                return;
              }

              console.log(`[SUCCESS] A transaction from ${cashTag} has been found with the subject: ${parsedEmail.subject}`); //Remove this if you wouldn't like to log the result in the console
              resolve(true);
            });
          });
        });
      });
    });
  });
};

imap.once('ready', () => {
  openInbox((err, box) => {
    if (err) throw err;
  });
});

imap.connect();
module.exports = verifyPayment;
```
In the provided code, we are essentially searching for the most recent email from Cash App with the subject line “sent you $__ for ____.” Moreover, it verifies if the email contains the phrase “Payment from $CashTag” (where “CashTag” represents the customer’s actual Cash Tag). If the email is located and its body is parsed successfully, the code returns true. Conversely, if the email is not found, it returns false. Please note that there is an additional line in the code that logs the result if successful. However, feel free to remove it if desired.

Now that we‘ve completed the necessary steps, it’s time to incorporate the functionality into our existing code. We can utilize it in the following manner:
```js
const verifyPayment = require('./cashapp.js');

const result = await verifyPayment('$Brian', 'abc123', 5); //Replace $Brian with the CashTag, abc123 with the secret code, and 5 with the amount that the user is paying
console.log(result);
```
**IMPORTANT: Please be aware that the function may fail initially when executing the code since it takes approximately 3 seconds for IMAP to open the inbox.**

In conclusion, this Node.js tutorial has provided a comprehensive guide on seamlessly integrating Cash App payments into your Node.js website without the need for external payment processing platforms like Stripe or Cash App Pay. The notable advantage is that you can handle payments without requiring SSN or ID verification. By following the steps outlined in this tutorial, you can easily incorporate Cash App payments into your Node.js website, offering a convenient and efficient payment solution for your users. Thanks for reading, and happy coding!