-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Send receipt email to users (#3)
* Updated to ruby 2.7 * Added Stripe integration * Update Stripe charge to make them send a receipt to the purchaser. More info at https://medium.com/typeforms-engineering-blog/send-a-receipt-after-accepting-payment-on-a-typeform-175e5261404d
- Loading branch information
Showing
1,047 changed files
with
173,831 additions
and
35 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ SLACK_API_KEY= | |
SLACK_TEAM= | ||
SLACK_NOTIFICATIONS_WEBHOOK= | ||
TYPEFORM_SECRET= | ||
STRIPE_SECRET= |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
source 'https://rubygems.org' | ||
ruby '2.5.3' | ||
ruby '2.7.2' | ||
|
||
gem 'sinatra' | ||
gem 'dotenv' | ||
gem 'httparty' | ||
gem 'rack' | ||
gem 'rack-contrib' | ||
gem 'json' | ||
gem 'stripe' |
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
class ReceiptEmailSender | ||
def initialize(form_id, response_token, email) | ||
@form_id = form_id | ||
@response_token = response_token | ||
@email = email | ||
end | ||
|
||
def send_email | ||
update_charge(user_charge.id) | ||
|
||
handle_success | ||
rescue Exception => error | ||
handle_error(error) | ||
end | ||
|
||
private | ||
|
||
def user_charge | ||
return @charge if @charge | ||
charge = stripe_api.list(limit: 10).find do |charge| | ||
charge.metadata.typeform_form_id == @form_id && | ||
charge.metadata.typeform_response_id == @response_token | ||
end | ||
|
||
raise RuntimeError, "Charge not found in Stripe for email #{@email}" unless charge | ||
|
||
@charge = charge | ||
end | ||
|
||
def update_charge(charge_id) | ||
stripe_api.update(charge_id, receipt_email: @email) | ||
end | ||
|
||
def handle_success | ||
notifier.notify_successfully_sent_receipt_email(@email) | ||
end | ||
|
||
def handle_error(error) | ||
notifier.notify_failed_sending_receipt_email(@email, error.message) | ||
end | ||
|
||
def stripe_api | ||
return @stripe_api if @stripe_api | ||
|
||
::Stripe.api_key = ENV['STRIPE_SECRET'] | ||
@stripe_api = ::Stripe::Charge | ||
end | ||
|
||
def notifier | ||
@notifier ||= Notifier::Slack.new(ENV['SLACK_NOTIFICATIONS_WEBHOOK']) | ||
end | ||
end |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/usr/bin/env ruby | ||
|
||
title = "ruby #{ARGV*" "}" | ||
$0 = ARGV.shift | ||
Process.setproctitle(title) if Process.methods.include?(:setproctitle) | ||
|
||
require 'rubygems' | ||
begin | ||
require 'executable-hooks/hooks' | ||
Gem::ExecutableHooks.run($0) | ||
rescue LoadError | ||
warn "unable to load executable-hooks/hooks" if ENV.key?('ExecutableHooks_DEBUG') | ||
end unless $0.end_with?('/executable-hooks-uninstaller') | ||
|
||
content = File.read($0) | ||
|
||
if (index = content.index("\n#!ruby\n")) && index > 0 | ||
skipped_content = content.slice!(0..index) | ||
start_line = skipped_content.count("\n") + 1 | ||
eval content, binding, $0, start_line | ||
else | ||
eval content, binding, $0 | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/usr/bin/env ruby_executable_hooks | ||
# | ||
# This file was generated by RubyGems. | ||
# | ||
# The application 'stripe' is installed as part of a gem, and | ||
# this file is here to facilitate running it. | ||
# | ||
|
||
require 'rubygems' | ||
|
||
version = ">= 0.a" | ||
|
||
str = ARGV.first | ||
if str | ||
str = str.b[/\A_(.*)_\z/, 1] | ||
if str and Gem::Version.correct?(str) | ||
version = str | ||
ARGV.shift | ||
end | ||
end | ||
|
||
if Gem.respond_to?(:activate_bin_path) | ||
load Gem.activate_bin_path('stripe', 'stripe-console', version) | ||
else | ||
gem "stripe", version | ||
load Gem.bin_path("stripe", "stripe-console", version) | ||
end |
Binary file not shown.
Oops, something went wrong.