-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #75 from OneBusAway/stripe
Stripe Integration
- Loading branch information
Showing
16 changed files
with
234 additions
and
21 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 |
---|---|---|
@@ -1,15 +1,18 @@ | ||
name: Fly Deploy | ||
name: Deploy to Fly.io | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
push: | ||
branches: | ||
- main | ||
|
||
env: | ||
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }} | ||
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }} | ||
|
||
jobs: | ||
deploy: | ||
name: Deploy app | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: superfly/flyctl-actions/setup-flyctl@master | ||
- run: flyctl deploy --remote-only | ||
build-and-deploy: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- uses: superfly/flyctl-actions/setup-flyctl@master | ||
- run: flyctl deploy --remote-only |
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 +1 @@ | ||
ruby-2.7.3 | ||
ruby-3.2 |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
web: ./bin/rails server | ||
web: bundle exec rails server -p 3000 | ||
worker: bundle exec sidekiq -t 25 -C config/sidekiq.yml |
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,19 @@ | ||
class Api::V1::PaymentIntentsController < Api::V1::ApiController | ||
skip_before_action :load_region | ||
|
||
def create | ||
if params[:donation_frequency] == 'recurring' | ||
@recurring_response = Donations::Recurring.new(params[:donation_amount_in_cents]).run | ||
@error = @recurring_response.error | ||
else | ||
@intent, @error = Donations::OneTime.new(params[:donation_amount_in_cents]).run | ||
end | ||
|
||
if @error | ||
logger.error(@error.message) | ||
head :internal_server_error | ||
else | ||
respond_to {|format| format.json} | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module Donations | ||
class OneTime | ||
def initialize(donation_amount_in_cents) | ||
@donation_amount_in_cents = donation_amount_in_cents | ||
end | ||
|
||
def run | ||
intent, error = [nil, nil] | ||
|
||
begin | ||
intent = Stripe::PaymentIntent.create( | ||
{ | ||
amount: @donation_amount_in_cents, | ||
currency: 'usd', | ||
# In the latest version of the API, specifying the | ||
# `automatic_payment_methods` parameter is optional | ||
# because Stripe enables its functionality by default. | ||
automatic_payment_methods: { enabled: true }, | ||
} | ||
) | ||
rescue Stripe::StripeError => e | ||
error = e | ||
end | ||
|
||
[intent, error] | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
|
||
module Donations | ||
class Recurring | ||
def initialize(donation_amount_in_cents) | ||
@donation_amount_in_cents = donation_amount_in_cents | ||
end | ||
|
||
def run | ||
recurring_response = RecurringResponse.new(@donation_amount_in_cents) | ||
|
||
begin | ||
customer = Stripe::Customer.create() | ||
recurring_response.customer_id = customer.id | ||
|
||
recurring_response.ephemeral_key = Stripe::EphemeralKey.create( | ||
{customer: recurring_response.customer_id}, | ||
{stripe_version: '2023-08-16'} | ||
) | ||
recurring_response.subscription = create_subscription(recurring_response.customer_id) | ||
rescue Stripe::StripeError => e | ||
recurring_response.error = e | ||
end | ||
|
||
recurring_response | ||
end | ||
|
||
private | ||
|
||
def create_subscription(customer_id) | ||
# Create a new price for the custom donation amount | ||
price = Stripe::Price.create( | ||
unit_amount: @donation_amount_in_cents, | ||
currency: 'usd', | ||
recurring: { interval: 'month' }, | ||
product: $stripe_recurring_donation_product_id, | ||
) | ||
|
||
# Create or update the subscription with the new price | ||
subscription = Stripe::Subscription.create( | ||
customer: customer_id, | ||
items: [{ price: price.id }], | ||
payment_behavior: 'default_incomplete', | ||
expand: ['latest_invoice.payment_intent'], | ||
) | ||
|
||
subscription | ||
end | ||
end | ||
|
||
class RecurringResponse | ||
attr_accessor :donation_amount_in_cents, :subscription, :ephemeral_key, :error, :customer_id | ||
|
||
def initialize(donation_amount_in_cents) | ||
@donation_amount_in_cents = donation_amount_in_cents | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
if @recurring_response | ||
json.client_secret(@recurring_response.subscription.latest_invoice.payment_intent.client_secret) | ||
json.customer_id(@recurring_response.customer_id) | ||
json.ephemeral_key(@recurring_response.ephemeral_key.secret) | ||
else | ||
json.client_secret(@intent.client_secret) | ||
end | ||
|
||
json.id(SecureRandom.uuid) |
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 +1 @@ | ||
MJwZKzEdAdLIxRzkIFbpFmuq/5oDLNqTtY+tYmjCvqxSBiO2TDQirH7As1nZ/ZAMAvyKzMI8134OtGvdq+Lem03Eqo74qNfJlKb6GUIqXbPRx4UaLEmSVSRaKQt5Uk3GUT+4f/SGh9ytlKGbKXWffEZGBfygMYMb+Vm301GMbLLOwusfF1p24luvBZC3dhrpUbsdaBLpZ3yJ6lnpZ9aP+A36nwwnbaeSAomlzvgKLPLyOGpx7XwevkhWxCNVoNJSBJTPlEeXXYVwR+tO9N9GqTotKAzcgY2On/HGkO/CY2qaul9aVxPGbxeS68CtEA8n3dj6OXv4TtykMgWCo5PxIYZempadDfT56lIyhZ/l4huhihElUYHSsdD7ztBGFAzsmOC1ncS35foYHF+JOlWbcVdqz8XFaZ1f+lWTyWwtIPPw/QsFUmEU8UKtzhrXt4+kyKF/WDIENa5AMMzSp52fB5qI8FqYKq/rkA/tGw4p4d1q0THxTaGMzqiAtQjZ5H33fMIeu1gYQ/uxlQ5aOkUZNTU2PcDTM+Kz6ezKbpYRGgYmeXrAqkIJQ/Jth1uXXON2P+5OitgZc58oYis6JLJdVrrDoKuVhq1K4jujQopuLZMsaH/dbCbJwkniEs8SaA5/Iiz6lnS3sDZgo/ZYA9wjZw/rUrjsGtOc9w==--DkBThg2KX7o0BrCe--yvrCQA4Puf8wp4WLCcNpuQ== | ||
iK+va/I+Cg4UlUl1yXabH1CxYW4BuKuwDqBbtZEK0cAu9FuAik8b/sE+GsVcISaW6xjb19XeOWwgPZbx++Vfl8yNWRCpURkgSPyCmTa4Vqr8tif5jU0Osn9ttlLhrvtoqfDG0acQbbV0h8uiwkLTBGyULLzyQp/6ihdxNS79STAB7VEcBHlNHF/SqIMWPm+X6Y40bL8TtWDs8xXuF5qYnroTjffDqJBRngmPsSiFfddMw8jrZrsIgCYxwHeag/L2XKspeH9swMK4EyMmNs+TkxuY+i6TCQMDscIrgcg1nlHsV3nftHPSGW1sWVDdzhHkv/WJmC5DY483uGdnlrrmcMevIEDtz/lLySurKZjko51xcNxxJ4+tdTjg0wgY/134CqigEgbRpxNALUqLoPj8ABoAZO51njopDs9QAfvJD1fI3aZyZRcqEeRhKxWCSAGI6t4e2wSXL7K4F+aAO3fGYxrZt1Y71y9MGyQNdp3rSOsQChtdw1QV5BzUsCCdqFZzV6u+5gY0x2KfLnEu0WpYAEgvOBHCeWe7HOIXar5HTCP42jWg0PUxbLDpnKvLnl4HXcrlvFWo9OmBaw+1HUJcLdWqSEqVnh+l+T9/JQnXUVnpTz2Enus9JLntU8nISyOqHTOgGoSQqlXQijiB95SAf4Nu+gF9NKZiUkg/9wRr8w5zN6mgvSntH4vn2g+pWAuJEeD2Oop+j57j1atoXKFqIHf77FOSP9y0M5COw3IzOnRqZ/9hP4+FGwFB7xWqyd0VXHaIltzlIOJAC+Fm0q1X+P+pj7RM0PL+Vy/znG5OFiO+QhaIEfQfkuIQupqrRhiDUDoR2Yu4VUhALHQCF/9I4miP1K7nd8zTe2AKmspYUFdJ0nFzVPZ300jgex+B2irkFVwSiV1HkBE2gw3Ih26LdgtdrjMtmPJwK9Zgd3NxXE/WiT8ROD14CcZquJLCuGx+af8qfOr05j7CGHvob0xjqSiWEST59eaUWl+2JH7DMV73mCzzYub8+VVA9w==--XBBlGfnBwetha++2--tU5synzhrItLtVOfnR0mQw== |
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,7 @@ | ||
if Rails.env.production? | ||
Stripe.api_key = Rails.application.credentials.dig(:stripe_secret_key, :production) | ||
$stripe_recurring_donation_product_id = "prod_OqlLl6mR66dLVQ" | ||
else | ||
Stripe.api_key = Rails.application.credentials.dig(:stripe_secret_key, :test) | ||
$stripe_recurring_donation_product_id = "prod_P1xUtsgjEfkGgu" | ||
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
Empty file.
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,55 @@ | ||
require 'rails_helper' | ||
|
||
describe "Donations::Recurring" do | ||
describe '#initialize' do | ||
it 'sets the donation amount' do | ||
donation = Donations::Recurring.new(500) | ||
expect(donation.instance_variable_get(:@donation_amount_in_cents)).to eq(500) | ||
end | ||
end | ||
|
||
describe '#run' do | ||
before do | ||
allow(Stripe::Customer).to receive(:create).and_return(double('Customer', id: 'cus_test')) | ||
allow(Stripe::EphemeralKey).to receive(:create).and_return(double('EphemeralKey')) | ||
allow_any_instance_of(Donations::Recurring).to receive(:create_subscription).and_return(double('Subscription')) | ||
end | ||
|
||
it 'creates a new Stripe customer' do | ||
donation = Donations::Recurring.new(500) | ||
donation.run | ||
expect(Stripe::Customer).to have_received(:create) | ||
end | ||
|
||
it 'creates a new Stripe ephemeral key' do | ||
donation = Donations::Recurring.new(500) | ||
donation.run | ||
expect(Stripe::EphemeralKey).to have_received(:create) | ||
end | ||
|
||
it 'creates or finds a subscription' do | ||
donation = Donations::Recurring.new(500) | ||
donation.run | ||
expect(donation).to have_received(:create_subscription) | ||
end | ||
end | ||
|
||
describe '#create_subscription' do | ||
before do | ||
allow(Stripe::Price).to receive(:create).and_return(double('Price', id: 'price_test')) | ||
allow(Stripe::Subscription).to receive(:create).and_return(double('Subscription')) | ||
end | ||
|
||
it 'creates a new Stripe price' do | ||
donation = Donations::Recurring.new(500) | ||
donation.send(:create_subscription, 'cus_test') | ||
expect(Stripe::Price).to have_received(:create) | ||
end | ||
|
||
it 'creates a new Stripe subscription' do | ||
donation = Donations::Recurring.new(500) | ||
donation.send(:create_subscription, 'cus_test') | ||
expect(Stripe::Subscription).to have_received(:create) | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe "Api::V1::PaymentIntents", type: :request do | ||
let(:donation_amount_in_cents) { 1000 } | ||
|
||
describe "POST /create" do | ||
it "creates a new PaymentIntent" do | ||
allow(Stripe::PaymentIntent).to receive(:create).and_return(OpenStruct.new(client_secret: "12435")) | ||
post api_v1_payment_intents_path(format: "json"), params: { donation_amount_in_cents: donation_amount_in_cents } | ||
expect(response).to be_successful | ||
expect(response.body).to include("12435") | ||
end | ||
|
||
context "when there is a Stripe error" do | ||
before do | ||
allow(Stripe::PaymentIntent).to receive(:create).and_raise(Stripe::StripeError.new) | ||
end | ||
|
||
it "returns a 500 status" do | ||
post api_v1_payment_intents_path, params: { donation_amount_in_cents: donation_amount_in_cents } | ||
|
||
expect(response).to have_http_status(:internal_server_error) | ||
end | ||
end | ||
end | ||
end |