Chromium Code Reviews| 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" |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 32 personal_data_manager_(personal_data_manager), | 32 personal_data_manager_(personal_data_manager), |
| 33 selected_shipping_profile_(nullptr), | 33 selected_shipping_profile_(nullptr), |
| 34 selected_contact_profile_(nullptr), | 34 selected_contact_profile_(nullptr), |
| 35 selected_instrument_(nullptr), | 35 selected_instrument_(nullptr), |
| 36 payment_request_delegate_(payment_request_delegate) { | 36 payment_request_delegate_(payment_request_delegate) { |
| 37 PopulateProfileCache(); | 37 PopulateProfileCache(); |
| 38 SetDefaultProfileSelections(); | 38 SetDefaultProfileSelections(); |
| 39 } | 39 } |
| 40 PaymentRequestState::~PaymentRequestState() {} | 40 PaymentRequestState::~PaymentRequestState() {} |
| 41 | 41 |
| 42 void PaymentRequestState::OnPaymentResponseReady( | |
| 43 mojom::PaymentResponsePtr payment_response) { | |
| 44 delegate_->OnPaymentResponseAvailable(std::move(payment_response)); | |
| 45 } | |
| 46 | |
| 42 bool PaymentRequestState::CanMakePayment() const { | 47 bool PaymentRequestState::CanMakePayment() const { |
| 43 for (const std::unique_ptr<PaymentInstrument>& instrument : | 48 for (const std::unique_ptr<PaymentInstrument>& instrument : |
| 44 available_instruments_) { | 49 available_instruments_) { |
| 45 if (instrument.get()->IsValid() && | 50 if (instrument.get()->IsValid() && |
| 46 spec_->supported_card_networks_set().count( | 51 spec_->supported_card_networks_set().count( |
| 47 instrument.get()->method_name())) { | 52 instrument.get()->method_name())) { |
| 48 return true; | 53 return true; |
| 49 } | 54 } |
| 50 } | 55 } |
| 51 return false; | 56 return false; |
| 52 } | 57 } |
| 53 | 58 |
| 54 void PaymentRequestState::AddObserver(Observer* observer) { | 59 void PaymentRequestState::AddObserver(Observer* observer) { |
| 55 CHECK(observer); | 60 CHECK(observer); |
| 56 observers_.AddObserver(observer); | 61 observers_.AddObserver(observer); |
| 57 } | 62 } |
| 58 | 63 |
| 59 void PaymentRequestState::RemoveObserver(Observer* observer) { | 64 void PaymentRequestState::RemoveObserver(Observer* observer) { |
| 60 observers_.RemoveObserver(observer); | 65 observers_.RemoveObserver(observer); |
| 61 } | 66 } |
| 62 | 67 |
| 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() { | 68 void PaymentRequestState::GeneratePaymentResponse() { |
| 113 DCHECK(is_ready_to_pay()); | 69 DCHECK(is_ready_to_pay()); |
| 114 // Fetch the instrument details, will call back into | 70 |
| 115 // PaymentRequest::OnInstrumentDetailsReady. | 71 // Once the response is ready, will call back into OnPaymentResponseReady. |
| 116 selected_instrument_->InvokePaymentApp(this); | 72 response_helper_.reset( |
|
Mathieu
2017/04/10 00:03:22
response_helper_ = base::MakeUnique...
sebsg
2017/04/10 20:48:24
Done.
| |
| 73 new PaymentResponseHelper(app_locale_, spec_, selected_shipping_profile_, | |
|
Mathieu
2017/04/10 00:03:22
should probably pass the selected_instrument_ here
sebsg
2017/04/10 20:48:24
Done.
| |
| 74 selected_contact_profile_, this)); | |
| 75 selected_instrument_->InvokePaymentApp(response_helper_.get()); | |
| 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 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 251 bool PaymentRequestState::ArePaymentOptionsSatisfied() { | 210 bool PaymentRequestState::ArePaymentOptionsSatisfied() { |
| 252 // TODO(mathp): Have a measure of shipping address completeness. | 211 // TODO(mathp): Have a measure of shipping address completeness. |
| 253 if (spec_->request_shipping() && selected_shipping_profile_ == nullptr) | 212 if (spec_->request_shipping() && selected_shipping_profile_ == nullptr) |
| 254 return false; | 213 return false; |
| 255 | 214 |
| 256 profile_util::PaymentsProfileComparator comparator(app_locale_, *spec_); | 215 profile_util::PaymentsProfileComparator comparator(app_locale_, *spec_); |
| 257 return comparator.IsContactInfoComplete(selected_contact_profile_); | 216 return comparator.IsContactInfoComplete(selected_contact_profile_); |
| 258 } | 217 } |
| 259 | 218 |
| 260 } // namespace payments | 219 } // namespace payments |
| OLD | NEW |