Last active
November 9, 2018 16:35
-
-
Save yhuag/45cd854518d747b3ee0dabe0816ea94b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
contract MerchantPayable { | |
enum Role {Client, Merchant, Owner} | |
mapping (address => Role) public addressToRole; | |
mapping (bytes32 => bool) public paymentSettled; | |
mapping (bytes32 => bool) public paymentIssued; | |
uint256 nonce; | |
event PaymentIssued( | |
address client, | |
address merchant, | |
uint256 value, | |
uint256 timeStamp, | |
bytes32 paymentHash, | |
uint256 nonce | |
); | |
event PaymentSettled(bytes32 paymentHash); | |
modifier onlyClient { | |
require(addressToRole[msg.sender] == Role.Client); | |
_; | |
} | |
modifier onlyMerchant { | |
require(addressToRole[msg.sender] == Role.Merchant); | |
_; | |
} | |
modifier paymentHasNotBeenSettled(bytes32 paymentHash) { | |
require(paymentSettled[paymentHash] == false); | |
_; | |
} | |
modifier paymentHasBeenIssued(bytes32 paymentHash) { | |
require(paymentIssued[paymentHash] == true); | |
_; | |
} | |
function pay | |
( | |
address _merchant, | |
uint256 _value | |
) | |
onlyClient | |
public | |
{ | |
// the actual token goes to the owner | |
token.transfer(owner, _value); | |
// increase nonce | |
nonce = nonce.add(1); | |
// generate a hash as the unique identifier for this payment | |
// @notice may NOT be unique. Can be somehow forgeable!!! | |
bytes32 _paymentHash = keccak256(abi.encoded(msg.sender, _merchant, _value, now, nonce)); | |
// all the requirements | |
require(paymentIssued[_paymentHash] == false); | |
require(paymentSettled[_paymentHash] == false); | |
// set payment to issued | |
paymentIssued[_paymentHash] = true; | |
// record the payment as settlable receipt for the merchant | |
emit PaymentIssued(msg.sender, _merchant, _value, now, _paymentHash, nonce); | |
} | |
function settle | |
( | |
bytes32 _paymentHash | |
) | |
onlyMerchant | |
paymentHasNotBeenSettled(_paymentHash) | |
paymentHasBeenIssued(_paymentHash) | |
public | |
{ | |
// set payment to settled | |
paymentHasBeenSettled[_paymentHash] = true; | |
// settle the payment with the exact unique identifier | |
emit PaymentSettled(_paymentHash); | |
} | |
} |
TODO:
- Register Role for a user
- Instantiate ERC20 token for payment
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Status of a Payment: None --> Issued --> Redeemed