Commit ada7f457 authored by Clemens Arbesser's avatar Clemens Arbesser Committed by Commit Bot

Added support to restrict the set of supported basic-card networks for a...

Added support to restrict the set of supported basic-card networks for a payment request (e.g., accept only visa cards).

An optional array of card issuers (e.g., `visa`, `mastercard`, ...) can now be specified in GetPaymentInformationProto. The default is as before: all cards are allowed. If the field is non-empty, the PaymentRequestUI will only show the subset of basic-card payment methods that matches the specified cards. Also, interactively adding payment methods will be restricted to the specified cards only.

In the future, we may want to expand the proto even further as defined by the w3c specification for payment requests (https://www.w3.org/TR/payment-request/), but for now this filter should be good enough (we currently only support basic-card payment methods).

Bug: 806868
Change-Id: I41b3017c06104045699818f8fe5a98bb63927ea4
Reviewed-on: https://chromium-review.googlesource.com/c/1307447
Commit-Queue: Clemens Arbesser <arbesser@google.com>
Reviewed-by: default avatarGanggui Tang <gogerald@chromium.org>
Cr-Commit-Position: refs/heads/master@{#604537}
parent 0fa1b4bb
......@@ -260,15 +260,16 @@ public class AutofillAssistantUiController implements AutofillAssistantUiDelegat
@CalledByNative
private void onRequestPaymentInformation(boolean requestShipping, boolean requestPayerName,
boolean requestPayerPhone, boolean requestPayerEmail, int shippingType, String title) {
boolean requestPayerPhone, boolean requestPayerEmail, int shippingType, String title,
String[] supportedBasicCardNetworks) {
PaymentOptions paymentOtions = new PaymentOptions();
paymentOtions.requestShipping = requestShipping;
paymentOtions.requestPayerName = requestPayerName;
paymentOtions.requestPayerPhone = requestPayerPhone;
paymentOtions.requestPayerEmail = requestPayerEmail;
paymentOtions.shippingType = shippingType;
mAutofillAssistantPaymentRequest =
new AutofillAssistantPaymentRequest(mWebContents, paymentOtions, title);
mAutofillAssistantPaymentRequest = new AutofillAssistantPaymentRequest(
mWebContents, paymentOtions, title, supportedBasicCardNetworks);
mUiDelegateHolder.performUiOperation(
uiDelegate -> mAutofillAssistantPaymentRequest.show(selectedPaymentInformation -> {
......
......@@ -32,6 +32,7 @@ import org.chromium.payments.mojom.PaymentOptions;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......@@ -88,12 +89,15 @@ public class AutofillAssistantPaymentRequest implements PaymentRequestUI.Client
/**
* Constructor of AutofillAssistantPaymentRequest.
*
* @webContents The web contents of the payment request associated with.
* @paymentOptions The options to request payment information.
* @title The title to display in the payment request.
* @webContents The web contents of the payment request associated with.
* @paymentOptions The options to request payment information.
* @title The title to display in the payment request.
* @supportedBasicCardNetworks Optional array of supported basic card networks (see {@link
* BasicCardUtils}). If non-empty, only the specified card networks
* will be available for the basic-card payment method.
*/
public AutofillAssistantPaymentRequest(
WebContents webContents, PaymentOptions paymentOptions, String title) {
public AutofillAssistantPaymentRequest(WebContents webContents, PaymentOptions paymentOptions,
String title, String[] supportedBasicCardNetworks) {
mWebContents = webContents;
mPaymentOptions = paymentOptions;
mTitle = title;
......@@ -105,6 +109,22 @@ public class AutofillAssistantPaymentRequest implements PaymentRequestUI.Client
// Only enable 'basic-card' payment method.
PaymentMethodData methodData = new PaymentMethodData();
methodData.supportedMethod = BASIC_CARD_PAYMENT_METHOD;
// Apply basic-card filter if specified
if (supportedBasicCardNetworks.length > 0) {
ArrayList<Integer> filteredNetworks = new ArrayList<>();
Map<String, Integer> networks = getNetworkIdentifiers();
for (int i = 0; i < supportedBasicCardNetworks.length; i++) {
assert networks.containsKey(supportedBasicCardNetworks[i]);
filteredNetworks.add(networks.get(supportedBasicCardNetworks[i]));
}
methodData.supportedNetworks = new int[filteredNetworks.size()];
for (int i = 0; i < filteredNetworks.size(); i++) {
methodData.supportedNetworks[i] = filteredNetworks.get(i);
}
}
mMethodData = new ArrayMap<>();
mMethodData.put(BASIC_CARD_PAYMENT_METHOD, methodData);
mCardEditor.addAcceptedPaymentMethodIfRecognized(methodData);
......@@ -445,6 +465,18 @@ public class AutofillAssistantPaymentRequest implements PaymentRequestUI.Client
});
}
/**
* @return a complete map of string identifiers to BasicCardNetworks.
*/
private static Map<String, Integer> getNetworkIdentifiers() {
Map<Integer, String> networksByInt = BasicCardUtils.getNetworks();
Map<String, Integer> networksByString = new HashMap<>();
for (Map.Entry<Integer, String> entry : networksByInt.entrySet()) {
networksByString.put(entry.getValue(), entry.getKey());
}
return networksByString;
}
@Override
public boolean onPayClicked(EditableOption selectedShippingAddress,
EditableOption selectedShippingOption, EditableOption selectedPaymentMethod) {
......
......@@ -115,4 +115,4 @@ public class BasicCardUtils {
}
private BasicCardUtils() {}
}
\ No newline at end of file
}
......@@ -249,7 +249,8 @@ void UiControllerAndroid::ChooseCard(
void UiControllerAndroid::GetPaymentInformation(
payments::mojom::PaymentOptionsPtr payment_options,
base::OnceCallback<void(std::unique_ptr<PaymentInformation>)> callback,
const std::string& title) {
const std::string& title,
const std::vector<std::string>& supported_basic_card_networks) {
DCHECK(!get_payment_information_callback_);
get_payment_information_callback_ = std::move(callback);
JNIEnv* env = AttachCurrentThread();
......@@ -259,7 +260,8 @@ void UiControllerAndroid::GetPaymentInformation(
payment_options->request_payer_phone,
payment_options->request_payer_email,
static_cast<int>(payment_options->shipping_type),
base::android::ConvertUTF8ToJavaString(env, title));
base::android::ConvertUTF8ToJavaString(env, title),
base::android::ToJavaArrayOfStrings(env, supported_basic_card_networks));
}
void UiControllerAndroid::HideDetails() {
......
......@@ -48,7 +48,8 @@ class UiControllerAndroid : public UiController,
void GetPaymentInformation(
payments::mojom::PaymentOptionsPtr payment_options,
base::OnceCallback<void(std::unique_ptr<PaymentInformation>)> callback,
const std::string& title) override;
const std::string& title,
const std::vector<std::string>& supported_basic_card_networks) override;
void HideDetails() override;
void ShowDetails(const DetailsProto& details) override;
void ShowProgressBar(int progress, const std::string& message) override;
......
......@@ -70,7 +70,8 @@ class ActionDelegate {
virtual void GetPaymentInformation(
payments::mojom::PaymentOptionsPtr payment_options,
base::OnceCallback<void(std::unique_ptr<PaymentInformation>)> callback,
const std::string& title) = 0;
const std::string& title,
const std::vector<std::string>& supported_basic_card_networks) = 0;
// Fill the address form given by |selectors| with the given address
// |profile|. |profile| cannot be nullptr.
......
......@@ -34,6 +34,10 @@ void GetPaymentInformationAction::InternalProcessAction(
payment_options->request_payer_email = ask_for_payment;
payment_options->request_payer_name = ask_for_payment;
payment_options->request_payer_phone = ask_for_payment;
std::vector<std::string> supported_basic_card_networks;
std::copy(get_payment_information.supported_basic_card_networks().begin(),
get_payment_information.supported_basic_card_networks().end(),
std::back_inserter(supported_basic_card_networks));
payment_options->request_shipping =
!get_payment_information.shipping_address_name().empty();
......@@ -43,7 +47,7 @@ void GetPaymentInformationAction::InternalProcessAction(
base::BindOnce(&GetPaymentInformationAction::OnGetPaymentInformation,
weak_ptr_factory_.GetWeakPtr(), delegate,
std::move(get_payment_information), std::move(callback)),
get_payment_information.prompt());
get_payment_information.prompt(), supported_basic_card_networks);
}
void GetPaymentInformationAction::OnGetPaymentInformation(
......
......@@ -87,12 +87,13 @@ class MockActionDelegate : public ActionDelegate {
void(const std::vector<std::string>& selectors,
base::OnceCallback<void(bool)> callback));
MOCK_METHOD3(
MOCK_METHOD4(
GetPaymentInformation,
void(payments::mojom::PaymentOptionsPtr payment_options,
base::OnceCallback<void(std::unique_ptr<PaymentInformation>)>
callback,
const std::string& title));
const std::string& title,
const std::vector<std::string>& supported_basic_card_networks));
void SetFieldValue(const std::vector<std::string>& selectors,
const std::string& value,
......
......@@ -40,12 +40,13 @@ class MockUiController : public UiController {
}
MOCK_METHOD1(OnChooseCard,
void(base::OnceCallback<void(const std::string&)>& callback));
MOCK_METHOD3(
MOCK_METHOD4(
GetPaymentInformation,
void(payments::mojom::PaymentOptionsPtr payment_options,
base::OnceCallback<void(std::unique_ptr<PaymentInformation>)>
callback,
const std::string& title));
const std::string& title,
const std::vector<std::string>& supported_basic_card_networks));
MOCK_METHOD0(HideDetails, void());
MOCK_METHOD1(ShowDetails, void(const DetailsProto& details));
MOCK_METHOD2(ShowProgressBar, void(int progress, const std::string& message));
......
......@@ -88,9 +88,11 @@ void ScriptExecutor::ClickElement(const std::vector<std::string>& selectors,
void ScriptExecutor::GetPaymentInformation(
payments::mojom::PaymentOptionsPtr payment_options,
base::OnceCallback<void(std::unique_ptr<PaymentInformation>)> callback,
const std::string& title) {
const std::string& title,
const std::vector<std::string>& supported_basic_card_networks) {
delegate_->GetUiController()->GetPaymentInformation(
std::move(payment_options), std::move(callback), title);
std::move(payment_options), std::move(callback), title,
supported_basic_card_networks);
}
void ScriptExecutor::ChooseAddress(
......
......@@ -72,7 +72,8 @@ class ScriptExecutor : public ActionDelegate {
void GetPaymentInformation(
payments::mojom::PaymentOptionsPtr payment_options,
base::OnceCallback<void(std::unique_ptr<PaymentInformation>)> callback,
const std::string& title) override;
const std::string& title,
const std::vector<std::string>& supported_basic_card_networks) override;
void ChooseAddress(
base::OnceCallback<void(const std::string&)> callback) override;
void FillAddressForm(const autofill::AutofillProfile* profile,
......
......@@ -407,6 +407,9 @@ message GetPaymentInformationProto {
optional string shipping_address_name = 3;
// When 'true' will ask the user for the credit card.
optional bool ask_for_payment = 4;
// If non-empty, the UI will filter the available basic-card networks
// accordingly (e.g., only `visa' and `mastercard').
repeated string supported_basic_card_networks = 6;
}
// Resets Autofill Assistant: clears any state and server payload.
......
......@@ -64,7 +64,8 @@ class UiController {
virtual void GetPaymentInformation(
payments::mojom::PaymentOptionsPtr payment_options,
base::OnceCallback<void(std::unique_ptr<PaymentInformation>)> callback,
const std::string& title) = 0;
const std::string& title,
const std::vector<std::string>& supported_basic_card_networks) = 0;
// Hide contextual information.
virtual void HideDetails() = 0;
......
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