Commit f4c4957a authored by Liquan (Max) Gu's avatar Liquan (Max) Gu Committed by Commit Bot

[WebLayer] PRService takes over PaymentResponseHelper

Changes:
* PRService takes over mPaymentResponseHelper and its result listener
  PaymentResponseRequesterDelegate from CPRService.
* Although CPRService no longer retain the helper, it's still
  responsible for creating an implementation of it, because WebLayer
  payment would have a different implementation of it.
* Chrome only tells PRService whether and how to patch the payment
  response according to its need of skip-to-GPay. WebLayer would have
  no such logic.

Bug: 1146159

Change-Id: Iede2e3d7a9f2d6b404aaaf59c880d5454b143099
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2522213
Commit-Queue: Liquan (Max) Gu <maxlg@chromium.org>
Reviewed-by: default avatarRouslan Solomakhin <rouslan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#825194}
parent e45fdec6
...@@ -43,7 +43,6 @@ import org.chromium.components.payments.PaymentRequestService; ...@@ -43,7 +43,6 @@ import org.chromium.components.payments.PaymentRequestService;
import org.chromium.components.payments.PaymentRequestServiceUtil; import org.chromium.components.payments.PaymentRequestServiceUtil;
import org.chromium.components.payments.PaymentRequestSpec; import org.chromium.components.payments.PaymentRequestSpec;
import org.chromium.components.payments.PaymentResponseHelperInterface; import org.chromium.components.payments.PaymentResponseHelperInterface;
import org.chromium.components.payments.PaymentResponseHelperInterface.PaymentResponseResultCallback;
import org.chromium.components.payments.PaymentUIsObserver; import org.chromium.components.payments.PaymentUIsObserver;
import org.chromium.components.payments.PaymentValidator; import org.chromium.components.payments.PaymentValidator;
import org.chromium.components.payments.Section; import org.chromium.components.payments.Section;
...@@ -74,7 +73,6 @@ import java.util.Set; ...@@ -74,7 +73,6 @@ import java.util.Set;
*/ */
public class ChromePaymentRequestService implements BrowserPaymentRequest, PaymentApp.AbortCallback, public class ChromePaymentRequestService implements BrowserPaymentRequest, PaymentApp.AbortCallback,
PaymentApp.InstrumentDetailsCallback, PaymentApp.InstrumentDetailsCallback,
PaymentResponseResultCallback,
PaymentDetailsConverter.MethodChecker, PaymentDetailsConverter.MethodChecker,
PaymentUiService.Delegate, PaymentUIsObserver { PaymentUiService.Delegate, PaymentUIsObserver {
private static final String TAG = "PaymentRequest"; private static final String TAG = "PaymentRequest";
...@@ -119,9 +117,6 @@ public class ChromePaymentRequestService implements BrowserPaymentRequest, Payme ...@@ -119,9 +117,6 @@ public class ChromePaymentRequestService implements BrowserPaymentRequest, Payme
*/ */
private boolean mDidRecordShowEvent; private boolean mDidRecordShowEvent;
/** The helper to create and fill the response to send to the merchant. */
private PaymentResponseHelperInterface mPaymentResponseHelper;
/** A helper to manage the Skip-to-GPay experimental flow. */ /** A helper to manage the Skip-to-GPay experimental flow. */
private SkipToGPayHelper mSkipToGPayHelper; private SkipToGPayHelper mSkipToGPayHelper;
private boolean mIsGooglePayBridgeActivated; private boolean mIsGooglePayBridgeActivated;
...@@ -839,7 +834,7 @@ public class ChromePaymentRequestService implements BrowserPaymentRequest, Payme ...@@ -839,7 +834,7 @@ public class ChromePaymentRequestService implements BrowserPaymentRequest, Payme
@Override @Override
public void onInstrumentDetailsLoadingWithoutUI() { public void onInstrumentDetailsLoadingWithoutUI() {
if (mPaymentRequestService == null || mPaymentUiService.getPaymentRequestUI() == null if (mPaymentRequestService == null || mPaymentUiService.getPaymentRequestUI() == null
|| mPaymentResponseHelper == null) { || mPaymentRequestService.getPaymentResponseHelper() == null) {
return; return;
} }
...@@ -855,10 +850,6 @@ public class ChromePaymentRequestService implements BrowserPaymentRequest, Payme ...@@ -855,10 +850,6 @@ public class ChromePaymentRequestService implements BrowserPaymentRequest, Payme
EditableOption selectedContact = mPaymentUiService.getContactSection() != null EditableOption selectedContact = mPaymentUiService.getContactSection() != null
? mPaymentUiService.getContactSection().getSelectedItem() ? mPaymentUiService.getContactSection().getSelectedItem()
: null; : null;
mPaymentResponseHelper = new ChromePaymentResponseHelper(selectedShippingAddress,
selectedShippingOption, selectedContact, selectedPaymentApp, mPaymentOptions,
mSkipToGPayHelper != null);
selectedPaymentApp.setPaymentHandlerHost(getPaymentHandlerHost()); selectedPaymentApp.setPaymentHandlerHost(getPaymentHandlerHost());
// Only native apps can use PaymentDetailsUpdateService. // Only native apps can use PaymentDetailsUpdateService.
if (selectedPaymentApp.getPaymentAppType() == PaymentAppType.NATIVE_MOBILE_APP) { if (selectedPaymentApp.getPaymentAppType() == PaymentAppType.NATIVE_MOBILE_APP) {
...@@ -866,7 +857,11 @@ public class ChromePaymentRequestService implements BrowserPaymentRequest, Payme ...@@ -866,7 +857,11 @@ public class ChromePaymentRequestService implements BrowserPaymentRequest, Payme
((AndroidPaymentApp) selectedPaymentApp).packageName(), ((AndroidPaymentApp) selectedPaymentApp).packageName(),
mPaymentRequestService /* PaymentApp.PaymentRequestUpdateEventListener */); mPaymentRequestService /* PaymentApp.PaymentRequestUpdateEventListener */);
} }
mPaymentRequestService.invokePaymentApp(selectedPaymentApp, /*callback=*/this); PaymentResponseHelperInterface paymentResponseHelper = new ChromePaymentResponseHelper(
selectedShippingAddress, selectedShippingOption, selectedContact,
selectedPaymentApp, mPaymentOptions, mSkipToGPayHelper != null);
mPaymentRequestService.invokePaymentApp(
selectedPaymentApp, paymentResponseHelper, /*callback=*/this);
return !selectedPaymentApp.isAutofillInstrument(); return !selectedPaymentApp.isAutofillInstrument();
} }
...@@ -1091,7 +1086,8 @@ public class ChromePaymentRequestService implements BrowserPaymentRequest, Payme ...@@ -1091,7 +1086,8 @@ public class ChromePaymentRequestService implements BrowserPaymentRequest, Payme
assert methodName != null; assert methodName != null;
assert stringifiedDetails != null; assert stringifiedDetails != null;
if (mPaymentRequestService == null || mPaymentResponseHelper == null) { if (mPaymentRequestService == null
|| mPaymentRequestService.getPaymentResponseHelper() == null) {
return; return;
} }
...@@ -1114,22 +1110,14 @@ public class ChromePaymentRequestService implements BrowserPaymentRequest, Payme ...@@ -1114,22 +1110,14 @@ public class ChromePaymentRequestService implements BrowserPaymentRequest, Payme
mJourneyLogger.setEventOccurred(Event.RECEIVED_INSTRUMENT_DETAILS); mJourneyLogger.setEventOccurred(Event.RECEIVED_INSTRUMENT_DETAILS);
mPaymentResponseHelper.generatePaymentResponse( mPaymentRequestService.getPaymentResponseHelper().generatePaymentResponse(methodName,
methodName, stringifiedDetails, payerData, /*resultCallback=*/this); stringifiedDetails, payerData, /*resultCallback=*/mPaymentRequestService);
} }
// Implements BrowserPaymentRequest:
@Override @Override
public void onPaymentResponseReady(PaymentResponse response) { public boolean patchPaymentResponseIfNeeded(PaymentResponse response) {
if (mSkipToGPayHelper != null && !mSkipToGPayHelper.patchPaymentResponse(response)) { return mSkipToGPayHelper == null || mSkipToGPayHelper.patchPaymentResponse(response);
disconnectFromClientWithDebugMessage(
ErrorStrings.PAYMENT_APP_INVALID_RESPONSE, PaymentErrorReason.NOT_SUPPORTED);
}
if (mPaymentRequestService == null) return;
mPaymentRequestService.onPaymentResponse(response);
mPaymentResponseHelper = null;
if (PaymentRequestService.getObserverForTest() != null) {
PaymentRequestService.getObserverForTest().onPaymentResponseReady();
}
} }
/** Called if unable to retrieve payment details. */ /** Called if unable to retrieve payment details. */
......
...@@ -12,6 +12,7 @@ import org.chromium.payments.mojom.PaymentErrorReason; ...@@ -12,6 +12,7 @@ import org.chromium.payments.mojom.PaymentErrorReason;
import org.chromium.payments.mojom.PaymentMethodData; import org.chromium.payments.mojom.PaymentMethodData;
import org.chromium.payments.mojom.PaymentOptions; import org.chromium.payments.mojom.PaymentOptions;
import org.chromium.payments.mojom.PaymentRequest; import org.chromium.payments.mojom.PaymentRequest;
import org.chromium.payments.mojom.PaymentResponse;
import org.chromium.payments.mojom.PaymentValidationErrors; import org.chromium.payments.mojom.PaymentValidationErrors;
import java.util.List; import java.util.List;
...@@ -162,4 +163,13 @@ public interface BrowserPaymentRequest { ...@@ -162,4 +163,13 @@ public interface BrowserPaymentRequest {
default boolean isPaymentSheetBasedPaymentAppSupported() { default boolean isPaymentSheetBasedPaymentAppSupported() {
return false; return false;
} }
/**
* Patches the given payment response if needed.
* @param response The payment response to be patched in place.
* @return Whether the patching is successful.
*/
default boolean patchPaymentResponseIfNeeded(PaymentResponse response) {
return true;
}
} }
...@@ -57,8 +57,10 @@ import java.util.Map; ...@@ -57,8 +57,10 @@ import java.util.Map;
* class need to close them with * class need to close them with
* {@link PaymentRequestService#close()}, after which no usage is allowed. * {@link PaymentRequestService#close()}, after which no usage is allowed.
*/ */
public class PaymentRequestService implements PaymentAppFactoryDelegate, PaymentAppFactoryParams, public class PaymentRequestService
PaymentRequestUpdateEventListener { implements PaymentAppFactoryDelegate, PaymentAppFactoryParams,
PaymentRequestUpdateEventListener,
PaymentResponseHelperInterface.PaymentResponseResultCallback {
private static final String TAG = "PaymentRequestServ"; private static final String TAG = "PaymentRequestServ";
private static PaymentRequestServiceObserverForTest sObserverForTest; private static PaymentRequestServiceObserverForTest sObserverForTest;
private static NativeObserverForTest sNativeObserverForTest; private static NativeObserverForTest sNativeObserverForTest;
...@@ -100,6 +102,11 @@ public class PaymentRequestService implements PaymentAppFactoryDelegate, Payment ...@@ -100,6 +102,11 @@ public class PaymentRequestService implements PaymentAppFactoryDelegate, Payment
// mBrowserPaymentRequest is null when it has closed or is uninitiated. // mBrowserPaymentRequest is null when it has closed or is uninitiated.
@Nullable @Nullable
private BrowserPaymentRequest mBrowserPaymentRequest; private BrowserPaymentRequest mBrowserPaymentRequest;
/** The helper to create and fill the response to send to the merchant. */
@Nullable
private PaymentResponseHelperInterface mPaymentResponseHelper;
/** /**
* A mapping of the payment method names to the corresponding payment method specific data. If * A mapping of the payment method names to the corresponding payment method specific data. If
* STRICT_HAS_ENROLLED_AUTOFILL_INSTRUMENT is enabled, then the key "basic-card-payment-options" * STRICT_HAS_ENROLLED_AUTOFILL_INSTRUMENT is enabled, then the key "basic-card-payment-options"
...@@ -474,12 +481,44 @@ public class PaymentRequestService implements PaymentAppFactoryDelegate, Payment ...@@ -474,12 +481,44 @@ public class PaymentRequestService implements PaymentAppFactoryDelegate, Payment
return true; return true;
} }
/**
* @return The {@link PaymentResponseHelperInterface} that's passed in through {@link
* #invokePaymentApp}. Returns null before the payment app is invoked.
*/
@Nullable
public PaymentResponseHelperInterface getPaymentResponseHelper() {
return mPaymentResponseHelper;
}
// Implements PaymentResponseHelper.PaymentResponseResultCallback:
@Override
public void onPaymentResponseReady(PaymentResponse response) {
if (!mBrowserPaymentRequest.patchPaymentResponseIfNeeded(response)) {
mBrowserPaymentRequest.disconnectFromClientWithDebugMessage(
ErrorStrings.PAYMENT_APP_INVALID_RESPONSE, PaymentErrorReason.NOT_SUPPORTED);
// Intentionally do not early-return.
}
if (mClient != null) {
mClient.onPaymentResponse(response);
}
mPaymentResponseHelper = null;
if (sObserverForTest != null) {
sObserverForTest.onPaymentResponseReady();
}
}
/** /**
* Invokes the given payment app. * Invokes the given payment app.
* @param paymentApp The payment app to be invoked. * @param paymentApp The payment app to be invoked.
* @param paymentResponseHelper The helper to create and fill the response to send to the
* merchant. The helper should have this instance as the delegate {@link
* PaymentResponseHelperInterface.PaymentResponseRequesterDelegate}.
* @param callback The callback of the invocation. * @param callback The callback of the invocation.
*/ */
public void invokePaymentApp(PaymentApp paymentApp, InstrumentDetailsCallback callback) { public void invokePaymentApp(PaymentApp paymentApp,
PaymentResponseHelperInterface paymentResponseHelper,
InstrumentDetailsCallback callback) {
mPaymentResponseHelper = paymentResponseHelper;
mJourneyLogger.recordCheckoutStep(CheckoutFunnelStep.PAYMENT_HANDLER_INVOKED); mJourneyLogger.recordCheckoutStep(CheckoutFunnelStep.PAYMENT_HANDLER_INVOKED);
// Create maps that are subsets of mMethodData and mModifiers, that contain the payment // Create maps that are subsets of mMethodData and mModifiers, that contain the payment
// methods supported by the selected payment app. If the intersection of method data // methods supported by the selected payment app. If the intersection of method data
...@@ -1007,14 +1046,6 @@ public class PaymentRequestService implements PaymentAppFactoryDelegate, Payment ...@@ -1007,14 +1046,6 @@ public class PaymentRequestService implements PaymentAppFactoryDelegate, Payment
mClient.onPayerDetailChange(detail); mClient.onPayerDetailChange(detail);
} }
/** Invokes {@link PaymentRequestClient.onPaymentResponse}. */
public void onPaymentResponse(PaymentResponse response) {
// Every caller should stop referencing this class once close() is called.
assert mClient != null;
mClient.onPaymentResponse(response);
}
/** Invokes {@link PaymentRequestClient.onError}. */ /** Invokes {@link PaymentRequestClient.onError}. */
public void onError(int error, String errorMessage) { public void onError(int error, String errorMessage) {
// Every caller should stop referencing this class once close() is called. // Every caller should stop referencing this class once close() is called.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment