Skip to content

Instantly share code, notes, and snippets.

@Developerayo
Last active January 19, 2024 10:19
Show Gist options
  • Save Developerayo/b08b1fb8f13e4543d30cded3bf83d04c to your computer and use it in GitHub Desktop.
Save Developerayo/b08b1fb8f13e4543d30cded3bf83d04c to your computer and use it in GitHub Desktop.
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
contract INTMAXWalletPayment is ReentrancyGuard {
struct Invoice {
uint256 amountDue;
address currency; // ERC20 token address
Network network;
uint256 expirationTime;
string memo;
bool paid;
}
using SafeERC20 for IERC20;
enum Network { Arbitrum, Polygon }
address public owner;
mapping(address => bool) public registeredBusinesses;
mapping(bytes32 => Invoice) public invoices;
// log actions
event BusinessRegistered(address business);
event InvoiceCreated(bytes32 indexed ref, uint256 amount, address currency, Network network, uint256 expirationTime, string memo);
event PaymentMade(bytes32 indexed ref, address payer);
constructor() {
owner = msg.sender;
}
// restrict to the owner of the contract
modifier onlyOwner() {
require(msg.sender == owner, "Only owner can perform this action");
_;
}
function registerBusiness(address business) external onlyOwner {
registeredBusinesses[business] = true;
emit BusinessRegistered(business);
}
function createInvoice(bytes32 ref, uint256 amount, address currency, Network network, uint256 validDuration, string memory memo) external {
require(registeredBusinesses[msg.sender], "Only registered businesses can create invoices");
Invoice memory newInvoice = Invoice(amount, currency, network, block.timestamp + validDuration, memo, false);
invoices[ref] = newInvoice;
emit InvoiceCreated(ref, amount, currency, network, newInvoice.expirationTime, memo);
}
function payInvoice(bytes32 ref, Network network) external nonReentrant {
Invoice storage invoice = invoices[ref];
require(!invoice.paid, "Invoice already paid");
require(block.timestamp <= invoice.expirationTime, "Invoice expired");
require(invoice.network == network, "Incorrect network");
IERC20 token = IERC20(invoice.currency);
token.safeTransferFrom(msg.sender, owner, invoice.amountDue);
// Mark invoice as paid
invoice.paid = true;
emit PaymentMade(ref, msg.sender);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment