Example #1
0
        private async Task RefundReservation(Reservation reservation)
        {
            var priceStr = reservation.Price.ToString().Replace(',', '.');
            var price    = priceStr.Contains('.') ? priceStr : priceStr + ".00";

            var parameters = new Dictionary <string, string>
            {
                { "merchantId", _configuration.GetValue <string>("Key:SafeCharge:Merchant") },
                { "merchantSiteId", _configuration.GetValue <string>("Key:SafeCharge:Site") },
                { "clientRequestId", DateTime.UtcNow.Ticks.ToString() },
                { "clientUniqueId", _currentUser.User.Email },
                { "amount", price },
                { "currency", "EUR" },
                { "relatedTransactionId", reservation.TransactionId.Value.ToString() },
                { "authCode", reservation.AuthCode ?? string.Empty },
                { "timeStamp", DateTime.UtcNow.ToString("yyyyMMddHHmmss") }
            };

            parameters.Add("checksum", _checkout.CalculateChecksum(parameters, _configuration.GetValue <string>("Key:SafeCharge:Secret"), false));

            using var http = new HttpClient();

            var response = await http.PostAsync("https://ppp-test.safecharge.com/ppp/api/v1/refundTransaction.do",
                                                new StringContent(JsonConvert.SerializeObject(parameters), Encoding.UTF8, "application/json"));

            var data = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()) as dynamic;

            if (data.status != "SUCCESS")
            {
                throw new BadRequestException("Couldn't refund the reservation with id " + reservation.Id);
            }
        }