Пример #1
0
        public ActionResult <SuccessResponse> RefundAppointmentCharge(int id, string reason)
        {
            int          responseCode = 200;
            BaseResponse responseData = null;

            try
            {
                // Get charge id using appointment id
                StripeAppointmentCharge appointmentCharge = _service.GetChargeByAppointmentId(id);
                if (appointmentCharge == null)
                {
                    string msg = "This appointment does not have an associated charge.";
                    responseCode = 404;
                    responseData = new ErrorResponse(msg);
                    return(StatusCode(responseCode, responseData));
                }
                else if (appointmentCharge.IsRefunded == true)
                {
                    string msg = "This appointment has already been refunded.";
                    responseCode = 400;
                    responseData = new ErrorResponse(msg);
                    return(StatusCode(responseCode, responseData));
                }
                string chargeId = appointmentCharge.StripeChargeId;

                // Process a refund for this charge with chargeId
                Refund refund = RefundCharge(chargeId);
                if (refund.Status != "succeeded")
                {
                    string msg = "This charge is not refundable.";
                    responseCode = 404;
                    responseData = new ErrorResponse(msg);
                    return(StatusCode(responseCode, responseData));
                }

                // Update stripe appointment charge table
                _service.UpdateStripeChargeByAppointmentId(id, true);
                _service.CancelAppointment(id, reason);

                // When successful return success response
                responseData = new SuccessResponse();
            }
            catch (Exception exception)
            {
                responseCode = 500;
                responseData = new ErrorResponse($"Generic Error: {exception.Message}");
                base.Logger.LogError(exception.ToString());
            }

            return(StatusCode(responseCode, responseData));
        }