payload-api 0.4.1 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/test.yml +2 -0
  3. data/LICENSE +1 -1
  4. data/README.md +23 -2
  5. data/lib/payload/arm/attr.rb +169 -0
  6. data/lib/payload/arm/object.rb +44 -1
  7. data/lib/payload/arm/request.rb +66 -6
  8. data/lib/payload/arm/session.rb +13 -9
  9. data/lib/payload/exceptions.rb +15 -0
  10. data/lib/payload/objects.rb +81 -18
  11. data/lib/payload/version.rb +2 -2
  12. data/lib/payload.rb +15 -5
  13. data/spec/objects/v1/access_token_spec.rb +19 -0
  14. data/spec/objects/v1/account_spec.rb +97 -0
  15. data/spec/objects/v1/billing_spec.rb +54 -0
  16. data/spec/objects/v1/invoice_spec.rb +53 -0
  17. data/spec/objects/v1/payment_link_spec.rb +50 -0
  18. data/spec/objects/v1/payment_method_spec.rb +106 -0
  19. data/spec/objects/{payment_spec.rb → v1/payment_spec.rb} +5 -6
  20. data/spec/objects/v1/session_spec.rb +89 -0
  21. data/spec/objects/v1/transaction_spec.rb +55 -0
  22. data/spec/objects/v2/account_spec.rb +211 -0
  23. data/spec/objects/v2/invoice_spec.rb +53 -0
  24. data/spec/objects/v2/payment_method_spec.rb +106 -0
  25. data/spec/objects/v2/transaction_spec.rb +48 -0
  26. data/spec/payload/arm/arm_request_query_spec.rb +226 -0
  27. data/spec/payload/arm/attr_spec.rb +216 -0
  28. data/spec/payload/arm/object_spec.rb +114 -0
  29. data/spec/payload/arm/request_format_integration_spec.rb +166 -0
  30. data/spec/payload/arm/request_spec.rb +259 -1
  31. data/spec/payload/arm/session_spec.rb +40 -0
  32. data/spec/payload/exceptions_spec.rb +82 -0
  33. data/spec/support/helpers/v1_helpers.rb +159 -0
  34. data/spec/support/helpers/v2_helpers.rb +205 -0
  35. data/spec/support/helpers.rb +15 -0
  36. data/spec/support/helpers_spec.rb +21 -0
  37. metadata +28 -6
