コード例 #1
0
    // # ConvertCurrency API Operation
    // The ConvertCurrency API operation to request the current foreign exchange (FX) rate for a specific amount and currency
    public ConvertCurrencyResponse ConvertCurrencyAPIOperation()
    {
        // Create the ConvertCurrencyResponse object
        ConvertCurrencyResponse responseConvertCurrency = new ConvertCurrencyResponse();

        try
        {
            // # ConvertCurrencyRequest
            // The ConvertCurrencyRequest message enables you to have your
            // application get an estimated exchange rate for a list of amounts
            // This API operation does not affect PayPal balances
            // The code for the language in which errors are returned
            RequestEnvelope envelopeRequest = new RequestEnvelope();
            envelopeRequest.errorLanguage = "en_US";

            // CurrencyTypeList which takes two arguments:
            //
            // * `CurrencyCodeType` - The currency code. Allowable values are:
            // * Australian Dollar - AUD
            // * Brazilian Real - BRL
            // `Note:
            // The Real is supported as a payment currency and currency balance only for Brazilian PayPal accounts.`
            //
            // * Canadian Dollar - CAD
            // * Czech Koruna - CZK
            // * Danish Krone - DKK
            // * Euro - EUR
            // * Hong Kong Dollar - HKD
            // * Hungarian Forint - HUF
            // * Israeli New Sheqel - ILS
            // * Japanese Yen - JPY
            // * Malaysian Ringgit - MYR
            // `Note:
            // The Ringgit is supported as a payment currency and currency balance only for Malaysian PayPal accounts.`
            //
            // * Mexican Peso - MXN
            // * Norwegian Krone - NOK
            // * New Zealand Dollar - NZD
            // * Philippine Peso - PHP
            // * Polish Zloty - PLN
            // * Pound Sterling - GBP
            // * Singapore Dollar - SGD
            // * Swedish Krona - SEK
            // * Swiss Franc - CHF
            // * Taiwan New Dollar - TWD
            // * Thai Baht - THB
            // * Turkish Lira - TRY
            // `Note:
            // The Turkish Lira is supported as a payment currency and currency balance only for Turkish PayPal accounts.`
            //
            // * U.S. Dollar - USD
            // * `amount`
            List <CurrencyType> currencyTypeList = new List <CurrencyType>();
            CurrencyType        currency         = new CurrencyType("USD", Convert.ToDecimal("4.00"));
            currencyTypeList.Add(currency);
            CurrencyList baseAmountList = new CurrencyList(currencyTypeList);

            // CurrencyCodeList which contains
            //
            // * `Currency Code` - Allowable values are:
            // * Australian Dollar - AUD
            // * Brazilian Real - BRL
            // `Note:
            // The Real is supported as a payment currency and currency balance only for Brazilian PayPal accounts.`
            // * Canadian Dollar - CAD
            // * Czech Koruna - CZK
            // * Danish Krone - DKK
            // * Euro - EUR
            // * Hong Kong Dollar - HKD
            // * Hungarian Forint - HUF
            // * Israeli New Sheqel - ILS
            // * Japanese Yen - JPY
            // * Malaysian Ringgit - MYR
            // `Note:
            // The Ringgit is supported as a payment currency and currency balance  only for Malaysian PayPal accounts.`
            // * Mexican Peso - MXN
            // * Norwegian Krone - NOK
            // * New Zealand Dollar - NZD
            // * Philippine Peso - PHP
            // * Polish Zloty - PLN
            // * Pound Sterling - GBP
            // * Singapore Dollar - SGD
            // * Swedish Krona - SEK
            // * Swiss Franc - CHF
            // * Taiwan New Dollar - TWD
            // * Thai Baht - THB
            // * Turkish Lira - TRY
            // `Note:
            // The Turkish Lira is supported as a payment currency and currency balance only for Turkish PayPal accounts.`
            //
            // * U.S. Dollar - USD
            List <String> currencyCodeList = new List <String>();
            currencyCodeList.Add("GBP");
            CurrencyCodeList convertToCurrencyList = new CurrencyCodeList(currencyCodeList);

            // ConvertCurrencyRequest which takes params:
            //
            // * `Request Envelope` - Information common to each API operation, such
            // as the language in which an error message is returned
            // * `BaseAmountList` - A list of amounts with associated currencies to
            // be converted.
            // * `ConvertToCurrencyList` - A list of currencies to convert to.
            ConvertCurrencyRequest requestConvertCurrency = new ConvertCurrencyRequest(envelopeRequest, baseAmountList, convertToCurrencyList);

            // # Create the service wrapper object to make the API call
            AdaptivePaymentsService service = new AdaptivePaymentsService();

            // # API call
            // Invoke the ConvertCurrency method in service wrapper object
            responseConvertCurrency = service.ConvertCurrency(requestConvertCurrency);

            if (responseConvertCurrency != null)
            {
                // Response envelope acknowledgement
                string acknowledgement = "ConvertCurrency API operation - ";
                acknowledgement += responseConvertCurrency.responseEnvelope.ack.ToString();
                logger.Info(acknowledgement + "\n");
                Console.WriteLine(acknowledgement + "\n");

                // # Success values
                if (responseConvertCurrency.responseEnvelope.ack.ToString().Trim().ToUpper().Equals("SUCCESS"))
                {
                    if (responseConvertCurrency.estimatedAmountTable.currencyConversionList != null &&
                        responseConvertCurrency.estimatedAmountTable.currencyConversionList.Count > 0)
                    {
                        IEnumerator <CurrencyConversionList> iterator = responseConvertCurrency.estimatedAmountTable.currencyConversionList.GetEnumerator();

                        while (iterator.MoveNext())
                        {
                            CurrencyConversionList currencyConversion = iterator.Current;
                            logger.Info("Amount to be Converted : " + currencyConversion.baseAmount.amount + currencyConversion.baseAmount.code + "\n");
                            Console.WriteLine("Amount to be Converted : " + currencyConversion.baseAmount.amount + currencyConversion.baseAmount.code + "\n");

                            IEnumerator <CurrencyType> currencyIterator = currencyConversion.currencyList.currency.GetEnumerator();

                            while (currencyIterator.MoveNext())
                            {
                                CurrencyType currencyType = currencyIterator.Current;
                                logger.Info("Converted amount : " + currencyType.amount + currencyType.code + "\n");
                                Console.WriteLine("Converted amount : " + currencyType.amount + currencyType.code + "\n");
                            }
                        }
                    }
                }
                // # Error Values
                else
                {
                    List <ErrorData> errorMessages = responseConvertCurrency.error;
                    foreach (ErrorData error in errorMessages)
                    {
                        logger.Debug("API Error Message : " + error.message);
                        Console.WriteLine("API Error Message : " + error.message + "\n");
                    }
                }
            }
        }
        // # Exception log
        catch (System.Exception ex)
        {
            // Log the exception message
            logger.Debug("Error Message : " + ex.Message);
            Console.WriteLine("Error Message : " + ex.Message);
        }

        return(responseConvertCurrency);
    }
