PageRenderTime 76ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/AdaptivePayments/sample/code/ConvertCurrency.php

https://gitlab.com/CORP-RESELLER/codesamples-php
PHP | 156 lines | 39 code | 15 blank | 102 comment | 5 complexity | d09d0b89d427ac6fef4650ab3cc845eb MD5 | raw file
  1. <?php
  2. $path = '../../lib';
  3. set_include_path(get_include_path() . PATH_SEPARATOR . $path);
  4. require_once('PPBootStrap.php');
  5. // # ConvertCurrency API
  6. // Use the ConvertCurrency API operation to request the current foreign exchange (FX) rate for a specific amount and currency.
  7. // This sample code uses AdaptivePayments PHP SDK to make API call. You can
  8. // download the SDKs [here](https://github.com/paypal/sdk-packages/tree/gh-pages/adaptivepayments-sdk/php)
  9. class ConvertCurrency
  10. {
  11. public function convert(){
  12. $logger = new PPLoggingManager('ConvertCurrency');
  13. // ##ConvertCurrencyRequest
  14. // The ConvertCurrencyRequest message enables you to have your
  15. // application get an estimated exchange rate for a list of amounts.
  16. // This API operation does not affect PayPal balances.
  17. // The code for the language in which errors are returned, which must be
  18. // en_US.
  19. $requestEnvelope = new RequestEnvelope("en_US");
  20. // `CurrencyList` which takes two arguments:
  21. //
  22. // * `CurrencyCodeType` - The currency code. Allowable values are:
  23. // * Australian Dollar - AUD
  24. // * Brazilian Real - BRL
  25. // `Note:
  26. // The Real is supported as a payment currency and currency balance only
  27. // for Brazilian PayPal accounts.`
  28. // * Canadian Dollar - CAD
  29. // * Czech Koruna - CZK
  30. // * Danish Krone - DKK
  31. // * Euro - EUR
  32. // * Hong Kong Dollar - HKD
  33. // * Hungarian Forint - HUF
  34. // * Israeli New Sheqel - ILS
  35. // * Japanese Yen - JPY
  36. // * Malaysian Ringgit - MYR
  37. // `Note:
  38. // The Ringgit is supported as a payment currency and currency balance
  39. // only for Malaysian PayPal accounts.`
  40. // * Mexican Peso - MXN
  41. // * Norwegian Krone - NOK
  42. // * New Zealand Dollar - NZD
  43. // * Philippine Peso - PHP
  44. // * Polish Zloty - PLN
  45. // * Pound Sterling - GBP
  46. // * Singapore Dollar - SGD
  47. // * Swedish Krona - SEK
  48. // * Swiss Franc - CHF
  49. // * Taiwan New Dollar - TWD
  50. // * Thai Baht - THB
  51. // * Turkish Lira - TRY
  52. // `Note:
  53. // The Turkish Lira is supported as a payment currency and currency
  54. // balance only for Turkish PayPal accounts.`
  55. // * U.S. Dollar - USD
  56. // * `amount`
  57. $baseAmountList = new CurrencyList();
  58. $baseAmountList->currency[] = new CurrencyType("USD","4.00");
  59. // `CurrencyCodeList` which contains
  60. //
  61. // * `Currency Code` - Allowable values are:
  62. // * Australian Dollar - AUD
  63. // * Brazilian Real - BRL
  64. // `Note:
  65. // The Real is supported as a payment currency and currency balance only
  66. // for Brazilian PayPal accounts.`
  67. // * Canadian Dollar - CAD
  68. // * Czech Koruna - CZK
  69. // * Danish Krone - DKK
  70. // * Euro - EUR
  71. // * Hong Kong Dollar - HKD
  72. // * Hungarian Forint - HUF
  73. // * Israeli New Sheqel - ILS
  74. // * Japanese Yen - JPY
  75. // * Malaysian Ringgit - MYR
  76. // `Note:
  77. // The Ringgit is supported as a payment currency and currency balance
  78. // only for Malaysian PayPal accounts.`
  79. // * Mexican Peso - MXN
  80. // * Norwegian Krone - NOK
  81. // * New Zealand Dollar - NZD
  82. // * Philippine Peso - PHP
  83. // * Polish Zloty - PLN
  84. // * Pound Sterling - GBP
  85. // * Singapore Dollar - SGD
  86. // * Swedish Krona - SEK
  87. // * Swiss Franc - CHF
  88. // * Taiwan New Dollar - TWD
  89. // * Thai Baht - THB
  90. // * Turkish Lira - TRY
  91. // `Note:
  92. // The Turkish Lira is supported as a payment currency and currency
  93. // balance only for Turkish PayPal accounts.`
  94. // * U.S. Dollar - USD
  95. $convertToCurrencyList = new CurrencyCodeList();
  96. $convertToCurrencyList->currencyCode[] = "GBP";
  97. // `ConvertCurrencyRequest` which takes params:
  98. //
  99. // * `Request Envelope` - Information common to each API operation, such
  100. // as the language in which an error message is returned
  101. // * `BaseAmountList` - A list of amounts with associated currencies to
  102. // be converted.
  103. // * `ConvertToCurrencyList` - A list of currencies to convert to.
  104. $convertCurrencyReq = new ConvertCurrencyRequest($requestEnvelope, $baseAmountList, $convertToCurrencyList);
  105. // ## Creating service wrapper object
  106. // Creating service wrapper object to make API call and loading
  107. // configuration file for your credentials and endpoint
  108. $service = new AdaptivePaymentsService();
  109. try {
  110. // ## Making API call
  111. // Invoke the appropriate method corresponding to API in service
  112. // wrapper object
  113. $response = $service->ConvertCurrency($convertCurrencyReq);
  114. } catch(Exception $ex) {
  115. $logger->error("Error Message : ". $ex->getMessage());
  116. }
  117. // ## Accessing response parameters
  118. // You can access the response parameters using getter methods in
  119. // response object as shown below
  120. // ### Success values
  121. if ($response->responseEnvelope->ack == "Success")
  122. {
  123. if($response->estimatedAmountTable->currencyConversionList != null && sizeof($response->estimatedAmountTable->currencyConversionList)>0){
  124. $currencyConversionList = $response->estimatedAmountTable->currencyConversionList;
  125. foreach($currencyConversionList as $fromCurrency){
  126. $logger->log("Amount to be Converted : ". $fromCurrency->baseAmount->amount . $fromCurrency->baseAmount->code);
  127. $toCurrency = $fromCurrency->currencyList->currency;
  128. foreach($toCurrency as $convertedTo){
  129. $logger->log("Converted amount : ". $convertedTo->amount . $convertedTo->code);
  130. }
  131. }
  132. }
  133. }
  134. // ### Error Values
  135. // Access error values from error list using getter methods
  136. else{
  137. $logger->error("API Error Message : ".$response->error[0]->message);
  138. }
  139. return $response;
  140. }
  141. }