php develops the payment system of Ethernet workshop

Keywords: PHP npm JSON Python

When I first considered paying in encrypted currency, I looked at something like Stripe Such an available solution. I think Stripe's problem is that it only allows Bitcoin payments from American merchant accounts, so it's not an option for me. In the world of Taifang, it looks worse. There are some newer services, but they all want to share the cake.

So what do we need to build the payment system from scratch?

  • Web server running PHP.
  • RPC-enabled private networks have at least one Parity node.
  • Virtual address generators on network servers, such as vanity-eth.

So how does it work?

  • Use the current price in coinbase or kraken API to calculate the price of ETH.
  • The virtual generator is used to generate address pairs and to encrypt or transfer the private key to another server.
  • Show 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: Setting up the server

We will use vanity-eth in nodejs to generate addresses.

npm install -g vanity-eth@1.0.4"

After installing vanity-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 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

Complete synchronization parity log:

To deploy faster, you can use the Parity Docker container. You can also save data so that you don't have to resynchronize every time you make a container.

Step 2: Writing Payment Category

First create a folder called libs, and then php-ethereum repo was cloned into it. The ethereum-php project is a good encapsulation of the json-rpc class.

Then we use the following classes and save them as ethpay.php. This is the main logic of payment processing. You can use it:

  • Generate address pairs
  • Check the balance (pending and completed)
  • Conversion 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 node
    function __construct()
    {
        $this->eth = new Ethereum(RPC_IP, RPC_PORT);
        if(!$this->eth->net_version()) die('RPC ERROR');
    }

    / *
    *Get the balance of an address.
    *Come from parity The balance appears in hexadecimal form wei in
    *Use bc Mathematical function transforms 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 Generating private key pairs
    * npm install -g vanity-eth
    *We have to 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);
    }
    //The following functions are used to convert and process large numbers
    function wei2eth($wei)
    {
        return bcdiv($wei,1000000000000000000,18);
    }
    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));
        }
    }
}

Last step: Integrate with your website

Depending on your service, there are several ways to do this.

stay API Heaven We provide an ETH address for each customer to deposit funds. cronjob checks all customer addresses every minute to detect changes. If they add ETH to the address, the balance will be converted to API quotas, so our customers don't even need to log on to the site to add funds.

Example integration in API Heaven:

Another method is to calculate the fixed price and save it in the user session. Customers must pay on the website, and you need to check with AJAX for payment received. If the full amount is received, the back end triggers sales.

Most importantly, you don't need external services to integrate the ETF payment system on your website. Let's learn and play in the ether square.

======================================================================

If you want to learn the actual combat between PHP and Taifang directly, we recommend our tutorial:

php Ethernet workshop course It mainly introduces the use of php for intelligent contract development interaction, account creation, transaction, transfer, token development, filters and events.

Other block chain tutorials:

  • C# Ethernet It mainly explains how to use C# to develop Ethernet Application Based on. Net, including account management, status and transaction, intelligent contract development and interaction, filters and events.
  • web3j tutorial It is mainly for java and android programmers to develop block chain etheric workshop web3j in detail.
  • Edelfang Course This paper mainly introduces the development of intelligent contract and dapp application, which is suitable for introducing.
  • Development of Etaifang It mainly introduces how to use node.js, mongodb, block chain and ipfs to realize the de-centralized e-commerce DApp, which is suitable for advanced stage.
  • python etheric workshop Mainly for python engineers to use web3.py for block chain Ethernet development in detail.
  • EOS Intelligent Contracts and Introduction to DApp Development It covers the core knowledge points of EOS tool chain, accounts and wallets, issuing tokens, intelligent contract development and deployment, interaction between code and intelligent contract, and finally uses react and knowledge points to complete the development of a note DApp.

Huizhi original translation, reprinted please indicate the source. Here is original text

Posted by alvinkoh on Fri, 17 May 2019 15:41:39 -0700