Skip to content

Commit

Permalink
Add payments endpoint.
Browse files Browse the repository at this point in the history
  • Loading branch information
ppostma committed Feb 2, 2024
1 parent a69a753 commit b70ff32
Show file tree
Hide file tree
Showing 6 changed files with 368 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/kentaa/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
require_relative 'api/resources/newsletter_subscription'
require_relative 'api/resources/order'
require_relative 'api/resources/order_item'
require_relative 'api/resources/payment'
require_relative 'api/resources/performance'
require_relative 'api/resources/performance_photo'
require_relative 'api/resources/photo'
Expand Down
4 changes: 4 additions & 0 deletions lib/kentaa/api/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def orders(options = {})
Kentaa::Api::Resources::List.new(@config, options.merge(resource_class: Kentaa::Api::Resources::Order, endpoint_path: '/orders'))
end

def payments(options = {})
Kentaa::Api::Resources::List.new(@config, options.merge(resource_class: Kentaa::Api::Resources::Payment, endpoint_path: '/payments'))
end

def projects(options = {})
Kentaa::Api::Resources::List.new(@config, options.merge(resource_class: Kentaa::Api::Resources::Project, endpoint_path: '/projects'))
end
Expand Down
101 changes: 101 additions & 0 deletions lib/kentaa/api/resources/payment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# frozen_string_literal: true

require 'bigdecimal'

module Kentaa
module Api
module Resources
class Payment < Resource
def object_key
"Payment_#{id}"
end

def site
Kentaa::Api::Resources::Site.new(config, id: site_id, options: options)
end

def site_id
data[:site_id]
end

def currency
data[:currency]
end

def amount
BigDecimal(data[:amount])
end

def invoicenumber
data[:invoicenumber]
end

def payment_method
data[:payment_method]
end

def payment_status
data[:payment_status]
end

def transaction_id
data[:transaction_id]
end

def payment_id
data[:payment_id]
end

def account_iban
data[:account_iban]
end

def account_bic
data[:account_bic]
end

def account_name
data[:account_name]
end

def description
data[:description]
end

def donations
@donations ||= begin
donations = []

if data[:donations]
data[:donations].each do |donation|
donations << Kentaa::Api::Resources::Donation.new(config, data: donation, options: options)
end
end

donations
end
end

def orders
@orders ||= begin
orders = []

if data[:orders]
data[:orders].each do |order|
orders << Kentaa::Api::Resources::Order.new(config, data: order, options: options)
end
end

orders
end
end

private

def load_resource
request.get("/payments/#{id}", options)
end
end
end
end
end
104 changes: 104 additions & 0 deletions spec/fixtures/responses/payment.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
{
"payment": {
"id": 1,
"site_id": 4,
"created_at": "2021-03-30T14:41:48Z",
"updated_at": "2021-03-30T14:46:25Z",
"currency": "EUR",
"amount": "90.4",
"invoicenumber": "T2021.0004.0000001",
"payment_method": "ideal",
"payment_status": "paid",
"transaction_id": "B5C7B7BCAF1ADF6E2032C7CF56969876",
"payment_id": "116D10DD544ECD1E2D77762C5C3E6D61",
"account_iban": "NL44RABO0123456789",
"account_bic": "RABONL2U",
"account_name": "J. de Tester",
"description": "Example payment",
"donations": [
{
"id": 1,
"site_id": 4,
"created_at": "2021-03-30T14:41:48Z",
"updated_at": "2021-03-30T14:46:25Z",
"first_name": "John",
"last_name": "Doe",
"anonymous": false,
"contact_details_type": "visible",
"email": "[email protected]",
"newsletter": false,
"device_type": "desktop",
"locale": "nl",
"frequency_type": "oneoff",
"currency": "EUR",
"amount": "15.0",
"transaction_costs": "0.4",
"start_donation": false,
"registration_fee": true,
"registration_fee_amount": "10.0",
"total_amount": "25.4",
"receivable_amount": "24.0",
"countable": true,
"invoicenumber": "T2021.0004.0000001",
"payment_method": "ideal",
"payment_status": "paid",
"payment_status_at": "2016-03-30T14:46:25Z",
"transaction_id": "B5C7B7BCAF1ADF6E2032C7CF56969876",
"payment_id": "116D10DD544ECD1E2D77762C5C3E6D61",
"payment_description": "Example payment",
"target_url": "https://demo1.kentaa.nl/"
}
],
"orders": [
{
"id": 21059,
"site_id": 4,
"action_id": 8755,
"created_at": "2021-03-30T14:41:48Z",
"updated_at": "2021-03-30T14:46:25Z",
"invoicenumber": "T2021.0004.0000001",
"total_amount": "65.0",
"currency": "EUR",
"payment_status": "paid",
"first_name": "John",
"last_name": "Doe",
"address": {
"address": "Jansbuitensingel 29",
"street": "Jansbuitensingel",
"house_number": "29",
"zipcode": "6811AD",
"city": "Arnhem",
"country": "NL"
},
"items": [
{
"product": {
"id": 26,
"variant_id": 3,
"title": "Sweater",
"variant_title": "XL",
"price": "25.0",
"currency": "EUR"
},
"quantity": 2,
"amount": "50.0",
"currency": "EUR"
},
{
"product": {
"id": 25,
"variant_id": 2,
"title": "T-shirt",
"variant_title": "L",
"price": "15.0",
"currency": "EUR"
},
"quantity": 1,
"amount": "15.0",
"currency": "EUR"
}
]
}
]
}
}
20 changes: 20 additions & 0 deletions spec/kentaa/api/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,26 @@
end
end

