Skip to content

Instantly share code, notes, and snippets.

@montague
Created April 2, 2015 19:59
Show Gist options
  • Save montague/147dffb9bf3163d96b45 to your computer and use it in GitHub Desktop.
Save montague/147dffb9bf3163d96b45 to your computer and use it in GitHub Desktop.
Create "card" example for stripe API v1
using System;
using System.Net;
using System.IO;
using System.Text;
namespace stripe_example
{
class MainClass
{
public static void Main (string[] args)
{
// for use against this API endpoint: https://stripe.com/docs/api/curl#create_card
var customerId = "YOUR_CUSTOMER_ID";
var url = String.Format("https://api.stripe.com/v1/customers/{0}/sources", customerId);
var secretKey = "YOUR_SECRET_KEY";
var cardNumber = "4242424242424242"; // test card number
var expirationMonth = "01"; // january
var expirationYear = "24"; // 2025
// code example below from https://msdn.microsoft.com/en-us/library/debx8sh9%28v=vs.110%29.aspx
// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create (url);
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
// notice that we use a dictionary here to pass the credit card data
string postData = String.Format(
"source[object]=card&source[number]={0}&source[exp_month]={1}&source[exp_year]={2}",
cardNumber,expirationMonth, expirationYear
);
byte[] byteArray = Encoding.UTF8.GetBytes (postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// authenticate
request.Credentials = new NetworkCredential(secretKey,String.Empty);
// Get the request stream.
Stream dataStream = request.GetRequestStream ();
// Write the data to the request stream.
dataStream.Write (byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close ();
// Get the response.
WebResponse response = request.GetResponse ();
// Display the status.
Console.WriteLine (((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream ();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader (dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd ();
// Display the content.
Console.WriteLine (responseFromServer);
// Clean up the streams.
reader.Close ();
dataStream.Close ();
response.Close ();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment