| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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_response_helper.h" | 5 #include "components/payments/content/payment_response_helper.h" |
| 6 | 6 |
| 7 #include "base/strings/string_split.h" | 7 #include "base/strings/string_split.h" |
| 8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "components/autofill/core/browser/autofill_profile.h" | 9 #include "components/autofill/core/browser/autofill_profile.h" |
| 10 #include "components/autofill/core/browser/autofill_type.h" | 10 #include "components/autofill/core/browser/autofill_type.h" |
| 11 #include "components/payments/content/payment_request_spec.h" | 11 #include "components/payments/content/payment_request_spec.h" |
| 12 #include "third_party/libphonenumber/phonenumber_api.h" |
| 12 | 13 |
| 13 namespace payments { | 14 namespace payments { |
| 14 | 15 |
| 16 namespace { |
| 17 |
| 18 using ::i18n::phonenumbers::PhoneNumberUtil; |
| 19 |
| 20 } // namespace |
| 21 |
| 15 PaymentResponseHelper::PaymentResponseHelper( | 22 PaymentResponseHelper::PaymentResponseHelper( |
| 16 const std::string& app_locale, | 23 const std::string& app_locale, |
| 17 PaymentRequestSpec* spec, | 24 PaymentRequestSpec* spec, |
| 18 PaymentInstrument* selected_instrument, | 25 PaymentInstrument* selected_instrument, |
| 19 autofill::AutofillProfile* selected_shipping_profile, | 26 autofill::AutofillProfile* selected_shipping_profile, |
| 20 autofill::AutofillProfile* selected_contact_profile, | 27 autofill::AutofillProfile* selected_contact_profile, |
| 21 Delegate* delegate) | 28 Delegate* delegate) |
| 22 : app_locale_(app_locale), | 29 : app_locale_(app_locale), |
| 23 spec_(spec), | 30 spec_(spec), |
| 24 delegate_(delegate), | 31 delegate_(delegate), |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 base::UTF16ToUTF8(selected_contact_profile_->GetInfo( | 112 base::UTF16ToUTF8(selected_contact_profile_->GetInfo( |
| 106 autofill::AutofillType(autofill::NAME_FULL), app_locale_)); | 113 autofill::AutofillType(autofill::NAME_FULL), app_locale_)); |
| 107 } | 114 } |
| 108 if (spec_->request_payer_email()) { | 115 if (spec_->request_payer_email()) { |
| 109 DCHECK(selected_contact_profile_); | 116 DCHECK(selected_contact_profile_); |
| 110 payment_response->payer_email = base::UTF16ToUTF8( | 117 payment_response->payer_email = base::UTF16ToUTF8( |
| 111 selected_contact_profile_->GetRawInfo(autofill::EMAIL_ADDRESS)); | 118 selected_contact_profile_->GetRawInfo(autofill::EMAIL_ADDRESS)); |
| 112 } | 119 } |
| 113 if (spec_->request_payer_phone()) { | 120 if (spec_->request_payer_phone()) { |
| 114 DCHECK(selected_contact_profile_); | 121 DCHECK(selected_contact_profile_); |
| 115 // TODO(crbug.com/705945): Format phone number according to spec. | 122 |
| 116 payment_response->payer_phone = | 123 // Try to format the phone number to the E.164 format to send in the Payment |
| 117 base::UTF16ToUTF8(selected_contact_profile_->GetRawInfo( | 124 // Response, as defined in the Payment Request spec. If it's not possible, |
| 118 autofill::PHONE_HOME_WHOLE_NUMBER)); | 125 // send the original. More info at: |
| 126 // https://w3c.github.io/browser-payment-api/#paymentrequest-updated-algorit
hm |
| 127 // TODO(sebsg): Move this code to a reusable location. |
| 128 const std::string original_number = |
| 129 base::UTF16ToUTF8(selected_contact_profile_->GetInfo( |
| 130 autofill::AutofillType(autofill::PHONE_HOME_WHOLE_NUMBER), |
| 131 app_locale_)); |
| 132 i18n::phonenumbers::PhoneNumber parsed_number; |
| 133 PhoneNumberUtil* phone_number_util = PhoneNumberUtil::GetInstance(); |
| 134 if (phone_number_util->Parse(original_number, "US", &parsed_number) == |
| 135 ::i18n::phonenumbers::PhoneNumberUtil::NO_PARSING_ERROR) { |
| 136 std::string formatted_number; |
| 137 phone_number_util->Format(parsed_number, |
| 138 PhoneNumberUtil::PhoneNumberFormat::E164, |
| 139 &formatted_number); |
| 140 payment_response->payer_phone = formatted_number; |
| 141 } else { |
| 142 payment_response->payer_phone = original_number; |
| 143 } |
| 119 } | 144 } |
| 120 | 145 |
| 121 delegate_->OnPaymentResponseReady(std::move(payment_response)); | 146 delegate_->OnPaymentResponseReady(std::move(payment_response)); |
| 122 } | 147 } |
| 123 | 148 |
| 124 } // namespace payments | 149 } // namespace payments |
| OLD | NEW |