| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/payments/content/payment_request_state.h" | 5 #include "components/payments/content/payment_request_state.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "components/autofill/core/browser/autofill_data_util.h" | 10 #include "components/autofill/core/browser/autofill_data_util.h" |
| 11 #include "components/autofill/core/browser/autofill_profile.h" | 11 #include "components/autofill/core/browser/autofill_profile.h" |
| 12 #include "components/autofill/core/browser/credit_card.h" | 12 #include "components/autofill/core/browser/credit_card.h" |
| 13 #include "components/autofill/core/browser/personal_data_manager.h" | 13 #include "components/autofill/core/browser/personal_data_manager.h" |
| 14 #include "components/payments/content/payment_request_spec.h" | 14 #include "components/payments/content/payment_request_spec.h" |
| 15 #include "components/payments/content/payment_response_helper.h" | 15 #include "components/payments/content/payment_response_helper.h" |
| 16 #include "components/payments/core/autofill_payment_instrument.h" | 16 #include "components/payments/core/autofill_payment_instrument.h" |
| 17 #include "components/payments/core/payment_instrument.h" |
| 17 #include "components/payments/core/payment_request_delegate.h" | 18 #include "components/payments/core/payment_request_delegate.h" |
| 18 #include "components/payments/core/profile_util.h" | 19 #include "components/payments/core/profile_util.h" |
| 19 | 20 |
| 20 namespace payments { | 21 namespace payments { |
| 21 | 22 |
| 22 PaymentRequestState::PaymentRequestState( | 23 PaymentRequestState::PaymentRequestState( |
| 23 PaymentRequestSpec* spec, | 24 PaymentRequestSpec* spec, |
| 24 Delegate* delegate, | 25 Delegate* delegate, |
| 25 const std::string& app_locale, | 26 const std::string& app_locale, |
| 26 autofill::PersonalDataManager* personal_data_manager, | 27 autofill::PersonalDataManager* personal_data_manager, |
| 27 PaymentRequestDelegate* payment_request_delegate) | 28 PaymentRequestDelegate* payment_request_delegate) |
| 28 : is_ready_to_pay_(false), | 29 : is_ready_to_pay_(false), |
| 29 app_locale_(app_locale), | 30 app_locale_(app_locale), |
| 30 spec_(spec), | 31 spec_(spec), |
| 31 delegate_(delegate), | 32 delegate_(delegate), |
| 32 personal_data_manager_(personal_data_manager), | 33 personal_data_manager_(personal_data_manager), |
| 33 selected_shipping_profile_(nullptr), | 34 selected_shipping_profile_(nullptr), |
| 34 selected_contact_profile_(nullptr), | 35 selected_contact_profile_(nullptr), |
| 35 selected_instrument_(nullptr), | 36 selected_instrument_(nullptr), |
| 36 payment_request_delegate_(payment_request_delegate) { | 37 payment_request_delegate_(payment_request_delegate) { |
| 37 PopulateProfileCache(); | 38 PopulateProfileCache(); |
| 38 SetDefaultProfileSelections(); | 39 SetDefaultProfileSelections(); |
| 39 } | 40 } |
| 40 PaymentRequestState::~PaymentRequestState() {} | 41 PaymentRequestState::~PaymentRequestState() {} |
| 41 | 42 |
| 43 void PaymentRequestState::OnPaymentResponseReady( |
| 44 mojom::PaymentResponsePtr payment_response) { |
| 45 delegate_->OnPaymentResponseAvailable(std::move(payment_response)); |
| 46 } |
| 47 |
| 42 bool PaymentRequestState::CanMakePayment() const { | 48 bool PaymentRequestState::CanMakePayment() const { |
| 43 for (const std::unique_ptr<PaymentInstrument>& instrument : | 49 for (const std::unique_ptr<PaymentInstrument>& instrument : |
| 44 available_instruments_) { | 50 available_instruments_) { |
| 45 if (instrument->IsValidForCanMakePayment() && | 51 if (instrument->IsValidForCanMakePayment() && |
| 46 spec_->supported_card_networks_set().count( | 52 spec_->supported_card_networks_set().count( |
| 47 instrument.get()->method_name())) { | 53 instrument.get()->method_name())) { |
| 48 return true; | 54 return true; |
| 49 } | 55 } |
| 50 } | 56 } |
| 51 return false; | 57 return false; |
| 52 } | 58 } |
| 53 | 59 |
| 54 void PaymentRequestState::AddObserver(Observer* observer) { | 60 void PaymentRequestState::AddObserver(Observer* observer) { |
| 55 CHECK(observer); | 61 CHECK(observer); |
| 56 observers_.AddObserver(observer); | 62 observers_.AddObserver(observer); |
| 57 } | 63 } |
| 58 | 64 |
| 59 void PaymentRequestState::RemoveObserver(Observer* observer) { | 65 void PaymentRequestState::RemoveObserver(Observer* observer) { |
| 60 observers_.RemoveObserver(observer); | 66 observers_.RemoveObserver(observer); |
| 61 } | 67 } |
| 62 | 68 |
| 63 // TODO(sebsg): Move this to the PaymentResponseHelper. | |
| 64 void PaymentRequestState::OnInstrumentDetailsReady( | |
| 65 const std::string& method_name, | |
| 66 const std::string& stringified_details) { | |
| 67 mojom::PaymentResponsePtr payment_response = mojom::PaymentResponse::New(); | |
| 68 | |
| 69 // Make sure that we return the method name that the merchant specified for | |
| 70 // this instrument: cards can be either specified through their name (e.g., | |
| 71 // "visa") or through basic-card's supportedNetworks. | |
| 72 payment_response->method_name = | |
| 73 spec_->IsMethodSupportedThroughBasicCard(method_name) | |
| 74 ? kBasicCardMethodName | |
| 75 : method_name; | |
| 76 payment_response->stringified_details = stringified_details; | |
| 77 | |
| 78 // Shipping Address section | |
| 79 if (spec_->request_shipping()) { | |
| 80 DCHECK(selected_shipping_profile_); | |
| 81 payment_response->shipping_address = | |
| 82 PaymentResponseHelper::GetMojomPaymentAddressFromAutofillProfile( | |
| 83 selected_shipping_profile_, app_locale_); | |
| 84 | |
| 85 DCHECK(spec_->selected_shipping_option()); | |
| 86 payment_response->shipping_option = spec_->selected_shipping_option()->id; | |
| 87 } | |
| 88 | |
| 89 // Contact Details section. | |
| 90 if (spec_->request_payer_name()) { | |
| 91 DCHECK(selected_contact_profile_); | |
| 92 payment_response->payer_name = | |
| 93 base::UTF16ToUTF8(selected_contact_profile_->GetInfo( | |
| 94 autofill::AutofillType(autofill::NAME_FULL), app_locale_)); | |
| 95 } | |
| 96 if (spec_->request_payer_email()) { | |
| 97 DCHECK(selected_contact_profile_); | |
| 98 payment_response->payer_email = base::UTF16ToUTF8( | |
| 99 selected_contact_profile_->GetRawInfo(autofill::EMAIL_ADDRESS)); | |
| 100 } | |
| 101 if (spec_->request_payer_phone()) { | |
| 102 DCHECK(selected_contact_profile_); | |
| 103 // TODO(crbug.com/705945): Format phone number according to spec. | |
| 104 payment_response->payer_phone = | |
| 105 base::UTF16ToUTF8(selected_contact_profile_->GetRawInfo( | |
| 106 autofill::PHONE_HOME_WHOLE_NUMBER)); | |
| 107 } | |
| 108 | |
| 109 delegate_->OnPaymentResponseAvailable(std::move(payment_response)); | |
| 110 } | |
| 111 | |
| 112 void PaymentRequestState::GeneratePaymentResponse() { | 69 void PaymentRequestState::GeneratePaymentResponse() { |
| 113 DCHECK(is_ready_to_pay()); | 70 DCHECK(is_ready_to_pay()); |
| 114 // Fetch the instrument details, will call back into | 71 |
| 115 // PaymentRequest::OnInstrumentDetailsReady. | 72 // Once the response is ready, will call back into OnPaymentResponseReady. |
| 116 selected_instrument_->InvokePaymentApp(this); | 73 response_helper_ = base::MakeUnique<PaymentResponseHelper>( |
| 74 app_locale_, spec_, selected_instrument_, selected_shipping_profile_, |
| 75 selected_contact_profile_, this); |
| 117 } | 76 } |
| 118 | 77 |
| 119 void PaymentRequestState::SetSelectedShippingOption( | 78 void PaymentRequestState::SetSelectedShippingOption( |
| 120 const std::string& shipping_option_id) { | 79 const std::string& shipping_option_id) { |
| 121 // This will inform the merchant and will lead to them calling updateWith with | 80 // This will inform the merchant and will lead to them calling updateWith with |
| 122 // new PaymentDetails. | 81 // new PaymentDetails. |
| 123 delegate_->OnShippingOptionIdSelected(shipping_option_id); | 82 delegate_->OnShippingOptionIdSelected(shipping_option_id); |
| 124 } | 83 } |
| 125 | 84 |
| 126 void PaymentRequestState::SetSelectedShippingProfile( | 85 void PaymentRequestState::SetSelectedShippingProfile( |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 bool PaymentRequestState::ArePaymentOptionsSatisfied() { | 207 bool PaymentRequestState::ArePaymentOptionsSatisfied() { |
| 249 // TODO(mathp): Have a measure of shipping address completeness. | 208 // TODO(mathp): Have a measure of shipping address completeness. |
| 250 if (spec_->request_shipping() && selected_shipping_profile_ == nullptr) | 209 if (spec_->request_shipping() && selected_shipping_profile_ == nullptr) |
| 251 return false; | 210 return false; |
| 252 | 211 |
| 253 profile_util::PaymentsProfileComparator comparator(app_locale_, *spec_); | 212 profile_util::PaymentsProfileComparator comparator(app_locale_, *spec_); |
| 254 return comparator.IsContactInfoComplete(selected_contact_profile_); | 213 return comparator.IsContactInfoComplete(selected_contact_profile_); |
| 255 } | 214 } |
| 256 | 215 |
| 257 } // namespace payments | 216 } // namespace payments |
| OLD | NEW |