Created
May 7, 2016 13:08
-
-
Save jb41/2a31f09a611b1e5ef13b37abc07bc36f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pry' | |
require 'json' | |
require 'httparty' | |
require 'stripe' | |
class ChargeApp < Sinatra::Base | |
AMOUNT = 300 | |
Stripe.api_key = "SECRET_STRIPE_KEY" | |
post '/charge' do | |
status = charge_customer(params[:stripeToken], params[:stripeEmail]) | |
add_email_to_spreadsheet(stripe_email) if status == 'succeeded' | |
content_type :json | |
{ transaction_status: status }.to_json | |
end | |
get '/has-paid' do | |
email = params['email'] | |
content_type :json | |
{ has_paid: check_if_email_in_spreadsheet(email) }.to_json | |
end | |
not_found do | |
status 404 | |
'<h1 style="text-align: center;">404<br>No such page.<br><a href="copyfixes.com">copyfixes.com</a></h1>' | |
end | |
protected | |
def charge_customer(token, customer_email) | |
begin | |
customer = create_customer(token, customer_email) | |
payment = create_payment(customer.id) | |
rescue Exception => e | |
end | |
payment.status | |
end | |
def create_customer(token, email) | |
Stripe::Customer.create(source: token, description: "#{email}") | |
end | |
def create_payment(customer_id) | |
Stripe::Charge.create(amount: AMOUNT, currency: 'usd', customer: customer_id) | |
end | |
def add_email_to_spreadsheet(email) | |
HTTParty.post( | |
'https://sheetsu.com/apis/v1.0/c12aec61', | |
body: { email: email }.to_json, | |
headers: { 'Content-Type' => 'application/json', 'Accept' => 'application/json'} | |
).body | |
end | |
def check_if_email_in_spreadsheet(email) | |
response = HTTParty.get("https://sheetsu.com/apis/v1.0/c12aec61/search?email=m@sheetsu.com") | |
response.is_a?(Array) && response.first['email'] == email | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment