Skip to content

Commit 5f9e6f1

Browse files
committed
update stripe api to fix request options
1 parent 7d0e9ff commit 5f9e6f1

File tree

11 files changed

+119
-114
lines changed

11 files changed

+119
-114
lines changed

stripe/src/main/scala/app/softnetwork/payment/config/StripeApi.scala

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,27 @@ import java.nio.file.Paths
1818
import scala.util.{Failure, Success, Try}
1919

2020
case class StripeApi(
21-
private val request: RequestOptionsBuilder,
21+
private val baseUrl: String,
22+
private val clientId: String,
23+
private val apiKey: String,
2224
hash: String
2325
) {
24-
lazy val requestOptionsBuilder: RequestOptionsBuilder = request.clearStripeAccount()
26+
private def requestOptionsBuilder(stripeAccount: Option[String]): RequestOptionsBuilder = {
27+
val options = RequestOptions
28+
.builder()
29+
.setBaseUrl(baseUrl)
30+
.setClientId(clientId)
31+
.setApiKey(apiKey)
32+
stripeAccount match {
33+
case Some(value) =>
34+
options.setStripeAccount(value)
35+
case _ =>
36+
options
37+
}
38+
}
2539

26-
lazy val requestOptions: RequestOptions = requestOptionsBuilder.build()
40+
def requestOptions(stripeAccount: Option[String] = None): RequestOptions =
41+
requestOptionsBuilder(stripeAccount).build()
2742

2843
lazy val secret: Option[String] = StripeApi.loadSecret(hash)
2944
}
@@ -96,16 +111,18 @@ object StripeApi {
96111
case Some(stripeApi) => stripeApi
97112
case _ =>
98113
// init Stripe request options
99-
val requestOptions = RequestOptions
100-
.builder()
101-
.setBaseUrl(config.baseUrl)
102-
.setClientId(provider.providerId)
103-
.setApiKey(provider.providerApiKey)
114+
val baseUrl = config.baseUrl
115+
val clientId = provider.providerId
116+
val apiKey = provider.providerApiKey
104117

105118
// create / update stripe webhook endpoint
106119

107120
val hash = sha256(provider.clientId)
108121

122+
val stripeApi = StripeApi(baseUrl, clientId, apiKey, hash)
123+
124+
val requestOptions = stripeApi.requestOptions()
125+
109126
val url = s"${config.hooksBaseUrl}?hash=$hash"
110127

111128
import collection.JavaConverters._
@@ -115,7 +132,7 @@ object StripeApi {
115132
WebhookEndpoint
116133
.list(
117134
WebhookEndpointListParams.builder().setLimit(3L).build(),
118-
requestOptions.build()
135+
requestOptions
119136
)
120137
.getData
121138
) match {
@@ -128,7 +145,7 @@ object StripeApi {
128145
Console.println(s"Webhook endpoint found: ${webhookEndpoint.getId}")
129146
loadSecret(hash) match {
130147
case None =>
131-
Try(webhookEndpoint.delete(requestOptions.build()))
148+
Try(webhookEndpoint.delete(requestOptions))
132149
None
133150
case value =>
134151
val url = s"${config.hooksBaseUrl}?hash=$hash"
@@ -144,7 +161,7 @@ object StripeApi {
144161
)
145162
.setUrl(url)
146163
.build(),
147-
requestOptions.build()
164+
requestOptions
148165
)
149166
)
150167
value
@@ -166,17 +183,16 @@ object StripeApi {
166183
.setApiVersion(WebhookEndpointCreateParams.ApiVersion.VERSION_2024_06_20)
167184
.setConnect(true)
168185
.build(),
169-
requestOptions.build()
186+
requestOptions
170187
)
171188
.getSecret
172189
}
173190

174191
} match {
175192
case Success(secret) =>
176-
val ret = StripeApi(requestOptions, hash)
177-
stripeApis = stripeApis.updated(provider.providerId, ret)
193+
stripeApis = stripeApis.updated(provider.providerId, stripeApi)
178194
addSecret(hash, secret)
179-
ret
195+
stripeApi
180196
case Failure(f) =>
181197
Console.err.println(s"Error creating stripe webhook endpoint: ${f.getMessage}")
182198
throw f

0 commit comments

Comments
 (0)