Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix account token failure on latest API version #822

Merged
merged 1 commit into from
Mar 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions stripe/src/main/java/com/stripe/android/ApiVersion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.stripe.android;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.stripe.android.utils.ObjectUtils;

/**
* A class that represents a Stripe API version.
*
* See <a href="https://stripe.com/docs/api/versioning">https://stripe.com/docs/api/versioning</a>
* for documentation on API versioning.
*
* See <a href="https://stripe.com/docs/upgrades">https://stripe.com/docs/upgrades</a> for latest
* API changes.
*/
public class ApiVersion {
static final String DEFAULT_API_VERSION = "2017-06-05";

@NonNull private static final ApiVersion DEFAULT_INSTANCE = new ApiVersion(DEFAULT_API_VERSION);

@NonNull private final String mCode;

@NonNull
public static ApiVersion create(@NonNull String code) {
return new ApiVersion(code);
}

@NonNull
public static ApiVersion getDefault() {
return DEFAULT_INSTANCE;
}

private ApiVersion(@NonNull String code) {
this.mCode = code;
}

@NonNull
public String getCode() {
return mCode;
}

@Override
public int hashCode() {
return ObjectUtils.hash(mCode);
}

@Override
public boolean equals(@Nullable Object obj) {
return this == obj || (obj instanceof ApiVersion && typedEquals((ApiVersion) obj));
}

private boolean typedEquals(@NonNull ApiVersion apiVersion) {
return ObjectUtils.equals(mCode, apiVersion.mCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void retrieveEphemeralKey(@Nullable String actionString,
mEphemeralKey,
mTimeBufferInSeconds,
mOverrideCalendar)) {
mEphemeralKeyProvider.createEphemeralKey(StripeApiHandler.API_VERSION,
mEphemeralKeyProvider.createEphemeralKey(ApiVersion.DEFAULT_API_VERSION,
new ClientKeyUpdateListener(this, actionString, arguments));
} else {
mListener.onKeyUpdate(mEphemeralKey, actionString, arguments);
Expand Down
9 changes: 9 additions & 0 deletions stripe/src/main/java/com/stripe/android/RequestOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ public static RequestOptions.RequestOptionsBuilder builder(@Nullable String publ
return builder(publishableApiKey, TYPE_QUERY);
}

@NonNull
public static RequestOptions.RequestOptionsBuilder builder(
@Nullable String publishableApiKey,
@Nullable String stripeAccount,
Expand Down Expand Up @@ -193,6 +194,14 @@ RequestOptionsBuilder setApiVersion(@Nullable String apiVersion) {
return this;
}

/**
* Convenience method for {@link #setApiVersion(String)}
*/
@NonNull
RequestOptionsBuilder setApiVersion(@Nullable ApiVersion apiVersion) {
return setApiVersion(apiVersion != null ? apiVersion.getCode() : null);
}

@NonNull
RequestOptionsBuilder setStripeAccount(@Nullable String stripeAccount) {
this.stripeAccount = stripeAccount;
Expand Down
26 changes: 21 additions & 5 deletions stripe/src/main/java/com/stripe/android/Stripe.java
Original file line number Diff line number Diff line change
Expand Up @@ -684,12 +684,26 @@ public Token createCvcUpdateTokenSynchronous(@NonNull String cvc,
* @throws APIException any other type of problem (for instance, a temporary issue with
* Stripe's servers)
*/
@Nullable
public Token createAccountTokenSynchronous(@NonNull final AccountParams accountParams)
throws AuthenticationException,
InvalidRequestException,
APIConnectionException,
APIException {
return createAccountTokenSynchronous(accountParams, mDefaultPublishableKey);
return createAccountTokenSynchronous(accountParams, mDefaultPublishableKey, null);
}

/**
* See {@link #createAccountTokenSynchronous(AccountParams)}
*/
@Nullable
public Token createAccountTokenSynchronous(@NonNull final AccountParams accountParams,
@NonNull final ApiVersion apiVersion)
throws AuthenticationException,
InvalidRequestException,
APIConnectionException,
APIException {
return createAccountTokenSynchronous(accountParams, mDefaultPublishableKey, apiVersion);
}

/**
Expand All @@ -707,9 +721,11 @@ public Token createAccountTokenSynchronous(@NonNull final AccountParams accountP
* @throws APIException any other type of problem (for instance, a temporary issue with
* Stripe's servers)
*/
@Nullable
public Token createAccountTokenSynchronous(
@NonNull final AccountParams accountParams,
@Nullable String publishableKey)
@Nullable String publishableKey,
@Nullable ApiVersion apiVersion)
throws AuthenticationException,
InvalidRequestException,
APIConnectionException,
Expand All @@ -720,9 +736,9 @@ public Token createAccountTokenSynchronous(
}
validateKey(publishableKey);
RequestOptions requestOptions = RequestOptions.builder(
publishableKey,
mStripeAccount,
RequestOptions.TYPE_QUERY).build();
publishableKey, mStripeAccount, RequestOptions.TYPE_QUERY)
.setApiVersion(apiVersion)
.build();
try {
return StripeApiHandler.createToken(
mContext,
Expand Down
40 changes: 25 additions & 15 deletions stripe/src/main/java/com/stripe/android/StripeApiHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ class StripeApiHandler {
private static final String DNS_CACHE_TTL_PROPERTY_NAME = "networkaddress.cache.ttl";
private static final SSLSocketFactory SSL_SOCKET_FACTORY = new StripeSSLSocketFactory();

static final String API_VERSION = "2017-06-05";

static void logApiCall(
@NonNull Map<String, Object> loggingMap,
@NonNull RequestOptions options,
Expand Down Expand Up @@ -127,7 +125,7 @@ static PaymentIntent confirmPaymentIntent(
publishableKey,
stripeAccount,
RequestOptions.TYPE_QUERY)
.setApiVersion(API_VERSION)
.setApiVersion(ApiVersion.DEFAULT_API_VERSION)
.build();

try {
Expand Down Expand Up @@ -179,7 +177,7 @@ static PaymentIntent retrievePaymentIntent(
publishableKey,
stripeAccount,
RequestOptions.TYPE_QUERY)
.setApiVersion(API_VERSION)
.setApiVersion(ApiVersion.DEFAULT_API_VERSION)
.build();

try {
Expand Down Expand Up @@ -457,16 +455,19 @@ static Source addCustomerSource(
context, productUsageTokens, publicKey, sourceType);

// We use the public key to log, so we need different RequestOptions.
final RequestOptions loggingOptions =
RequestOptions.builder(publicKey).setApiVersion(API_VERSION).build();
final RequestOptions loggingOptions = RequestOptions.builder(publicKey)
.setApiVersion(ApiVersion.DEFAULT_API_VERSION)
.build();
logApiCall(loggingParamsMap, loggingOptions, listener);
}

final StripeResponse response = getStripeResponse(
POST,
getAddCustomerSourceUrl(customerId),
paramsMap,
RequestOptions.builder(secret).setApiVersion(API_VERSION).build());
RequestOptions.builder(secret)
.setApiVersion(ApiVersion.DEFAULT_API_VERSION)
.build());
// Method throws if errors are found, so no return value occurs.
convertErrorsToExceptionsAndThrowIfNecessary(response);
return Source.fromString(response.getResponseBody());
Expand All @@ -492,16 +493,19 @@ static Source deleteCustomerSource(
LoggingUtils.getDeleteSourceParams(context, productUsageTokens, publicKey);

// We use the public key to log, so we need different RequestOptions.
final RequestOptions loggingOptions =
RequestOptions.builder(publicKey).setApiVersion(API_VERSION).build();
final RequestOptions loggingOptions = RequestOptions.builder(publicKey)
.setApiVersion(ApiVersion.DEFAULT_API_VERSION)
.build();
logApiCall(loggingParamsMap, loggingOptions, listener);
}

final StripeResponse response = getStripeResponse(
DELETE,
getDeleteCustomerSourceUrl(customerId, sourceId),
paramsMap,
RequestOptions.builder(secret).setApiVersion(API_VERSION).build());
RequestOptions.builder(secret)
.setApiVersion(ApiVersion.DEFAULT_API_VERSION)
.build());
// Method throws if errors are found, so no return value occurs.
convertErrorsToExceptionsAndThrowIfNecessary(response);
return Source.fromString(response.getResponseBody());
Expand All @@ -528,7 +532,7 @@ static Customer setDefaultCustomerSource(
// Context can be nullable because this action is performed with only a weak reference
if (context != null) {
final RequestOptions loggingOptions = RequestOptions.builder(publicKey)
.setApiVersion(API_VERSION)
.setApiVersion(ApiVersion.DEFAULT_API_VERSION)
.build();

final Map<String, Object> loggingParameters = LoggingUtils.getEventLoggingParams(
Expand All @@ -546,7 +550,9 @@ static Customer setDefaultCustomerSource(
POST,
getRetrieveCustomerUrl(customerId),
paramsMap,
RequestOptions.builder(secret).setApiVersion(API_VERSION).build());
RequestOptions.builder(secret)
.setApiVersion(ApiVersion.DEFAULT_API_VERSION)
.build());

// Method throws if errors are found, so no return value occurs.
convertErrorsToExceptionsAndThrowIfNecessary(response);
Expand All @@ -573,7 +579,7 @@ static Customer setCustomerShippingInfo(
// Context can be nullable because this action is performed with only a weak reference
if (context != null) {
final RequestOptions loggingOptions = RequestOptions.builder(publicKey)
.setApiVersion(API_VERSION)
.setApiVersion(ApiVersion.DEFAULT_API_VERSION)
.build();

final Map<String, Object> loggingParameters = LoggingUtils.getEventLoggingParams(
Expand All @@ -591,7 +597,9 @@ static Customer setCustomerShippingInfo(
POST,
getRetrieveCustomerUrl(customerId),
paramsMap,
RequestOptions.builder(secret).setApiVersion(API_VERSION).build());
RequestOptions.builder(secret)
.setApiVersion(ApiVersion.DEFAULT_API_VERSION)
.build());
// Method throws if errors are found, so no return value occurs.
convertErrorsToExceptionsAndThrowIfNecessary(response);
return Customer.fromString(response.getResponseBody());
Expand All @@ -609,7 +617,9 @@ static Customer retrieveCustomer(@NonNull String customerId, @NonNull String sec
GET,
getRetrieveCustomerUrl(customerId),
null,
RequestOptions.builder(secret).setApiVersion(API_VERSION).build());
RequestOptions.builder(secret)
.setApiVersion(ApiVersion.DEFAULT_API_VERSION)
.build());
convertErrorsToExceptionsAndThrowIfNecessary(response);
return Customer.fromString(response.getResponseBody());
}
Expand Down
Loading