TL;DR call payable methods on a different contract
As Michael C says in the previous comment (probably meant to be an answer):
buy and subscribe look like you mean them to be different methods in the same contract. However, the value has already arrived to your contract through the payable singleTransaction method, so there's no way to split up the value now: all of the value will go into the current contract's balance if the tx succeeds.
You can call payable methods on a different contract, and then send half the value to each one, using the syntax that you used above, and following the advice in this other answer
The key line below specifies which contract you are expecting to call this payable method on
buyContract.buyMethod.value(halfValue)(BUY_ID);
The full source file is:
import "./SafeMath.sol";
contract Constants {
uint8 public constant BUY_ID = 1;
uint8 public constant SUBSCRIBE_ID = 2;
}
contract BuyContract is Constants {
uint256 public lastAmount;
function buyMethod(uint8 _buyArg) payable public {
require(_buyArg == BUY_ID);
lastAmount = msg.value;
}
}
contract SubscribeContract is Constants {
uint256 public lastAmount;
function subscribeMethod(uint8 _subscribeArg) payable public {
require(_subscribeArg == SUBSCRIBE_ID);
lastAmount = msg.value;
}
}
contract MyContract is Constants {
using SafeMath for uint256;
function singleTransaction(
address _buyAddress,
address _subscribeAddress
) payable public {
BuyContract buyContract = BuyContract(_buyAddress);
SubscribeContract subContract = SubscribeContract(_subscribeAddress);
uint256 halfValue = msg.value.div(2);
buyContract.buyMethod.value(halfValue)(BUY_ID);
subContract.subscribeMethod.value(halfValue)(SUBSCRIBE_ID);
}
}
Here's the full example in this gist:
https://gist.github.com/cryptogoth/9bb1caa9193adb73dc640ec8f979963e
buyandsubscribe? It looks from the error like maybe those are functions in the same contract. Internal functions can't be payable, so you can't use that syntax with them. Why not just pass the amount as a parameter? (foo(msg.value / 2, ...)) – user19510 Sep 27 '19 at 22:40