uc_cmcic.inc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * @file uc_cmcic.inc
  4. * Written by Henri MEDOT <henri.medot[AT]absyx[DOT]fr>
  5. * http://www.absyx.fr
  6. *
  7. * This file is the API-like part of the module.
  8. * It is separated from the module file so that it can be imported from another module, even without Ubercart.
  9. * So if you want to use these utilities in a custom module, just add:
  10. * require_once drupal_get_path('module', 'uc_cmcic') .'/uc_cmcic.inc;
  11. * at the beginning of your module.
  12. */
  13. function uc_cmcic_complete_request($key, $fields) {
  14. $fields = array_merge(array(
  15. 'version' => '3.0',
  16. 'texte-libre' => '',
  17. 'lgue' => 'EN',
  18. ), $fields);
  19. $ordered_fields = array();
  20. foreach (array(
  21. 'TPE',
  22. 'date',
  23. 'montant',
  24. 'reference',
  25. 'texte-libre',
  26. 'version',
  27. 'lgue',
  28. 'societe',
  29. 'mail',
  30. 'nbrech',
  31. 'dateech1',
  32. 'montantech1',
  33. 'dateech2',
  34. 'montantech2',
  35. 'dateech3',
  36. 'montantech3',
  37. 'dateech4',
  38. 'montantech4',
  39. 'options',
  40. ) as $name) {
  41. $ordered_fields[$name] = isset($fields[$name]) ? $fields[$name] : '';
  42. }
  43. $fields['MAC'] = uc_cmcic_hmac_sha1(uc_cmcic_usable_key($key), implode('*', $ordered_fields));
  44. return $fields;
  45. }
  46. function uc_cmcic_validate_response($key, $fields) {
  47. if (empty($fields['MAC'])) {
  48. return FALSE;
  49. }
  50. $ordered_fields = array();
  51. foreach (array(
  52. 'TPE',
  53. 'date',
  54. 'montant',
  55. 'reference',
  56. 'texte-libre',
  57. 'version',
  58. 'code-retour',
  59. 'cvx',
  60. 'vld',
  61. 'brand',
  62. 'status3ds',
  63. 'numauto',
  64. 'motifrefus',
  65. 'originecb',
  66. 'bincb',
  67. 'hpancb',
  68. 'ipclient',
  69. 'originetr',
  70. 'veres',
  71. 'pares',
  72. ) as $name) {
  73. $ordered_fields[$name] = isset($fields[$name]) ? $fields[$name] : '';
  74. }
  75. $ordered_fields['version'] = '3.0';
  76. $ordered_fields[] = '';
  77. $mac = uc_cmcic_hmac_sha1(uc_cmcic_usable_key($key), implode('*', $ordered_fields));
  78. return (strtolower($mac) == strtolower($fields['MAC']));
  79. }
  80. function uc_cmcic_receipt($mac_ok) {
  81. return sprintf("version=2\ncdr=%s\n", ($mac_ok) ? '0' : '1');
  82. }
  83. function uc_cmcic_usable_key($key) {
  84. if (strlen($key) != 20) {
  85. $suffix = ''. substr($key, 38, 2) .'00';
  86. $key = substr($key, 0, 38);
  87. $cca0 = ord($suffix);
  88. if (($cca0 > 70) && ($cca0 < 97)) {
  89. $key .= chr($cca0 - 23) . substr($suffix, 1, 1);
  90. }
  91. elseif (substr($suffix, 1, 1) == 'M') {
  92. $key .= substr($suffix, 0, 1) .'0';
  93. }
  94. else {
  95. $key .= substr($suffix, 0, 2);
  96. }
  97. $key = pack('H*', $key);
  98. }
  99. return $key;
  100. }
  101. /**
  102. * RFC 2104 HMAC implementation for PHP >= 4.3.0 - Creates a SHA1 HMAC.
  103. * Eliminates the need to install mhash to compute a HMAC.
  104. * Adjusted from the md5 version by Lance Rushing.
  105. */
  106. function uc_cmcic_hmac_sha1($key, $data) {
  107. $length = 64; // block length for SHA1
  108. if (strlen($key) > $length) {
  109. $key = pack('H*', sha1($key));
  110. }
  111. $key = str_pad($key, $length, chr(0x00));
  112. $ipad = str_pad('', $length, chr(0x36));
  113. $opad = str_pad('', $length, chr(0x5c));
  114. $k_ipad = $key ^ $ipad;
  115. $k_opad = $key ^ $opad;
  116. return sha1($k_opad . pack('H*', sha1($k_ipad . $data)));
  117. }
  118. function uc_cmcic_languages() {
  119. return array('FR', 'EN', 'DE', 'IT', 'ES', 'NL');
  120. }