utils.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* Copyright 2015-2016 PayPal, Inc. */
  2. "use strict";
  3. var https = require('https');
  4. var isArray = Array.isArray;
  5. var hasOwn = Object.prototype.hasOwnProperty;
  6. var getDefaultEndpoint = exports.getDefaultEndpoint = function getDefaultEndpoint(mode) {
  7. return (typeof mode === "string" && mode === "live") ? "paypal.com" : "sandbox.paypal.com";
  8. };
  9. var getDefaultApiEndpoint = exports.getDefaultApiEndpoint = function getDefaultApiEndpoint(mode) {
  10. var api = (typeof mode === "string" && mode === "security-test-sandbox") ? "test-api." : "api.";
  11. return api + getDefaultEndpoint(mode);
  12. };
  13. /**
  14. * Recursively copies given object into a new object. Helper method for merge
  15. * @param {Object} v
  16. * @return {Object}
  17. */
  18. function clone(v) {
  19. if (v === null || typeof v !== "object") {
  20. return v;
  21. }
  22. if (isArray(v)) {
  23. var arr = v.slice();
  24. for (var i = 0; i < v.length; i++) {
  25. arr[i] = clone(arr[i]);
  26. }
  27. return arr;
  28. }
  29. else {
  30. var obj = {};
  31. for (var k in v) {
  32. obj[k] = clone(v[k]);
  33. }
  34. return obj;
  35. }
  36. }
  37. /**
  38. * Merges two Objects recursively, setting property of obj1 to those of obj2
  39. * and creating property as necessary.
  40. *
  41. * Implementation suggested by @kobalicek on https://github.com/paypal/PayPal-node-SDK/issues/69
  42. * @param {Object} obj1
  43. * @param {Object} obj2
  44. * @return {Object}
  45. */
  46. var merge = exports.merge = function merge(obj1, obj2, appendOnly) {
  47. //Handle invalid arguments
  48. if (obj1 === null || typeof obj1 !== "object") {
  49. throw new TypeError("merge() - first parameter has to be an object, not " + typeof obj1 + ".");
  50. }
  51. if (obj2 === null || typeof obj2 !== "object") {
  52. throw new TypeError("merge() - first parameter has to be an object, not " + typeof obj2 + ".");
  53. }
  54. if (isArray(obj1) || isArray(obj2)) {
  55. throw new TypeError("merge() - Unsupported for arrays.");
  56. }
  57. for (var k in obj2) {
  58. var obj1Val, obj2Val = obj2[k];
  59. if (hasOwn.call(obj1, k)) {
  60. if (!appendOnly) {
  61. obj1Val = obj1[k];
  62. if (obj1Val !== null && typeof obj1Val === "object" &&
  63. obj2Val !== null && typeof obj2Val === "object") {
  64. merge(obj1Val, obj2Val);
  65. }
  66. else {
  67. obj1[k] = clone(obj2Val);
  68. }
  69. }
  70. }
  71. else {
  72. obj1[k] = clone(obj2Val);
  73. }
  74. }
  75. return obj1;
  76. };
  77. /**
  78. * Checks if access token for client id has expired
  79. * @param {Object} token_hash object returned from paypal access token request
  80. * with expires_in set and sdk sets the created_at
  81. * @return {Boolean} true if token expired else false
  82. */
  83. var checkExpiredToken = exports.checkExpiredToken = function checkExpiredToken(token_hash) {
  84. var delta = (new Date().getTime() / 1000) - token_hash.created_at;
  85. return (delta < token_hash.expires_in) ? false : true;
  86. };