forked from zinc-collective/convene
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfigure-pricing
executable file
·123 lines (104 loc) · 3.94 KB
/
configure-pricing
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env ruby
require 'net/http'
require 'json'
API_URI = URI("https://api.stripe.com/v1/")
API_URI.user = ENV['STRIPE_API_KEY']
# This gets called on the final line of the script.
def main
# https://stripe.com/docs/billing/prices-guide
Net::HTTP.start(API_URI.hostname, API_URI.port, use_ssl: true) do |http|
product = find_or_create_product(name: "Convene", http: http)
raise "Can't find/create product for Convene" unless product
price = find_or_create_price(product_id: product[:id],
amount: 20_00,
nickname: "Early Access Small Room",
currency: :usd,
interval: :month,
http: http)
raise "Can't find/create price for Convene" unless price
# Assembled from
# https://stripe.com/docs/payments/checkout/client-subscription
# TODO: Move this to Compensated and/or Support
puts <<~DOCS
Congratulations! You're set up to sell Convene for $20.00/mo.
Embed the following HTML into your page!
```
<script src="https://js.stripe.com/v3/"></script>
<script>
var stripe = Stripe('#{ENV.fetch('STRIPE_PUBLISHABLE_API_KEY', 'STRIPE_PUBLISHABLE_API_KEY')}');
function beginPurchaseFlow() {
stripe.redirectToCheckout({
lineItems: [{
// Replace with the ID of your price
price: '#{price[:id]}',
quantity: 1,
}],
mode: 'subscription',
successUrl: 'https://example.com/success',
cancelUrl: 'https://example.com/cancel',
}).then(function (result) {
// If `redirectToCheckout` fails due to a browser or network
// error, display the localized error message to your customer
// using `result.error.message`.
});
}
window.addEventListener('DOMContentLoaded', (event) => {
const beginPurchaseButtons = document.getElementsByClassName('begin-purchase-flow')
for(const beginPurchaseButton of beginPurchaseButtons) {
beginPurchaseButton.addEventListener('click', beginPurchaseFlow)
}
});
</script>
<button class="begin-purchase-flow">Buy Now</button>
```
DOCS
end
end
def api_uri(path)
URI.join(API_URI, path)
end
def request(req, http:)
req.basic_auth ENV['STRIPE_API_KEY'],''
res = http.request(req)
JSON.parse(res.body, symbolize_names: true)
end
def get(path, http:)
request(Net::HTTP::Get.new(api_uri(path)), http: http)
end
def post(path, data, http:)
req = Net::HTTP::Post.new(api_uri(path))
req.form_data = data
request(req, http: http)
end
# TODO: Move this to Compensated
def find_or_create_product(name:, http:)
response_body = get("products", http: http)
products = response_body[:data]
product = products.find { |product| product[:name] == name }
if(!product && response_body[:has_more])
raise "Couldn't find product in first page of products..."
end
product ||= post("products", { name: name } , http: http)
end
# TODO: Move this to Compensated
def find_or_create_price(product_id:, amount:, nickname:, currency: , interval:, http:)
# https://stripe.com/docs/api/prices/list#list_prices
response_body = get("prices", http: http)
prices = response_body[:data]
price = prices.find do |potential_price|
potential_price[:product] == product_id &&
potential_price[:unit_amount] == amount &&
potential_price[:currency].to_sym == currency.to_sym
potential_price[:recurring][:interval].to_sym == interval.to_sym
end
if(!price && response_body[:has_more])
raise "Couldn't find price in first page of prices..."
end
price ||= post("prices", { product: product_id,
unit_amount: amount,
nickname: nickname,
currency: currency,
"recurring[interval]": interval },
http: http)
end
main