Pay a request
You have learned how to create a payment request in the previous sections. We will now explain to you how to pay it.
This section is useful if:
- You plan to embed request payment features like a Pay button
- You want to test the payment stage of requests you create
The payment of a request depends on its payment network:
- Address-based payment networks (available for BTC and ERC20) don't have any specific requirement: any payment sent to the specified address will be considered a payment of this request. Never re-use an address!
- Input data payment networks (ETH only) require you to specify the Payment Reference in the data of the transaction.
- Proxy payment networks (ETH and ERC20) require you to call a smart contract method that forwards the payment.
To simplify the payment procedures of the various payment networks, you can use the dedicated library, @requestnetwork/payment-processor. On top of calculating the Payment Reference and handling the transaction for you, it provides a few utilities to ensure the user meets all requirements to pay the request (enough funds for example)
npm install @requestnetwork/payment-processor
# or
yarn add @requestnetwork/payment-processor
ETH request
import { hasSufficientFunds, payRequest } from '@requestnetwork/payment-processor';
const requestNetwork = new RequestNetwork();
// usually, the connected account.
const account = '[WALLET_ADDRESS]';
const request = await requestNetwork.fromRequestId('[REQUEST_ID]');
const requestData = request.getData();
if (!(await hasSufficientFunds(requestData, account))) {
throw new Error('You do not have enough funds to pay this request');
}
const tx = await payRequest(requestData);
await tx.wait(1);
For Goerli tests, the request currency should be
ETH-goerli
.ERC20 request
import {
approveErc20,
hasErc20Approval,
hasSufficientFunds,
payRequest,
} from '@requestnetwork/payment-processor';
// usually, the connected account.
const account = '[WALLET_ADDRESS]';
const requestNetwork = new RequestNetwork();
const request = await requestNetwork.fromRequestId('[REQUEST_ID]');
const requestData = request.getData();
if (!(await hasSufficientFunds(requestData, account))) {
throw new Error('You do not have enough funds to pay this request');
}
if (!(await hasErc20Approval(requestData, account))) {
const approvalTx = await approveErc20(requestData);
await approvalTx.wait(1);
}
const tx = await payRequest(requestData);
await tx.wait(1);
Last modified 4mo ago