December 5, 2024

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 wonderful future blockchain has in store for us, how stylish, fashionable andinnovative”. However, there is very little information on how to practically take advantage of all this splendor. Let's try to partially fill this gap.

Let's look at the main milestones of the long journey using the example of integrating bitcoin as a method of accepting payments.  

Suppose you – developer, and yourAn advanced and wealthy customer, having read about how cool it is to accept payments in cryptocurrency, came to you with a simple request – “Give us Bitcoin!”.

First, I’ll try to discourage you from engaging in this thankless task. 

Here is a partial list of problems you will encounter: 

- constantly changing course 

-Transaction time through the network

- complete lack of control over the amount sent by the buyer 

-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  to begin with, let's look at how,in fact, we will accept payments. As always, there are at least two ways. The first is relatively simple, not much different from integration with, 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 he is interested in,added them to the cart and clicked the coveted “buy” button. Filled out all the necessary fields, such as delivery address, phone number, email address, and finally got to the payment. This is where a little magic begins. 

The seller's website software generatesa special form, most often called SCI (Shopping Cart interface). After clicking the “Pay” button this form will redirect the buyer to the website of the payment system (for example PayPal), with pre-specified payment information. Obviously this will be at least – order number, amount due and probably some note on the payment, for example “Invoice for tennis balls #00012”. 

The user logs in using their credentials andconfirms that he really wants to make the payment. If he has enough funds, all data is entered correctly and there are no problems with transferring money, payment is made directly. In case of successful payment, the payment gateway sends a notification to the merchant's server, which includes the amount, time of payment and the invoice 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 promise to send the goods right now. 

What is important to us in this sequence? 

Three key points. 

The first – the payment amount is set by the seller. 

Secondly, the payment occurs with a fairly accurate identification of which particular order. 

And third – 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 is 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 transmit a payment ID. 

Secondly – you have virtually no control over how much the user sends (with a few caveats).

In general, starting from the moment the user proceeds to pay, the further process will look something like this. 

You receive a new bitcoin address and transfer itto the user, along with the calculated amount to be paid. It is considered good form to indicate how long the payment details are valid (we remember, the cryptocurrency exchange rate is very unstable), usually 15 minutes. Next, you regularly check the status of this address to see whether the required amount has been received or not. If the amount has been received – The payment can be considered completed and proceed to further processing of the order (shipment of goods, replenishment of 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 tonetwork – this does not mean that you received it. Until the first confirmation, your server may not even know that such a transaction took place. 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.