Skip to content

Commit dc83c3d

Browse files
authored
EPT-2966 Add carts endpoints (#85)
* Add carts endpoints * Add cart tests * Use factories
1 parent fdb7ba5 commit dc83c3d

File tree

20 files changed

+6991
-0
lines changed

20 files changed

+6991
-0
lines changed

lib/beyond_api/services/checkout/cart.rb

Lines changed: 476 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe BeyondApi::Checkout::Cart, vcr: true do
4+
let(:client) { described_class.new(api_url: ENV.fetch('API_URL', nil), access_token: beyond_access_token) }
5+
6+
context 'with cart' do
7+
before(:each) do
8+
@cart = client.create
9+
end
10+
11+
describe '.create' do
12+
it 'creates a new cart' do
13+
expect(@cart).not_to be nil
14+
expect(@cart[:id]).to be_kind_of(String)
15+
end
16+
end
17+
18+
describe '.find' do
19+
it 'retrieves the details of a cart' do
20+
cart_details = client.find(@cart[:id])
21+
expect(cart_details).not_to be nil
22+
expect(cart_details[:id]).to eq(@cart[:id])
23+
end
24+
end
25+
26+
describe '.billing_address' do
27+
it 'sets the billing address of the cart' do
28+
body = build(:address_data)
29+
billing_address = client.billing_address(@cart[:id], body)
30+
expect(billing_address).not_to be nil
31+
expect(billing_address[:billing_address][:first_name]).to eq(body[:first_name])
32+
end
33+
end
34+
35+
describe '.shipping_address' do
36+
it 'sets the shipping address of the cart' do
37+
body = build(:address_data)
38+
shipping_address = client.shipping_address(@cart[:id], body)
39+
expect(shipping_address).not_to be nil
40+
expect(shipping_address[:shipping_address][:first_name]).to eq(body[:first_name])
41+
end
42+
end
43+
44+
describe '.payment_methods' do
45+
it 'lists all applicable payment methods for the current cart' do
46+
response = client.payment_methods(@cart[:id])
47+
expect(response).not_to be nil
48+
expect(response.dig(:embedded, :payment_methods)).to be_an(Array)
49+
end
50+
end
51+
52+
describe '.assign_payment_method_to_default' do
53+
it 'sets the cart payment method to the current default payment method' do
54+
response = client.assign_payment_method_to_default(@cart[:id])
55+
expect(response).not_to be nil
56+
end
57+
end
58+
59+
describe '.create_payment' do
60+
it 'initiates the creation of a payment' do
61+
body = {
62+
return_uri: 'https://example.com/return',
63+
cancel_uri: 'https://example.com/cancel'
64+
}
65+
# There is no payment method definition associated with this payment method
66+
expect { client.create_payment(@cart[:id], body) }.to raise_error(BeyondApi::Error)
67+
end
68+
end
69+
70+
describe '.create_payment_and_order' do
71+
it 'initiates the creation of a payment and order for the cart' do
72+
body = build(:payment_data)
73+
# There is no payment method definition associated with this payment method
74+
expect { client.create_payment_and_order(@cart[:id], body) }.to raise_error(BeyondApi::Error)
75+
end
76+
end
77+
78+
describe '.assign_shipping_method_to_default' do
79+
it 'sets the cart shipping method to the current default shipping method' do
80+
response = client.assign_shipping_method_to_default(@cart[:id])
81+
expect(response).not_to be nil
82+
end
83+
end
84+
85+
describe '.pickup_options' do
86+
it 'lists all applicable pickup options for the current cart' do
87+
response = client.pickup_options(@cart[:id])
88+
expect(response).not_to be nil
89+
expect(response.dig(:embedded, :pickup_options)).to be_an(Array)
90+
end
91+
end
92+
93+
describe '.redeem_coupon' do
94+
it 'redeems a coupon' do
95+
coupon_code = 'FIRST-ORDER'
96+
# The coupon code validation failed with following messages: Code doesn't exist
97+
expect { client.redeem_coupon(@cart[:id], coupon_code) }.to raise_error(BeyondApi::Error)
98+
end
99+
end
100+
101+
describe '.delete_coupon' do
102+
it 'removes a coupon from the cart' do
103+
coupon_code = 'FIRST-ORDER'
104+
# The coupon code validation failed with following messages: Code doesn't exist
105+
expect { client.redeem_coupon(@cart[:id], coupon_code) } .to raise_error(BeyondApi::Error)
106+
end
107+
end
108+
109+
describe '.delete' do
110+
it 'deletes a cart' do
111+
response = client.delete(@cart[:id])
112+
expect(response).to eq({})
113+
end
114+
end
115+
116+
after(:each) do
117+
client.delete(@cart[:id])
118+
rescue BeyondApi::Error # rubocop:disable Lint/SuppressedException
119+
end
120+
end
121+
end

spec/factories/address_data.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
FactoryBot.define do
4+
factory :address_data, class: Hash do
5+
salutation { 'Mrs' }
6+
gender { 'FEMALE' }
7+
title { 'Dr' }
8+
first_name { 'Astrid' }
9+
middle_name { 'Agnes' }
10+
last_name { 'Alster' }
11+
street { 'Alsterwasserweg' }
12+
house_number { '2' }
13+
street2 { 'Erdgeschoss' }
14+
door_code { '0185' }
15+
address_extension { 'Hinterhof' }
16+
postal_code { '20999' }
17+
dependent_locality { 'Seevetal' }
18+
city { 'Alsterwasser' }
19+
country { 'DE' }
20+
state { 'Hamburg' }
21+
email { 'a.alsterh@example.com' }
22+
phone { '(800) 555-0102' }
23+
mobile { '(800) 555-0103' }
24+
vat_id { '123456789' }
25+
tax_number { '123-34-6789' }
26+
birth_date { '1985-03-20' }
27+
28+
initialize_with { attributes }
29+
end
30+
end

spec/factories/payment_data.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# frozen_string_literal: true
2+
3+
FactoryBot.define do
4+
factory :payment_data, class: Hash do
5+
initialize_with {
6+
{
7+
return_uri: 'https://example.com/return',
8+
cancel_uri: 'https://example.com/cancel',
9+
customer_comment: 'Please send with UPS.',
10+
sales_channel: 'Storefront',
11+
marketing_channel: 'Google Shopping',
12+
marketing_subchannel: 'Summer Sale',
13+
test_order: false,
14+
terms_and_conditions_explicitly_accepted: true
15+
}
16+
}
17+
end
18+
end

0 commit comments

Comments
 (0)