fork download
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. '''
  5. INSTRUÇÕES:
  6.  
  7.  
  8. Para utilizar basta colocar no seu código a função abaixo apresentada e depois
  9. invocar a função GenerateMbRef(ent_id, subent_id, order_id, order_value) passando
  10. os respectivos parámetros:
  11.  
  12. ent_id - Entidade fornecida pela IfthenPay software no acto da realização de contracto
  13. subent_id - Subentidade fornecida pela ifthen software no acto da realização de contracto
  14. order_id - número de identificação do pagamento que pode ser o número de cliente, número de encomenda, etc
  15. order_value - valor a pagar
  16.  
  17. '''
  18.  
  19. def GenerateMbRef( ent_id, subent_id, order_id, order_value ):
  20. chk_val = 0;
  21.  
  22. order_id = "0000" + order_id
  23.  
  24. if len(ent_id) != 5:
  25. print("Lamentamos mas tem de indicar uma entidade válida")
  26. return;
  27.  
  28. if len(subent_id) == 0:
  29. print("Lamentamos mas tem de indicar uma subentidade válida")
  30. return;
  31.  
  32. if order_value < 1:
  33. print("Lamentamos mas não é possível gerar uma referência MB para valores inferiores a 1 Euro")
  34. return;
  35.  
  36. order_value = '%01.2f' % order_value
  37.  
  38. if len(subent_id) == 1:
  39. #apenas serão considerados os 6 caracteres mais à direita do order_id
  40. chk_str = ent_id + subent_id + order_id[-6:] + str('%08.0f' % int(round(float(order_value) * 100)))
  41. elif len(subent_id) == 2:
  42. #apenas serão considerados os 5 caracteres mais à direita do order_id
  43. chk_str = ent_id + subent_id + order_id[-5:] + str('%08.0f' % int(round(float(order_value) * 100)))
  44. else:
  45. chk_str = ent_id + subent_id + order_id[-4:] + str('%08.0f' % int(round(float(order_value) * 100)))
  46.  
  47. chk_array = [3, 30, 9, 90, 27, 76, 81, 34, 49, 5, 50, 15, 53, 45, 62, 38, 89, 17, 73, 51];
  48.  
  49. i = len(chk_str)
  50.  
  51. for chk_item in chk_array:
  52. chk_val += (int(chk_str[i-1]) % 10) * chk_item
  53. i -= 1
  54.  
  55. chk_val %= 97
  56.  
  57. chk_digits = '%02.0f' % (98-chk_val)
  58.  
  59. print("Entidade: %s" % ent_id)
  60. print("Referencia: %s %s %s" % (chk_str[5:5+3], chk_str[8:8+3], chk_str[11:11+1] + chk_digits))
  61. print("Valor: %s" % order_value)
  62.  
  63. GenerateMbRef("93999", "99", "0404", 14.5)
Success #stdin #stdout 0.02s 9984KB
stdin
Standard input is empty
stdout
Entidade: 93999
Referencia: 990 040 478
Valor: 14.50