How to use PHP to develop the Ethereum payment system

Source: Internet
Author: User
Tags generator php define
This article is the PHP development Ethereum system, mainly introduces the use of PHP for intelligent Contract Development interaction, account creation, transaction, transfer, coin development and filters and events and other content.

When I first considered paying in crypto currency, I looked at available solutions like stripe. I think the problem with stripe is that it only allows Bitcoin payments with a U.S. merchant account, so that's not a choice for me. In the Ethereum world, it looks worse. There are some newer services, but they all want to share the cake.

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

    • A Web server running PHP.

    • There is at least one parity node in the private network that has RPC enabled.

    • A virtual address generator on a network server, such as Vanity-eth.

So how does it work?

    • Calculates the price of the ETH using the current price in the Coinbase or Kraken API.

    • Use the virtual generator to generate an address pair and encrypt or transfer the private key to another server.

    • Displays the generated address to the customer and, if payment is received, checks the address every few seconds.

There seems to be no problem in theory, so let's build it.

1th Step: Setting up the server

We will use the Vanity-eth in Nodejs to generate the address.

NPM install-g vanity-eth@1.0.4 "

After you install Vanity-eth on Windows:

Some etherum nodes are also required. I am using parity because it is 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

To complete the Synchronized parity log:

For faster deployment, you can use the Parity Docker container. You can also save data so that you do not have to resynchronize each time you re-create the container.

2nd Step: Write the Payment class

First create a folder named libs , and then clone the Php-ethereum repo into it. The ethereum-php project is a good package for the Json-rpc class.

We then use the following classes and save them as ethpay.php. This is the primary logic for payment processing. You can use it to:

    • Generate address Pairs

    • Check the balance (pending and completed)

    • Convert from Wei to ETH

<?php define (' rpc_ip ', ' 127.0.0.1 ');d efine (' Rpc_port ', 8545); require ' libs/ethereum-php/ethereum.php '; $e = new        Ethpay (); class ethpay{private $eth;        Let's establish the connection function __construct () {$this->eth = new Ethereum (RPC_IP, Rpc_port) with the parity node;    if (! $this->eth->net_version ()) Die (' RPC ERROR ');        }/* * Get the balance of an address, * the balance from parity in the form of 16 in Wei * convert it with BC Math function */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 for 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 digital function Wei2eth ($wei) {return bcp ($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, $this->bchexdec ($remain)), Hexdec ($last)); }    }}

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 a ETH address that can be deposited into the funds. Cronjob Check all customer addresses every minute to detect changes. If they add ETH to the address, the balance is converted to API quotas, so our customers don't even need to sign in to the site to add money.

Example Integration in API Heaven:

Another method is to calculate the fixed price and save it in a user session. The customer must pay on the site, and you need to query Ajax for the payment that has been received. If you receive the full amount, the backend triggers the sale.

Most importantly, you do not need an external service to integrate the Ethereum payment system on your website. Come and learn to play ethereum.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.