The post has been translated automatically. Original language: Russian
We built our own payment service, BayPay. The seller enters a phone number and an amount, the customer receives an invoice, and the store website learns about the payment automatically. Here is what the service does when something goes wrong. These are four situations that anyone who takes payments on a website may run into.
The store website does not respond
We first write the payment notification to the database and only then send it. If the website is silent, we try again after 10 seconds, then after 30 seconds, after a minute, and then less and less often. That makes 10 retries in about two hours. If the website is still down after that, the notification can be sent again from the dashboard.
The notification arrived twice
It happens: the website accepted the message, but its reply was lost on the network. That is why every event has its own number. The store only needs to remember it to avoid shipping the order twice.
The notification came from someone else
Every message is signed with a key that only the store and we have: HMAC-SHA256 over the sending time and the request body. The store will spot a fake as soon as it checks the signature. The sending time also lets the store reject an old message that someone intercepted and sent again.
The invoice was issued twice
The seller pressed the button, the page froze, and they pressed it again. If an order with this number is already waiting for payment, the customer will not get a second invoice.
If you are adding payments to your website, test all four situations before the first real payment.
Мы сделали свой платёжный сервис BayPay. Продавец вводит номер телефона и сумму, покупатель получает счёт, а сайт магазина узнаёт об оплате сам. Расскажем, что он делает, когда что-то идёт не по плану. Ниже четыре случая, с которыми может столкнуться любой, кто принимает оплату на сайте.
Сайт магазина не отвечает
Уведомление об оплате мы сначала записываем в базу и только потом отправляем. Если сайт молчит, повторяем через 10 секунд, потом через 30, через минуту и дальше всё реже. Всего 10 повторов примерно за два часа. Если сайт не поднялся и за это время, уведомление можно отправить ещё раз из кабинета.
Уведомление пришло дважды
Так бывает: сайт принял сообщение, но ответ потерялся в сети. Поэтому у каждого события есть свой номер. Магазину достаточно его запомнить, чтобы не отгрузить заказ второй раз.
Уведомление прислал кто-то другой
Каждое сообщение подписано ключом, который есть только у магазина и у нас: HMAC-SHA256 от времени отправки и тела запроса. Подделку магазин увидит сразу, когда проверит подпись. По времени магазин отсеет и старое сообщение, если кто-то перехватил его и прислал снова.
Счёт выставили два раза
Продавец нажал кнопку, страница зависла, он нажал ещё раз. Если заказ с этим номером уже ждёт оплаты, второй счёт покупателю не уйдёт.
Если подключаете оплату на свой сайт, проверьте все четыре случая до первого настоящего платежа.
Comments 5
Newest Popular