需要先安装插件MetaMask
MetaMask自动将Web3库插入浏览器,下面是判别安装与否的代码
render(){ if (web3){ return (<metamaskCheckout/>); } else { return (<checkout/>); }; }
支付的代码
sendTxn(web3, ether, nonce) { return (dispatch) => { let user = web3.eth.accounts[0]; let wei = ether * Math.pow(10, 18); let gasPrice = 3 * Math.pow(10, 9); let gasLimit = 30000; let rawTx = { nonce: `0x${nonce.toString(16)}`, gasPrice: `0x${gasPrice.toString(16)}`, gasLimit: `0x${gasLimit.toString(16)}`, from: user, to: this.owner_address, value: `0x${wei.toString(16)}` }; web3.eth.sendTransaction(rawTx, (err, txHash) => { if (err) { console.log('Error sending txn:', err); } // Save the transaction hash yourAPI.saveOrder(rawTx, txHash); }) } }