payments ( pre set value) in solidity

Viewed 44

i am developing a Movie renting smartcontract. Where the owner can add new movies, clients can search movies and pay for the movies they select.Adding and searching is working to my liking.

problem: i want to develop the pay function as such- where it takes one argument( the title of the movie) and clients has to pay the exact amount set by the owner, he can not pay less then the price.

for example: owner add a movie: title-titanic,price-10 eth. when client use the pay function he put the title titanic and pay 10 eth. if he tries to pay less or more the transaction will not be successful.

here is the code

    pragma solidity 0.8.7;

// SPDX-License-Identifier: MIT   

contract Movie{
   address public owner;  

 struct Move{

   uint year;

   uint price;
}
 
   mapping (string => Move ) public movieInfo;
   mapping(uint => Move) amount;

   constructor()  payable{
      owner= msg.sender;
   }

   function addMovie(string memory _title, uint _year, uint _price) public {
      require( msg.sender==owner);

      movieInfo[_title]= Move(_year, _price);
   }

   

function pay(string memory _title) public payable{

   }
   function totalFunds() public view returns(uint){
      return address(this).balance;
   }

 }
1 Answers

Don't think that this kind of stuff is good to store into the blockchain ATM, due to the highly cost of storing.

In case you would like to have it:

mapping(string -> address[]) paidUsers;
function pay(string memory _title) public payable {
   require(msg.value == movieInfo[_title].price, "Invalid price for the film");
   
   paidUsers[_title].push(msg.sender);
}
Related