Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. pragma solidity >=0.4.22 <0.7.0;
  2.  
  3. /**
  4. * @title Payments. Management of payments
  5. * @dev Provide functions for payment and withdraw of funds. Stores payments.
  6. */
  7. contract Payments {
  8. address payable owner;
  9.  
  10. struct Payment {
  11. string id;
  12. uint256 amount;
  13. uint256 date;
  14. }
  15.  
  16. // Log of payments
  17. mapping(address=>Payment[]) private payments;
  18.  
  19. // Event to notify payments
  20. event Pay(address, string, uint);
  21.  
  22. constructor() public {
  23. owner = msg.sender;
  24. }
  25.  
  26. // Optional. Fallback function to receive funds
  27. function() external payable {
  28. require (msg.data.length == 0, 'The called function does not exist');
  29. }
  30.  
  31. /**
  32. * @dev `pay` Payment in wei
  33. * Emits `Pay` on success with payer account, purchase reference and amount.
  34. * @param id Reference of the purchase
  35. * @param value Amount in wei of the payment
  36. */
  37. function pay(string memory id, uint value) public payable {
  38. require(msg.value == value, 'The payment does not match the value of the transaction');
  39. payments[msg.sender].push(Payment(id, msg.value, block.timestamp));
  40. emit Pay(msg.sender, id, msg.value);
  41. }
  42.  
  43. /**
  44. * @dev `withdraw` Withdraw funds to the owner of the contract
  45. */
  46. function withdraw() public payable {
  47. require(msg.sender == owner, 'Only owner can withdraw funds');
  48. owner.transfer(address(this).balance);
  49. }
  50.  
  51. /**
  52. * @dev `paymentsOf` Number of payments made by an account
  53. * @param buyer Account or address
  54. * @return number of payments
  55. */
  56. function paymentsOf(address buyer) public view returns (uint) {
  57. return payments[buyer].length;
  58. }
  59.  
  60. /**
  61. * @dev `paymentOfAt` Returns the detail of a payment of an account
  62. * @param buyer Account or addres
  63. * @param index Index of the payment
  64. * @return {0: "Purchase reference", 1: "Payment amount", 2: "Payment date"}
  65. */
  66. function paymentOfAt(address buyer, uint256 index) public view returns (string memory, uint256 amount, uint256 date) {
  67. Payment[] memory pays = payments[buyer];
  68. require(pays.length > index, "Payment does not exist");
  69. Payment memory payment = pays[index];
  70. return (payment.id, payment.amount, payment.date);
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement