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

Create GooglePayConfig #1302

Merged
merged 1 commit into from
Aug 5, 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
4 changes: 3 additions & 1 deletion stripe/src/main/java/com/stripe/android/ApiRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ final class ApiRequest extends StripeRequest {
static final String ANALYTICS_HOST = "https://q.stripe.com";

@NonNull final Options options;
@NonNull private final String mApiVersion;
@Nullable private final AppInfo mAppInfo;

@VisibleForTesting
Expand All @@ -36,6 +37,7 @@ final class ApiRequest extends StripeRequest {
@Nullable AppInfo appInfo) {
super(method, url, params, MIME_TYPE);
this.options = options;
mApiVersion = ApiVersion.get().code;
mAppInfo = appInfo;
}

Expand Down Expand Up @@ -90,7 +92,7 @@ Map<String, String> createHeaders() {
headers.put("Accept-Charset", CHARSET);
headers.put("Accept", "application/json");
headers.put("X-Stripe-Client-User-Agent", createStripeClientUserAgent());
headers.put("Stripe-Version", ApiVersion.getDefault().getCode());
headers.put("Stripe-Version", mApiVersion);
headers.put("Authorization",
String.format(Locale.ENGLISH, "Bearer %s", options.apiKey));
if (options.stripeAccount != null) {
Expand Down
26 changes: 11 additions & 15 deletions stripe/src/main/java/com/stripe/android/ApiVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,30 @@
* API changes.
*/
final class ApiVersion {
private static final String DEFAULT_API_VERSION = "2019-05-16";
@NonNull private static final String API_VERSION_CODE = "2019-05-16";

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

@NonNull private final String mCode;
@NonNull public final String code;

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

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

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

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

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

@Override
Expand All @@ -51,6 +47,6 @@ public boolean equals(@Nullable Object obj) {
}

private boolean typedEquals(@NonNull ApiVersion apiVersion) {
return ObjectUtils.equals(mCode, apiVersion.mCode);
return ObjectUtils.equals(code, apiVersion.code);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class EphemeralKeyManager<TEphemeralKey extends EphemeralKey> {
@NonNull private final KeyManagerListener<TEphemeralKey> mListener;
private final long mTimeBufferInSeconds;
@NonNull private final EphemeralKey.Factory<TEphemeralKey> mFactory;
@NonNull private final String mApiVersion;

@Nullable private TEphemeralKey mEphemeralKey;

Expand All @@ -33,6 +34,7 @@ class EphemeralKeyManager<TEphemeralKey extends EphemeralKey> {
mListener = keyManagerListener;
mTimeBufferInSeconds = timeBufferInSeconds;
mOverrideCalendar = overrideCalendar;
mApiVersion = ApiVersion.get().code;
retrieveEphemeralKey(operationIdFactory.create(), null, null);
}

Expand All @@ -43,7 +45,7 @@ void retrieveEphemeralKey(@NonNull String operationId,
mEphemeralKey,
mTimeBufferInSeconds,
mOverrideCalendar)) {
mEphemeralKeyProvider.createEphemeralKey(ApiVersion.getDefault().getCode(),
mEphemeralKeyProvider.createEphemeralKey(mApiVersion,
new ClientKeyUpdateListener(this, operationId, actionString, arguments));
} else {
mListener.onKeyUpdate(mEphemeralKey, operationId, actionString, arguments);
Expand Down
43 changes: 43 additions & 0 deletions stripe/src/main/java/com/stripe/android/GooglePayConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.stripe.android;

import android.support.annotation.NonNull;

import org.json.JSONException;
import org.json.JSONObject;

@SuppressWarnings("WeakerAccess")
public final class GooglePayConfig {
@NonNull private final String mPublishableKey;
@NonNull private final String mApiVersion;

/**
* Instantiate with {@link PaymentConfiguration}. {@link PaymentConfiguration} must be
* initialized.
*/
public GooglePayConfig() {
this(PaymentConfiguration.getInstance().getPublishableKey());
}

public GooglePayConfig(@NonNull String publishableKey) {
mPublishableKey = ApiKeyValidator.get().requireValid(publishableKey);
mApiVersion = ApiVersion.get().code;
}

/**
* @return a {@link JSONObject} representing a
* <a href="https://developers.google.com/pay/api/android/reference/object#gateway">
* Google Pay TokenizationSpecification</a> configured for Stripe
*/
@NonNull
public JSONObject getTokenizationSpecification() throws JSONException {
return new JSONObject()
.put("type", "PAYMENT_GATEWAY")
.put(
"parameters",
new JSONObject()
.put("gateway", "stripe")
.put("stripe:version", mApiVersion)
.put("stripe:publishableKey", mPublishableKey)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void getHeaders_withAllRequestOptions_properlyMapsRequestOptions() {
assertNotNull(headerMap);
assertEquals("Bearer " + ApiKeyFixtures.FAKE_PUBLISHABLE_KEY,
headerMap.get("Authorization"));
assertEquals(ApiVersion.getDefault().getCode(), headerMap.get("Stripe-Version"));
assertEquals(ApiVersion.get().code, headerMap.get("Stripe-Version"));
assertEquals(stripeAccount, headerMap.get("Stripe-Account"));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.stripe.android;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class GooglePayConfigTest {

@Test
public void getTokenizationSpecification() throws JSONException {
PaymentConfiguration.init(ApiKeyFixtures.FAKE_PUBLISHABLE_KEY);
final JSONObject tokenizationSpec = new GooglePayConfig().getTokenizationSpecification();
final JSONObject params = tokenizationSpec.getJSONObject("parameters");
assertEquals("stripe",
params.getString("gateway"));
assertEquals(ApiVersion.get().code,
params.getString("stripe:version"));
assertEquals(ApiKeyFixtures.FAKE_PUBLISHABLE_KEY,
params.getString("stripe:publishableKey"));
}
}