-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathstripeInitialisation.ps1
More file actions
175 lines (147 loc) · 6.85 KB
/
stripeInitialisation.ps1
File metadata and controls
175 lines (147 loc) · 6.85 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[String]
$StripeApiKey,
[Parameter(Mandatory=$true)]
[String]
$ApimGatewayUrl,
[Parameter(Mandatory=$true)]
[String]
$ApimSubscriptionKey,
[Parameter(Mandatory=$true)]
[String]
$StripeWebhookUrl,
[Parameter(Mandatory=$true)]
[String]
$AppServiceResourceGroup,
[Parameter(Mandatory=$true)]
[String]
$AppServiceName
)
$ErrorActionPreference = "Stop"
$here = Split-Path -Parent $PSCommandPath
$toolsDir = "$here/../tools"
if ($IsWindows) {
$osPath = "windows_x86_64.zip"
}
elseif ($IsLinux) {
$osPath = "linux_x86_64.tar.gz"
}
elseif ($IsMacOS) {
$osPath = "mac-os_x86_64.tar.gz"
}
else {
throw "Unsupported OS"
}
function installStripe {
$version = "1.5.13"
$stripeUri = "https://github.com/stripe/stripe-cli/releases/download/v$version/stripe_$($version)_$osPath"
$dir = New-Item -Path $toolsDir/stripe/$version -ItemType Directory -Force
$stripe = "$dir/stripe"
if (Test-Path $dir/* -Filter stripe*) {
Write-Host "Stripe CLI already installed."
}
else {
if ($IsWindows) {
$stripeZipPath = "$dir/stripe.zip"
Invoke-WebRequest -Uri $stripeUri -OutFile $stripeZipPath | Out-Null
Expand-Archive -Path $stripeZipPath -DestinationPath $dir -Force | Out-Null
Remove-Item $stripeZipPath -Force | Out-Null
}
else {
$stripeTarGzPath = "$dir/stripe.tar.gz"
Invoke-WebRequest -Uri $stripeUri -OutFile $stripeTarGzPath | Out-Null
tar -xvf $stripeTarGzPath -C $dir
Remove-Item $stripeTarGzPath -Force | Out-Null
}
}
return $stripe
}
$stripe = installStripe
$env:STRIPE_API_KEY = $StripeApiKey
$monetizationModels = Invoke-WebRequest -Uri $ApimGatewayUrl/billing/monetizationModels -Headers @{"Ocp-Apim-Subscription-Key"=$ApimSubscriptionKey} | ConvertFrom-Json
$apimProducts = Invoke-WebRequest -Uri $ApimGatewayUrl/billing/products -Headers @{"Ocp-Apim-Subscription-Key"=$ApimSubscriptionKey} | ConvertFrom-Json
foreach ($model in $monetizationModels) {
$apimProduct = $apimProducts.value | Where-Object { $_.name -eq $model.id }
Write-Host "Creating product and prices for model $($model.id) ($($apimProduct.name))..."
. $stripe post /v1/products `
-d "name=$($apimProduct.properties.displayName)" `
-d "id=$($apimProduct.name)" `
-d "description=$($apimProduct.properties.description)"
$prices = . $stripe get /v1/prices `
-d "product=$($model.id)" `
-d "active=true" | ConvertFrom-Json
if (!($prices.data[0])) {
if ($model.pricingModelType -eq "Metered") {
. $stripe post /v1/prices `
-d "product=$($model.id)" `
-d "currency=$($model.prices.metered.currency)" `
-d "unit_amount_decimal=$($model.prices.metered.unitAmount)" `
-d "recurring[interval]=$($model.recurring.interval)" `
-d "recurring[interval_count]=$($model.recurring.intervalCount)" `
-d "recurring[usage_type]=metered"
}
if (($model.pricingModelType -eq "Freemium") -or ($model.pricingModelType -eq "TierWithOverage")) {
. $stripe post /v1/prices `
-d "product=$($model.id)" `
-d "currency=$($model.prices.metered.currency)" `
-d "recurring[interval]=$($model.recurring.interval)" `
-d "recurring[interval_count]=$($model.recurring.intervalCount)" `
-d "recurring[usage_type]=metered" `
-d "billing_scheme=tiered" `
-d "tiers_mode=graduated" `
-d "tiers[0][up_to]=$($model.prices.unit.quota)" `
-d "tiers[0][flat_amount]=$($model.prices.unit.unitAmount)" `
-d "tiers[1][up_to]=inf" `
-d "tiers[1][unit_amount_decimal]=$($model.prices.metered.unitAmount)"
}
if (($model.pricingModelType -eq "Tier")) {
. $stripe post /v1/prices `
-d "product=$($model.id)" `
-d "currency=$($model.prices.unit.currency)" `
-d "unit_amount_decimal=$($model.prices.unit.unitAmount)" `
-d "recurring[interval]=$($model.recurring.interval)" `
-d "recurring[interval_count]=$($model.recurring.intervalCount)" `
-d "recurring[usage_type]=licensed"
}
if (($model.pricingModelType -eq "Unit")) {
# NOTE: we're only going up to 5 tiers for this example
. $stripe post /v1/prices `
-d "product=$($model.id)" `
-d "currency=$($model.prices.unit.currency)" `
-d "recurring[interval]=$($model.recurring.interval)" `
-d "recurring[interval_count]=$($model.recurring.intervalCount)" `
-d "recurring[usage_type]=metered" `
-d "billing_scheme=tiered" `
-d "tiers_mode=graduated" `
-d "tiers[0][up_to]=$($model.prices.unit.quota)" `
-d "tiers[0][flat_amount]=$($model.prices.unit.unitAmount)" `
-d "tiers[1][up_to]=$(($model.prices.unit.quota) * 2)" `
-d "tiers[1][flat_amount]=$($model.prices.unit.unitAmount)" `
-d "tiers[2][up_to]=$(($model.prices.unit.quota) * 3)" `
-d "tiers[2][flat_amount]=$($model.prices.unit.unitAmount)" `
-d "tiers[3][up_to]=$(($model.prices.unit.quota) * 4)" `
-d "tiers[3][flat_amount]=$($model.prices.unit.unitAmount)" `
-d "tiers[4][up_to]=$(($model.prices.unit.quota) * 5)" `
-d "tiers[4][flat_amount]=$($model.prices.unit.unitAmount)" `
-d "tiers[5][up_to]=inf" `
-d "tiers[5][flat_amount]=$($model.prices.unit.unitAmount)"
}
}
}
$webhookEndpoints = (. $stripe get /v1/webhook_endpoints) | ConvertFrom-Json
$webhookEndpoint = $webhookEndpoints.data | Where-Object { ($_.url -eq $StripeWebhookUrl) -and ($_.enabled_events -contains 'charge.failed') -and ($_.enabled_events -contains 'checkout.session.completed')}
if ($webhookEndpoint) {
Write-Host "Found existing webhook endpoint. Deleting..."
. $stripe delete /v1/webhook_endpoints/$($webhookEndpoint.id) --confirm
}
Write-Host "Creating new webhook endpoint..."
$webhookEndpoint = (. $stripe post /v1/webhook_endpoints `
-d "enabled_events[]=customer.subscription.created" `
-d "enabled_events[]=customer.subscription.updated" `
-d "enabled_events[]=customer.subscription.deleted" `
-d "url=$StripeWebhookUrl") | ConvertFrom-Json
$webhookSecret = $webhookEndpoint.secret
Write-Host "Updating appsettings with webhook secret..."
az webapp config appsettings set -g $AppServiceResourceGroup -n $AppServiceName --settings STRIPE_WEBHOOK_SECRET=$webhookSecret | Out-Null