Skip to content

Instantly share code, notes, and snippets.

@yhuag
Last active November 9, 2018 16:35
Show Gist options
  • Save yhuag/45cd854518d747b3ee0dabe0816ea94b to your computer and use it in GitHub Desktop.
Save yhuag/45cd854518d747b3ee0dabe0816ea94b to your computer and use it in GitHub Desktop.
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);
}
}
@yhuag
Copy link
Author

yhuag commented Nov 9, 2018

Status of a Payment: None --> Issued --> Redeemed

@yhuag
Copy link
Author

yhuag commented Nov 9, 2018

TODO:

  1. Register Role for a user
  2. Instantiate ERC20 token for payment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment