Skip to content
This repository has been archived by the owner on Mar 27, 2023. It is now read-only.

Commit

Permalink
Add support for listing standalone transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
Tuuleh committed Aug 11, 2016
1 parent c019032 commit 02ed615
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
14 changes: 14 additions & 0 deletions app/controllers/api/stateless/braintree/transactions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Api
module Stateless
module Braintree
class TransactionsController < StatelessController
before_filter :authenticate_request!

def index
@transactions = PaymentHelper::Braintree.transactions_for_member(@current_member)
end

end
end
end
end
4 changes: 4 additions & 0 deletions app/controllers/api/stateless/payment_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@ def subscriptions_for_member(member)
def subscription_for_member(member:, id:)
customer(member).subscriptions.find(id)
end

def transactions_for_member(member)
customer(member).transactions.where(subscription_id: nil)
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
json.array! @transactions do |transaction|
json.(transaction, :id, :status, :amount, :created_at)
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@
namespace :braintree do
resources :payment_methods
resources :subscriptions
resources :transactions
end
namespace :auth do
post :password
Expand Down
54 changes: 54 additions & 0 deletions spec/requests/api/stateless/transactions_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require 'rails_helper'

describe 'API::Stateless Subscriptions' do
include Requests::JsonHelpers
include AuthToken
let!(:member) { create(:member, email: 'test@example.com') }
let!(:customer) { create(:payment_braintree_customer, member: member) }
let!(:subscription) { create(:payment_braintree_subscription, customer: customer) }
let!(:subscription_transaction) { create(:payment_braintree_transaction, subscription: subscription, customer: customer ) }
let!(:standalone_transaction_a) { create(:payment_braintree_transaction, customer: customer ) }
let!(:standalone_transaction_b) { create(:payment_braintree_transaction, customer: customer ) }

before :each do
member.create_authentication(password: 'password')
end

def auth_headers
token = encode_jwt(member.token_payload)
{ authorization: "Bearer #{token}" }
end

describe 'GET index' do
it 'returns a list of stand-alone transactions for member' do
get '/api/stateless/braintree/transactions', nil, auth_headers
expect(response.status).to eq(200)
expect(json_hash).to include({
id: standalone_transaction_a.id,
status: standalone_transaction_a.status,
amount: standalone_transaction_a.amount,
created_at: standalone_transaction_a.created_at
}.as_json)
expect(json_hash).to include({
id: standalone_transaction_b.id,
status: standalone_transaction_b.status,
amount: standalone_transaction_b.amount,
created_at: standalone_transaction_b.created_at
}.as_json)
end
it 'does not list transactions that are associated with subscriptions' do
get '/api/stateless/braintree/transactions', nil, auth_headers
expect(response.status).to eq(200)
expect(json_hash).to_not include({
id: subscription_transaction.id,
status: subscription_transaction.status,
amount: subscription_transaction.amount,
created_at: subscription_transaction.created_at
}.as_json)
end
end

end



0 comments on commit 02ed615

Please sign in to comment.