コード例 #2
0
ファイル: Setup.cs プロジェクト: Cruciatum/Haphrain
        internal static async Task <Dictionary <string, Currency> > GetCurrencies(string APIKey)
        {
            Dictionary <string, Currency> updatedList = new Dictionary <string, Currency>();

            string             WEBSERVICE_URL = string.Format("http://api.currencylayer.com/list?access_key={0}", APIKey);
            string             jsonResponse   = "";
            CurrencyDefinition cd             = new CurrencyDefinition();

            try
            {
                var webRequest = WebRequest.Create(WEBSERVICE_URL);
                if (webRequest != null)
                {
                    webRequest.Method      = "GET";
                    webRequest.Timeout     = 12000;
                    webRequest.ContentType = "application/json";


                    using (Stream s = webRequest.GetResponse().GetResponseStream())
                    {
                        using (StreamReader sr = new StreamReader(s))
                        {
                            jsonResponse = sr.ReadToEnd();
                            cd           = JsonConvert.DeserializeObject <CurrencyDefinition>(jsonResponse);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                await LogWriter.WriteLogFile($"ERROR: Exception thrown : {ex.Message}");

                await LogWriter.WriteLogFile($"{ex.StackTrace}");

                Console.WriteLine($"Exception: {ex.Message}");
            }

            WEBSERVICE_URL = string.Format("http://api.currencylayer.com/live?access_key={0}&source={1}", APIKey, "USD");
            CurrencyConversionList ccl = new CurrencyConversionList();

            try
            {
                var webRequest = WebRequest.Create(WEBSERVICE_URL);
                if (webRequest != null)
                {
                    webRequest.Method      = "GET";
                    webRequest.Timeout     = 12000;
                    webRequest.ContentType = "application/json";


                    using (Stream s = webRequest.GetResponse().GetResponseStream())
                    {
                        using (StreamReader sr = new StreamReader(s))
                        {
                            jsonResponse = sr.ReadToEnd();
                            ccl          = JsonConvert.DeserializeObject <CurrencyConversionList>(jsonResponse);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                await LogWriter.WriteLogFile($"ERROR: Exception thrown : {ex.Message}");

                await LogWriter.WriteLogFile($"{ex.StackTrace}");

                Console.WriteLine($"Exception: {ex.Message}");
            }

            foreach (string s in cd.Currencies.Keys)
            {
                updatedList.Add(s, new Currency()
                {
                    FullName = cd.Currencies[s], ValueInUSD = 0d
                });
            }

            foreach (string s in ccl.Quotes.Keys)
            {
                updatedList[s.Substring(3, 3)].ValueInUSD = ccl.Quotes[s];
            }

            return(updatedList);
        }