Ejemplo n.º 1
0
        private void setupPayment()
        {
            try
            {
                string accessKey   = ConfigurationManager.AppSettings["accesskey"];
                string secretKey   = ConfigurationManager.AppSettings["secretkey"];
                string environment = ConfigurationManager.AppSettings["environment"];

                layerAPI = new clsLayer(accessKey, secretKey, environment);

                data                = new clsPaymentData();
                data.amount         = Convert.ToDecimal(ConfigurationManager.AppSettings["sampleDataAmount"]);
                data.currency       = ConfigurationManager.AppSettings["sampleDataCurrency"].ToString();
                data.name           = ConfigurationManager.AppSettings["sampleDataName"].ToString();
                data.contact_number = ConfigurationManager.AppSettings["sampleDataContactNumber"].ToString();
                data.email_id       = ConfigurationManager.AppSettings["sampleDataEmail"].ToString();
                data.mtx            = DateTime.Now.ToString("yyyyMMdd") + "-" + new Random().Next(1000, 9999).ToString();

                JObject jObject      = new JObject();
                JObject jObjectToken = new JObject();

                //create payment token
                var result = layerAPI.CreatePaymentToken(data);

                jObject = JsonConvert.DeserializeObject <JObject>(result);

                string errMessage = "";

                if (jObject["error"] != null)
                {
                    errMessage = "E55 Payment error. " + jObject.GetValue("error").ToString();
                    //add code for error list
                    if (jObject["error_data"] != null)
                    {
                        foreach (JProperty p in jObject["error_data"])
                        {
                            string e = p.Value.ToString().Replace("\r", "").Replace("\n", "").Replace("[", "").Replace("]", "").Trim();
                            errMessage += " " + e;
                        }
                    }
                }

                if (errMessage == "" && jObject["id"] != null && jObject.GetValue("id").ToString() == "")
                {
                    errMessage = "Payment error. Layer token ID cannot be empty.";
                }

                if (errMessage == "")
                {
                    //check that the payment is setup correctly and has not been pad
                    var resultToken = layerAPI.GetPaymentToken(jObject.GetValue("id").ToString());

                    jObjectToken = JsonConvert.DeserializeObject <JObject>(result);

                    if (jObjectToken["error"] != null)
                    {
                        errMessage = "E56 Payment error. " + jObjectToken.GetValue("error").ToString();
                        //add code for error list
                    }

                    if (errMessage == "" && jObjectToken["status"] != null && jObject.GetValue("status").ToString() == "paid")
                    {
                        errMessage = "Layer: this order has already been paid.";
                    }

                    if (errMessage == "" && jObjectToken["amount"] != null &&
                        Convert.ToDecimal(jObjectToken.GetValue("amount")) != Convert.ToDecimal(jObject.GetValue("amount")))
                    {
                        errMessage = "Layer: an amount mismatch occurred";
                    }
                }

                if (errMessage.Length > 0)
                {
                    lblError.Text = errMessage;
                    return;
                }

                //setup hidden values
                string paymentTokenId = jObjectToken.GetValue("id").ToString();
                string amount         = (Convert.ToDecimal(jObjectToken.GetValue("amount"))).ToString("#0.00");

                //create hash
                string hashValue = layerAPI.CreateHash(paymentTokenId,
                                                       Convert.ToDecimal(jObjectToken.GetValue("amount")), data.mtx);

                layer_pay_token_id.Value = paymentTokenId;
                tranid.Value             = data.mtx;
                layer_order_amount.Value = amount;
                layer_payment_id.Value   = "";
                fallback_url.Value       = "";
                hash.Value = hashValue;
                key.Value  = layerAPI.accessKey;
            }
            catch (Exception ex)
            {
                lblError.Text = "Error: " + ex.Message;
            }
        }
Ejemplo n.º 2
0
        private string verifyPayment()
        {
            try
            {
                string accessKey   = ConfigurationManager.AppSettings["accesskey"];
                string secretKey   = ConfigurationManager.AppSettings["secretkey"];
                string environment = ConfigurationManager.AppSettings["environment"];

                layerAPI = new clsLayer(accessKey, secretKey, environment);

                string status = "";

                string paymentId = Request.Params["layer_payment_id"];
                if (string.IsNullOrEmpty(paymentId))
                {
                    status = "Invalid response.";
                }

                decimal amount = 0;
                if (status == "" && decimal.TryParse(Request.Params["layer_order_amount"], out amount) == false)
                {
                    status = "Invalid response.";
                }

                string transactionId = Request.Params["tranid"];
                if (status == "" && string.IsNullOrEmpty(transactionId))
                {
                    status = "Invalid response.";
                }

                string paymentTokenId = Request.Params["layer_pay_token_id"];
                if (status == "" && string.IsNullOrEmpty(transactionId))
                {
                    status = "Invalid response.";
                }

                //calculate and compare hash
                string hashValue = layerAPI.CreateHash(paymentTokenId, amount, transactionId);
                if (status == "" && hashValue != Request.Params["hash"])
                {
                    status = "Invalid hash";
                }

                if (status == "")
                {
                    var result = layerAPI.GetPaymentDetails(paymentId);

                    JObject jObject = JsonConvert.DeserializeObject <JObject>(result);

                    if (jObject["error"] != null)
                    {
                        status = "Layer: an error occurred E14" + jObject.GetValue("error").ToString();
                    }

                    if (status == "" && (jObject["id"] == null || jObject.GetValue("id").ToString() == ""))
                    {
                        status = "Invalid payment data received E98";
                    }

                    if (status == "" && jObject["payment_token"]["id"].ToString() != paymentTokenId)
                    {
                        status = "Layer: received layer_pay_token_id and collected layer_pay_token_id doesnt match";
                    }

                    if (status == "" && Convert.ToDecimal(jObject.GetValue("amount")) != amount)
                    {
                        status = "Layer: received amount and collected amount doesnt match";
                    }

                    if (status == "")
                    {
                        string id = jObject.GetValue("id").ToString();

                        switch (jObject.GetValue("status").ToString())
                        {
                        case "authorized":
                        case "captured":
                            status = "Payment captured: Payment ID " + id;
                            break;

                        case "failed":
                        case "cancelled":
                            status = "Payment cancelled/failed: Payment ID " + id;
                            break;

                        default:
                            status = "Payment pending: Payment ID " + id;
                            break;
                        }
                    }
                }

                return(status);
            }
            catch (Exception ex)
            {
                return("Error: " + ex.Message);
            }
        }