// Ignore Spelling: Zarin

using Newtonsoft.Json;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZarinPal
{
    public static class ZarinPalHelper
    {
        public static NewPurchaseResult CreateRequest(string callbackUrl,long amount,string merchantId,string desc)
        {            
            var client = new RestClient("https://www.zarinpal.com/pg/rest/WebGate/PaymentRequest.json");
            var request = new RestRequest("", Method.Post)
            {
                RequestFormat = RestSharp.DataFormat.Json,
            };
            request.AddJsonBody(new ZarinPalPaymentRequestModel
            {
                Amount = amount,
                MerchantID = merchantId,
                Description = desc,
                CallbackURL = callbackUrl
            });
            var response = client.Execute<ZarinpalPaymentStartResult>(request);
            var paymentStartResult = JsonConvert.DeserializeObject<ZarinpalPaymentStartResult>(response.Content);
            if (paymentStartResult.Status == 100)
            {
                return new NewPurchaseResult
                {
                    Authority = paymentStartResult.Authority,
                    Success = true
                };
            }
            return new NewPurchaseResult
            {
                Success = false
            };
        }
        public static ConfirmResult Confirm(long amount,string merchantId,string auth)
        {
           
            var client = new RestClient("https://www.zarinpal.com/pg/rest/WebGate/PaymentVerification.json");
            var request = new RestRequest("", Method.Post)
            {
                RequestFormat = RestSharp.DataFormat.Json,
            };
            request.AddJsonBody(new ZarinPalConfirmModel
            {
                Amount = amount,
                MerchantID = merchantId,
                Authority = auth,
            });
            var response = client.Execute<ZarinPalPaymentResponse>(request);
            ZarinPalPaymentResponse paymentStartResult = JsonConvert.DeserializeObject<ZarinPalPaymentResponse>(response.Content);
            if (paymentStartResult.Status == 100)
            {
                 
                return new ConfirmResult
                {
                    Ref = paymentStartResult.RefID,
                    Successful = true
                };
            }            
                return new ConfirmResult
                {                   
                    Successful = false
                };
        }

     
    }
}
