Skip to content

Instantly share code, notes, and snippets.

@jb41
Last active May 1, 2016 20:00
Show Gist options
  • Save jb41/ca6ac22d5578389fbcfb6945b923e5d6 to your computer and use it in GitHub Desktop.
Save jb41/ca6ac22d5578389fbcfb6945b923e5d6 to your computer and use it in GitHub Desktop.
copyfixes.com
require 'json'
require 'httparty'
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
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(stripe_token, stripe_email)
add_email_to_spreadsheet(stripe_email) if status == 'succeeded'
content_type :json
{ transaction_status: status }.to_json
end
not_found do
status 404
'<h1 style="text-align: center;">404<br>No such page.</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 stripe_token
params[:stripeToken]
end
def stripe_email
params[:stripeEmail]
end
def add_email_to_spreadsheet(email)
HTTParty.post(
'https://sheetsu.com/apis/v1.0/1521d646',
body: { email: email }.to_json,
headers: { 'Content-Type' => 'application/json', 'Accept' => 'application/json'}
).body
end
end
require 'json'
require 'stripe'
class ChargeApp < Sinatra::Base
AMOUNT = 300
Stripe.api_key = "SECRET_STRIPE_API_KEY"
post '/charge' do
status = charge_customer(params[:stripeToken, params[:stripeEmail])
content_type :json
{ transaction_status: status }.to_json
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
end
<html lang="en">
<head>
<meta charset="utf-8">
<title>CopyFixes.com - Quick fixes of your copywriting from professionals</title>
</head>
<body>
<section>
<h1>CopyFixes.com</h1>
<h2>Quick fixes of your copywriting by professionals</h2>
<ul class="steps">
<li>1. Send an email to <a href="mailto:ineed@copyfixes.com">ineed@copyfixes.com</a></li>
<li>2. Get offers from professionals with prices</li>
<li>3. Pick any and contact them</li>
<li>4. Publish your awesome copy!</li>
</ul>
</section>
<section>
<h2 class="question">Are you a copywriter?</h2>
<a href="requests.html" class="show-reqs-button">Check requests!</a>
</section>
<section>
<h2>About this website</h2>
</section>
</body>
</html>
<button id="stripe-payment-button" class="button-pay">Get 2-months Access for $3</button>
<script type="text/javascript">
var handler = StripeCheckout.configure({
key: 'STRIPE_KEY',
locale: 'auto',
token: function(token) {
var data = {
stripeToken: token.id,
stripeEmail: token.email
};
$.ajax({
url: '/charge',
data: data,
dataType: 'json',
type: 'POST',
success: function(data) {
window.location.href = 'requests.html?payment_complete=' + token.email;
},
error: function(data) {
console.log(data);
}
});
}
});
$('#stripe-payment-button').on('click', function(e) {
// Open Checkout with further options
handler.open({
name: 'CopyFixes',
description: 'Lifetime access to all requests',
currency: "usd",
amount: 300
});
e.preventDefault();
});
// Close Checkout on page navigation
$(window).on('popstate', function() {
handler.close();
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment