Payments ( pre set value) in solidity

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;
   }

 }

You would just add a require statement in your pay function

require(msg.value == movieInfo[_title].price, "Invalid price");

(if the movie price is in wei)

1 Like

if i use struct like this what should be my next line

Move storage newMove= movieInfo[_title];

I will need a bit more context

same problem instead of your lines i want to use Struct

hence : Move storage newMove= movieInfo[_title];

Why do you want to use a struct? Can you give me more code and explenantion on what exactly you are trying to achieve? Is the price set in the Move struct or what?

yes. the price is set in struct. your solution is working fine i just want to use struct for further learning. for reference plz go through the entire question once again.

you should be able to do

newMove.year;
newMove.price;

The code I provided should be working just fine, but if you really
want to set a new variable for newMove, instead of using that you would do

require(msg.value == newMove.price, "Invalid price");

I hope I understood what you want to do correctly

1 Like