Example #1
0
        public ActionResult <PaymoTransactionResponseVM> Post([FromBody] PaymoTransactionRequestVM requestVM)
        {
            origin += "Post";
            TelegramBotService.SendMessage("Starting post", origin);

            string requestVMSerialized = JsonConvert.SerializeObject(requestVM);

            TelegramBotService.SendMessage("requestVMSerialized: " + requestVMSerialized, origin);

            string  message         = null;
            Invoice existingInvoice = null;

            try
            {
                if (requestVM.store_id != Constants.PAYMO.SETTINGS.STORE_ID)
                {
                    message = "Неверный магазин";
                }
                else
                {
                    var invoice = _invoiceService.GetByID(requestVM.invoice);
                    if (invoice == null)
                    {
                        message = "Инвойс с номером " + requestVM.invoice + " отсутствует в системе";
                    }
                    else
                    {
                        existingInvoice = invoice;

                        if (invoice.Amount != requestVM.amount)
                        {
                            message = "сумма инвойса не совпадает";
                        }
                        else
                        {
                            string text = requestVM.store_id
                                          + requestVM.transaction_id
                                          + requestVM.invoice
                                          + requestVM.amount
                                          + Constants.PAYMO.SETTINGS.API_KEY;

                            using (MD5 md5Hash = MD5.Create())
                            {
                                text = GetMd5Hash(md5Hash, text);
                            }
                            if (!text.Equals(requestVM.sign))
                            {
                                message = "Подписи не совпадают";
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                TelegramBotService.SendMessage("1st catch: " + ex.Message, origin);
                message = ex.Message;
                if (ex.InnerException != null)
                {
                    message += "Inner exception: " + ex.InnerException.Message;
                }
            }

            //PaymoTransactionResponseVM responseVM = new PaymoTransactionResponseVM();
            int status = 0;

            try
            {
                if (string.IsNullOrWhiteSpace(message))
                {
                    status  = 1;
                    message = "Успешно";

                    try
                    {
                        existingInvoice.transaction_id   = requestVM.transaction_id;
                        existingInvoice.transaction_time = requestVM.transaction_time;

                        _invoiceService.Update(existingInvoice, null);
                    }
                    catch (Exception ex) { }
                }
                else
                {
                    //откатить оплату
                    _invoiceService.MarkAsFailedToWithdrawMoney(existingInvoice);

                    var videoRequest = _videoRequestService.GetByID(existingInvoice.VideoRequestID.Value);
                    _videoRequestService.MarkAsFailedToWithdrawMoney(videoRequest);
                }
            }
            catch (Exception ex)
            {
                TelegramBotService.SendMessage("2nd catch: " + ex.Message, origin);
                message = "Системная ошибка";
            }

            TelegramBotService.SendMessage(message, origin);

            PaymoTransactionResponseVM responseVM = new PaymoTransactionResponseVM()
            {
                status  = status,
                message = message
            };

            return(Ok(responseVM));
        }