describe '#payments' do
describe '#get' do
it 'returns a single payment' do
data = File.read('spec/fixtures/responses/payment.json')
stub_request(:get, 'https://api.kentaa.nl/v1/payments/1').to_return(status: 200, body: data)

payment = client.payments.get(1)
expect(payment).to be_a(Kentaa::Api::Resources::Payment)
expect(payment.invoicenumber).to eq('T2021.0004.0000001')
end

it 'returns an error when the donapaymenttion was not found' do
data = File.read('spec/fixtures/responses/404.json')
stub_request(:get, 'https://api.kentaa.nl/v1/payments/1').to_return(status: 404, body: data)

expect { client.payments.get(1) }.to raise_error(Kentaa::Api::RequestError, '404: Requested resource was not found.')
end
end
end

describe '#projects' do
describe '#all' do
it 'returns an enumerator for retrieving all projects' do
Expand Down
138 changes: 138 additions & 0 deletions spec/kentaa/api/resources/payment_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Kentaa::Api::Resources::Payment do
subject(:response) { described_class.new(config, data: data[:payment]) }

let(:config) { Kentaa::Api::Config.new(api_key: '12345') }
let(:data) { JSON.parse(File.read('spec/fixtures/responses/payment.json'), symbolize_names: true) }

describe '#object_key' do
it 'returns the object key' do
expect(response.object_key).to eq('Payment_1')
end
end

describe '#id' do
it 'returns the resource id' do
expect(response.id).to eq(1)
end
end

describe '#site' do
it 'returns the site resource' do
expect(response.site).to be_a(Kentaa::Api::Resources::Site)
end
end

describe '#site_id' do
it 'returns the site id' do
expect(response.site_id).to eq(4)
end
end

describe '#created_at' do
it 'returns the created_at timestamp' do
expect(response.created_at).to be_a(Time)
end
end

describe '#updated_at' do
it 'returns the updated_at timestamp' do
expect(response.updated_at).to be_a(Time)
end
end

describe '#currency' do
it 'returns the payment currency' do
expect(response.currency).to eq('EUR')
end
end

describe '#amount' do
it 'returns the payment amount' do
expect(response.amount).to eq(BigDecimal('90.4'))
end
end

describe '#invoicenumber' do
it 'returns the invoice number' do
expect(response.invoicenumber).to eq('T2021.0004.0000001')
end
end

describe '#payment_method' do
it 'returns the payment method' do
expect(response.payment_method).to eq('ideal')
end
end

describe '#payment_status' do
it 'returns the payment status' do
expect(response.payment_status).to eq('paid')
end
end

describe '#transaction_id' do
it 'returns the PSP transaction ID' do
expect(response.transaction_id).to eq('B5C7B7BCAF1ADF6E2032C7CF56969876')
end
end

describe '#payment_id' do
it 'returns the PSP payment ID' do
expect(response.payment_id).to eq('116D10DD544ECD1E2D77762C5C3E6D61')
end
end

describe '#account_iban' do
it 'returns the consumer IBAN' do
expect(response.account_iban).to eq('NL44RABO0123456789')
end
end

describe '#account_bic' do
it 'returns the consumer BIC' do
expect(response.account_bic).to eq('RABONL2U')
end
end

describe '#account_name' do
it 'returns the consumer name' do
expect(response.account_name).to eq('J. de Tester')
end
end

describe '#description' do
it 'returns the payment description' do
expect(response.description).to eq('Example payment')
end
end

describe '#donations' do
it 'returns the associated donations' do
expect(response.donations).not_to be_empty
expect(response.donations.count).to eq(1)

donation = response.donations.first
expect(donation).to be_a(Kentaa::Api::Resources::Donation)
expect(donation.first_name).to eq('John')
expect(donation.last_name).to eq('Doe')
expect(donation.total_amount).to eq(BigDecimal('25.4'))
end
end

describe '#orders' do
it 'returns the associated orders' do
expect(response.orders).not_to be_empty
expect(response.orders.count).to eq(1)

order = response.orders.first
expect(order).to be_a(Kentaa::Api::Resources::Order)
expect(order.first_name).to eq('John')
expect(order.last_name).to eq('Doe')
expect(order.total_amount).to eq(65)
end
end
end

0 comments on commit b70ff32

Please sign in to comment.