Exemple #1
0
        public async Task Run(CheckPaymentRequestData data)
        {
            try
            {
                bool isSuccessStatusCode = false;

                await RepeatedCall.ActionAsync(async() =>
                {
                    using (HttpResponseMessage response = await this.GetResponse(data.RequestData, HttpMethod.Get, null))
                    {
                        string str = await response.Content.ReadAsStringAsync();
                        ResponseData <PaymentDoc> result = data.CreateResponseInstance();
                        result.RequestJson  = "";
                        result.ResponseJson = str;
                        isSuccessStatusCode = response.IsSuccessStatusCode;
                    }
                }, 5, 500);

                data.DeserializeResults(isSuccessStatusCode);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Ошибка при отправке запроса CheckRequest и десериализации ответа");
                data.ExceptionData = ex;
            }
        }
Exemple #2
0
 private async Task CompleteRequest(PaymentRequest request, CheckPaymentRequestData data, SqlConnection conn, SqlTransaction tx)
 {
     using (SqlCommand cmd = _sqlServer.GetSpCommand("payment.CompleteYandexKassaRequest", conn, tx))
     {
         cmd.AddIntParam("@RequestId", request.Id);
         cmd.AddVarCharParam("@KassaPaymentId", 50, data.ResponseData.Response.Id);
         cmd.AddBigIntParam("@PaymentId", request.PaymentId);
         await cmd.ExecuteNonQueryAsync();
     }
 }
Exemple #3
0
 private async Task SaveRequestStatus(SqlConnection conn, int requestId, CheckPaymentRequestData data, PaymentRequestStatus status)
 {
     using (SqlCommand cmd = _sqlServer.GetSpCommand("payment.UpdateYandexKassaRequestStatus", conn))
     {
         cmd.AddIntParam("@RequestId", requestId);
         cmd.AddTinyIntParam("@Status", (byte)status);
         cmd.AddVarCharParam("@KassaPaymentId", 50, data.ResponseData.Response.Id);
         await cmd.ExecuteNonQueryAsync();
     }
 }
Exemple #4
0
        public async Task <bool> ProcessPaymentFromHook(string json)
        {
            HookDoc hookDoc = null;

            try
            {
                hookDoc = JsonConvert.DeserializeObject <HookDoc>(json, Startup.JsonSettings);
                this._fileLogger.WriteHookLog(json, hookDoc.Payment.Metadata.RequestId);
            }
            catch (Exception ex)
            {
                this._fileLogger.WriteHookErrorLog(json, ex);
                return(false);
            }

            PaymentDoc payment = hookDoc.Payment;

            using (SqlConnection conn = _sqlServer.GetConnection())
            {
                await conn.OpenAsync();

                PaymentRequest request = null;
                using (SqlCommand cmd = _sqlServer.GetSpCommand("payment.GetYandexKassaRequestById", conn))
                {
                    cmd.AddIntParam("@RequestId", payment.Metadata.RequestId);
                    using (SqlDataReader dr = await cmd.ExecuteReaderAsync())
                    {
                        if (dr.Read())
                        {
                            request = new PaymentRequest();
                            request.FillFromReader(dr);
                        }
                    }
                }

                CheckPaymentRequestData   data     = new CheckPaymentRequestData(null);
                ResponseData <PaymentDoc> response = data.CreateResponseInstance();
                response.ResponseJson = json;
                response.Response     = payment;
                await ProcessCheckPaymentResponse(conn, request, data);

                return(true);
            }
        }
Exemple #5
0
        private async Task <CheckPaymentRequestData> SendCheckPaymentRequest(PaymentRequest request)
        {
            CheckPaymentRequestData data = new CheckPaymentRequestData(request.KassaPaymentId);

            try
            {
                await CheckPaymentRequest.Run(this._client, data, this._logger);
            }
            catch (Exception e)
            {
                this._logger.LogError(e.ToString());
            }
            finally
            {
                this._fileLogger.WriteCheckLog(request.Id, data);
            }

            return(data);
        }
Exemple #6
0
        private async Task ProcessPayments()
        {
            using (SqlConnection conn = _sqlServer.GetConnection())
            {
                await conn.OpenAsync();

                using (SqlCommand cmd = _sqlServer.GetSpCommand("payment.PrepareYandexKassaRequestsForNewCheckIteration", conn))
                {
                    await cmd.ExecuteNonQueryAsync();
                }

                while (true)
                {
                    PaymentRequest request = null;
                    using (SqlCommand cmd = _sqlServer.GetSpCommand("payment.GetYandexKassaRequest", conn))
                    {
                        cmd.AddTinyIntParam("@Status", (byte)PaymentRequestStatus.Created);
                        using (SqlDataReader dr = await cmd.ExecuteReaderAsync())
                        {
                            if (dr.Read())
                            {
                                request = new PaymentRequest();
                                request.FillFromReader(dr);
                            }
                        }
                    }

                    if (request == null)
                    {
                        break;
                    }

                    CheckPaymentRequestData data = await this.SendCheckPaymentRequest(request);
                    await ProcessCheckPaymentResponse(conn, request, data);
                }
            }
        }
Exemple #7
0
 public static async Task Run(HttpClient client, CheckPaymentRequestData data, ILogger logger)
 {
     CheckPaymentRequest request = new CheckPaymentRequest(client, logger);
     await request.Run(data);
 }
Exemple #8
0
 public void WriteCheckLog(int requestId, CheckPaymentRequestData data)
 {
     WriteLog(requestId, "CheckPayment", data.ResponseData, data.ExceptionData);
 }
Exemple #9
0
        private async Task ProcessCheckPaymentResponse(SqlConnection conn, PaymentRequest request, CheckPaymentRequestData data)
        {
            if (data.HasError || data.ResponseData.Response.Status.IsAny(PaymentDoc.StatusType.Pending, PaymentDoc.StatusType.Canceled))
            {
                PaymentRequestStatus status;
                if (data.HasError)
                {
                    status = PaymentRequestStatus.CheckFailed;
                }
                else if (data.ResponseData.Response.Status == PaymentDoc.StatusType.Pending)
                {
                    status = PaymentRequestStatus.CheckPending;
                }
                else
                {
                    status = (data.ResponseData.Response.CancellationDetails == null || data.ResponseData.Response.CancellationDetails.Party == PaymentDoc.CancellationParty.PaymentNetwork ?
                              PaymentRequestStatus.CanceledByUser : PaymentRequestStatus.CanceledByKassa);
                }

                await SaveRequestStatus(conn, request.Id, data, status);
            }
            else if (data.ResponseData.Response.Status == PaymentDoc.StatusType.Succeeded)
            {
                using (SqlTransaction tx = conn.BeginTransaction())
                {
                    bool result = await this.CreatePayment(request, data.ResponseData, conn, tx);

                    if (!result)
                    {
                        this._logger.LogError("Платёж уже создан. Запрос:" + request.Id.ToString() + ", идентификатор в системе Яндекс.Касса: " + request.KassaPaymentId);
                    }
                    else
                    {
                        await CompleteRequest(request, data, conn, tx);
                    }

                    tx.Commit();
                }
            }
        }