Creating a Smart Contract for Subscription Payments in Ethereum Solidity
Solidity is the programming language for writing smart contracts on the Ethereum blockchain. With its simple syntax and object-oriented design, you can create robust, self-executing contracts that run exactly as programmed. In this article, you will learn how to create a smart contract for subscription payments in Solidity.
Setting up the Contract Structure
To start, you need to create a contract file using a Solidity compiler. This will be the foundation for your subscription payment system. Begin by defining the contract and its variables:
pragma solidity >=0.7.0 <0.8.0;
contract SubscriptionPayment {
uint256 public price;
uint256 public frequency;
mapping (address => uint256) public balances;
address public owner;
constructor() public {
owner = msg.sender;
price = 1 ether;
frequency = 30 days;
}
}
Try it in Remix
In this code, you declare a contract called SubscriptionPayment
. The contract has four public variables: price
, frequency
, balances
, and owner
. The price
variable specifies the cost of the subscription in wei, and the frequency
variable specifies the time between payments. The balances
mapping stores the balance of each subscriber, and the owner
variable stores the address of the contract creator.
Accepting Subscription Payments
Next, you need to write a function that accepts payments from subscribers. To do this, you will use the payable
keyword, which allows the contract to receive ether.
function subscribe() public payable {
require(msg.value == price, "Incorrect payment amount");
balances[msg.sender] += msg.value;
}
Try it in Remix
The subscribe()
function checks that the payment amount matches the price specified in the price
variable. If the payment is correct, the function adds the payment to the subscriber’s balance.
Automating Subscription Payments
Now that you have a way to accept payments, you need to automate the payment process. To do this, you will use the schedule
function, which executes a function at a specific time interval.
function schedulePayment() public {
require(msg.sender == owner, "Only the owner can schedule payments");
require(now + frequency <= block.timestamp, "Payments already scheduled");
schedule(frequency, this, "paySubscriber");
}
function paySubscriber() public {
for (address subscriber : balances) {
require(subscriber.send(price), "Payment failed");
}
}
Try it in Remix
The schedulePayment()
function checks that the person calling the function is the owner and that no payments have already been scheduled. If these checks pass, the function schedules the paySubscriber()
function to run every frequency
days. The paySubscriber()
function iterates over all subscribers and sends them the price
in ether.
Refunding Payments
Finally, you need a way for subscribers to get a refund if they cancel their subscription. To do this, you will write a refund()
function.
function refund() public {
require(balances[msg.sender] > 0, "No balance to refund
Try it in Remix
Resources
Blockchain Networks
Below is a list of EVM compatible Mainnet and Testnet blockchain networks. Each link contains network configuration, links to multiple faucets for test ETH and tokens, bridge details, and technical resources for each blockchain. Basically everything you need to test and deploy smart contracts or decentralized applications on each chain. For a list of popular Ethereum forums and chat applications click here.
Ethereum test network configuration and test ETH faucet information | |
Optimistic Ethereum Mainnet and Testnet configuration, bridge details, etc. | |
Polygon network Mainnet and Testnet configuration, faucets for test MATIC tokens, bridge details, etc. | |
Binance Smart Chain Mainnet and Testnet configuration, faucets for test BNB tokens, bridge details, etc. | |
Fanton networt Mainnet and Testnet configuration, faucets for test FTM tokens, bridge details, etc. | |
Kucoin Chain Mainnet and Testnet configuration, faucets for test KCS tokens, bridge details, etc. |
Web3 Software Libraries
You can use the following libraries to interact with an EVM compatible blockchain.
- Python: Web3.py A Python library for interacting with Ethereum. Web3.py examples
- Js: Web3.js Ethereum JavaScript API
- Java: Web3j Web3 Java Ethereum Ðapp API
- PHP: Web3.php A php interface for interacting with the Ethereum blockchain and ecosystem.