Whenever a smart contract receives ether via a transfer, it executes the fallback function, here you can access the msg.value and know how much ether in 'Wei' the msg.sender sent. If you have a token rate you can issue your tokens depending on the amount of Wei sent.
Follwing fallback function might be of help-
//fallback function can be used to buy tokens
function () external payable {
buyTokens(msg.sender);
}
// low level token purchase function
function buyTokens(address beneficiary) public payable {
require(beneficiary != address(0));
require(validPurchase());
uint256 weiAmount = msg.value;
// calculate token amount to be created
uint256 tokens = weiAmount.mul(rate);
// update state
weiRaised = weiRaised.add(weiAmount);
token.mint(beneficiary, tokens);
TokenPurchase(msg.sender, beneficiary, weiAmount, tokens);
forwardFunds();
}
Or you can also create a payable function that allows your contract to get paid and send them to the people.
A simple code is given below:
function() public payable {
uint toMint = msg.value/price;
totalSupply += toMint;
balances[msg.sender] += toMint;
emit Transfer(0, msg.sender, toMint);
}