@@ -0,0 +1,19 @@
1
+ require 'payload'
2
+ require 'payload/arm/object'
3
+ require_relative '../../support/helpers'
4
+
5
+ RSpec.describe 'Access Token Integration Tests' do
6
+ include_context 'test helpers'
7
+
8
+ let(:session) { Payload::Session.new(Payload.api_key, Payload.api_url, 1) }
9
+
10
+ describe 'Access Token' do
11
+ it 'creates a client token' do
12
+ client_token = session.ClientToken.create
13
+ expect(client_token.status).to eq('active')
14
+ expect(client_token.type).to eq('client')
15
+ expect(client_token.environ).to eq('test')
16
+ end
17
+ end
18
+ end
19
+
@@ -0,0 +1,97 @@
1
+ require 'payload'
2
+ require 'payload/arm/object'
3
+ require_relative '../../support/helpers'
4
+
5
+ RSpec.describe 'Account Integration Tests - V1' do
6
+ include_context 'test helpers'
7
+
8
+ let(:session) { Payload::Session.new(Payload.api_key, Payload.api_url, 1) }
9
+ let(:h) { V1Helpers.new(session) }
10
+
11
+ describe 'Customer Account' do
12
+ it 'creates a customer account' do
13
+ customer_account = session.Customer.create(name: 'Test', email: 'test@example.com')
14
+ expect(customer_account.id).to be_truthy
15
+ end
16
+
17
+ it 'deletes a customer account' do
18
+ cust_account = session.Customer.create(name: 'Test', email: 'test@example.com')
19
+ cust_account.delete
20
+
21
+ expect {
22
+ session.Customer.get(cust_account.id)
23
+ }.to raise_error(Payload::NotFound)
24
+ end
25
+
26
+ it 'creates multiple accounts' do
27
+ rand_email1 = (0...5).map { ('a'..'z').to_a[rand(26)] }.join + '@example.com'
28
+ rand_email2 = (0...5).map { ('a'..'z').to_a[rand(26)] }.join + '@example.com'
29
+
30
+ name1 = 'Matt Perez'
31
+ name2 = 'Andrea Kearney'
32
+
33
+ accounts_to_create = [
34
+ session.Customer.new(email: rand_email1, name: name1),
35
+ session.Customer.new(email: rand_email2, name: name2)
36
+ ]
37
+ session.create(accounts_to_create)
38
+
39
+ get_account_1 = session.Customer.filter_by(email: rand_email1).all[0]
40
+ get_account_2 = session.Customer.filter_by(email: rand_email2).all[0]
41
+
42
+ expect(get_account_1).to be_truthy
43
+ expect(get_account_2).to be_truthy
44
+ end
45
+
46
+ it 'gets a processing account' do
47
+ proc_account = h.create_processing_account
48
+ retrieved = session.ProcessingAccount.get(proc_account.id)
49
+ expect(retrieved).to be_truthy
50
+ expect(retrieved.status).to eq('pending')
51
+ end
52
+
53
+ it 'pages and orders results' do
54
+ accounts_to_create = [
55
+ session.Customer.new(email: 'account1@example.com', name: 'Randy Robson'),
56
+ session.Customer.new(email: 'account2@example.com', name: 'Brandy Bobson'),
57
+ session.Customer.new(email: 'account3@example.com', name: 'Mandy Johnson')
58
+ ]
59
+ session.create(accounts_to_create)
60
+ customers = session.Customer.filter_by(order_by: 'created_at', limit: 3, offset: 1).all
61
+
62
+ expect(customers.length).to eq(3)
63
+ require 'time'
64
+ expect(Time.parse(customers[0].created_at)).to be <= Time.parse(customers[1].created_at)
65
+ expect(Time.parse(customers[1].created_at)).to be <= Time.parse(customers[2].created_at)
66
+ end
67
+
68
+ it 'updates a customer' do
69
+ cust_account = session.Customer.create(name: 'Test', email: 'test@example.com')
70
+ cust_account.update(email: 'test2@example.com')
71
+ expect(cust_account.email).to eq('test2@example.com')
72
+ end
73
+
74
+ it 'updates multiple accounts' do
75
+ customer_account_1 = session.Customer.create(name: 'Brandy', email: 'test1@example.com')
76
+ customer_account_2 = session.Customer.create(name: 'Sandy', email: 'test2@example.com')
77
+ updated_accounts = session.update([
78
+ [customer_account_1, { email: 'brandy@example.com' }],
79
+ [customer_account_2, { email: 'sandy@example.com' }]
80
+ ])
81
+
82
+ expect(updated_accounts[0].email).to eq('brandy@example.com')
83
+ expect(updated_accounts[1].email).to eq('sandy@example.com')
84
+ end
85
+
86
+ it 'gets a customer' do
87
+ cust_account = session.Customer.create(name: 'Test', email: 'test@example.com')
88
+ expect(session.Customer.get(cust_account.id)).to be_truthy
89
+ end
90
+
91
+ it 'selects customer attributes' do
92
+ cust_account = session.Customer.create(name: 'Test', email: 'test@example.com')
93
+ selected = session.Customer.select('id').get(cust_account.id)
94
+ expect(selected['id']).to eq(cust_account.id)
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,54 @@
1
+ require 'payload'
2
+ require 'payload/arm/object'
3
+ require_relative '../../support/helpers'
4
+
5
+ RSpec.describe 'Billing Integration Tests - V1' do
6
+ include_context 'test helpers'
7
+
8
+ let(:session) { Payload::Session.new(Payload.api_key, Payload.api_url, 1) }
9
+ let(:h) { V1Helpers.new(session) }
10
+
11
+ describe 'Billing Schedule' do
12
+ let(:billing_schedule) do
13
+ proc_account = h.create_processing_account
14
+ customer_account = h.create_customer_account
15
+ session.BillingSchedule.create(
16
+ start_date: '2019-01-01',
17
+ end_date: '2019-12-31',
18
+ recurring_frequency: 'monthly',
19
+ type: 'subscription',
20
+ customer_id: customer_account.id,
21
+ processing_id: proc_account.id,
22
+ charges: [
23
+ {
24
+ type: 'option_1',
25
+ amount: 39.99
26
+ }
27
+ ]
28
+ )
29
+ end
30
+
31
+ it 'creates a billing schedule' do
32
+ schedule = billing_schedule
33
+ expect(schedule.charges[0]['amount']).to eq(39.99)
34
+ end
35
+
36
+ it 'updates billing schedule frequency' do
37
+ schedule = billing_schedule
38
+ expect(schedule.charges[0]['amount']).to eq(39.99)
39
+
40
+ schedule.update(recurring_frequency: 'quarterly')
41
+ expect(schedule.recurring_frequency).to eq('quarterly')
42
+ end
43
+
44
+ it 'deletes a billing schedule' do
45
+ schedule = billing_schedule
46
+ schedule.delete
47
+
48
+ expect {
49
+ session.BillingSchedule.get(schedule.id)
50
+ }.to raise_error(Payload::NotFound)
51
+ end
52
+ end
53
+ end
54
+
@@ -0,0 +1,53 @@
1
+ require 'payload'
2
+ require 'payload/arm/object'
3
+ require 'date'
4
+ require_relative '../../support/helpers'
5
+
6
+ RSpec.describe 'Invoice Integration Tests - V1' do
7
+ include_context 'test helpers'
8
+
9
+ let(:session) { Payload::Session.new(Payload.api_key, Payload.api_url, 1) }
10
+ let(:h) { V1Helpers.new(session) }
11
+
12
+ let(:proc_account) { h.create_processing_account }
13
+ let(:customer_account) { h.create_customer_account }
14
+ let(:invoice) { h.create_invoice(proc_account, customer_account) }
15
+
16
+ describe 'Invoice' do
17
+ it 'creates an invoice' do
18
+ inv = invoice
19
+ expect(inv.due_date).to eq(Date.today.strftime('%Y-%m-%d'))
20
+ expect(inv.status).to eq('unpaid')
21
+ end
22
+
23
+ it 'pays an invoice' do
24
+ inv = invoice
25
+ expect(inv.due_date).to eq(Date.today.strftime('%Y-%m-%d'))
26
+ expect(inv.status).to eq('unpaid')
27
+
28
+ amount = inv.amount_due
29
+
30
+ if inv.status != 'paid'
31
+ h.create_card_payment(
32
+ proc_account.id,
33
+ amount: amount,
34
+ description: 'Test Payment',
35
+ customer_id: customer_account.id,
36
+ invoice_id: inv.id
37
+ )
38
+ end
39
+
40
+ get_invoice = session.Invoice.get(inv.id)
41
+ expect(get_invoice.status).to eq('paid')
42
+ end
43
+
44
+ it 'deletes an invoice' do
45
+ inv = invoice
46
+ inv.delete
47
+
48
+ expect {
49
+ session.Invoice.get(inv.id)
50
+ }.to raise_error(Payload::NotFound)
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,50 @@
1
+ require 'payload'
2
+ require 'payload/arm/object'
3
+ require_relative '../../support/helpers'
4
+
5
+ RSpec.describe 'Payment Link Integration Tests - V1' do
6
+ include_context 'test helpers'
7
+
8
+ let(:session) { Payload::Session.new(Payload.api_key, Payload.api_url, 1) }
9
+ let(:h) { V1Helpers.new(session) }
10
+
11
+ describe 'Payment Link' do
12
+
13
+ it 'creates a one-time payment link' do
14
+ proc_account = h.create_processing_account
15
+ payment_link = h.create_payment_link_one_time(proc_account)
16
+
17
+ expect(payment_link.processing_id).to eq(proc_account.id)
18
+ expect(payment_link.type).to eq('one_time')
19
+ end
20
+
21
+ it 'creates a reusable payment link' do
22
+ proc_account = h.create_processing_account
23
+ payment_link = h.create_payment_link_reusable(proc_account)
24
+
25
+ expect(payment_link.processing_id).to eq(proc_account.id)
26
+ expect(payment_link.type).to eq('reusable')
27
+ end
28
+
29
+ it 'deletes a reusable payment link' do
30
+ proc_account = h.create_processing_account
31
+ payment_link = h.create_payment_link_reusable(proc_account)
32
+ payment_link.delete
33
+
34
+ expect {
35
+ session.PaymentLink.get(payment_link.id)
36
+ }.to raise_error(Payload::NotFound)
37
+ end
38
+
39
+ it 'deletes a one-time payment link' do
40
+ proc_account = h.create_processing_account
41
+ payment_link = h.create_payment_link_one_time(proc_account)
42
+ payment_link.delete
43
+
44
+ expect {
45
+ session.PaymentLink.get(payment_link.id)
46
+ }.to raise_error(Payload::NotFound)
47
+ end
48
+ end
49
+ end
50
+
@@ -0,0 +1,106 @@
1
+ require 'payload'
2
+ require 'payload/arm/object'
3
+ require_relative '../../support/helpers'
4
+
5
+ RSpec.describe 'Payment Method Integration Tests - V1' do
6
+ include_context 'test helpers'
7
+
8
+ let(:session) { Payload::Session.new(Payload.api_key, Payload.api_url, 1) }
9
+ let(:h) { V1Helpers.new(session) }
10
+ let(:proc_account) { h.create_processing_account }
11
+
12
+ describe 'Payment Methods' do
13
+ it 'creates a payment with card' do
14
+ card_payment = h.create_card_payment(proc_account.id)
15
+ expect(card_payment.status).to eq('processed')
16
+ end
17
+
18
+ it 'creates a payment with bank account' do
19
+ bank_payment = h.create_bank_payment
20
+ expect(bank_payment.status).to eq('processed')
21
+ end
22
+
23
+ it 'filters payments' do
24
+ rand_description = (0...10).map { ('a'..'z').to_a[rand(26)] }.join
25
+
26
+ amounts = [90.0, 100.0, 110.0]
27
+ card_payments = []
28
+ amounts.each do |amount|
29
+ card_payment = h.create_card_payment(proc_account.id, amount: amount, description: rand_description)
30
+ card_payments << card_payment
31
+ end
32
+
33
+ payments = session.Transaction.filter_by(
34
+ type: 'payment',
35
+ amount: '100',
36
+ description: rand_description
37
+ ).all
38
+
39
+ expect(payments.length).to be == 1
40
+ expect(payments.map(&:id)).to include(card_payments[1].id)
41
+ end
42
+
43
+ it 'voids a card payment' do
44
+ card_payment = h.create_card_payment(proc_account.id)
45
+ card_payment.update(status: 'voided')
46
+ expect(card_payment.status).to eq('voided')
47
+ end
48
+
49
+ it 'voids a bank payment' do
50
+ bank_payment = h.create_bank_payment
51
+ bank_payment.update(status: 'voided')
52
+ expect(bank_payment.status).to eq('voided')
53
+ end
54
+
55
+ it 'refunds a card payment' do
56
+ card_payment = h.create_card_payment(proc_account.id)
57
+ refund = h.create_refund(card_payment)
58
+
59
+ expect(refund.type).to eq('refund')
60
+ expect(refund.amount).to eq(card_payment.amount)
61
+ end
62
+
63
+ it 'partially refunds a card payment' do
64
+ card_payment = h.create_card_payment(proc_account.id)
65
+ amount = (card_payment.amount/2).round(2) # rounded to 2 decimal places
66
+ refund = h.create_refund(card_payment, amount: amount)
67
+
68
+ expect(refund.type).to eq('refund')
69
+ expect(refund.amount).to eq(amount)
70
+ end
71
+
72
+ it 'creates a blind refund for card payment' do
73
+ refund = h.create_blind_refund(10, proc_account.id)
74
+
75
+ expect(refund.type).to eq('refund')
76
+ expect(refund.amount).to eq(10)
77
+ end
78
+
79
+ it 'refunds a bank payment' do
80
+ bank_payment = h.create_bank_payment
81
+ refund = h.create_refund(bank_payment)
82
+
83
+ expect(refund.type).to eq('refund')
84
+ expect(refund.amount).to eq(bank_payment.amount)
85
+ end
86
+
87
+ it 'partially refunds a bank payment' do
88
+ bank_payment = h.create_bank_payment
89
+ amount = (bank_payment.amount/2).round(2) # rounded to 2 decimal places
90
+ refund = h.create_refund(bank_payment, amount: amount)
91
+
92
+ expect(refund.type).to eq('refund')
93
+ expect(refund.amount).to eq(amount)
94
+ end
95
+
96
+ it 'raises error for invalid payment method type' do
97
+ expect {
98
+ session.Transaction.create(
99
+ type: 'invalid',
100
+ card_number: '4242 4242 4242 4242',
101
+ expiry: '12/29'
102
+ )
103
+ }.to raise_error(Payload::InvalidAttributes)
104
+ end
105
+ end
106
+ end
@@ -1,7 +1,7 @@
1
1
  require "payload"
