/sites/all/modules/uc_spnsagepaynow/sagepaynow_common.inc

https://github.com/SagePay/PayNow-UberCart-Drupal7 · PHP · 148 lines · 83 code · 18 blank · 47 comment · 8 complexity · c7ff4238d3d1008588496839954c02a5 MD5 · raw file

  1. <?php
  2. /**
  3. * sagepaynow_common.inc
  4. */
  5. //enable or disable debugging
  6. define('PN_DEBUG', true);
  7. //// Create user agent string
  8. // User agent constituents (for cURL)
  9. define( 'PN_SOFTWARE_NAME', 'Ubercart' );
  10. define( 'PN_SOFTWARE_VER', "7.x-1.2" );
  11. define( 'PN_MODULE_NAME', 'SagePayNow-Ubercart' );
  12. define( 'PN_MODULE_VER', '1.0.0' );
  13. //define( 'PN_NEVER_USE_CURL', 1 );
  14. // Features
  15. // - PHP
  16. $pnFeatures = 'PHP '. phpversion() .';';
  17. // - cURL
  18. if( in_array( 'curl', get_loaded_extensions() ) )
  19. {
  20. define( 'PN_CURL', '' );
  21. $pnVersion = curl_version();
  22. $pnFeatures .= ' curl '. $pnVersion['version'] .';';
  23. }
  24. else
  25. $pnFeatures .= ' nocurl;';
  26. // Create user agrent
  27. define( 'PN_USER_AGENT', PN_SOFTWARE_NAME .'/'. PN_SOFTWARE_VER .' ('. trim( $pnFeatures ) .') '. PN_MODULE_NAME .'/'. PN_MODULE_VER );
  28. // General Defines
  29. define( 'PN_TIMEOUT', 15 );
  30. define( 'PN_EPSILON', 0.01 );
  31. // Messages
  32. // Error
  33. define( 'PN_ERR_AMOUNT_MISMATCH', 'Amount mismatch' );
  34. define( 'PN_ERR_BAD_ACCESS', 'Bad access of page' );
  35. define( 'PN_ERR_BAD_SOURCE_IP', 'Bad source IP address' );
  36. define( 'PN_ERR_CONNECT_FAILED', 'Failed to connect to Sage Pay Now' );
  37. define( 'PN_ERR_INVALID_SIGNATURE', 'Security signature mismatch' );
  38. define( 'PN_ERR_MERCHANT_ID_MISMATCH', 'Merchant ID mismatch' );
  39. define( 'PN_ERR_NO_SESSION', 'No saved session found for IPN transaction' );
  40. define( 'PN_ERR_ORDER_ID_MISSING_URL', 'Order ID not present in URL' );
  41. define( 'PN_ERR_ORDER_ID_MISMATCH', 'Order ID mismatch' );
  42. define( 'PN_ERR_ORDER_INVALID', 'This order ID is invalid' );
  43. define( 'PN_ERR_ORDER_PROCESSED', 'This order has already been processed' );
  44. define( 'PN_ERR_PDT_FAIL', 'PDT query failed' );
  45. define( 'PN_ERR_PDT_TOKEN_MISSING', 'PDT token not present in URL' );
  46. define( 'PN_ERR_SESSIONID_MISMATCH', 'Session ID mismatch' );
  47. define( 'PN_ERR_UNKNOWN', 'Unkown error occurred' );
  48. define( 'PN_ERR_BAD_ATTENDEE_SESSION_ID', 'Incorrect attendee session id' );
  49. // General
  50. define( 'PN_MSG_OK', 'Payment was successful' );
  51. define( 'PN_MSG_FAILED', 'Payment has failed' );
  52. define( 'PN_MSG_PENDING',
  53. 'The payment is pending. Please note, you will receive another Instant'.
  54. ' Transaction Notification when the payment status changes to'.
  55. ' "Completed", or "Failed"' );
  56. /**
  57. * pnlog
  58. *
  59. * Log function for logging output.
  60. *
  61. * @param $msg String Message to log
  62. * @param $close Boolean Whether to close the log file or not
  63. */
  64. function pnlog( $msg = '', $close = false )
  65. {
  66. static $fh = 0;
  67. global $module;
  68. // Only log if debugging is enabled
  69. if( PN_DEBUG )
  70. {
  71. if( $close )
  72. {
  73. fclose( $fh );
  74. }
  75. else
  76. {
  77. // If file doesn't exist, create it
  78. if( !$fh )
  79. {
  80. $pathinfo = pathinfo( __FILE__ );
  81. $fh = fopen( $pathinfo['dirname'] .'/sagepaynow.log', 'a+' );
  82. }
  83. // If file was successfully created
  84. if( $fh )
  85. {
  86. $line = date( 'Y-m-d H:i:s' ) .' : '. $msg ."\n";
  87. fwrite( $fh, $line );
  88. }
  89. }
  90. }
  91. }
  92. /**
  93. * pnGetData
  94. *
  95. */
  96. function pnGetData()
  97. {
  98. // Posted variables from IPN
  99. $pnData = $_POST;
  100. // Strip any slashes in data
  101. foreach( $pnData as $key => $val )
  102. $pnData[$key] = stripslashes( $val );
  103. // Return "false" if no data was received
  104. if( sizeof( $pnData ) == 0 )
  105. return( false );
  106. else
  107. return( $pnData );
  108. }
  109. /**
  110. * pnAmountsEqual
  111. *
  112. * TODO Not used in Ubercart Drupal module and can be removed
  113. *
  114. * Checks to see whether the given amounts are equal using a proper floating
  115. * point comparison with an Epsilon which ensures that insignificant decimal
  116. * places are ignored in the comparison.
  117. *
  118. * eg. 100.00 is equal to 100.0001
  119. *
  120. * @param $amount1 Float 1st amount for comparison
  121. * @param $amount2 Float 2nd amount for comparison
  122. */
  123. function pnAmountsEqual( $amount1, $amount2 )
  124. {
  125. if( abs( floatval( $amount1 ) - floatval( $amount2 ) ) > PN_EPSILON )
  126. return( false );
  127. else
  128. return( true );
  129. }
  130. ?>