Guest User

Untitled

a guest
Dec 15th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. /** @dev Buy a product.
  2. * param orderValues:
  3. [0] DIN The Decentralized Identification Number (DIN) of the product to buy.
  4. [1] quantity The quantity to buy.
  5. [2] totalPrice Total price of the purchase, in wei.
  6. [3] priceValidUntil Expiration time (Unix timestamp).
  7. [4] affiliateReward Affiliate reward (optional), denominated in base units of Market Token (MARK).
  8. [5] loyaltyReward Loyalty reward (optional), denominated in loyaltyToken.
  9. * param orderAddresses:
  10. [0] merchant Address of the merchant to pay.
  11. [1] affiliate Address of the affiliate. Use null address (0x0...) for no affiliate.
  12. [2] loyaltyToken Address of the Loyalty Token. Use null address for no loyalty reward.
  13. * @param nonceHash The hash of a nonce generated by a client. The nonce can be used as a proof of purchase.
  14. * @param v ECDSA signature parameter v.
  15. * @param r ECDSA signature parameter r.
  16. * @param s ECDSA signature parameter s.
  17. * @return orderID A unique ID for the order.
  18. */
  19. function buy(
  20. uint256[6] orderValues,
  21. address[3] orderAddresses,
  22. bytes32 nonceHash,
  23. uint8 v,
  24. bytes32 r,
  25. bytes32 s
  26. )
  27. payable
  28. public
  29. returns (uint256)
  30. {
  31. address merchant = orderAddresses[0];
  32.  
  33. Order memory order = Order({
  34. DIN: orderValues[0],
  35. quantity: orderValues[1],
  36. totalPrice: orderValues[2],
  37. priceValidUntil: orderValues[3],
  38. affiliateReward: orderValues[4],
  39. affiliate: orderAddresses[1],
  40. loyaltyReward: orderValues[5],
  41. loyaltyToken: orderAddresses[2],
  42. merchant: merchant,
  43. owner: registry.owner(orderValues[0]) // Get the DIN owner address from the DIN registry.
  44. });
  45.  
  46. if (isValidOrder(order, v, r, s) == false) {
  47. // Return Ether to buyer.
  48. msg.sender.transfer(msg.value);
  49. return 0;
  50. }
  51.  
  52. // Transfer a mix of Ether and loyalty tokens (if applicable) from buyer to merchant.
  53. payMerchant(merchant, order.totalPrice, order.loyaltyToken);
  54.  
  55. // Transfer affiliate reward from DIN owner to affiliate.
  56. if (order.affiliateReward > 0 && order.affiliate != address(0x0)) {
  57. rewards.sendAffiliateReward(order.owner, order.affiliate, order.affiliateReward);
  58. }
  59.  
  60. // Transfer loyalty reward from DIN owner to buyer.
  61. if (order.loyaltyReward > 0 && order.loyaltyToken != address(0x0)) {
  62. rewards.sendLoyaltyReward(order.loyaltyToken, order.owner, msg.sender, order.loyaltyReward);
  63. }
  64.  
  65. // Create a new order
  66. uint256 orderID = orders.createOrder(
  67. nonceHash,
  68. msg.sender, // Buyer
  69. merchant,
  70. order.DIN,
  71. order.quantity,
  72. order.totalPrice
  73. );
  74.  
  75. // Return the unique order ID.
  76. return orderID;
  77. }
Add Comment
Please, Sign In to add comment