# Payments cashback ## Desription We want to send cashback in TWT tokens for users, who made a successfull crypto purchase. For now: - Only Simplex and MoonPay support - Cashback is 10% of fiat amount ## Global tech scheme ![](https://i.imgur.com/lVpGpAj.png) ### Tech details ### Getting information from payment providers We can use webhook or worker to fetch all succsessfull purchases using provider api We need to get: internal_transaction_id transaction_id provider_id fiat_currency_id fiat_amount token_id crypto_amount status ### Create a cashback event for each completed payment 1. Each N minutes we need to search for all new payment purchases at payment_purchases table 2. For all new purchases we need to: A. Find user id (We know user's address and token) B. Calculate $TWT for cashback (We know user fiat currency, fiat amount, we know TWT price for TWT in any fiat amount as well) 3. Each cashback event must be saved at db at new table #### Cashback event table specification: id created_at updated_at deleted_at user_id cashback_amount status (completed, created, in_queue, signed_and_pending, failed) transaction_id payment_purchase_id (internal_id + wallet_address + status) ### Proccess cashback event (send TWT) 1. Get event from queue 2. Send this amount in TWT to the address 3. Change status to signed_and_pending and save status + transaction_id in cashback event table ### Proccess crypto payment status 1. Find all cashback events with status = signed_and_pending 2. Check status of transction 3. If transaction is failed or succsessfull - change it's status at DB