Thursday, February 7, 2019

coinpayments gatway integration in asp.net c# using coinpayments.net API

Bitcoin integration in asp.net c# using coinpayments.net

Coinpayment is payment gateway providing buy now buttons, shopping carts, and more to accept Bitcoin, Litecoin  and more than 165 currencies.

Coinpayment  have plugins for WordPress and php developers that provide you an easy way to integrate to your website.

For developers who don't want to write coding just create html form and button to hit coinpayments server

Example 1.


<form action="https://www.coinpayments.net/index.php" method="post">
 <input type="hidden" name="cmd" value="_pay">
 <input type="hidden" name="reset" value="1">
 <input type="hidden" name="merchant" value="606a89bb575311badf510a4a8b79a45e">
 <input type="hidden" name="currency" value="LTC">
 <input type="hidden" name="amountf" value="10.00">
 <input type="hidden" name="item_name" value="Test Item"> 
 <input type="image" src="https://www.coinpayments.net/images/pub/buynow-grey.png" alt="Buy Now with CoinPayments.net">
</form>
For ASP.NET developer.

Please create your class with the following code and call api with your public and private key





public class CoinPaymentAPIClass
    {
        private string s_privkey = "youraccountprivatekey";
        private string s_pubkey = "youraccountpublickey";
        private static readonly Encoding encoding = Encoding.UTF8;
        public CoinPaymentAPIClass(string privkey, string pubkey)
        {
            s_privkey = privkey;
            s_pubkey = pubkey;
            if (s_privkey.Length == 0 || s_pubkey.Length == 0)
            {
                throw new ArgumentException("Private or Public Key is empty");
            }
        }
        public Dictionary<string, dynamic> CallAPI(string cmd, SortedList<string, string> parms = null)
        {
            if (parms == null)
            {
                parms = new SortedList<string, string>();
            }
            parms["version"] = "1";
            parms["key"] = s_pubkey;
            parms["cmd"] = cmd;
            string post_data = "";
            foreach (KeyValuePair<string, string> parm in parms)
            {
                if (post_data.Length > 0) { post_data += "&"; }
                post_data += parm.Key + "=" + Uri.EscapeDataString(parm.Value);
            }
            byte[] keyBytes = encoding.GetBytes(s_privkey);
            byte[] postBytes = encoding.GetBytes(post_data);
            var hmacsha512 = new System.Security.Cryptography.HMACSHA512(keyBytes);
            string hmac = BitConverter.ToString(hmacsha512.ComputeHash(postBytes)).Replace("-", string.Empty);
            // do the post:
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            System.Net.WebClient cl = new System.Net.WebClient();
            cl.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
            cl.Headers.Add("HMAC", hmac);
            cl.Encoding = encoding;
            var ret = new Dictionary<string, dynamic>();
            try
            {
                string resp = cl.UploadString("https://www.coinpayments.net/api.php", post_data);
                var decoder = new System.Web.Script.Serialization.JavaScriptSerializer();
                ret = decoder.Deserialize<Dictionary<string, dynamic>>(resp);
            }
            catch (System.Net.WebException e)
            {
                ret["error"] = "Exception while contacting CoinPayments.net: " + e.Message;
            }
            catch (Exception e)
            {
                ret["error"] = "Unknown exception: " + e.Message;
            }
            return ret;
        }
    }
----------------------------------------------------------------------------------
in my controller class i have just call this class callapi method.
 CoinPayments.CoinPaymentAPIClass coinPayObject = new CoinPayments.CoinPaymentAPIClass(s_privkey, s_pubkey);
            //coinPayObject.CallAPI("get_basic_info", null);
            // coinPayObject.CallAPI("rates", null);
            // coinPayObject.CallAPI("get_tx_ids", null);
            SortedList<string, string> lstSortedList = new SortedList<string, string>();
            lstSortedList.Add("amount", "500");
            lstSortedList.Add("currency1", "USD");
            lstSortedList.Add("currency2", "LTC");
            lstSortedList.Add("ipn_url", "http://localhost:3996/Home/SuccessfullyCompleted");
            lstSortedList.Add("buyer_email", "hostcoders@gmail.com");
            var result = coinPayObject.CallAPI("create_transaction", lstSortedList);
  












3 comments:

  1. Thanks for the blog loaded with so many information. Stopping by your blog helped me to get what I was looking for. gc templates

    #delivery within 24 hours
    #100% verified account

    Free Chat With Our Expert: buy verified Coinpayments account

    ReplyDelete
  2. Sir
    i want to create transfer with coin payments api in c#
    please help with code

    ReplyDelete
  3. how to confrim about payment is successfull or not, any call back url which give return information

    ReplyDelete