PageRenderTime 24ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/samples/ConvertCurrencyReceipt.php

https://gitlab.com/CORP-RESELLER/adaptivepayments-sdk-php
PHP | 185 lines | 67 code | 9 blank | 109 comment | 15 complexity | e7a90fa8ba26b6f975fde18f097de63e MD5 | raw file
  1. <?php
  2. use PayPal\Service\AdaptivePaymentsService;
  3. use PayPal\Types\AP\ConvertCurrencyRequest;
  4. use PayPal\Types\AP\CurrencyCodeList;
  5. use PayPal\Types\AP\CurrencyList;
  6. use PayPal\Types\Common\CurrencyType;
  7. use PayPal\Types\Common\RequestEnvelope;
  8. require_once('PPBootStrap.php');
  9. /*
  10. * # ConvertCurrency API
  11. Use the ConvertCurrency API operation to request the current foreign exchange (FX) rate for a specific amount and currency.
  12. This sample code uses AdaptivePayments PHP SDK to make API call
  13. */
  14. /*
  15. * ##ConvertCurrencyRequest
  16. The ConvertCurrencyRequest message enables you to have your
  17. application get an estimated exchange rate for a list of amounts.
  18. This API operation does not affect PayPal balances.
  19. */
  20. /*
  21. * `CurrencyList` which takes two arguments:
  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. */
  58. $baseAmountList = new CurrencyList();
  59. foreach($_POST['currencyCode'] as $idx => $currencyCode) {
  60. if($_POST['currencyCode'][$idx] != "" && $_POST['currencyAmount'][$idx] != "") {
  61. $baseAmountList->currency[] = new CurrencyType($_POST['currencyCode'][$idx], $_POST['currencyAmount'][$idx]);
  62. }
  63. }
  64. /*
  65. * `CurrencyCodeList` which contains
  66. * `Currency Code` - Allowable values are:
  67. * Australian Dollar - AUD
  68. * Brazilian Real - BRL
  69. `Note:
  70. The Real is supported as a payment currency and currency balance only
  71. for Brazilian PayPal accounts.`
  72. * Canadian Dollar - CAD
  73. * Czech Koruna - CZK
  74. * Danish Krone - DKK
  75. * Euro - EUR
  76. * Hong Kong Dollar - HKD
  77. * Hungarian Forint - HUF
  78. * Israeli New Sheqel - ILS
  79. * Japanese Yen - JPY
  80. * Malaysian Ringgit - MYR
  81. `Note:
  82. The Ringgit is supported as a payment currency and currency balance
  83. only for Malaysian PayPal accounts.`
  84. * Mexican Peso - MXN
  85. * Norwegian Krone - NOK
  86. * New Zealand Dollar - NZD
  87. * Philippine Peso - PHP
  88. * Polish Zloty - PLN
  89. * Pound Sterling - GBP
  90. * Singapore Dollar - SGD
  91. * Swedish Krona - SEK
  92. * Swiss Franc - CHF
  93. * Taiwan New Dollar - TWD
  94. * Thai Baht - THB
  95. * Turkish Lira - TRY
  96. `Note:
  97. The Turkish Lira is supported as a payment currency and currency
  98. balance only for Turkish PayPal accounts.`
  99. * U.S. Dollar - USD
  100. */
  101. $convertToCurrencyList = new CurrencyCodeList();
  102. foreach($_POST['toCurrencyCode'] as $idx => $currencyCode) {
  103. if($currencyCode != "") {
  104. $convertToCurrencyList->currencyCode[] = $currencyCode;
  105. }
  106. }
  107. /*
  108. *
  109. The code for the language in which errors are returned, which must be
  110. en_US.
  111. */
  112. /*
  113. * `ConvertCurrencyRequest` which takes params:
  114. * `Request Envelope` - Information common to each API operation, such
  115. as the language in which an error message is returned
  116. * `BaseAmountList` - A list of amounts with associated currencies to
  117. be converted.
  118. * `ConvertToCurrencyList` - A list of currencies to convert to.
  119. */
  120. $convertCurrencyReq = new ConvertCurrencyRequest(new RequestEnvelope("en_US"), $baseAmountList, $convertToCurrencyList);
  121. if($_POST['countryCode'] != "") {
  122. $convertCurrencyReq->countryCode = $_POST['countryCode'];
  123. }
  124. if($_POST['conversionType'] != "" && $_POST['conversionType'] != "- Select -") {
  125. $convertCurrencyReq->conversionType = $_POST['conversionType'];
  126. }
  127. /*
  128. * ## Creating service wrapper object
  129. Creating service wrapper object to make API call and loading
  130. Configuration::getAcctAndConfig() returns array that contains credential and config parameters
  131. */
  132. $service = new AdaptivePaymentsService(Configuration::getAcctAndConfig());
  133. try {
  134. /* wrap API method calls on the service object with a try catch */
  135. $response = $service->ConvertCurrency($convertCurrencyReq);
  136. } catch(Exception $ex) {
  137. require_once 'Common/Error.php';
  138. exit;
  139. }
  140. ?>
  141. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  142. <html>
  143. <head>
  144. <title>PayPal Adaptive Payments - Convert Currency</title>
  145. <link href="Common/sdk.css" rel="stylesheet" type="text/css" />
  146. <script type="text/javascript" src="Common/sdk_functions.js"></script>
  147. </head>
  148. <body>
  149. <div id="wrapper">
  150. <img src="https://devtools-paypal.com/image/bdg_payments_by_pp_2line.png"/>
  151. <div id="response_form">
  152. <h3>Convert Currency</h3>
  153. <?php
  154. $ack = strtoupper($response->responseEnvelope->ack);
  155. if($ack != "SUCCESS"){
  156. echo "<b>Error </b>";
  157. echo "<pre>";
  158. print_r($response);
  159. echo "</pre>";
  160. } else {
  161. echo "<table>";
  162. echo "<tr><td>Ack :</td><td><div id='Ack'>$ack</div> </td></tr>";
  163. echo "</table>";
  164. echo "<pre>";
  165. print_r($response);
  166. echo "</pre>";
  167. }
  168. require_once 'Common/Response.php';
  169. ?></div>
  170. </div>
  171. </body>
  172. </html>