Skip to content

Commit

Permalink
Fix issue in ConfirmSetupIntentParams#toBuilder()
Browse files Browse the repository at this point in the history
This method was missing a call to `setPaymentMethodCreateParams()`.

Fixes #1311
  • Loading branch information
mshafrir-stripe committed Aug 6, 2019
1 parent 2540cda commit e49bef9
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;

import com.stripe.android.ObjectBuilder;
import com.stripe.android.utils.ObjectUtils;
Expand Down Expand Up @@ -402,7 +403,8 @@ public Map<String, Object> toParamMap() {
}

@NonNull
private Builder toBuilder() {
@VisibleForTesting
Builder toBuilder() {
return new Builder(mClientSecret)
.setReturnUrl(mReturnUrl)
.setPaymentMethodId(mPaymentMethodId)
Expand All @@ -413,7 +415,8 @@ private Builder toBuilder() {
.setExtraParams(mExtraParams);
}

private static final class Builder implements ObjectBuilder<ConfirmPaymentIntentParams> {
@VisibleForTesting
static final class Builder implements ObjectBuilder<ConfirmPaymentIntentParams> {
@NonNull private final String mClientSecret;

@Nullable private PaymentMethodCreateParams mPaymentMethodCreateParams;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;

import com.stripe.android.ObjectBuilder;
import com.stripe.android.utils.ObjectUtils;
Expand Down Expand Up @@ -59,6 +60,15 @@ public static ConfirmSetupIntentParams create(
.build();
}

/**
* Create the parameters necessary for confirming a SetupIntent with a new PaymentMethod
*
* @param paymentMethodCreateParams the params to create a new PaymentMethod that will be
* attached to the SetupIntent being confirmed
* @param clientSecret client secret from the SetupIntent being confirmed
* @param returnUrl the URL the customer should be redirected to after the authorization process
* @return params that can be use to confirm a SetupIntent
*/
@NonNull
public static ConfirmSetupIntentParams create(
@NonNull PaymentMethodCreateParams paymentMethodCreateParams,
Expand All @@ -71,10 +81,10 @@ public static ConfirmSetupIntentParams create(
}

/**
* See {@link #create(String, String, String)}
* See {@link #create(PaymentMethodCreateParams, String, String)}
*/
@NonNull
public static ConfirmSetupIntentParams createWithPaymentMethodCreateParams(
public static ConfirmSetupIntentParams create(
@NonNull PaymentMethodCreateParams paymentMethodCreateParams,
@NonNull String clientSecret) {
return create(paymentMethodCreateParams, clientSecret, null);
Expand Down Expand Up @@ -133,10 +143,12 @@ public PaymentMethodCreateParams getPaymentMethodCreateParams() {
}

@NonNull
private ConfirmSetupIntentParams.Builder toBuilder() {
@VisibleForTesting
ConfirmSetupIntentParams.Builder toBuilder() {
return new ConfirmSetupIntentParams.Builder(mClientSecret)
.setReturnUrl(mReturnUrl)
.setPaymentMethodId(mPaymentMethodId)
.setPaymentMethodCreateParams(mPaymentMethodCreateParams)
.setShouldUseSdk(mUseStripeSdk);
}

Expand All @@ -146,11 +158,12 @@ public boolean equals(@Nullable Object obj) {
typedEquals((ConfirmSetupIntentParams) obj));
}

private boolean typedEquals(@NonNull ConfirmSetupIntentParams confirmSetupIntentParams) {
return ObjectUtils.equals(mReturnUrl, confirmSetupIntentParams.mReturnUrl)
&& ObjectUtils.equals(mClientSecret, confirmSetupIntentParams.mClientSecret)
&& ObjectUtils.equals(mPaymentMethodId, confirmSetupIntentParams.mPaymentMethodId)
&& ObjectUtils.equals(mUseStripeSdk, confirmSetupIntentParams.mUseStripeSdk);
private boolean typedEquals(@NonNull ConfirmSetupIntentParams params) {
return ObjectUtils.equals(mReturnUrl, params.mReturnUrl)
&& ObjectUtils.equals(mClientSecret, params.mClientSecret)
&& ObjectUtils.equals(mPaymentMethodId, params.mPaymentMethodId)
&& ObjectUtils.equals(mPaymentMethodCreateParams,params.mPaymentMethodCreateParams)
&& ObjectUtils.equals(mUseStripeSdk, params.mUseStripeSdk);
}

@Override
Expand All @@ -159,7 +172,8 @@ public int hashCode() {
}


private static final class Builder implements ObjectBuilder<ConfirmSetupIntentParams> {
@VisibleForTesting
static final class Builder implements ObjectBuilder<ConfirmSetupIntentParams> {
@NonNull private final String mClientSecret;
@Nullable private String mPaymentMethodId;
@Nullable private PaymentMethodCreateParams mPaymentMethodCreateParams;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,33 @@ public class ConfirmPaymentIntentParamsTest {
.currency("usd")
.build();

private static final String TEST_CLIENT_SECRET =
private static final String CLIENT_SECRET =
"pi_1CkiBMLENEVhOs7YMtUehLau_secret_s4O8SDh7s6spSmHDw1VaYPGZA";

private static final String TEST_RETURN_URL = "stripe://return_url";
private static final String TEST_SOURCE_ID = "src_123testsourceid";
private static final String TEST_PAYMENT_METHOD_ID = "pm_123456789";
private static final String RETURN_URL = "stripe://return_url";
private static final String SOURCE_ID = "src_123testsourceid";
private static final String PM_ID = "pm_123456789";

private static final PaymentMethodCreateParams PM_CREATE_PARAMS =
PaymentMethodCreateParams.create(
new PaymentMethodCreateParams.Card.Builder()
.setNumber("4242424242424242")
.setExpiryMonth(1)
.setExpiryYear(2024)
.setCvc("111")
.build(),
null
);

@Test
public void createConfirmPaymentIntentWithSourceDataParams_withAllFields_hasExpectedFields() {
final SourceParams sourceParams = SourceParams.createCardParams(FULL_FIELDS_VISA_CARD);
final ConfirmPaymentIntentParams params = ConfirmPaymentIntentParams
.createWithSourceParams(
sourceParams, TEST_CLIENT_SECRET, TEST_RETURN_URL);
sourceParams, CLIENT_SECRET, RETURN_URL);

assertEquals(TEST_CLIENT_SECRET, params.getClientSecret());
assertEquals(TEST_RETURN_URL, params.getReturnUrl());
assertEquals(CLIENT_SECRET, params.getClientSecret());
assertEquals(RETURN_URL, params.getReturnUrl());
assertEquals(sourceParams, params.getSourceParams());
assertFalse(params.shouldSavePaymentMethod());
}
Expand All @@ -48,23 +59,23 @@ public void createConfirmPaymentIntentWithSourceDataParams_withAllFields_hasExpe
public void createConfirmPaymentIntentWithSourceIdParams_withAllFields_hasExpectedFields() {
final ConfirmPaymentIntentParams params = ConfirmPaymentIntentParams
.createWithSourceId(
TEST_SOURCE_ID, TEST_CLIENT_SECRET, TEST_RETURN_URL);
SOURCE_ID, CLIENT_SECRET, RETURN_URL);

assertEquals(TEST_CLIENT_SECRET, params.getClientSecret());
assertEquals(TEST_RETURN_URL, params.getReturnUrl());
assertEquals(TEST_SOURCE_ID, params.getSourceId());
assertEquals(CLIENT_SECRET, params.getClientSecret());
assertEquals(RETURN_URL, params.getReturnUrl());
assertEquals(SOURCE_ID, params.getSourceId());
assertFalse(params.shouldSavePaymentMethod());
}

@Test
public void createConfirmPaymentIntentWithSourceIdParams_withSavePaymentMethod_hasExpectedFields() {
final ConfirmPaymentIntentParams params = ConfirmPaymentIntentParams
.createWithSourceId(
TEST_SOURCE_ID, TEST_CLIENT_SECRET, TEST_RETURN_URL, true);
SOURCE_ID, CLIENT_SECRET, RETURN_URL, true);

assertEquals(TEST_CLIENT_SECRET, params.getClientSecret());
assertEquals(TEST_RETURN_URL, params.getReturnUrl());
assertEquals(TEST_SOURCE_ID, params.getSourceId());
assertEquals(CLIENT_SECRET, params.getClientSecret());
assertEquals(RETURN_URL, params.getReturnUrl());
assertEquals(SOURCE_ID, params.getSourceId());
assertTrue(params.shouldSavePaymentMethod());

assertEquals(Boolean.TRUE,
Expand All @@ -78,10 +89,10 @@ public void createWithPaymentMethodCreateParams_hasExpectedFields() {
.build(), null);
final ConfirmPaymentIntentParams params = ConfirmPaymentIntentParams
.createWithPaymentMethodCreateParams(paymentMethodCreateParams,
TEST_CLIENT_SECRET, TEST_RETURN_URL);
CLIENT_SECRET, RETURN_URL);

assertEquals(TEST_CLIENT_SECRET, params.getClientSecret());
assertEquals(TEST_RETURN_URL, params.getReturnUrl());
assertEquals(CLIENT_SECRET, params.getClientSecret());
assertEquals(RETURN_URL, params.getReturnUrl());
assertEquals(paymentMethodCreateParams, params.getPaymentMethodCreateParams());
assertFalse(params.shouldSavePaymentMethod());
}
Expand All @@ -90,11 +101,11 @@ public void createWithPaymentMethodCreateParams_hasExpectedFields() {
public void createConfirmPaymentIntentWithPaymentMethodId_hasExpectedFields() {
final ConfirmPaymentIntentParams params = ConfirmPaymentIntentParams
.createWithPaymentMethodId(
TEST_PAYMENT_METHOD_ID, TEST_CLIENT_SECRET, TEST_RETURN_URL);
PM_ID, CLIENT_SECRET, RETURN_URL);

assertEquals(TEST_CLIENT_SECRET, params.getClientSecret());
assertEquals(TEST_RETURN_URL, params.getReturnUrl());
assertEquals(TEST_PAYMENT_METHOD_ID, params.getPaymentMethodId());
assertEquals(CLIENT_SECRET, params.getClientSecret());
assertEquals(RETURN_URL, params.getReturnUrl());
assertEquals(PM_ID, params.getPaymentMethodId());
assertFalse(params.shouldSavePaymentMethod());
}

Expand All @@ -105,10 +116,10 @@ public void createConfirmPaymentIntentWithPaymentMethodCreateParams_withSavePaym
.build(), null);
final ConfirmPaymentIntentParams params = ConfirmPaymentIntentParams
.createWithPaymentMethodCreateParams(paymentMethodCreateParams,
TEST_CLIENT_SECRET, TEST_RETURN_URL, true);
CLIENT_SECRET, RETURN_URL, true);

assertEquals(TEST_CLIENT_SECRET, params.getClientSecret());
assertEquals(TEST_RETURN_URL, params.getReturnUrl());
assertEquals(CLIENT_SECRET, params.getClientSecret());
assertEquals(RETURN_URL, params.getReturnUrl());
assertEquals(paymentMethodCreateParams, params.getPaymentMethodCreateParams());
assertTrue(params.shouldSavePaymentMethod());
}
Expand All @@ -117,11 +128,11 @@ public void createConfirmPaymentIntentWithPaymentMethodCreateParams_withSavePaym
public void createConfirmPaymentIntentWithPaymentMethodId_withSavePaymentMethod_hasExpectedFields() {
final ConfirmPaymentIntentParams params = ConfirmPaymentIntentParams
.createWithPaymentMethodId(
TEST_PAYMENT_METHOD_ID, TEST_CLIENT_SECRET, TEST_RETURN_URL, true);
PM_ID, CLIENT_SECRET, RETURN_URL, true);

assertEquals(TEST_CLIENT_SECRET, params.getClientSecret());
assertEquals(TEST_RETURN_URL, params.getReturnUrl());
assertEquals(TEST_PAYMENT_METHOD_ID, params.getPaymentMethodId());
assertEquals(CLIENT_SECRET, params.getClientSecret());
assertEquals(RETURN_URL, params.getReturnUrl());
assertEquals(PM_ID, params.getPaymentMethodId());
assertTrue(params.shouldSavePaymentMethod());

assertEquals(Boolean.TRUE,
Expand All @@ -132,46 +143,46 @@ public void createConfirmPaymentIntentWithPaymentMethodId_withSavePaymentMethod_
public void createWithSourceId_toParamMap_createsExpectedMap() {
final ConfirmPaymentIntentParams confirmPaymentIntentParams = ConfirmPaymentIntentParams
.createWithSourceId(
TEST_SOURCE_ID, TEST_CLIENT_SECRET, TEST_RETURN_URL);
SOURCE_ID, CLIENT_SECRET, RETURN_URL);

final Map<String, Object> paramMap = confirmPaymentIntentParams.toParamMap();

assertEquals(paramMap.get(ConfirmPaymentIntentParams.API_PARAM_SOURCE_ID), TEST_SOURCE_ID);
assertEquals(paramMap.get(ConfirmPaymentIntentParams.API_PARAM_SOURCE_ID), SOURCE_ID);
assertEquals(
paramMap.get(ConfirmPaymentIntentParams.API_PARAM_CLIENT_SECRET), TEST_CLIENT_SECRET);
paramMap.get(ConfirmPaymentIntentParams.API_PARAM_CLIENT_SECRET), CLIENT_SECRET);
assertEquals(
paramMap.get(ConfirmPaymentIntentParams.API_PARAM_RETURN_URL), TEST_RETURN_URL);
paramMap.get(ConfirmPaymentIntentParams.API_PARAM_RETURN_URL), RETURN_URL);
assertFalse(paramMap.containsKey(ConfirmPaymentIntentParams.API_PARAM_SAVE_PAYMENT_METHOD));
}

@Test
public void createWithPaymentMethodId_withoutReturnUrl_toParamMap_createsExpectedMap() {
final ConfirmPaymentIntentParams confirmPaymentIntentParams = ConfirmPaymentIntentParams
.createWithPaymentMethodId(TEST_PAYMENT_METHOD_ID, TEST_CLIENT_SECRET);
.createWithPaymentMethodId(PM_ID, CLIENT_SECRET);

final Map<String, Object> paramMap = confirmPaymentIntentParams.toParamMap();

assertEquals(paramMap.get(ConfirmPaymentIntentParams.API_PARAM_PAYMENT_METHOD_ID),
TEST_PAYMENT_METHOD_ID);
PM_ID);
assertEquals(
paramMap.get(ConfirmPaymentIntentParams.API_PARAM_CLIENT_SECRET), TEST_CLIENT_SECRET);
paramMap.get(ConfirmPaymentIntentParams.API_PARAM_CLIENT_SECRET), CLIENT_SECRET);
assertFalse(paramMap.containsKey(ConfirmPaymentIntentParams.API_PARAM_RETURN_URL));
assertFalse(paramMap.containsKey(ConfirmPaymentIntentParams.API_PARAM_SAVE_PAYMENT_METHOD));
}

@Test
public void createWithPaymentMethodId_withReturnUrl_toParamMap_createsExpectedMap() {
final ConfirmPaymentIntentParams confirmPaymentIntentParams = ConfirmPaymentIntentParams
.createWithPaymentMethodId(TEST_PAYMENT_METHOD_ID, TEST_CLIENT_SECRET, TEST_RETURN_URL);
.createWithPaymentMethodId(PM_ID, CLIENT_SECRET, RETURN_URL);

final Map<String, Object> paramMap = confirmPaymentIntentParams.toParamMap();

assertEquals(paramMap.get(ConfirmPaymentIntentParams.API_PARAM_PAYMENT_METHOD_ID),
TEST_PAYMENT_METHOD_ID);
PM_ID);
assertEquals(
paramMap.get(ConfirmPaymentIntentParams.API_PARAM_CLIENT_SECRET), TEST_CLIENT_SECRET);
paramMap.get(ConfirmPaymentIntentParams.API_PARAM_CLIENT_SECRET), CLIENT_SECRET);
assertEquals(
paramMap.get(ConfirmPaymentIntentParams.API_PARAM_RETURN_URL), TEST_RETURN_URL);
paramMap.get(ConfirmPaymentIntentParams.API_PARAM_RETURN_URL), RETURN_URL);
assertFalse(paramMap.containsKey(ConfirmPaymentIntentParams.API_PARAM_SAVE_PAYMENT_METHOD));
}