2
2
  require "payload/arm/object"
3
3
  require 'base64'
4
-
4
+ require_relative '../../support/helpers'
5
5
 
6
6
  RSpec.describe Payload::ARMRequest do
7
7
 
@@ -46,11 +46,10 @@ RSpec.describe Payload::ARMRequest do
46
46
  payment = instance.create(
47
47
  amount: 129.0,
48
48
  customer_id: 'acct_3bW9JMoGYQul5fCIa9f8q',
49
- allocations: [
50
- Payload::PaymentItem.new(
51
- invoice_id: 'inv_3eNP6uf94xHTXr0rMyvZJ'
52
- )
53
- ],
49
+ allocations: [{
50
+ entry_type: 'payment',
51
+ invoice_id: 'inv_3eNP6uf94xHTXr0rMyvZJ'
52
+ }],
54
53
  )
55
54
 
56
55
  expect(payment.id).to eq($test_id)
@@ -0,0 +1,89 @@
1
+ require 'payload'
2
+ require 'payload/arm/object'
3
+ require 'payload/arm/session'
4
+ require 'date'
5
+ require_relative '../../support/helpers'
6
+
7
+ RSpec.describe 'Session Integration Tests' do
8
+ include_context 'test helpers'
9
+
10
+ describe 'Session' do
11
+ let(:session) { Payload::Session.new(Payload.api_key, Payload.api_url, 1) }
12
+
13
+ it 'creates a customer account with session' do
14
+ customer_account = session.Customer.create(name: 'Test', email: 'test@example.com')
15
+ expect(customer_account.id).to be_truthy
16
+ end
17
+
18
+ it 'deletes with session' do
19
+ customer_account = session.Customer.create(name: 'Test', email: 'test@example.com')
20
+ customer_account.delete
21
+
22
+ expect {
23
+ session.Customer.get(customer_account.id)
24
+ }.to raise_error(Payload::NotFound)
25
+ end
26
+
27
+ it 'creates multiple accounts with session' do
28
+ rand_email1 = (0...5).map { ('a'..'z').to_a[rand(26)] }.join + '@example.com'
29
+ rand_email2 = (0...5).map { ('a'..'z').to_a[rand(26)] }.join + '@example.com'
30
+
31
+ session.create([
32
+ session.Customer.new(email: rand_email1, name: 'Matt Perez'),
33
+ session.Customer.new(email: rand_email2, name: 'Andrea Kearney')
34
+ ])
35
+
36
+ get_account_1 = session.Customer.filter_by(email: rand_email1).all[0]
37
+ get_account_2 = session.Customer.filter_by(email: rand_email2).all[0]
38
+
39
+ expect(get_account_1).to be_truthy
40
+ expect(get_account_2).to be_truthy
41
+ end
42
+
43
+ it 'pages and orders results with session' do
44
+ session.create([
45
+ session.Customer.new(email: 'account1@example.com', name: 'Randy Robson'),
46
+ session.Customer.new(email: 'account2@example.com', name: 'Brandy Bobson'),
47
+ session.Customer.new(email: 'account3@example.com', name: 'Mandy Johnson')
48
+ ])
49
+
50
+ customers = session.Customer.filter_by(order_by: 'created_at', limit: 3, offset: 1).all
51
+
52
+ expect(customers.length).to eq(3)
53
+ require 'time'
54
+ expect(Time.parse(customers[0].created_at)).to be <= Time.parse(customers[1].created_at)
55
+ expect(Time.parse(customers[1].created_at)).to be <= Time.parse(customers[2].created_at)
56
+ end
57
+
58
+ it 'updates customer with session' do
59
+ customer_account = session.Customer.create(name: 'Test', email: 'test@example.com')
60
+ customer_account.update(email: 'test2@example.com')
61
+ expect(customer_account.email).to eq('test2@example.com')
62
+ end
63
+
64
+ it 'updates multiple accounts with session' do
65
+ customer_account_1 = session.Customer.create(name: 'Brandy', email: 'test1@example.com')
66
+ customer_account_2 = session.Customer.create(name: 'Sandy', email: 'test2@example.com')
67
+
68
+ updated_accounts = session.update([
69
+ [customer_account_1, { email: 'brandy@example.com' }],
70
+ [customer_account_2, { email: 'sandy@example.com' }]
71
+ ])
72
+
73
+ expect(updated_accounts[0].email).to eq('brandy@example.com')
74
+ expect(updated_accounts[1].email).to eq('sandy@example.com')
75
+ end
76
+
77
+ it 'gets customer with session' do
78
+ customer_account = session.Customer.create(name: 'Test', email: 'test@example.com')
79
+ expect(session.Customer.get(customer_account.id)).to be_truthy
80
+ end
81
+
82
+ it 'selects customer attributes with session' do
83
+ customer_account = session.Customer.create(name: 'Test', email: 'test@example.com')
84
+ selected = session.query(Payload::Customer).select('id').get(customer_account.id)
85
+ expect(selected['id']).to eq(customer_account.id)
86
+ end
87
+ end
88
+ end
89
+
@@ -0,0 +1,55 @@
1
+ require 'payload'
2
+ require 'payload/arm/object'
3
+ require_relative '../../support/helpers'
4
+
5
+ RSpec.describe 'Transaction Integration Tests - V1' do
6
+ include_context 'test helpers'
7
+
8
+ let(:session) { Payload::Session.new(Payload.api_key, Payload.api_url, 1) }
9
+ let(:h) { V1Helpers.new(session) }
10
+ let(:proc_account) { h.create_processing_account }
11
+
12
+ describe 'Transactions' do
13
+
14
+ it 'has empty transaction ledger' do
15
+ card_payment = h.create_card_payment(proc_account.id)
16
+ transaction = session.Transaction.select('*', 'ledger').get(card_payment.id)
17
+ expect(transaction.ledger).to eq([])
18
+ end
19
+
20
+ it 'tests unified payout batching' do
21
+ h.create_blind_refund(10, proc_account.id)
22
+
23
+ transactions = session.Transaction.select('*', 'ledger')
24
+ .filter_by(type: 'refund', processing_id: proc_account.id)
25
+ .all
26
+
27
+ expect(transactions.length).to eq(1)
28
+ expect(transactions[0].processing_id).to eq(proc_account.id)
29
+ end
30
+
31
+ it 'gets transactions' do
32
+ h.create_card_payment(proc_account.id)
33
+ payments = session.Transaction.filter_by(status: 'processed', type: 'payment').all
34
+ expect(payments.length).to be > 0
35
+ end
36
+
37
+ it 'checks risk flag' do
38
+ card_payment = h.create_card_payment(proc_account.id)
39
+ expect(card_payment.risk_flag).to eq('allowed')
40
+ end
41
+
42
+ it 'updates processed transaction' do
43
+ card_payment = h.create_card_payment(proc_account.id)
44
+ card_payment.update(status: 'voided')
45
+ expect(card_payment.status).to eq('voided')
46
+ end
47
+
48
+ it 'raises error for transaction not found' do
49
+ expect {
50
+ session.Transaction.get('invalid')
51
+ }.to raise_error(Payload::NotFound)
52
+ end
53
+ end
54
+ end
55
+