Skip to content

Commit

Permalink
Refactor PaymentIntentParams and SetupIntentParams (#1172)
Browse files Browse the repository at this point in the history
- Rename `PaymentIntentParams` to `ConfirmPaymentIntentParams`
- Rename `SetupIntentParams` to `ConfirmSetupIntentParams`
- Simplify method names for both classes
- Make fields immutable
- Make retrieve methods take a clientSecret String instead
  of IntentParams

MOBILE3DS2-437
  • Loading branch information
mshafrir-stripe authored Jul 12, 2019
1 parent c46bf16 commit 5e56663
Show file tree
Hide file tree
Showing 25 changed files with 664 additions and 702 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@
import com.google.android.gms.wallet.TransactionInfo;
import com.google.android.gms.wallet.Wallet;
import com.google.android.gms.wallet.WalletConstants;
import com.stripe.android.ApiResultCallback;
import com.stripe.android.PaymentConfiguration;
import com.stripe.android.Stripe;
import com.stripe.android.model.Address;
import com.stripe.android.model.PaymentMethod;
import com.stripe.android.model.PaymentMethodCreateParams;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
import com.stripe.android.PaymentIntentResult;
import com.stripe.android.SetupIntentResult;
import com.stripe.android.Stripe;
import com.stripe.android.model.ConfirmPaymentIntentParams;
import com.stripe.android.model.ConfirmSetupIntentParams;
import com.stripe.android.model.PaymentIntent;
import com.stripe.android.model.PaymentIntentParams;
import com.stripe.android.model.SetupIntent;
import com.stripe.android.model.SetupIntentParams;
import com.stripe.example.R;
import com.stripe.example.module.RetrofitFactory;
import com.stripe.example.service.StripeService;
Expand All @@ -40,7 +40,7 @@

/**
* An example of creating a PaymentIntent, then confirming it with
* {@link Stripe#confirmPayment(Activity, PaymentIntentParams)}}
* {@link Stripe#confirmPayment(Activity, ConfirmPaymentIntentParams)}}
*/
public class PaymentAuthActivity extends AppCompatActivity {

Expand Down Expand Up @@ -100,7 +100,7 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
private void confirmPaymentIntent(@NonNull String paymentIntentClientSecret) {
mStatusTextView.append("\n\nStarting payment authentication");
mStripe.confirmPayment(this,
PaymentIntentParams.createConfirmPaymentIntentWithPaymentMethodId(
ConfirmPaymentIntentParams.createWithPaymentMethodId(
PAYMENT_METHOD_3DS2_REQUIRED,
paymentIntentClientSecret,
RETURN_URL));
Expand All @@ -109,7 +109,7 @@ private void confirmPaymentIntent(@NonNull String paymentIntentClientSecret) {
private void confirmSetupIntent(@NonNull String setupIntentClientSecret) {
mStatusTextView.append("\n\nStarting setup intent authentication");
mStripe.confirmSetupIntent(this,
SetupIntentParams.createConfirmParams(
ConfirmSetupIntentParams.create(
PAYMENT_METHOD_AUTH_REQUIRED_ON_SETUP,
setupIntentClientSecret,
RETURN_URL));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import com.stripe.android.PaymentConfiguration;
import com.stripe.android.Stripe;
import com.stripe.android.model.Card;
import com.stripe.android.model.ConfirmPaymentIntentParams;
import com.stripe.android.model.PaymentIntent;
import com.stripe.android.model.PaymentIntentParams;
import com.stripe.android.model.PaymentMethodCreateParams;
import com.stripe.android.view.CardInputWidget;
import com.stripe.example.R;
Expand Down Expand Up @@ -148,8 +148,7 @@ private void onCreatedPaymentIntent(@NonNull ResponseBody responseBody) {

private void retrievePaymentIntent() {
final Observable<PaymentIntent> paymentIntentObservable = Observable.fromCallable(
() -> mStripe.retrievePaymentIntentSynchronous(
PaymentIntentParams.createRetrievePaymentIntentParams(mClientSecret)));
() -> mStripe.retrievePaymentIntentSynchronous(mClientSecret));
final Disposable disposable = paymentIntentObservable
.subscribeOn(Schedulers.io())
.doOnSubscribe((d) ->
Expand All @@ -170,11 +169,11 @@ private void confirmPaymentIntent(@NonNull final Card card) {
PaymentMethodCreateParams.create(card.toPaymentMethodParamsCard(), null);
final Observable<PaymentIntent> paymentIntentObservable = Observable.fromCallable(
() -> {
final PaymentIntentParams paymentIntentParams = PaymentIntentParams
.createConfirmPaymentIntentWithPaymentMethodCreateParams(
final ConfirmPaymentIntentParams confirmPaymentIntentParams =
ConfirmPaymentIntentParams.createWithPaymentMethodCreateParams(
paymentMethodCreateParams, mClientSecret, RETURN_URL);
return mStripe.confirmPaymentIntentSynchronous(
paymentIntentParams,
confirmPaymentIntentParams,
PaymentConfiguration.getInstance().getPublishableKey());
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
import com.stripe.android.PaymentConfiguration;
import com.stripe.android.Stripe;
import com.stripe.android.model.Card;
import com.stripe.android.model.ConfirmSetupIntentParams;
import com.stripe.android.model.PaymentMethod;
import com.stripe.android.model.PaymentMethodCreateParams;
import com.stripe.android.model.SetupIntent;
import com.stripe.android.model.SetupIntentParams;
import com.stripe.android.view.CardInputWidget;
import com.stripe.example.R;
import com.stripe.example.controller.ErrorDialogHandler;
Expand Down Expand Up @@ -140,8 +140,7 @@ private void onCreatedSetupIntent(@NonNull ResponseBody responseBody) {

private void retrieveSetupIntent() {
final Observable<SetupIntent> setupIntentObservable = Observable.fromCallable(
() -> mStripe.retrieveSetupIntentSynchronous(
SetupIntentParams.createRetrieveParams(mClientSecret)));
() -> mStripe.retrieveSetupIntentSynchronous(mClientSecret));

final Disposable disposable = setupIntentObservable
.subscribeOn(Schedulers.io())
Expand Down Expand Up @@ -190,11 +189,11 @@ private void createPaymentMethod(@NonNull final Card card) {
private void confirmSetupIntent() {
final Observable<SetupIntent> setupIntentObservable = Observable.fromCallable(
() -> {
final SetupIntentParams setupIntentParams =
SetupIntentParams.createConfirmParams(
final ConfirmSetupIntentParams confirmSetupIntentParams =
ConfirmSetupIntentParams.create(
Objects.requireNonNull(mPaymentMethod.id), mClientSecret,
RETURN_URL);
return mStripe.confirmSetupIntentSynchronous(setupIntentParams,
return mStripe.confirmSetupIntentSynchronous(confirmSetupIntentParams,
PaymentConfiguration.getInstance().getPublishableKey());
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import android.support.v7.app.AlertDialog;

import com.stripe.android.model.Card;
import com.stripe.android.model.SourceCardData;
import com.stripe.example.R;

import java.util.Map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.app.DialogFragment;

import com.stripe.example.R;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import com.stripe.android.model.Address;
import com.stripe.android.model.Customer;
import com.stripe.android.model.PaymentIntent;
import com.stripe.android.model.PaymentIntentParams;
import com.stripe.android.model.PaymentMethod;
import com.stripe.android.model.ShippingInformation;
import com.stripe.android.model.ShippingMethod;
Expand Down Expand Up @@ -353,12 +352,10 @@ private void confirmPaymentIntent(@NonNull String paymentIntentId) {
private void onPaymentIntentClientSecretResponse(@NonNull ResponseBody responseBody)
throws IOException, JSONException {
final String clientSecret = new JSONObject(responseBody.string()).getString("secret");
final PaymentIntentParams paymentIntentParams =
PaymentIntentParams.createRetrievePaymentIntentParams(clientSecret);
mCompositeDisposable.add(
Observable
.fromCallable(() ->
mStripe.retrievePaymentIntentSynchronous(paymentIntentParams))
mStripe.retrievePaymentIntentSynchronous(clientSecret))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe(disposable -> startLoading())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.stripe.android.PaymentConfiguration;
import com.stripe.android.Stripe;
import com.stripe.android.model.PaymentIntent;
import com.stripe.android.model.PaymentIntentParams;
import com.stripe.samplestore.service.SampleStoreEphemeralKeyProvider;

import java.lang.ref.WeakReference;
Expand Down Expand Up @@ -134,16 +133,14 @@ private void handlePostAuthReturn() {
if (intentUri != null) {
if ("stripe".equals(intentUri.getScheme()) &&
"payment-auth-return".equals(intentUri.getHost())) {
final String paymentIntentClientSecret =
final String clientSecret =
intentUri.getQueryParameter("payment_intent_client_secret");
if (paymentIntentClientSecret != null) {
if (clientSecret != null) {
final Stripe stripe = new Stripe(getApplicationContext(),
PaymentConfiguration.getInstance().getPublishableKey());
final PaymentIntentParams paymentIntentParams = PaymentIntentParams
.createRetrievePaymentIntentParams(paymentIntentClientSecret);
mCompositeDisposable.add(Observable
.fromCallable(() ->
stripe.retrievePaymentIntentSynchronous(paymentIntentParams))
stripe.retrievePaymentIntentSynchronous(clientSecret))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe((paymentIntent -> {
Expand Down
47 changes: 19 additions & 28 deletions stripe/src/main/java/com/stripe/android/PaymentController.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
import android.support.annotation.VisibleForTesting;

import com.stripe.android.exception.StripeException;
import com.stripe.android.model.ConfirmPaymentIntentParams;
import com.stripe.android.model.ConfirmSetupIntentParams;
import com.stripe.android.model.PaymentIntent;
import com.stripe.android.model.PaymentIntentParams;
import com.stripe.android.model.SetupIntent;
import com.stripe.android.model.SetupIntentParams;
import com.stripe.android.model.Stripe3ds2AuthResult;
import com.stripe.android.model.Stripe3ds2Fingerprint;
import com.stripe.android.model.Stripe3dsRedirect;
Expand Down Expand Up @@ -97,7 +97,7 @@ void startAuth(@NonNull Stripe stripe,
@NonNull final String publishableKey) {
mApiKeyValidator.requireValid(publishableKey);
new RetrieveIntentTask(stripe,
PaymentIntentParams.createRetrievePaymentIntentParams(clientSecret),
clientSecret,
publishableKey,
new ApiResultCallback<StripeIntent>() {
@Override
Expand Down Expand Up @@ -154,7 +154,7 @@ void handlePaymentResult(@NonNull Stripe stripe, @NonNull Intent data,

@StripeIntentResult.Status final int authStatus = data.getIntExtra(
StripeIntentResultExtras.AUTH_STATUS, StripeIntentResult.Status.UNKNOWN);
new RetrieveIntentTask(stripe, createPaymentIntentParams(data), publishableKey,
new RetrieveIntentTask(stripe, getClientSecret(data), publishableKey,
new ApiResultCallback<StripeIntent>() {
@Override
public void onSuccess(@NonNull StripeIntent stripeIntent) {
Expand Down Expand Up @@ -196,7 +196,7 @@ void handleSetupResult(@NonNull Stripe stripe, @NonNull Intent data,
@StripeIntentResult.Status final int authStatus = data.getIntExtra(
StripeIntentResultExtras.AUTH_STATUS, StripeIntentResult.Status.UNKNOWN);

new RetrieveIntentTask(stripe, createSetupIntentParams(data), publishableKey,
new RetrieveIntentTask(stripe, getClientSecret(data), publishableKey,
new ApiResultCallback<StripeIntent>() {
@Override
public void onSuccess(@NonNull StripeIntent stripeIntent) {
Expand All @@ -218,16 +218,8 @@ public void onError(@NonNull Exception e) {

@VisibleForTesting
@NonNull
PaymentIntentParams createPaymentIntentParams(@NonNull Intent data) {
final String clientSecret = data.getStringExtra(StripeIntentResultExtras.CLIENT_SECRET);
return PaymentIntentParams.createRetrievePaymentIntentParams(clientSecret);
}

@VisibleForTesting
@NonNull
SetupIntentParams createSetupIntentParams(@NonNull Intent data) {
final String clientSecret = data.getStringExtra(StripeIntentResultExtras.CLIENT_SECRET);
return SetupIntentParams.createRetrieveParams(clientSecret);
String getClientSecret(@NonNull Intent data) {
return data.getStringExtra(StripeIntentResultExtras.CLIENT_SECRET);
}

/**
Expand Down Expand Up @@ -290,7 +282,7 @@ static int getRequestCode(@NonNull StripeIntent intent) {
* @return PAYMENT_REQUEST_CODE or SETUP_REQUEST_CODE
*/
static int getRequestCode(@NonNull StripeIntentParams params) {
if (params instanceof PaymentIntentParams) {
if (params instanceof ConfirmPaymentIntentParams) {
return PAYMENT_REQUEST_CODE;
}
return SETUP_REQUEST_CODE;
Expand Down Expand Up @@ -359,28 +351,27 @@ private static void handleError(@NonNull Activity activity,

private static final class RetrieveIntentTask extends ApiOperation<StripeIntent> {
@NonNull private final Stripe mStripe;
@NonNull private final StripeIntentParams mParams;
@NonNull private final String mClientSecret;
@NonNull private final String mPublishableKey;

private RetrieveIntentTask(@NonNull Stripe stripe,
@NonNull StripeIntentParams params,
@NonNull String clientSecret,
@NonNull String publishableKey,
@NonNull ApiResultCallback<StripeIntent> callback) {
super(callback);
mStripe = stripe;
mParams = params;
mClientSecret = clientSecret;
mPublishableKey = publishableKey;
}

@Nullable
@Override
StripeIntent getResult() throws StripeException {
if (mParams instanceof PaymentIntentParams) {
if (mClientSecret.startsWith("pi_")) {
return mStripe.retrievePaymentIntentSynchronous(
(PaymentIntentParams) mParams, mPublishableKey);
} else if (mParams instanceof SetupIntentParams) {
return mStripe.retrieveSetupIntentSynchronous(
(SetupIntentParams) mParams, mPublishableKey);
mClientSecret, mPublishableKey);
} else if (mClientSecret.startsWith("seti_")) {
return mStripe.retrieveSetupIntentSynchronous(mClientSecret, mPublishableKey);
}
return null;
}
Expand All @@ -404,12 +395,12 @@ private ConfirmStripeIntentTask(@NonNull Stripe stripe,
@Nullable
@Override
StripeIntent getResult() throws StripeException {
if (mParams instanceof PaymentIntentParams) {
if (mParams instanceof ConfirmPaymentIntentParams) {
return mStripe.confirmPaymentIntentSynchronous(
(PaymentIntentParams) mParams, mPublishableKey);
} else if (mParams instanceof SetupIntentParams) {
(ConfirmPaymentIntentParams) mParams, mPublishableKey);
} else if (mParams instanceof ConfirmSetupIntentParams) {
return mStripe.confirmSetupIntentSynchronous(
(SetupIntentParams) mParams, mPublishableKey);
(ConfirmSetupIntentParams) mParams, mPublishableKey);
}
return null;
}
Expand Down
Loading

0 comments on commit 5e56663

Please sign in to comment.