예제 #1
0
 /// <summary>
 /// Updates the following fields
 /// <list type="bullet">
 /// <item><term>Description</term></item>
 /// </list>
 /// </summary>
 /// <exception cref="ApiException">Thrown when an exception occurs at server</exception>
 /// <exception cref="NetworkException">Thrown when server is unreachable</exception>
 public void Update()
 {
     MoyasarService.SendRequest("PUT", GetUpdateUrl(Id), new Dictionary <string, object>()
     {
         { DescriptionField, Description }
     });
 }
예제 #2
0
        /// <summary>
        /// Creates a new invoice at Moyasar and returns an <code>Invoice</code> instance for it
        /// </summary>
        /// <param name="info">Information needed to create a new invoice</param>
        /// <returns><code>Invoice</code> instance representing an invoice created at Moyasar</returns>
        /// <exception cref="ApiException">Thrown when an exception occurs at server</exception>
        /// <exception cref="NetworkException">Thrown when server is unreachable</exception>
        public static Invoice Create(InvoiceInfo info)
        {
            info.Validate();
            var response = MoyasarService.SendRequest("POST", GetCreateUrl(), info);

            return(DeserializeInvoice(response));
        }
예제 #3
0
        /// <summary>
        /// Updates the following fields
        /// <list type="bullet">
        /// <item><term>Amount</term></item>
        /// <item><term>Currency</term></item>
        /// <item><term>Description</term></item>
        /// </list>
        /// </summary>
        /// <exception cref="ApiException">Thrown when an exception occurs at server</exception>
        /// <exception cref="NetworkException">Thrown when server is unreachable</exception>
        public void Update()
        {
            Validate(true);

            var requestParams = new Dictionary <string, object>()
            {
                { "amount", Amount },
                { "currency", Currency },
                { "description", Description }
            };

            if (CallbackUrl != null)
            {
                requestParams.Add("callback_url", CallbackUrl);
            }

            if (ExpiredAt != null)
            {
                requestParams.Add("expired_at", ExpiredAt);
            }

            if (Metadata != null)
            {
                requestParams.Add("metadata", Metadata);
            }

            var response = MoyasarService.SendRequest("PUT", GetUpdateUrl(Id), requestParams);

            DeserializeInvoice(response, this);
        }
예제 #4
0
 /// <summary>
 /// Get an payment from Moyasar by Id
 /// </summary>
 /// <param name="id">Payment Id</param>
 /// <returns><code>Payment</code> instance representing an payment created at Moyasar</returns>
 /// <exception cref="ApiException">Thrown when an exception occurs at server</exception>
 /// <exception cref="NetworkException">Thrown when server is unreachable</exception>
 public static Payment Fetch(string id)
 {
     return(DeserializePayment(MoyasarService.SendRequest(
                                   "GET",
                                   GetFetchUrl(id),
                                   null
                                   )));
 }
예제 #5
0
        /// <summary>
        /// Retrieve provisioned payments at Moyasar
        /// </summary>
        /// <param name="query">Used to filter results</param>
        /// <returns>A list of payments</returns>
        /// <exception cref="ApiException">Thrown when an exception occurs at server</exception>
        /// <exception cref="NetworkException">Thrown when server is unreachable</exception>
        public static PaginationResult <Payment> List(SearchQuery query = null)
        {
            var responseJson = MoyasarService.SendRequest(
                "GET",
                GetListUrl(),
                query
                );

            return(JsonConvert.DeserializeObject <PaginationResult <Payment> >(responseJson));
        }
예제 #6
0
        /// <summary>
        /// Voids the current authorized payment
        /// </summary>
        /// <returns>An updated <code>Payment</code> instance</returns>
        /// <exception cref="ApiException">Thrown when an exception occurs at server</exception>
        /// <exception cref="NetworkException">Thrown when server is unreachable</exception>
        public Payment Void()
        {
            if (Status != "authorized")
            {
                throw new ValidationException("Payment is not in authorized status.");
            }

            return(DeserializePayment
                   (
                       MoyasarService.SendRequest("POST", GetVoidUrl(Id), null),
                       this
                   ));
        }
예제 #7
0
        /// <summary>
        /// Retrieve provisioned invoices at Moyasar
        /// </summary>
        /// <param name="query">Used to filter results</param>
        /// <returns>A list of invoices</returns>
        /// <exception cref="ApiException">Thrown when an exception occurs at server</exception>
        /// <exception cref="NetworkException">Thrown when server is unreachable</exception>
        public static PaginationResult <Invoice> List(SearchQuery query = null)
        {
            var responseJson = MoyasarService.SendRequest
                               (
                "GET",
                GetListUrl(),
                query?.ToDictionary()
                               );

            dynamic response = MoyasarService.Serializer.Deserialize <object>(responseJson);

            string metaJson = null;

            try
            {
                metaJson = MoyasarService.Serializer.Serialize((object)response.meta);
            }
            catch
            {
                // ignored
            }

            var invoiceObjects =
                MoyasarService.Serializer.Deserialize <List <object> >(
                    MoyasarService.Serializer.Serialize((object)response.invoices));
            var invoicesList = invoiceObjects
                               .Select(i => DeserializeInvoice(MoyasarService.Serializer.Serialize(i))).ToList();

            var pagination = new PaginationResult <Invoice>
            {
                Paginator = page =>
                {
                    var q = query?.Clone() ?? new SearchQuery();
                    q.Page = page;
                    return(List(q));
                },
                Items = invoicesList
            };

            if (metaJson != null)
            {
                MoyasarService.Serializer.PopulateObject(metaJson, pagination);
            }

            return(pagination);
        }
