Skip to content

Instantly share code, notes, and snippets.

@ezekg
Last active October 18, 2017 00:34
Show Gist options
  • Save ezekg/91e0d71d460e7354ae539ae0b81a1af2 to your computer and use it in GitHub Desktop.
Save ezekg/91e0d71d460e7354ae539ae0b81a1af2 to your computer and use it in GitHub Desktop.
Using metadata to store Stripe and Keygen resource details to later use
// Set your Stripe API key. Remember to change this to your live secret key in production.
//
// NEVER USE YOUR SECRET KEY IN CLIENT-SIDE CODE.
//
// See your keys here: https://dashboard.stripe.com/account/apikeys
StripeConfiguration.SetApiKey("YOUR_STRIPE_SECRET_KEY");
using RestSharp;
using System;
using System.Collections.Generic;
using Stripe;
public class Example
{
// Set this to your Keygen account ID or slug.
//
// Available at: https://app.keygen.sh/settings
const string KEYGEN_ACCOUNT_ID = "YOUR_KEYGEN_ACCOUNT_ID_HERE";
// This is a Keygen token. If you're running code client-side, always use a *user* token. If
// you're running code server-side, you can either use an admin token or a product token.
//
// NEVER HARDCODE YOUR ADMIN OR PRODUCT TOKEN IN CLIENT-SIDE CODE.
// More security tips at: https://keygen.sh/docs/api#security
//
// Available at: https://app.keygen.sh/tokens
const string KEYGEN_TOKEN = "YOUR_KEYGEN_TOKEN_HERE";
public static void Main()
{
var keygen = new RestClient(string.Format("https://api.keygen.sh/v1/accounts/{0}", KEYGEN_ACCOUNT_ID));
/*
* Scenario #1: You have access to the MoonClerk/Stripe customer ID and want to retrieve
* the related Keygen user object.
*/
{
var STRIPE_CUSTOMER_ID = "A_STRIPE_CUSTOMER_ID"; // THIS IS THE CUSTOMER ID YOU HAVE ACCESS TO
// Get the Stripe customer object
var customerService = new StripeCustomerService();
var customer = customerService.Get(STRIPE_CUSTOMER_ID);
// Get the Keygen user using the ID stored in the Stripe customer's metadata
var request = new RestRequest(string.Format("users/{0}", customer.Metadata["keygenUserId"]), Method.GET);
request.AddHeader("Authorization", string.Format("Bearer {0}", KEYGEN_TOKEN));
request.AddHeader("Accept", "application/vnd.api+json");
var response = keygen.Execute<Dictionary<string, object>>(request).Data;
if (response.ContainsKey("errors"))
{
Console.WriteLine("Failed to retrieve user: {0}", response["errors"]);
return;
}
var user = (Dictionary<string, object>) response["data"];
// … do something with the newly retrieved Keygen user
}
/*
* Scenario #2: You have access to the Keygen user ID and want to retrieve the related
* MoonClerk/Stripe customer object.
*/
{
var KEYGEN_USER_ID = "A_KEYGEN_USER_ID"; // THIS IS THE USER ID YOU HAVE ACCESS TO
// Get the Keygen user object
var request = new RestRequest(string.Format("users/{0}", KEYGEN_USER_ID), Method.GET);
request.AddHeader("Authorization", string.Format("Bearer {0}", KEYGEN_TOKEN));
request.AddHeader("Accept", "application/vnd.api+json");
var response = keygen.Execute<Dictionary<string, object>>(request).Data;
if (response.ContainsKey("errors"))
{
Console.WriteLine("Failed to retrieve user: {0}", response["errors"]);
return;
}
var user = (Dictionary<string, object>) response["data"];
var attrs = (Dictionary<string, object>) user["attributes"];
var meta = (Dictionary<string, object>) user["metadata"];
// Get the Stripe customer using the ID stored in the Keygen user's metadata
var customerService = new StripeCustomerService();
var customer = customerService.Get((string) meta["stripeCustomerId"]);
// … do something with the newly retrieved Stripe customer
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment