-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpayment_service.sol
95 lines (76 loc) · 3.41 KB
/
payment_service.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
pragma solidity 0.5.4;
import {Content} from "./content.sol";
/* -- Revision history --
PaymentService20190318102800ML: First versioned released, migrated to 0.4.24, made a Content object
*/
contract PaymentService is Content {
bytes32 public version ="PaymentService20190318102800ML"; //class name (max 16), date YYYYMMDD, time HHMMSS and Developer initials XX
struct RedeemRequest {
address id; // client address requesting
string redeemCurrency;
uint256 numTokens; // number of token to be redeemed
string payTo;
string nonce;
bool valid; // to distinguish non-existing objects
}
RedeemRequest[] redeemRequests;
uint256 redeemRequestsLength = 0;
string tokenCurrency = "USD";
uint256 tokenValue = 1;
address public payerAddress;
event RedeemTokenRequest(uint256 numtokens, string pay_to, string nonce);
event RedeemTokenExecuted(string currency, uint256 value, string payment_proof, string nonce);
event SetTokenValue(string currency, uint256 value);
function redeemTokenRequest(string memory payment_account, string memory tx_nonce) public payable returns (uint) {
//If the request is not backed by a balance
if (msg.value == 0) {
return 1;
}
RedeemRequest memory request = RedeemRequest(msg.sender, "USD", msg.value, payment_account, tx_nonce, true);
if (redeemRequestsLength < redeemRequests.length) {
redeemRequests[redeemRequestsLength] = request;
} else {
redeemRequests.push(request);
}
redeemRequestsLength ++;
emit RedeemTokenRequest(msg.value, payment_account, tx_nonce);
return 0;
}
function getPendingRedeemRequest() public view returns ( address, string memory, uint256, string memory, string memory) {
if (redeemRequestsLength == 0) {
return (address(0), "", 0, "", "");
}
RedeemRequest memory req = redeemRequests[0];
return (req.id, req.redeemCurrency, req.numTokens, req.payTo, req.nonce);
}
function redeemTokenExecuted(string memory currency, uint256 value, string memory payment_proof, string memory tx_nonce) public returns (uint) {
if ((msg.sender != creator) && (msg.sender != payerAddress)) {
return 3;
}
if (keccak256(abi.encodePacked(redeemRequests[0].nonce)) == keccak256(abi.encodePacked(tx_nonce))) {
delete redeemRequests[0];
redeemRequestsLength --;
if (redeemRequestsLength > 0) {
redeemRequests[0] = redeemRequests[redeemRequestsLength];
delete redeemRequests[redeemRequestsLength];
}
emit RedeemTokenExecuted(currency, value, payment_proof, tx_nonce);
return 0;
}
return 1;
}
function redeemDbg(uint256 idx) public view returns (uint256, uint256, string memory) {
return (redeemRequests.length, redeemRequestsLength, redeemRequests[idx].nonce);
}
function setPayerAdress(address payer_address) public onlyOwner {
payerAddress = payer_address;
}
function setTokenValue(string memory currency, uint256 value) public onlyOwner {
tokenCurrency = currency;
tokenValue = value;
emit SetTokenValue(currency, value);
}
function getTokenValue() public view returns(string memory, uint256) {
return (tokenCurrency, tokenValue);
}
}