<commit_before>require 'sinatra'
require 'stripe'
require 'dotenv'
require 'json'
require 'encrypted_cookie'

Dotenv.load
Stripe.api_key = ENV['STRIPE_TEST_SECRET_KEY']

use Rack::Session::EncryptedCookie,
  :secret => 'replace_me_with_a_real_secret_key' # Actually use something secret here!

get '/' do
  status 200
  return "Great, your backend is set up. Now you can configure the Stripe example apps to point here."
end

post '/ephemeral_keys' do
  authenticate!
  begin
    key = Stripe::EphemeralKey.create(
      {customer: @customer.id},
      {stripe_version: params["api_version"]}
    )
  rescue Stripe::StripeError => e
    status 402
    return "Error creating ephemeral key: #{e.message}"
  end

  status 200
  key.to_json
end

post '/charge' do
  authenticate!
  # Get the credit card details submitted
  payload = params
  if request.content_type.include? 'application/json' and params.empty? 
    payload = indifferent_params(JSON.parse(request.body.read))
  end

  source = payload[:source]
  customer = payload[:customer_id] || @customer.id
  # Create the charge on Stripe's servers - this will charge the user's card
  begin
    charge = Stripe::Charge.create(
      :amount => payload[:amount], # this number should be in cents
      :currency => "usd",
      :customer => customer,
      :source => source,
      :description => "Example Charge",
      :shipping => payload[:shipping],
    )
  rescue Stripe::StripeError => e
    status 402
    return "Error creating charge: #{e.message}"
  end

  status 200
  return "Charge successfully created"
end

def authenticate!
  # This code simulates "loading the Stripe customer for your current session".
  # Your own logic will likely look very different.
  return @customer if @customer
  if session.has_key?(:customer_id)
    customer_id = session[:customer_id]
    begin
      @customer = Stripe::Customer.retrieve(customer_id)
    rescue Stripe::InvalidRequestError
    end
  else
    begin
      @customer = Stripe::Customer.create(:description => "mobile SDK example customer")
    rescue Stripe::InvalidRequestError
    end
    session[:customer_id] = @customer.id
  end
  @customer
end

# This endpoint is used by the Obj-C and Android example apps to create a charge.
post '/create_charge' do
  # Create the charge on Stripe's servers
  begin

    # if request.content_type.include? 'application/json' and params.empty? 
    #   payload = indifferent_params(JSON.parse(request.body.read))
    # end
    charge = Stripe::Charge.create(
      :amount => params["amount"], # this number should be in cents
      :currency => "usd",
      :source => params["source"],
      :description => "Example Charge"
    )
  rescue Stripe::StripeError => e
    status 402
    return "Error creating charge: #{e.message}"
  end

  status 200
  return "Charge successfully created"
end
<commit_msg>[Fixbug] Currency default is sgd<commit_after>require 'sinatra'
require 'stripe'
require 'dotenv'
require 'json'
require 'encrypted_cookie'

Dotenv.load
Stripe.api_key = ENV['STRIPE_TEST_SECRET_KEY']

use Rack::Session::EncryptedCookie,
  :secret => 'replace_me_with_a_real_secret_key' # Actually use something secret here!

get '/' do
  status 200
  return "Great, your backend is set up. Now you can configure the Stripe example apps to point here."
end

post '/ephemeral_keys' do
  authenticate!
  begin
    key = Stripe::EphemeralKey.create(
      {customer: @customer.id},
      {stripe_version: params["api_version"]}
    )
  rescue Stripe::StripeError => e
    status 402
    return "Error creating ephemeral key: #{e.message}"
  end

  status 200
  key.to_json
end

post '/charge' do
  authenticate!
  # Get the credit card details submitted
  payload = params
  if request.content_type.include? 'application/json' and params.empty? 
    payload = indifferent_params(JSON.parse(request.body.read))
  end

  source = payload[:source]
  customer = payload[:customer_id] || @customer.id
  # Create the charge on Stripe's servers - this will charge the user's card
  begin
    charge = Stripe::Charge.create(
      :amount => payload[:amount], # this number should be in cents
      :currency => "usd",
      :customer => customer,
      :source => source,
      :description => "Example Charge",
      :shipping => payload[:shipping],
    )
  rescue Stripe::StripeError => e
    status 402
    return "Error creating charge: #{e.message}"
  end

  status 200
  return "Charge successfully created"
end

def authenticate!
  # This code simulates "loading the Stripe customer for your current session".
  # Your own logic will likely look very different.
  return @customer if @customer
  if session.has_key?(:customer_id)
    customer_id = session[:customer_id]
    begin
      @customer = Stripe::Customer.retrieve(customer_id)
    rescue Stripe::InvalidRequestError
    end
  else
    begin
      @customer = Stripe::Customer.create(:description => "mobile SDK example customer")
    rescue Stripe::InvalidRequestError
    end
    session[:customer_id] = @customer.id
  end
  @customer
end

# This endpoint is used by the Obj-C and Android example apps to create a charge.
post '/create_charge' do
  # Create the charge on Stripe's servers
  begin

    # if request.content_type.include? 'application/json' and params.empty? 
    #   payload = indifferent_params(JSON.parse(request.body.read))
    # end
    charge = Stripe::Charge.create(
      :amount => params["amount"], # this number should be in cents
      :currency => "sgd",
      :source => params["source"],
      :description => "Example Charge"
    )
  rescue Stripe::StripeError => e
    status 402
    return "Error creating charge: #{e.message}"
  end

  status 200
  return "Charge successfully created"
end

{'fraction_non_alphanumeric': 0.10099251262406408, 'fraction_numerical': 0.007313250914156365, 'mean_word_length': 3.6136546184738956, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5050352', 'n_tokens_mistral': 1814, 'n_tokens_neox': 1708, 'n_words': 540}