Example #1
0
        public MoyasarRefundBase Refund(string id, string amount)
        {
            var finalUrl = MakePaymentUrl + "/" + id + "/refund?amount=" + amount;

            if (amount.Equals("0"))
            {
                MoyasarValidationException ex = new MoyasarValidationException(EnMessages.AmountNotZero);
                throw ex;
            }

            try
            {
                var httpWebRequest = (HttpWebRequest)WebRequest.Create(finalUrl);
                httpWebRequest.ContentType = "application/json; charset=utf-8";
                httpWebRequest.Method      = "POST";
                httpWebRequest.Credentials = new NetworkCredential(ApiKey, ApiKey);

                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var          result       = streamReader.ReadToEnd();
                    var          rs           = JObject.Parse(result);
                    RefundResult refundResult = new RefundResult();
                    var          type         = (string)rs["type"];
                    if (type == null)
                    {
                        refundResult = new RefundResult()
                        {
                            Currency   = (string)rs["currency"],
                            Amount     = (string)rs["amount"],
                            Id         = (string)rs["id"],
                            Fee        = (string)rs["fee"],
                            Refunded   = (string)rs["refunded"],
                            RefundedAt = (string)rs["refunded_at"]
                        };
                        if ((string)rs["source"]["type"] == "creditcard")
                        {
                            refundResult.Source = new CreditCard()
                            {
                                Type    = (string)rs["source"]["type"],
                                Company = (string)rs["source"]["company"],
                                Name    = (string)rs["source"]["name"],
                                Number  = (string)rs["source"]["number"],
                                Message = (string)rs["source"]["message"]
                            };
                        }
                        return(refundResult);
                    }
                    else
                    {
                        RefundException exception = new RefundException
                        {
                            Type    = (string)rs["type"],
                            Message = (string)rs["message"],
                            Error   = (string)rs["errors"]
                        };
                        return(exception);
                    }



                    return(null);
                }
            }
            catch (Exception ex)
            {
                return(new RefundException()
                {
                    Message = ex.Message, Error = null, Type = ex.Source
                });
            }
        }
Example #2
0
        private void Validation()
        {
            if (ApiKey == "" || ApiKey == string.Empty)
            {
                var ex = new MoyasarValidationException(EnMessages.ApiKeyNotFound)
                {
                    ErrorCode = "#1559"
                };
                throw ex;
            }
            if (SourceType == 0)
            {
                var ex = new MoyasarValidationException(EnMessages.SelectType)
                {
                    ErrorCode = "#1550"
                };
                throw ex;
            }

            if (this.SourceReault == null && this.SourceType == SourceType.CreditCard)
            {
                var ex = new MoyasarValidationException(EnMessages.SelectCreditCardType)
                {
                    ErrorCode = "#1555"
                };
                throw ex;
            }

            if (this.SourceReault == null)
            {
                var ex = new MoyasarValidationException(EnMessages.TypeEmpty)
                {
                    ErrorCode = "#1500"
                };
                throw ex;
            }
            if (string.IsNullOrEmpty(this.Currency) || this.Currency == string.Empty)
            {
                var ex = new MoyasarValidationException(EnMessages.CurrencyEmpty)
                {
                    ErrorCode = "#1000"
                };
                throw ex;
            }
            //check if this creditCard Type
            if (this.SourceType == SourceType.CreditCard)
            {
                if (this.SourceReault != null)
                {
                    var credit = (CreditCard)SourceReault;
                    if (credit.Company == string.Empty)
                    {
                        var ex = new MoyasarValidationException(EnMessages.CreatedCardCompanyNotFound)
                        {
                            ErrorCode = "#1110"
                        };
                        throw ex;
                    }
                    if (credit.Name == string.Empty)
                    {
                        var ex = new MoyasarValidationException(EnMessages.CreatedCardNameNotFound)
                        {
                            ErrorCode = "#1111"
                        };
                        throw ex;
                    }
                    if (credit.Name == string.Empty)
                    {
                        var ex = new MoyasarValidationException(EnMessages.CreatedCardNameNotFound)
                        {
                            ErrorCode = "#1112"
                        };
                        throw ex;
                    }
                    if (credit.Number == string.Empty)
                    {
                        var ex = new MoyasarValidationException(EnMessages.CreatedCardNumberNotFound)
                        {
                            ErrorCode = "#1113"
                        };
                        throw ex;
                    }
                    if (credit.Month == 0)
                    {
                        var ex = new MoyasarValidationException(EnMessages.CreatedCardNumberNotFound)
                        {
                            ErrorCode = "#1114"
                        };
                        throw ex;
                    }
                    if (credit.Year == 0)
                    {
                        var ex = new MoyasarValidationException(EnMessages.CreatedCardNumberNotFound)
                        {
                            ErrorCode = "#1115"
                        };
                        throw ex;
                    }
                }
                else
                {
                    var ex = new MoyasarValidationException(EnMessages.CreatedCardNotReady)
                    {
                        ErrorCode = "#1110"
                    };
                    throw ex;
                }
            }
        }