Expand All @@ -186,13 +197,13 @@ public void toParamMap_whenExtraParamsProvided_createsExpectedMap() {
extraParams.put(extraParamKey2, extraParamValue2);

final ConfirmPaymentIntentParams confirmPaymentIntentParams = ConfirmPaymentIntentParams
.createWithPaymentMethodId("pm_123", TEST_CLIENT_SECRET,
TEST_RETURN_URL, false, extraParams);
.createWithPaymentMethodId("pm_123", CLIENT_SECRET,
RETURN_URL, false, extraParams);

final Map<String, Object> paramMap = confirmPaymentIntentParams.toParamMap();

assertEquals(
paramMap.get(ConfirmPaymentIntentParams.API_PARAM_CLIENT_SECRET), TEST_CLIENT_SECRET);
paramMap.get(ConfirmPaymentIntentParams.API_PARAM_CLIENT_SECRET), CLIENT_SECRET);
assertEquals(
paramMap.get(extraParamKey1), extraParamValue1);
assertEquals(
Expand All @@ -217,4 +228,16 @@ public void shouldUseStripeSdk() {
.withShouldUseStripeSdk(true)
.shouldUseStripeSdk());
}

@Test
public void toBuilder_withPaymentMethodCreateParams_shouldCreateEqualObject() {
final Map<String, Object> extraParams = new HashMap<>();
extraParams.put("key", "value");
final ConfirmPaymentIntentParams params =
ConfirmPaymentIntentParams.createWithPaymentMethodCreateParams(
PM_CREATE_PARAMS, CLIENT_SECRET, RETURN_URL, true, extraParams
);

assertEquals(params, params.toBuilder().build());
}
}
Loading

0 comments on commit e49bef9

Please sign in to comment.