Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** @dev Buy a product.
- * param orderValues:
- [0] DIN The Decentralized Identification Number (DIN) of the product to buy.
- [1] quantity The quantity to buy.
- [2] totalPrice Total price of the purchase, in wei.
- [3] priceValidUntil Expiration time (Unix timestamp).
- [4] affiliateReward Affiliate reward (optional), denominated in base units of Market Token (MARK).
- [5] loyaltyReward Loyalty reward (optional), denominated in loyaltyToken.
- * param orderAddresses:
- [0] merchant Address of the merchant to pay.
- [1] affiliate Address of the affiliate. Use null address (0x0...) for no affiliate.
- [2] loyaltyToken Address of the Loyalty Token. Use null address for no loyalty reward.
- * @param nonceHash The hash of a nonce generated by a client. The nonce can be used as a proof of purchase.
- * @param v ECDSA signature parameter v.
- * @param r ECDSA signature parameter r.
- * @param s ECDSA signature parameter s.
- * @return orderID A unique ID for the order.
- */
- function buy(
- uint256[6] orderValues,
- address[3] orderAddresses,
- bytes32 nonceHash,
- uint8 v,
- bytes32 r,
- bytes32 s
- )
- payable
- public
- returns (uint256)
- {
- address merchant = orderAddresses[0];
- Order memory order = Order({
- DIN: orderValues[0],
- quantity: orderValues[1],
- totalPrice: orderValues[2],
- priceValidUntil: orderValues[3],
- affiliateReward: orderValues[4],
- affiliate: orderAddresses[1],
- loyaltyReward: orderValues[5],
- loyaltyToken: orderAddresses[2],
- merchant: merchant,
- owner: registry.owner(orderValues[0]) // Get the DIN owner address from the DIN registry.
- });
- if (isValidOrder(order, v, r, s) == false) {
- // Return Ether to buyer.
- msg.sender.transfer(msg.value);
- return 0;
- }
- // Transfer a mix of Ether and loyalty tokens (if applicable) from buyer to merchant.
- payMerchant(merchant, order.totalPrice, order.loyaltyToken);
- // Transfer affiliate reward from DIN owner to affiliate.
- if (order.affiliateReward > 0 && order.affiliate != address(0x0)) {
- rewards.sendAffiliateReward(order.owner, order.affiliate, order.affiliateReward);
- }
- // Transfer loyalty reward from DIN owner to buyer.
- if (order.loyaltyReward > 0 && order.loyaltyToken != address(0x0)) {
- rewards.sendLoyaltyReward(order.loyaltyToken, order.owner, msg.sender, order.loyaltyReward);
- }
- // Create a new order
- uint256 orderID = orders.createOrder(
- nonceHash,
- msg.sender, // Buyer
- merchant,
- order.DIN,
- order.quantity,
- order.totalPrice
- );
- // Return the unique order ID.
- return orderID;
- }
Add Comment
Please, Sign In to add comment