February 1, 2023

We accept payment in bitcoin: Part one, theoretical

We accept payment in bitcoin: Part one, theoretical

The Internet is full of articles about what a great future blockchain has for us, how it is “stylish, fashionable and innovative. ” However, there is very little information on how to practically use all this magnificence. Let's try to partially fill this gap.

Let's take a look at the main milestones of a long journey using the example of bitcoin integration as a method of receiving payments.

Suppose you are a developer, and your advanced and not-poor customer, having read about how cool it is to accept payments in cryptocurrency, came to you with a simple request - “But screw us bitcoin!”.

To begin with, I will try to discourage you from doing this thankless task.

Here is a partial list of the problems you will encounter:

- constantly changing course

-Transaction time through the network

- complete lack of control over the amount the buyer sends

-lack of a clear and transparent mechanism that allows you to simply organize payment tracking

But let's imagine, purely theoretically, that the customer is ready to put up with all these and a dozen other problems, and it did not work out.

Then for a start we will consider how, in fact, weWe will accept payments. As always, there are at least two ways. The first, relatively simple, not much different from integration, for example, PayPal or Privat24. Use a ready-made service, for example - bitpay. There are many such services, many of them, in addition tobitcoin, allow you to accept several different cryptocurrencies. Everything is prosaic and simple enough, documentation and examples are most often on the service website in sufficient quantities.

But we are not looking for easy ways, right? In addition, the amount exposed to the persistent customer needs to be worked out (or pretended to work).

Therefore, we will consider the second way, full of difficulties, problems and non-obvious solutions.

Its plus is that having mastered it once, you will master not only several levels of marine curses, but also much more deeply understand how cryptocurrencies are organized and what they allow you to implement.

In the usual case, when integrating a payment gateway spherical in a vacuum, the sequence of actions (with minor details) looks something like this.

The user has selected the products of interest to him,added them to the basket and pressed the treasured “buy” button. Filled in all the necessary fields, such as delivery address, phone, email address, and finally got to payment. Here a little magic begins.

The seller’s website software formsa special form, most commonly referred to as SCI (Shopping Cart interface). After clicking on the “Pay” button, this form will redirect the buyer to the site of the payment system (for example PayPal), with pre-set payment data. Obviously this will be at least - the order number, the amount payable and, probably, some kind of note for the payment, for example, “Invoice for tennis balls No. 00012”.

The user logs in with his credentials andconfirms that he really wants to pay. If he has enough funds, all the data is entered correctly and there are no problems with the transfer of money, payment is made directly. In case of successful payment, the payment gateway sends a notification to the seller’s server, which includes the amount, payment time and account number that was paid. Additionally, arbitrary data can be transmitted, but for our example this is not so important. At the same time, the buyer himself is redirected back to the seller’s website, where he receives a solemn congratulation on successful payment and an oath of promise to send the goods right now.

What is important to us in this sequence?

Three key points.

First, the amount of payment is set by the seller.

The second - payment occurs with a fairly accurate identification for which particular order.

And the third is the notification that the seller’s server receives is almost impossible to fake.

Such a system was not formed immediately, but in a more or less similar form it was implemented in most payment gateways and electronic payment systems, such as Skrill or PayPal.

Now let's see what awaits us when we are going to integrate bitcoin as a means of payment.

Firstly, in bitcoin (and in all cryptocurrencies built on its basis) there is no direct way to transfer a payment identifier.

Secondly - you have practically no control over how much the user will send (with a few caveats).

In the general case, starting from the moment the user switches to payment, the further process will look something like this.

You get a new bitcoin address and pass itto the user, together with the calculated amount payable. It’s considered good practice to indicate how long payment details are valid (we remember that the cryptocurrency rate is very unstable), usually 15 minutes. Further, you regularly check the status of this address, whether the necessary amount has been received for it or not. If the amount has been received, the payment can be considered completed and proceed to further processing of the order (sending goods, replenishing a mobile account, and so on)

Given the features of the functioning of cryptocurrencies, some pitfalls are potentially awaiting us.

If the user sent a transaction to the network -this does not mean that you received it. Until the first confirmation, your server may not even know that such a transaction was. And from the moment of payment to the moment the transaction is included in the block, several hours can pass. This situation gives rise to a lot of unpleasant consequences.

Firstly, if the course at the time of receipttransactions are much lower, or, as an option, if the payment is indicated with a too low commission, you can receive a notification about the transaction, but the funds themselves may come in a few days later. But the buyer will be sure that he paid for the order and demand its implementation.

In subsequent articles I will tell you how to implement the above in practice and what you need to do this.