Example #3
0
        public PaymentResult Refund(string id, string amount = null)
        {
            var finalUrl = this.MakePaymentUrl + "/" + id + "/refund";

            finalUrl = amount == null ? finalUrl : (finalUrl + "? amount = " + amount);
            if (amount != null && amount.Equals("0"))
            {
                MoyasarValidationException ex = new MoyasarValidationException(EnMessages.AmountNotZero);
                throw ex;
            }

            var httpWebRequest = (HttpWebRequest)WebRequest.Create(finalUrl);

            httpWebRequest.ContentType = "application/json; charset=utf-8";
            httpWebRequest.Method      = "POST";
            httpWebRequest.Credentials = new NetworkCredential(ApiKey, ApiKey);

            try
            {
                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd();
                    var rs     = this.js.Deserialize <PaymentResult>(result); //JObject.Parse(result);

                    PaymentResult payment = rs;                               /*new PaymentResult
                                                                               * {
                                                                               * Id = (string)rs["id"],
                                                                               * Status = (string)rs["status"],
                                                                               * Amount = (int)rs["amount"],
                                                                               * Description = (string)rs["description"],
                                                                               * CallbackUrl = (string)rs["callback_url"],
                                                                               * Currency = (string)rs["currency"],
                                                                               * AmountFormat = (string)rs["amount_format"],
                                                                               * CreatedAt = (string)rs["created_at"],
                                                                               * Fee = (string)rs["fee"],
                                                                               * FeeFormat = (string)rs["fee_format"],
                                                                               * InvoiceId = (string)rs["invoice_id"],
                                                                               * Ip = (string)rs["ip"],
                                                                               * Refunded = (string)rs["refunded"],
                                                                               * RefundedAt = (string)rs["refunded_at"],
                                                                               * UpdatedAt = (string)rs["updated_at"]
                                                                               *
                                                                               * }; */
                    if ("sadad" == rs.Source.Type)                            //(string)rs["source"]["type"]
                    {
                        //payment.Source = new SadadType()
                        //{
                        //    Type = (string)rs["source"]["type"],
                        //    Username = (string)rs["source"]["username"],
                        //    TransactionUrl = (string)rs["source"]["transaction_url"],
                        //    ErrorCode = (string)rs["source"]["error_code"],
                        //    TransactionId = (string)rs["source"]["transaction_id"],
                        //    Message = (string)rs["source"]["message"]
                        //};
                        payment.Source = new SadadType()
                        {
                            Type           = rs.Source.Type,            //(string)rs["source"]["type"],
                            Username       = rs.Source.UserName,        //(string)rs["source"]["username"],
                            TransactionUrl = rs.Source.Transaction_Url, //(string)rs["source"]["transaction_url"],
                            ErrorCode      = rs.Source.Error_Code,      //(string)rs["source"]["error_code"],
                            TransactionId  = rs.Source.Transaction_Id,  //(string)rs["source"]["transaction_id"],
                            Message        = rs.Source.Message          //(string)rs["source"]["message"]
                        };
                    }
                    if ("creditcard" == rs.Source.Type)  //(string)rs["source"]["type"]
                    {
                        //payment.Source = new CreditCard()
                        //{
                        //    Type = (string)rs["source"]["type"],
                        //    Company = (string)rs["source"]["company"],
                        //    Name = (string)rs["source"]["name"],
                        //    Number = (string)rs["source"]["number"],
                        //    Message = (string)rs["source"]["message"],
                        //    TransactionUrl = (string)rs["source"]["transaction_url"],
                        //};
                        payment.Source = new CreditCard()
                        {
                            Type           = rs.Source.Type,           // (string)rs["source"]["type"],
                            Company        = rs.Source.Company,        //(string)rs["source"]["company"],
                            Name           = rs.Source.Name,           //(string)rs["source"]["name"],
                            Number         = rs.Source.Number,         //(string)rs["source"]["number"],
                            Message        = rs.Source.Message,        //(string)rs["source"]["message"],
                            TransactionUrl = rs.Source.Transaction_Url //(string)rs["source"]["transaction_url"],
                        };
                    }
                    return(payment);
                }
            }
            catch (WebException webEx)
            {
                throw this.HandleRequestErrors(webEx);
            }
        }