# For Developers

## JavaScript example

```javascript
const express = require('express');
const Web3 = require('web3');
const TruthPayABI = require('./TruthPayABI.json'); // contract ABI
const app = express();
const web3 = new Web3('https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID'); // infura ID
const TruthPayContract = new web3.eth.Contract(TruthPayABI, '0xYOUR_CONTRACT_ADDRESS'); // Truth Pay smart contract

app.get('/pay/:recipient/:amount', async (req, res) => {
  const recipient = req.params.recipient;
  const amount = req.params.amount;
  const accounts = await web3.eth.getAccounts();
  const options = {
    from: accounts[0],
    value: amount
  };
  try {
    const result = await TruthPayContract.methods.pay(recipient).send(options);
    res.json({ txHash: result.transactionHash });
  } catch (error) {
    res.status(500).json({ error: error.message });
  }
});

app.listen(3000, () => {
  console.log('Server listening on port 3000');
});

```

The code above creates an API endpoint that allows users to receive payments on their website through a Solidity-based system called Truth Pay. The API is designed as a GET method, which means it is called via a URL with the necessary parameters.

To achieve this, the code defines a Node.js server that uses the web3.js library to interact with the Ethereum network. It connects to the Truth Pay contract through the contract's address and calls the `pay` function, passing the recipient's address and the amount to be paid as parameters.

The API endpoint is configured to receive the recipient's address and the amount to be paid as parameters in the URL. Once the parameters are received, the server calls the `pay` function on the Truth Pay contract, sending the payment to the specified recipient. The result of the transaction is then returned to the API caller as a response in the form of a transaction hash.

In summary, this code serves as a way to facilitate payments for website users through the Truth Pay system, enabling seamless transactions and payments using the Binance network (BSC).

## PHP + MySQLi

```php
<?php

require 'vendor/autoload.php';

use Web3\Web3;
use Web3\Contract;


$web3 = new Web3('http://localhost:8545');


$contract_address = '0x123456789abcdef';
$contract_abi = '[{"constant":false,"inputs":[{"name":"recipient","type":"address"},{"name":"amount","type":"uint256"}],"name":"pay","outputs":[],"payable":true,"stateMutability":"payable","type":"function"}]';


$truth_pay = new Contract($web3->provider, $contract_abi);
$truth_pay->at($contract_address);


$recipient = $_GET['recipient'];
$amount = $_GET['amount'];


$result = $truth_pay->send('pay', $recipient, $amount);


$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";

$conn = new mysqli($servername, $username, $password, $dbname);


if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}


$sql = "INSERT INTO transactions (recipient, amount, transaction_hash) VALUES ('$recipient', '$amount', '$result')";
if ($conn->query($sql) === TRUE) {
    echo "Transaction saved successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}


$conn->close();

?>

```

## React Js

```jsx
import React, { useState } from 'react';
import Web3 from 'web3';
import TruthPayContract from './TruthPayContract.json';

function App() {
  const [web3, setWeb3] = useState(null);
  const [TruthPay, setTruthPay] = useState(null);
  const [recipient, setRecipient] = useState('');
  const [amount, setAmount] = useState(0);
  const [transactionHash, setTransactionHash] = useState('');


  const connectToWeb3 = async () => {

    if (window.ethereum) {
      try {
        
        const web3Instance = new Web3(window.ethereum);
        await window.ethereum.enable();
        setWeb3(web3Instance);


        const truthPayContract = new web3Instance.eth.Contract(TruthPayContract.abi, TruthPayContract.address);
        setTruthPay(TruthPayContract);
      } catch (error) {
        console.error(error);
      }
    } else {
      console.error('Please install MetaMask to use this app');
    }
  };


  const sendPayment = async () => {
    try {

      const result = await truthPay.methods.pay(recipient, amount).send({ from: web3.eth.defaultAccount, value: amount });


      setTransactionHash(result.transactionHash);
    } catch (error) {
      console.error(error);
    }
  };

  return (
    <div>
      <h1>Truth Pay Demo</h1>
      {web3 && truthPay ? (
        <div>
          <label>Recipient Address:</label>
          <input type="text" onChange={(event) => setRecipient(event.target.value)} />

          <label>Amount (ETH):</label>
          <input type="number" onChange={(event) => setAmount(event.target.value)} />

          <button onClick={sendPayment}>Send Payment</button>

          {transactionHash && (
            <div>
              <p>Transaction Hash: {transactionHash}</p>
              <a href={`https://rinkeby.etherscan.io/tx/${transactionHash}`} target="_blank" rel="noreferrer">
                View on Etherscan
              </a>
            </div>
          )}
        </div>
      ) : (
        <button onClick={connectToWeb3}>Connect to Web3</button>
      )}
    </div>
  );
}

export default App;

```

The Truth Pay system is currently in the testing phase and its beta version has not yet been launched. Therefore, it is not recommended to use the system to receive payments for products on websites until the beta version is released.

During the testing phase, the system may undergo significant changes, and it is not guaranteed that all functionalities are fully developed and tested. In addition, the security and stability of the system have not yet been completely validated in a production environment.

Therefore, it is important to wait for the official launch of the Truth Pay beta version to use its functionalities in production and ensure a safe and reliable experience for its users.


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://meus-books.gitbook.io/truth-pay-whitepaper/use-cases/for-developers.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