예제 #8
0
        /// <summary>
        /// Refunds the current payment
        /// </summary>
        /// <param name="amount">Optional amount to refund, should be equal to or less than current amount</param>
        /// <returns>An updated <code>Payment</code> instance</returns>
        /// <exception cref="ApiException">Thrown when an exception occurs at server</exception>
        /// <exception cref="NetworkException">Thrown when server is unreachable</exception>
        public Payment Refund(int?amount = null)
        {
            if (amount > this.Amount)
            {
                throw new ValidationException("Payment must be equal to or less than current amount");
            }

            var reqParams = amount != null ? new Dictionary <string, Object>()
            {
                { AmountField, amount.Value }
            } : null;

            return(DeserializePayment
                   (
                       MoyasarService.SendRequest("POST", GetRefundUrl(Id), reqParams),
                       this
                   ));
        }
예제 #9
0
        /// <summary>
        /// Creates a new invoice at Moyasar and returns an <code>Invoice</code> instance for it
        /// </summary>
        /// <param name="info">Information needed to create a new invoice</param>
        /// <returns><code>Invoice</code> instance representing an invoice created at Moyasar</returns>
        /// <exception cref="ApiException">Thrown when an exception occurs at server</exception>
        /// <exception cref="NetworkException">Thrown when server is unreachable</exception>
        public static List <Invoice> CreateBulk(List <InvoiceInfo> info)
        {
            if (info.Count == 0)
            {
                throw new ValidationException("At least one invoice is required.");
            }

            if (info.Count > 50)
            {
                throw new ValidationException("No more than 50 invoices is allowed at once.");
            }

            var data = new Dictionary <string, List <InvoiceInfo> >
            {
                { "invoices", info }
            };

            var response = MoyasarService.SendRequest("POST", GetCreateBulkUrl(), data);

            return(JsonConvert.DeserializeObject <Dictionary <string, List <Invoice> > >(response).First().Value);
        }
예제 #10
0
        /// <summary>
        /// Captures the current authorized payment
        /// </summary>
        /// <param name="amount">Optional amount to capture, should be equal to or less than current amount</param>
        /// <returns>An updated <code>Payment</code> instance</returns>
        /// <exception cref="ApiException">Thrown when an exception occurs at server</exception>
        /// <exception cref="NetworkException">Thrown when server is unreachable</exception>
        public Payment Capture(int?amount = null)
        {
            if (amount > this.Amount)
            {
                throw new ValidationException("Amount to be refunded must be equal to or less than current amount");
            }

            if (Status != "authorized")
            {
                throw new ValidationException("Payment is not in authorized status.");
            }

            var reqParams = amount != null ? new Dictionary <string, object>
            {
                { "amount", amount.Value }
            } : null;

            return(DeserializePayment
                   (
                       MoyasarService.SendRequest("POST", GetCaptureUrl(Id), reqParams),
                       this
                   ));
        }
예제 #11
0
        /// <summary>
        /// Updates the following fields
        /// <list type="bullet">
        /// <item><term>Amount</term></item>
        /// <item><term>Currency</term></item>
        /// <item><term>Description</term></item>
        /// </list>
        /// </summary>
        /// <exception cref="ApiException">Thrown when an exception occurs at server</exception>
        /// <exception cref="NetworkException">Thrown when server is unreachable</exception>
        public void Update()
        {
            Validate();

            var requestParams = new Dictionary <string, object>()
            {
                { AmountField, Amount },
                { CurrencyField, Currency },
                { DescriptionField, Description }
            };

            if (CallbackUrl != null)
            {
                requestParams.Add(CallbackUrlField, CallbackUrl);
            }
            if (ExpiredAt != null)
            {
                requestParams.Add(ExpiredAtField, ExpiredAt);
            }

            var response = MoyasarService.SendRequest("PUT", GetUpdateUrl(Id), requestParams);

            DeserializeInvoice(response, this);
        }
예제 #12
0
 /// <summary>
 /// Get an invoice from Moyasar by Id
 /// </summary>
 /// <param name="id">Invoice Id</param>
 /// <returns><code>Invoice</code> instance representing an invoice created at Moyasar</returns>
 /// <exception cref="ApiException">Thrown when an exception occurs at server</exception>
 /// <exception cref="NetworkException">Thrown when server is unreachable</exception>
 public static Invoice Fetch(string id)
 {
     return(DeserializeInvoice(MoyasarService.SendRequest("GET", GetFetchUrl(id), null)));
 }
예제 #13
0
        /// <summary>
        /// Changes invoice state to canceled
        /// </summary>
        /// <exception cref="ApiException">Thrown when an exception occurs at server</exception>
        /// <exception cref="NetworkException">Thrown when server is unreachable</exception>
        public void Cancel()
        {
            var response = MoyasarService.SendRequest("PUT", GetCancelUrl(Id), null);

            DeserializeInvoice(response, this);
        }