def add_payment(self, payment): """ Function to add payments @param payment: The payment dict @raise exception: when payment is invalid """ # Validate the payment self.check_payment(payment) # Get the CstmrDrctDbtInitnNode if not self._config['batch']: # Start building the non batch payment PmtInf_nodes = self._create_PmtInf_node() PmtInf_nodes['PmtInfIdNode'].text = make_id(self._config['name']) PmtInf_nodes['PmtMtdNode'].text = "DD" PmtInf_nodes['BtchBookgNode'].text = "false" PmtInf_nodes['NbOfTxsNode'].text = "1" PmtInf_nodes['CtrlSumNode'].text = int_to_decimal_str( payment['amount']) PmtInf_nodes['Cd_SvcLvl_Node'].text = "SEPA" PmtInf_nodes['Cd_LclInstrm_Node'].text = self._config['instrument'] PmtInf_nodes['SeqTpNode'].text = payment['type'] PmtInf_nodes['ReqdColltnDtNode'].text = payment['collection_date'] PmtInf_nodes['Nm_Cdtr_Node'].text = self._config['name'] PmtInf_nodes['IBAN_CdtrAcct_Node'].text = self._config['IBAN'] if 'BIC' in self._config: PmtInf_nodes['BIC_CdtrAgt_Node'].text = self._config['BIC'] PmtInf_nodes['ChrgBrNode'].text = "SLEV" PmtInf_nodes['Nm_CdtrSchmeId_Node'].text = self._config['name'] PmtInf_nodes['Id_Othr_Node'].text = self._config['creditor_id'] PmtInf_nodes['PrtryNode'].text = "SEPA" if 'BIC' in payment: bic = True else: bic = False TX_nodes = self._create_TX_node(bic) TX_nodes['InstdAmtNode'].set("Ccy", self._config['currency']) TX_nodes['InstdAmtNode'].text = int_to_decimal_str(payment['amount']) TX_nodes['MndtIdNode'].text = payment['mandate_id'] TX_nodes['DtOfSgntrNode'].text = payment['mandate_date'] if bic: TX_nodes['BIC_DbtrAgt_Node'].text = payment['BIC'] TX_nodes['Nm_Dbtr_Node'].text = payment['name'] TX_nodes['IBAN_DbtrAcct_Node'].text = payment['IBAN'] TX_nodes['UstrdNode'].text = payment['description'] if not payment.get('endtoend_id', ''): payment['endtoend_id'] = make_id(self._config['name']) TX_nodes['EndToEndIdNode'].text = payment['endtoend_id'] if self._config['batch']: self._add_batch(TX_nodes, payment) else: self._add_non_batch(TX_nodes, PmtInf_nodes)
def export(self): """ Method to output the xml as string. It will finalize the batches and then calculate the checksums (amount sum and transaction count), fill these into the group header and output the XML. """ self._finalize_batch() ctrl_sum_total = 0 nb_of_txs_total = 0 for ctrl_sum in self._xml.iter('CtrlSum'): if ctrl_sum.text is None: continue ctrl_sum_total += decimal_str_to_int(ctrl_sum.text) for nb_of_txs in self._xml.iter('NbOfTxs'): if nb_of_txs.text is None: continue nb_of_txs_total += int(nb_of_txs.text) CstmrDrctDbtInitn_node = self._xml.find('CstmrDrctDbtInitn') GrpHdr_node = CstmrDrctDbtInitn_node.find('GrpHdr') CtrlSum_node = GrpHdr_node.find('CtrlSum') NbOfTxs_node = GrpHdr_node.find('NbOfTxs') CtrlSum_node.text = int_to_decimal_str(ctrl_sum_total) NbOfTxs_node.text = str(nb_of_txs_total) # Prepending the XML version is hacky, but cElementTree only offers this # automatically if you write to a file, which we don't necessarily want. return b"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + ET.tostring( self._xml, "utf-8")
def _finalize_batch(self): """ Method to finalize the batch, this will iterate over the _batches dict and create a PmtInf node for each batch. The correct information (from the batch_key and batch_totals) will be inserted and the batch transaction nodes will be folded. Finally, the batches will be added to the main XML. """ for batch_meta, batch_nodes in self._batches.items(): batch_meta_split = batch_meta.split("::") PmtInf_nodes = self._create_PmtInf_node() PmtInf_nodes['PmtInfIdNode'].text = make_id(self._config['name']) PmtInf_nodes['PmtMtdNode'].text = "DD" PmtInf_nodes['BtchBookgNode'].text = "true" PmtInf_nodes['Cd_SvcLvl_Node'].text = "SEPA" PmtInf_nodes['Cd_LclInstrm_Node'].text = self._config['instrument'] PmtInf_nodes['SeqTpNode'].text = batch_meta_split[0] PmtInf_nodes['ReqdColltnDtNode'].text = batch_meta_split[1] PmtInf_nodes['Nm_Cdtr_Node'].text = self._config['name'] PmtInf_nodes['IBAN_CdtrAcct_Node'].text = self._config['IBAN'] if 'BIC' in self._config: PmtInf_nodes['BIC_CdtrAgt_Node'].text = self._config['BIC'] PmtInf_nodes['ChrgBrNode'].text = "SLEV" PmtInf_nodes['Nm_CdtrSchmeId_Node'].text = self._config['name'] PmtInf_nodes['Id_Othr_Node'].text = self._config['creditor_id'] PmtInf_nodes['PrtryNode'].text = "SEPA" PmtInf_nodes['NbOfTxsNode'].text = str(len(batch_nodes)) PmtInf_nodes['CtrlSumNode'].text = int_to_decimal_str( self._batch_totals[batch_meta]) PmtInf_nodes['PmtInfNode'].append(PmtInf_nodes['PmtInfIdNode']) PmtInf_nodes['PmtInfNode'].append(PmtInf_nodes['PmtMtdNode']) PmtInf_nodes['PmtInfNode'].append(PmtInf_nodes['BtchBookgNode']) PmtInf_nodes['PmtInfNode'].append(PmtInf_nodes['NbOfTxsNode']) PmtInf_nodes['PmtInfNode'].append(PmtInf_nodes['CtrlSumNode']) PmtInf_nodes['SvcLvlNode'].append(PmtInf_nodes['Cd_SvcLvl_Node']) PmtInf_nodes['LclInstrmNode'].append( PmtInf_nodes['Cd_LclInstrm_Node']) PmtInf_nodes['PmtTpInfNode'].append(PmtInf_nodes['SvcLvlNode']) PmtInf_nodes['PmtTpInfNode'].append(PmtInf_nodes['LclInstrmNode']) PmtInf_nodes['PmtTpInfNode'].append(PmtInf_nodes['SeqTpNode']) PmtInf_nodes['PmtInfNode'].append(PmtInf_nodes['PmtTpInfNode']) PmtInf_nodes['PmtInfNode'].append(PmtInf_nodes['ReqdColltnDtNode']) PmtInf_nodes['CdtrNode'].append(PmtInf_nodes['Nm_Cdtr_Node']) PmtInf_nodes['PmtInfNode'].append(PmtInf_nodes['CdtrNode']) PmtInf_nodes['Id_CdtrAcct_Node'].append( PmtInf_nodes['IBAN_CdtrAcct_Node']) PmtInf_nodes['CdtrAcctNode'].append( PmtInf_nodes['Id_CdtrAcct_Node']) PmtInf_nodes['PmtInfNode'].append(PmtInf_nodes['CdtrAcctNode']) if 'BIC' in self._config: PmtInf_nodes['FinInstnId_CdtrAgt_Node'].append( PmtInf_nodes['BIC_CdtrAgt_Node']) PmtInf_nodes['CdtrAgtNode'].append( PmtInf_nodes['FinInstnId_CdtrAgt_Node']) PmtInf_nodes['PmtInfNode'].append(PmtInf_nodes['CdtrAgtNode']) PmtInf_nodes['PmtInfNode'].append(PmtInf_nodes['ChrgBrNode']) if self.schema == 'pain.008.001.02': PmtInf_nodes['CdtrSchmeIdNode'].append( PmtInf_nodes['Nm_CdtrSchmeId_Node']) PmtInf_nodes['OthrNode'].append(PmtInf_nodes['Id_Othr_Node']) PmtInf_nodes['SchmeNmNode'].append(PmtInf_nodes['PrtryNode']) PmtInf_nodes['OthrNode'].append(PmtInf_nodes['SchmeNmNode']) PmtInf_nodes['PrvtIdNode'].append(PmtInf_nodes['OthrNode']) PmtInf_nodes['Id_CdtrSchmeId_Node'].append( PmtInf_nodes['PrvtIdNode']) PmtInf_nodes['CdtrSchmeIdNode'].append( PmtInf_nodes['Id_CdtrSchmeId_Node']) PmtInf_nodes['PmtInfNode'].append(PmtInf_nodes['CdtrSchmeIdNode']) for txnode in batch_nodes: PmtInf_nodes['PmtInfNode'].append(txnode) CstmrDrctDbtInitn_node = self._xml.find('CstmrDrctDbtInitn') CstmrDrctDbtInitn_node.append(PmtInf_nodes['PmtInfNode'])