summaryrefslogtreecommitdiff
path: root/chromium/components/payments/core/payment_currency_amount.cc
blob: 213b34681d4900ca004f74f41ca6185802eaa6f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/payments/core/payment_currency_amount.h"

#include "base/values.h"

namespace payments {

namespace {

// These are defined as part of the spec at:
// https://w3c.github.io/browser-payment-api/#dom-paymentcurrencyamount
static const char kPaymentCurrencyAmountCurrencySystem[] = "currencySystem";
static const char kPaymentCurrencyAmountCurrency[] = "currency";
static const char kPaymentCurrencyAmountValue[] = "value";

}  // namespace

bool PaymentCurrencyAmountFromDictionaryValue(
    const base::DictionaryValue& dictionary_value,
    mojom::PaymentCurrencyAmount* amount) {
  if (!dictionary_value.GetString(kPaymentCurrencyAmountCurrency,
                                  &amount->currency)) {
    return false;
  }

  if (!dictionary_value.GetString(kPaymentCurrencyAmountValue,
                                  &amount->value)) {
    return false;
  }

  // Currency_system is optional
  dictionary_value.GetString(kPaymentCurrencyAmountCurrencySystem,
                             &amount->currency_system);

  return true;
}

std::unique_ptr<base::DictionaryValue> PaymentCurrencyAmountToDictionaryValue(
    const mojom::PaymentCurrencyAmount& amount) {
  auto result = std::make_unique<base::DictionaryValue>();
  result->SetString(kPaymentCurrencyAmountCurrency, amount.currency);
  result->SetString(kPaymentCurrencyAmountValue, amount.value);
  if (!amount.currency_system.empty()) {
    result->SetString(kPaymentCurrencyAmountCurrencySystem,
                      amount.currency_system);
  }

  return result;
}

}  // namespace payments