Esempio n. 1
0
File: journey.py Progetto: stdk/u2py
def refill(card,amount):
 '''
 Refills card with journey contract inside reader field.
 `card` argument should be valid card in active state with transport_card.validate
 performed on it. `amount` - summ in cents to be added to transport purse,
 converted to journeys and subsequently removed from transport purse.
 '''
 sector = card.sector(num=SECTOR,key=KEY,enc=ENCRYPTION)
 contract = CONTRACT_A.validate(sector,CONTRACT1_STATIC,CONTRACT1_DYNAMIC)

 purse_value = purse.change_value(card,amount)
 if purse_value < JOURNEY_COST: return 0

 amount_used = contract.dynamic.refill(purse_value)
 purse.change_value(card,-amount_used)
 event = EVENT_CONTRACT_ADD2(card)
 event.AID,event.PIX = AID,PIX
 event.Value = contract.dynamic.journeys
 event.TransactionValue = amount_used / JOURNEY_COST
 event.Amount = amount_used
 event.LocalTransactions = contract.dynamic.transaction

 try:
  card.auth(sector)
  contract.commit(low_endian = 1)
  sector.write(blocks=(1,2))
 except Exception as e: event.set_error_code(e); raise
 finally: event.save(card)

 return amount_used
Esempio n. 2
0
File: term.py Progetto: stdk/u2py
def refill(card,amount):
 cost,(begin,end) = available_refill(*card.contract())

 if purse.change_value(card,amount) < cost: return 0
 purse.change_value(card,-cost)

 events = []
 static_sector,static_data = read_static(card)
 dyn_sector,dyn_proxy,dyn_data = read_dynamic(card)

 if not static_data.active():
  sell_event = EVENT_CONTRACT(card, Value = cost)
  sell_event.LocalTransactions = dyn_data.update()
  events.append(sell_event)

 static_data.refill(begin,end)

 term_pack = ((end.year - 2000) << 8) + end.month
 refill_event = EVENT_CONTRACT_ADD2(card,TransactionValue=term_pack,Amount=cost)
 refill_event.LocalTransactions = dyn_data.update()
 events.append(refill_event)

 try:
  dyn_proxy.commit(dyn_data,low_endian = 1)
  dyn_sector.write()

  card.auth(static_sector)
  static_sector.data.crc16_calc(low_endian = 1)
  static_sector.write()
 except Exception as e: [event.set_error_code(e) for event in events]; raise
 finally: [event.save(card) for event in events]

 return cost