4

I'm having trouble finding much details on this. I need to work with an api that processes credit cards and it uses curl. All documentation is in php and while I could use php my main site is entirely in MVC4 using razor view engine. I need to convert this wich is from php to something useable in .net

$ curl https://api.stripe.com/v1/customers -u *private key here*: 
       -d "description=Customer for test@example.com" -d "card[number]=4242424242424242" 
       -d "card[exp_month]=12" -d "card[exp_year]=2013"

Thanks in advance for your time

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user1840865
  • 83
  • 1
  • 5

1 Answers1

5

from the curl manual page

  • -u, --user <user:password> is the User credentials
  • -d, --data <data> is the POST data

so you can "decode" this as:

using (var wc = new System.Net.WebClient())
{
    // data
    string parameters = string.Concat("description=", description, "&amp;card[number]=" , cardNumber, "&amp;card[exp_month]=", cardExpirationMonth, "&amp;card[exp_year]=", cardExpirationYear),
           url = "https://api.stripe.com/v1/customers;

    // let's fake it and make it was a browser requesting the data
    wc.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

    // credentials
    wc.Credentials = new System.Net.NetworkCredential("*private key here*", "");

    // make it a POST instead of a GET
    wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";

    // send and get answer in a string
    string result = wc.UploadString(url, parameters);
}

updated with POST tweak from an existing answer.

Community
  • 1
  • 1
balexandre
  • 73,608
  • 45
  • 233
  • 342