Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit cc075e32981308dfb154bc520c715fb44982e26a @boucher committed Jan 30, 2012
Showing with 119 additions and 0 deletions.
  1. +6 −0 Gemfile
  2. +26 −0 README
  3. +2 −0 config.ru
  4. +85 −0 mailer.rb
@@ -0,0 +1,6 @@
+source 'https://code.stripe.com'
+source 'http://rubygems.org'
+gem 'sinatra'
+gem 'stripe'
+gem 'json'
+gem 'pony'
@@ -0,0 +1,26 @@
+Webhook-Mailer: A simple example of using Stripe webhooks
+---------------------------------------------------------
+
+This example was built on heroku, but should be easily deployable in any
+environment that can support Ruby and rack.
+
+The app sends an email anytime an invoice.payment_succeeded Stripe event
+webhook is received.
+
+To deploy on heroku:
+
+1. Create a new heroku app
+ heroku create <APP_NAME>
+
+2. Add the mailgun add-on
+ heroku addons:add mailgun:starter
+
+3. Add your stripe api key to your heroku config environment
+ https://manage.stripe.com/#account/apikeys
+ heroku config:add STRIPE_SECRET_KEY=<YOUR_KEY>
+
+4. Add your webhook url to your stripe dashboard
+ https://manage.stripe.com/#account/webhooks
+
+5. Customize your email body
+6. Deploy your app to heroku!
@@ -0,0 +1,2 @@
+require './mailer'
+run Sinatra::Application
@@ -0,0 +1,85 @@
+require 'sinatra'
+require 'stripe'
+require 'json'
+require 'pony'
+
+Stripe::api_key = ENV['STRIPE_SECRET_KEY']
+
+post '/stripe-webhook-url' do
+ data = JSON.parse request.body.read, :symbolize_names => true
+ p data
+
+ puts "Received event with ID: #{data[:id]} Type: #{data[:type]}"
+
+ # Retrieving the event from the Stripe API guarantees its authenticity
+ event = Stripe::Event.retrieve(data[:id])
+
+ # This will send receipts on succesful invoices
+ # You could also send emails on all charge.succeeded events
+ if event.type == 'invoice.payment_succeeded'
+ email_invoice_receipt(event.data.object)
+ end
+end
+
+def email_invoice_receipt(invoice)
+ puts "Emailing customer for invoice: #{invoice.id} amount: #{format_stripe_amount(invoice.total)}"
+
+ customer = Stripe::Customer.retrieve(invoice.customer)
+
+ # Make sure to customize your from address
+ from_address = "MyApp Support <support@myapp.com>"
+ subject = "Your payment has been received"
+
+ Pony.mail(
+ :from => from_address,
+ :to => customer.email,
+ :subject => subject,
+ :body => payment_received_body(invoice, customer),
+ :via => :smtp,
+ :via_options => default_email_options)
+
+ puts "Email sent to #{customer.email}"
+end
+
+def format_stripe_amount(amount)
+ sprintf('$%0.2f', amount.to_f / 100.0).gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,")
+end
+
+def format_stripe_timestamp(timestamp)
+ Time.at(timestamp).strftime("%m/%d/%Y")
+end
+
+def payment_received_body(invoice, customer)
+ subscription = invoice.lines.subscriptions[0]
+ <<EOF
+Dear #{customer.email}:
+
+This is a receipt for your subscription. This is only a receipt,
+no payment is due. Thanks for your continued support!
+
+-------------------------------------------------
+SUBSCRIPTION RECEIPT - #{Time.now.strftime("%m/%d/%Y")}
+
+Email: #{customer.email}
+Plan: #{subscription.plan.name}
+Amount: #{format_stripe_amount(invoice.total)} (USD)
+
+For service between #{format_stripe_timestamp(subscription.period.start)} and #{format_stripe_timestamp(subscription.period.end)}
+
+-------------------------------------------------
+
+EOF
+end
+
+# You can customize this for whatever email provider you want to use,
+# like Mailgun, SendGrid, or even Gmail. These settings are for Mailgun
+def default_email_options
+ {
+ :address => ENV['MAILGUN_SMTP_SERVER'],
+ :port => ENV['MAILGUN_SMTP_PORT'],
+ :enable_starttls_auto => true,
+ :user_name => ENV['MAILGUN_SMTP_LOGIN'],
+ :password => ENV['MAILGUN_SMTP_PASSWORD'],
+ :authentication => :plain,
+ }
+end

0 comments on commit cc075e3

Please sign in to comment.