| 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 | 12 |
| 12 namespace payments { | 13 namespace payments { |
| 13 | 14 |
| 14 PaymentResponseHelper::PaymentResponseHelper(){}; | 15 PaymentResponseHelper::PaymentResponseHelper( |
| 16 const std::string& app_locale, |
| 17 PaymentRequestSpec* spec, |
| 18 PaymentInstrument* selected_instrument, |
| 19 autofill::AutofillProfile* selected_shipping_profile, |
| 20 autofill::AutofillProfile* selected_contact_profile, |
| 21 Delegate* delegate) |
| 22 : app_locale_(app_locale), |
| 23 spec_(spec), |
| 24 delegate_(delegate), |
| 25 selected_instrument_(selected_instrument), |
| 26 selected_shipping_profile_(selected_shipping_profile), |
| 27 selected_contact_profile_(selected_contact_profile) { |
| 28 DCHECK(spec_); |
| 29 DCHECK(selected_instrument_); |
| 30 DCHECK(delegate_); |
| 31 |
| 32 // Start to get the instrument details. Will call back into |
| 33 // OnInstrumentDetailsReady. |
| 34 selected_instrument_->InvokePaymentApp(this); |
| 35 }; |
| 36 |
| 15 PaymentResponseHelper::~PaymentResponseHelper(){}; | 37 PaymentResponseHelper::~PaymentResponseHelper(){}; |
| 16 | 38 |
| 17 // static | 39 // static |
| 18 mojom::PaymentAddressPtr | 40 mojom::PaymentAddressPtr |
| 19 PaymentResponseHelper::GetMojomPaymentAddressFromAutofillProfile( | 41 PaymentResponseHelper::GetMojomPaymentAddressFromAutofillProfile( |
| 20 const autofill::AutofillProfile* const profile, | 42 const autofill::AutofillProfile* const profile, |
| 21 const std::string& app_locale) { | 43 const std::string& app_locale) { |
| 22 mojom::PaymentAddressPtr payment_address = mojom::PaymentAddress::New(); | 44 mojom::PaymentAddressPtr payment_address = mojom::PaymentAddress::New(); |
| 23 | 45 |
| 24 payment_address->country = | 46 payment_address->country = |
| (...skipping 19 matching lines...) Expand all Loading... |
| 44 payment_address->recipient = base::UTF16ToUTF8(profile->GetInfo( | 66 payment_address->recipient = base::UTF16ToUTF8(profile->GetInfo( |
| 45 autofill::AutofillType(autofill::NAME_FULL), app_locale)); | 67 autofill::AutofillType(autofill::NAME_FULL), app_locale)); |
| 46 | 68 |
| 47 // TODO(crbug.com/705945): Format phone number according to spec. | 69 // TODO(crbug.com/705945): Format phone number according to spec. |
| 48 payment_address->phone = | 70 payment_address->phone = |
| 49 base::UTF16ToUTF8(profile->GetRawInfo(autofill::PHONE_HOME_WHOLE_NUMBER)); | 71 base::UTF16ToUTF8(profile->GetRawInfo(autofill::PHONE_HOME_WHOLE_NUMBER)); |
| 50 | 72 |
| 51 return payment_address; | 73 return payment_address; |
| 52 } | 74 } |
| 53 | 75 |
| 76 void PaymentResponseHelper::OnInstrumentDetailsReady( |
| 77 const std::string& method_name, |
| 78 const std::string& stringified_details) { |
| 79 mojom::PaymentResponsePtr payment_response = mojom::PaymentResponse::New(); |
| 80 |
| 81 // Make sure that we return the method name that the merchant specified for |
| 82 // this instrument: cards can be either specified through their name (e.g., |
| 83 // "visa") or through basic-card's supportedNetworks. |
| 84 payment_response->method_name = |
| 85 spec_->IsMethodSupportedThroughBasicCard(method_name) |
| 86 ? kBasicCardMethodName |
| 87 : method_name; |
| 88 payment_response->stringified_details = stringified_details; |
| 89 |
| 90 // Shipping Address section |
| 91 if (spec_->request_shipping()) { |
| 92 DCHECK(selected_shipping_profile_); |
| 93 payment_response->shipping_address = |
| 94 GetMojomPaymentAddressFromAutofillProfile(selected_shipping_profile_, |
| 95 app_locale_); |
| 96 |
| 97 DCHECK(spec_->selected_shipping_option()); |
| 98 payment_response->shipping_option = spec_->selected_shipping_option()->id; |
| 99 } |
| 100 |
| 101 // Contact Details section. |
| 102 if (spec_->request_payer_name()) { |
| 103 DCHECK(selected_contact_profile_); |
| 104 payment_response->payer_name = |
| 105 base::UTF16ToUTF8(selected_contact_profile_->GetInfo( |
| 106 autofill::AutofillType(autofill::NAME_FULL), app_locale_)); |
| 107 } |
| 108 if (spec_->request_payer_email()) { |
| 109 DCHECK(selected_contact_profile_); |
| 110 payment_response->payer_email = base::UTF16ToUTF8( |
| 111 selected_contact_profile_->GetRawInfo(autofill::EMAIL_ADDRESS)); |
| 112 } |
| 113 if (spec_->request_payer_phone()) { |
| 114 DCHECK(selected_contact_profile_); |
| 115 // TODO(crbug.com/705945): Format phone number according to spec. |
| 116 payment_response->payer_phone = |
| 117 base::UTF16ToUTF8(selected_contact_profile_->GetRawInfo( |
| 118 autofill::PHONE_HOME_WHOLE_NUMBER)); |
| 119 } |
| 120 |
| 121 delegate_->OnPaymentResponseReady(std::move(payment_response)); |
| 122 } |
| 123 |
| 54 } // namespace payments | 124 } // namespace payments |
| OLD | NEW |