forked from stripe-archive/stripe-payments-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinventory.rb
50 lines (40 loc) · 1.06 KB
/
inventory.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
require 'stripe'
require 'dotenv'
Dotenv.load(File.dirname(__FILE__) + '/../../.env')
Stripe.api_key = ENV['STRIPE_SECRET_KEY']
Stripe.api_version = '2019-03-14'
class Inventory
def self.calculate_payment_amount(items)
total = 0
items.each do |item|
sku = Stripe::SKU.retrieve(item['parent'])
total += sku.price * item['quantity']
end
total
end
def self.get_shipping_cost(id)
shipping_cost = {
free: 0,
express: 500,
}
shipping_cost[id.to_sym]
end
def self.list_products
Stripe::Product.list(limit: 3)
end
def self.list_skus(product_id)
Stripe::SKU.list(
limit: 1,
product: product_id
)
end
def self.retrieve_product(product_id)
Stripe::Product.retrieve(product_id)
end
def self.products_exist(product_list)
valid_products = ['increment', 'shirt', 'pins']
product_list_data = product_list['data']
products_present = product_list_data.map {|product| product['id']}
product_list_data.length == 3 && products_present & valid_products == products_present
end
end