-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first run at adding basic stripe functionality
- Loading branch information
Showing
8 changed files
with
68 additions
and
1 deletion.
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
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,23 @@ | ||
class V1::PaymentsController < V1::BaseController | ||
|
||
def create | ||
|
||
charge = Stripe::Charge.create(payment_params) | ||
|
||
render json: { charge_id: charge.id } | ||
|
||
rescue Stripe::CardError => e | ||
render json: { error: e.message } | ||
end | ||
|
||
private | ||
|
||
def payment_params | ||
defaults = { | ||
'currency' => 'usd', | ||
'description' => 'donation from [email protected]' | ||
} | ||
|
||
defaults.merge(params.require(:payment).permit(:amount, :source)) | ||
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,6 @@ | ||
Rails.configuration.stripe = { | ||
:publishable_key => ENV['STRIPE_PUBLISHABLE_KEY'], | ||
:secret_key => ENV['STRIPE_SECRET_KEY'] | ||
} | ||
|
||
Stripe.api_key = Rails.configuration.stripe[:secret_key] |
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,27 @@ | ||
require 'rails_helper' | ||
|
||
describe V1::PaymentsController, type: :controller do | ||
|
||
describe "POST create" do | ||
it "works" do | ||
stub_stripe_charge(amount: 400, source: 'test token', charge_id: 'test id') | ||
post :create, payment: { amount: 400, source: 'test token' } | ||
expect(json(response)['charge_id']).to eq 'test id' | ||
end | ||
end | ||
|
||
end | ||
|
||
def json(response) | ||
JSON.parse(response.body) | ||
end | ||
|
||
def stub_stripe_charge(amount: 100, source: 'token', charge_id: 'id') | ||
charge = double('charge', id: charge_id) | ||
allow(Stripe::Charge).to receive(:create). | ||
with(hash_including( | ||
'amount' => amount.to_s, | ||
'source' => source, | ||
'currency' => 'usd')). | ||
and_return(charge) | ||
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