Skip to content

Commit

Permalink
Use new detach endpoint for customer sessions.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaynewstrom-stripe committed Dec 18, 2024
1 parent 7f53835 commit defbce6
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,15 @@ abstract class AbsFakeStripeRepository : StripeRepository {
TODO("Not yet implemented")
}

override suspend fun detachPaymentMethod(
customerSessionClientSecret: String,
productUsageTokens: Set<String>,
paymentMethodId: String,
requestOptions: ApiRequest.Options
): Result<PaymentMethod> {
TODO("Not yet implemented")
}

override suspend fun getPaymentMethods(
listPaymentMethodsParams: ListPaymentMethodsParams,
productUsageTokens: Set<String>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,36 @@ class StripeApiRepository @JvmOverloads internal constructor(
}
}

/**
* Analytics event: [PaymentAnalyticsEvent.CustomerDetachPaymentMethod]
*/
@Throws(
InvalidRequestException::class,
APIConnectionException::class,
APIException::class,
AuthenticationException::class,
CardException::class
)
override suspend fun detachPaymentMethod(
customerSessionClientSecret: String,
productUsageTokens: Set<String>,
paymentMethodId: String,
requestOptions: ApiRequest.Options
): Result<PaymentMethod> {
return fetchStripeModelResult(
apiRequest = apiRequestFactory.createPost(
url = getElementsDetachPaymentMethodUrl(paymentMethodId),
options = requestOptions,
params = mapOf("customer_session_client_secret" to customerSessionClientSecret),
),
jsonParser = PaymentMethodJsonParser()
) {
fireAnalyticsRequest(
paymentAnalyticsRequestFactory.createDetachPaymentMethod(productUsageTokens)
)
}
}

/**
* Retrieve a Customer's [PaymentMethod]s
*
Expand Down Expand Up @@ -1380,6 +1410,14 @@ class StripeApiRepository @JvmOverloads internal constructor(
return getApiUrl("payment_methods/%s/detach", paymentMethodId)
}

/**
* @return `https://api.stripe.com/v1/payment_methods/:id/detach`
*/
@VisibleForTesting
internal fun getElementsDetachPaymentMethodUrl(paymentMethodId: String): String {
return getApiUrl("elements/payment_methods/%s/detach", paymentMethodId)
}

override suspend fun retrieveElementsSession(
params: ElementsSessionParams,
options: ApiRequest.Options,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,14 @@ interface StripeRepository {
requestOptions: ApiRequest.Options
): Result<PaymentMethod>

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
suspend fun detachPaymentMethod(
customerSessionClientSecret: String,
productUsageTokens: Set<String>,
paymentMethodId: String,
requestOptions: ApiRequest.Options
): Result<PaymentMethod>

@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
suspend fun getPaymentMethods(
listPaymentMethodsParams: ListPaymentMethodsParams,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,26 @@ internal class CustomerApiRepository @Inject constructor(
paymentMethodId
)
} else {
stripeRepository.detachPaymentMethod(
productUsageTokens = productUsageTokens,
paymentMethodId = paymentMethodId,
requestOptions = ApiRequest.Options(
apiKey = customerInfo.ephemeralKeySecret,
stripeAccount = lazyPaymentConfig.get().stripeAccountId,
if (customerInfo.customerSessionClientSecret != null) {
stripeRepository.detachPaymentMethod(
customerSessionClientSecret = customerInfo.customerSessionClientSecret,
productUsageTokens = productUsageTokens,
paymentMethodId = paymentMethodId,
requestOptions = ApiRequest.Options(
apiKey = customerInfo.ephemeralKeySecret,
stripeAccount = lazyPaymentConfig.get().stripeAccountId,
)
)
)
} else {
stripeRepository.detachPaymentMethod(
productUsageTokens = productUsageTokens,
paymentMethodId = paymentMethodId,
requestOptions = ApiRequest.Options(
apiKey = customerInfo.ephemeralKeySecret,
stripeAccount = lazyPaymentConfig.get().stripeAccountId,
)
)
}
}

return result.onFailure {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ class CustomerSessionCustomerSheetActivityTest {
networkRule.enqueue(
host("api.stripe.com"),
method("POST"),
path("/v1/payment_methods/$id/detach")
path("/v1/elements/payment_methods/$id/detach")
) { response ->
response.createPaymentMethodDetachResponse(id = id)
}
Expand Down

0 comments on commit defbce6

Please sign in to comment.