def send(self, invoice, signature=True): ''' Sends an invoice @param invoices:list List of invoices to send. @return: InvoiceReport ''' from pyxmli import Invoice as XMLiInvoice if not issubclass(invoice.__class__, XMLiInvoice): raise ValueError('\'invoice\' is not an instance of ' \ 'pyxmli.Invoice or one of its subclasses.') private_key, public_key = self.email.client.keys enable_signature = (signature and id(private_key) != id(None) and id(public_key) != id(None)) if enable_signature != signature: logging.warn('Missing private and/or public key(s). Invoices ' \ 'will not be signed.') ''' Filling the invoice with information about the seller. ''' if not invoice.identifier: import uuid invoice.identifier = str(uuid.uuid1()) invoice.domain = 'greendizer.com' invoice.seller.identifier = self.email.id invoice.seller.name = self.email.user.company.name address = self.email.user.company.address invoice.seller.address.street_address = address.street_address invoice.seller.address.city = address.city invoice.seller.address.zipcode = address.zipcode invoice.seller.address.state = address.state invoice.seller.address.country = address.country invoice.mentions = (invoice.mentions or self.email.user.company.legal_mentions) data = (invoice.to_signed_str(private_key, public_key) if enable_signature else invoice.to_string()) if size_in_bytes(data) > MAX_INVOICE_CONTENT_LENGTH: raise Exception('An invoice cannot be more than %dkb.' % MAX_INVOICE_CONTENT_LENGTH) request = Request(client=self.email.client, method='POST', uri=self._uri, data=data, content_type=XMLI_MIMETYPE, ) response = request.get_response() if response.status_code == 201: return self[response.data["id"]]
def __create_transaction(self, trans_type, **data): ''' Creates a transaction to perform a payment or a withdrawal. @param trans_type:str Transaction type. @return: Transaction ''' request = Request(self.balance.client, method="POST", uri=self.uri, data=data) response = request.get_response() if response.get_status_code() == 201: transaction_id = extract_id_from_uri(response["Location"]) transaction = self[transaction_id] transaction.sync(response.data, response["Etag"]) return transaction
def add(self, payment_date, method, amount=None): ''' Records a payment. @param payment_date:datetime date of the payment. @param method:str Payment method. @param amount:float (optional) Amount to transfer. @returns: Payment ''' amount = amount or self.invoice.total request = Request(self.invoice.client, method="POST", uri=self.uri, data={'date': payment_date, 'method': method, 'amount': amount}) response = request.get_response() if response.get_status_code() == 201: payment_id = extract_id_from_uri(response["Location"]) payment = self[payment_id] payment.sync(response.data, response["Etag"]) self.invoice.load() return payment