When I first considered implementing payments through cryptocurrencies, I looked at available solutions like Stripe. I think the problem with Stripe is that it only allows us merchant accounts for Bitcoin payments, so that’s not an option for me. In the Ethereum world, it looks even worse. There are some newer services, but they all want to share the cake.
So what do we need to build an Ethereum payment system from scratch?
- A Web server running PHP.
- There is at least one Parity node in the RPC-enabled private network.
- A virtual address generator on a web server, such as Invanity-eth.
So how does it work?
- Calculate the price of ETH using the current price in coinbase or Kraken API.
- Use the virtual generator to generate the address pair and encrypt or transfer the private key to another server.
- Display the generated address to the customer and check the address every few seconds if payment is received.
There seems to be no problem in theory, so let’s build it.
Step 1: Set up the server
We will use vanity-eth in NodeJS to generate the address.
NPM install - g [email protected]"
Copy the code
After installing Intele-eth on Windows:
You also need some Etherum nodes. I’m using Parity because it’s fast and reliable.
Start it with these parameters, but do not expose the nodes directly to the Internet, leaving them behind the firewall without port forwarding.
Parity, jsonrpc - interface 0.0.0.0 - jsonrpc - hosts ="all" --auto-update=all --jsonrpc-cors null
Copy the code
Parity log that completes synchronization:
For faster deployment, you can use the Parity Docker container. You can also save the data so that you don’t have to resynchronize each time you remake the container.
Step 2: Write the payment class
First create a folder called libs and then clone the PHP-Ethereum repo into it. The Ethereum-php project is a nice wrapper around the JSON-RPC class.
We then use the following class and save it as ethpay.php. This is the main logic of payment processing. You can use it to:
- Generate address pair
- Check balances (pending and completed)
- Switch from WEI to ETH
<? php define('RPC_IP'.'127.0.0.1');
define('RPC_PORT', 8545); require'libs/ethereum-php/ethereum.php';
$e = new EthPay();
class EthPay
{
private $eth; // Let's establish a connection to the Parity nodefunction __construct()
{
$this->eth = new Ethereum(RPC_IP, RPC_PORT);
if(!$this->eth->net_version()) die('RPC ERROR'); } / * * get the balance of an address, * the balance from parity appears in hex in Wei * use the BC math function to convert it * /function getBalanceOfAddress($addr)
{
$eth_hex = $this->eth->eth_getBalance($addr.'latest');
$eth = $this->wei2eth($this->bchexdec($eth_hex));
$pending_hex = $this->eth->eth_getBalance($addr.'pending');
$pending = $this->wei2eth($this->bchexdec($pending_hex));
return array('balance'= >$eth.'pending'= >$pending);
}
function getCurrentPrice($currency='USD')
{
$data = json_decode(file_get_contents('https://api.coinbase.com/v2/prices/ETH-'.$currency.'/spot'),true);
return $data['data'] ['amount']; } /* * We will use vanityeth to generate the private key pair * NPM install -g vanity-eth * we must reformat the output string to use as JSON * /function genPair()
{
exec('vanityeth'.$outputAndErrors.$return_value);
$answer = implode(NULL,$outputAndErrors);
$answer = str_replace('address:'.'"address":'.$answer);
$answer = str_replace('privKey:'.'"privKey":'.$answer);
$answer = str_replace('\'', '"',$answer);
return json_decode($answer,true); } // Function wei2eth($wei)
{
return bcdiv($wei, 1000000000000000000, 17); } function bchexdec($hex) {
if(strlen($hex) == 1) {
return hexdec($hex);
} else {
$remain = substr($hex, 0, 1);$last = substr($hex, 1); return bcadd(bcmul(16,$this->bchexdec($remain)), hexdec($last)); }}}Copy the code
Final step: Integrate with your site
Depending on your service, there are several ways to do this.
At API Heaven, we provide each customer with an ETH address where they can deposit their money. Cronjob checks all customer addresses every minute to detect changes. If they add ETH to the address, the balance will be converted to API quota, so our customers don’t even need to log into the site to add funds.
Example integration in API Heaven:
Another approach is to calculate a fixed price and store it in the user session. The customer must pay on the site, and you need to query AJAX for received payments. If the full amount is received, the back end triggers a sale.
Best of all, you don’t need an external service to integrate the Ethereum payment system on your site. Come and learn to play with Ethereum.
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
If you want to get your hands on PHP and Ethereum in action, we recommend this tutorial:
PHP Ethereum tutorial introduces smart contract development interaction using PHP, including account creation, transactions, transfers, token development, filters and events.
Huizhi net original translation, reprint please indicate the source. Here is the original text