OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "modules/payments/PaymentAppRequestConversion.h" | |
6 | |
7 #include "bindings/core/v8/ToV8ForCore.h" | |
8 #include "modules/payments/PaymentAppRequest.h" | |
9 #include "modules/payments/PaymentCurrencyAmount.h" | |
10 #include "modules/payments/PaymentDetailsModifier.h" | |
11 #include "modules/payments/PaymentItem.h" | |
12 #include "modules/payments/PaymentMethodData.h" | |
13 #include "platform/bindings/ScriptState.h" | |
14 #include "public/platform/modules/payments/WebPaymentAppRequest.h" | |
15 #include "public/platform/modules/payments/WebPaymentMethodData.h" | |
16 | |
17 namespace blink { | |
18 namespace { | |
19 | |
20 PaymentCurrencyAmount ToPaymentCurrencyAmount( | |
21 const WebPaymentCurrencyAmount& web_amount) { | |
22 PaymentCurrencyAmount amount; | |
23 amount.setCurrency(web_amount.currency); | |
24 amount.setValue(web_amount.value); | |
25 amount.setCurrencySystem(web_amount.currency_system); | |
26 return amount; | |
27 } | |
28 | |
29 PaymentItem ToPaymentItem(const WebPaymentItem& web_item) { | |
30 PaymentItem item; | |
31 item.setLabel(web_item.label); | |
32 item.setAmount(ToPaymentCurrencyAmount(web_item.amount)); | |
33 item.setPending(web_item.pending); | |
34 return item; | |
35 } | |
36 | |
37 PaymentDetailsModifier ToPaymentDetailsModifier( | |
38 ScriptState* script_state, | |
39 const WebPaymentDetailsModifier& web_modifier) { | |
40 PaymentDetailsModifier modifier; | |
41 Vector<String> supported_methods; | |
42 for (const auto& web_method : web_modifier.supported_methods) { | |
43 supported_methods.push_back(web_method); | |
44 } | |
45 modifier.setSupportedMethods(supported_methods); | |
46 modifier.setTotal(ToPaymentItem(web_modifier.total)); | |
47 HeapVector<PaymentItem> additional_display_items; | |
48 for (const auto& web_item : web_modifier.additional_display_items) { | |
49 additional_display_items.push_back(ToPaymentItem(web_item)); | |
50 } | |
51 modifier.setAdditionalDisplayItems(additional_display_items); | |
52 return modifier; | |
53 } | |
54 | |
55 ScriptValue StringDataToScriptValue(ScriptState* script_state, | |
56 const WebString& stringified_data) { | |
57 if (!script_state->ContextIsValid()) | |
58 return ScriptValue(); | |
59 | |
60 ScriptState::Scope scope(script_state); | |
61 v8::Local<v8::Value> v8_value; | |
62 if (!v8::JSON::Parse(script_state->GetIsolate(), | |
63 V8String(script_state->GetIsolate(), stringified_data)) | |
64 .ToLocal(&v8_value)) { | |
65 return ScriptValue(); | |
66 } | |
67 return ScriptValue(script_state, v8_value); | |
68 } | |
69 | |
70 PaymentMethodData ToPaymentMethodData( | |
71 ScriptState* script_state, | |
72 const WebPaymentMethodData& web_method_data) { | |
73 PaymentMethodData method_data; | |
74 Vector<String> supported_methods; | |
75 for (const auto& method : web_method_data.supported_methods) { | |
76 supported_methods.push_back(method); | |
77 } | |
78 method_data.setSupportedMethods(supported_methods); | |
79 method_data.setData( | |
80 StringDataToScriptValue(script_state, web_method_data.stringified_data)); | |
81 return method_data; | |
82 } | |
83 | |
84 } // namespace | |
85 | |
86 PaymentAppRequest PaymentAppRequestConversion::ToPaymentAppRequest( | |
87 ScriptState* script_state, | |
88 const WebPaymentAppRequest& web_app_request) { | |
89 DCHECK(script_state); | |
90 | |
91 PaymentAppRequest app_request; | |
92 if (!script_state->ContextIsValid()) | |
93 return app_request; | |
94 | |
95 ScriptState::Scope scope(script_state); | |
96 | |
97 app_request.setTopLevelOrigin(web_app_request.top_level_origin); | |
98 app_request.setPaymentRequestOrigin(web_app_request.payment_request_origin); | |
99 app_request.setPaymentRequestId(web_app_request.payment_request_id); | |
100 HeapVector<PaymentMethodData> method_data; | |
101 for (const auto& md : web_app_request.method_data) { | |
102 method_data.push_back(ToPaymentMethodData(script_state, md)); | |
103 } | |
104 app_request.setMethodData(method_data); | |
105 app_request.setTotal(ToPaymentItem(web_app_request.total)); | |
106 HeapVector<PaymentDetailsModifier> modifiers; | |
107 for (const auto& modifier : web_app_request.modifiers) { | |
108 modifiers.push_back(ToPaymentDetailsModifier(script_state, modifier)); | |
109 } | |
110 app_request.setModifiers(modifiers); | |
111 app_request.setInstrumentKey(web_app_request.instrument_key); | |
112 return app_request; | |
113 } | |
114 | |
115 } // namespace blink | |
OLD | NEW |