From 941846eb957c55e70afca2ea3ac420bbe5bc9177 Mon Sep 17 00:00:00 2001 From: chicosilva Date: Tue, 26 Sep 2017 10:15:53 -0300 Subject: [PATCH 1/3] =?UTF-8?q?in=C3=ADcio?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 8 +++ MANIFEST | 3 ++ README.md | 80 ++++++++++++++++++++++++++++ moipy/__init__.py | 1 + moipy/moip.py | 129 ++++++++++++++++++++++++++++++++++++++++++++++ setup.py | 21 ++++++++ test/test.py | 0 7 files changed, 242 insertions(+) create mode 100644 .gitignore create mode 100644 MANIFEST create mode 100644 README.md create mode 100644 moipy/__init__.py create mode 100644 moipy/moip.py create mode 100644 setup.py create mode 100644 test/test.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..900205c --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +build/** +dist/** +*.pyc +*.pyo +.project +.pydevproject +.settings +nbproject/ diff --git a/MANIFEST b/MANIFEST new file mode 100644 index 0000000..8531ee5 --- /dev/null +++ b/MANIFEST @@ -0,0 +1,3 @@ +setup.py +moipy/__init__.py +moipy/moip.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..5f073de --- /dev/null +++ b/README.md @@ -0,0 +1,80 @@ +MoIPy +===== + +Camada de abstração para integração via API com o MoIP em Python. + + - Author: Hebert Amaral + - Contributor: Ale Borba + - Contributor: Igor Hercowitz + + - Version: v0.2 + +Dependências +------------ + +O MoIPy tem as seguintes dependências: + + - lxml + - pycurl + +Instalação +---------- + +A instalação pode ser feita via pip: + + pip install moipy + +Ou então pelo repositório do Github: + + git clone git://github.com/moiplabs/moipy.git + cd moipy + python moipy/moip.py # executa os testes + python setup.py build + sudo python setup.py install + +Uso +---- + +Basta importar a classe do MoIP e sair brincando :-) + + from moipy import MoIP + + moip = MoIP('Razao do Pagamento') + + moip.set_credenciais(token='seu_token',key='sua_key') + moip.set_ambiente('sandbox') + moip.set_valor('12345') + moip.set_data_vencimento('yyyy-mm-dd') + moip.set_id_proprio('abc123') + moip.envia() + + print moip.get_resposta() # {sucesso:'Sucesso','token':'KJHSDASKD392847293AHFJKDSAH'} + +ChangeLog +---------- + + v0.2 + - Refatorações de código + - Retirada dos DocTests + + v0.1 + - First version + +ToDo +------ + + - Aplicar testes automatizados usando unittest + - Incluir dados do pagador + - Validar campos + + +Licença +------ + +MoIPy Copyright (C) 2010 Herberth Amaral + +This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA diff --git a/moipy/__init__.py b/moipy/__init__.py new file mode 100644 index 0000000..b8c0780 --- /dev/null +++ b/moipy/__init__.py @@ -0,0 +1 @@ +from moip import Moip \ No newline at end of file diff --git a/moipy/moip.py b/moipy/moip.py new file mode 100644 index 0000000..4692846 --- /dev/null +++ b/moipy/moip.py @@ -0,0 +1,129 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- +import base64 +import pycurl + +from lxml import etree + + +class Moip(): + + def __init__(self, razao, xml_node = "EnviarInstrucao"): + + if xml_node: + self.xml_node = etree.Element(xml_node) + + self._monta_xml(self.xml_node, unique = True, InstrucaoUnica = dict(Razao=razao)) + + + def _monta_xml(self, parent, unique=False, **kwargs): + """Metodo interno que monta o XML utilizando os parametros passados + por outros metodos.""" + + if isinstance(parent, etree._Element): + node_parent = parent + else: + node_parent = etree.Element(parent) + + for k,v in kwargs.items(): + if unique and node_parent.find(k) is not None: + node = node_parent.find(k) + else: + node = etree.SubElement(node_parent, k) + + if isinstance(v, dict): + self._monta_xml(node, **v) + else: + node.text = v + + return node_parent + + + def set_credenciais(self, token, key): + + self.token = token + self.key = key + + return self + + + def set_ambiente(self,ambiente): + + if ambiente=="sandbox": + self.url = "https://desenvolvedor.moip.com.br/sandbox/ws/alpha/EnviarInstrucao/Unica" + elif ambiente=="producao": + self.url = "https://www.moip.com.br/ws/alpha/EnviarInstrucao/Unica" + else: + raise ValueError("Ambiente inválido") + + + def set_valor(self, valor): + + self._monta_xml(self.xml_node, unique=True, InstrucaoUnica=dict(Valores=dict(Valor=valor))) + + return self + + + def set_id_proprio(self, id): + + self._monta_xml(self.xml_node, unique=True, InstrucaoUnica=dict(IdProprio=id)) + + return self + + + def set_data_vencimento(self,data): + + self._monta_xml(self.xml_node, unique=True, InstrucaoUnica=dict(DataVencimento=data)) + + return self + + + def set_recebedor(self,login_moip,email,apelido): + + self._monta_xml(self.xml_node, unique=True, InstrucaoUnica=dict(Recebedor=dict(LoginMoip=login_moip, Email=email, Apelido=apelido))) + + return self + + + def envia(self): + resposta = RespostaMoIP() + + passwd = self.token + ":" + self.key + + passwd64 = base64.b64encode(passwd) + + curl = pycurl.Curl() + curl.setopt(pycurl.URL,self.url) + curl.setopt(pycurl.HTTPHEADER,["Authorization: Basic " + passwd64]) + curl.setopt(pycurl.USERAGENT,"Mozilla/4.0") + curl.setopt(pycurl.USERPWD,passwd) + curl.setopt(pycurl.POST,True) + curl.setopt(pycurl.POSTFIELDS,self._get_xml()) + curl.setopt(pycurl.WRITEFUNCTION,resposta.callback) + curl.perform() + curl.close() + + self.retorno = resposta.conteudo + + return self + + + def _get_xml(self): + """Metodo interno que retorna o objeto etree em formato string""" + + return etree.tostring(self.xml_node) + + + def get_resposta(self): + + resposta = etree.XML(self.retorno) + return {'sucesso':resposta[0][1].text,'token':resposta[0][2].text} + + +class RespostaMoIP: + def __init__(self): + self.conteudo = '' + + + def callback(self,buf): + self.conteudo = buf \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..c9cf5b7 --- /dev/null +++ b/setup.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python + +from distutils.core import setup + +setup(name='Moipy', + version='0.2', + description='Python integration with MoIP payment gateway via API', + author=['Herberth Amaral','Ale Borba'], + author_email=['herberthamaral@gmail.com','ale.alvesborba@gmail.com'], + url='http://labs.moip.com.br/', + packages=['moipy'], + classifiers=[ + 'Development Status :: 4 - Beta', + 'Environment :: Console', + 'Environment :: Web Environment', + 'License :: OSI Approved :: Python Software Foundation License', + 'Operating System :: MacOS :: MacOSX', + 'Operating System :: Microsoft :: Windows', + 'Operating System :: POSIX', + 'Programming Language :: Python', + ]) diff --git a/test/test.py b/test/test.py new file mode 100644 index 0000000..e69de29 From 8971143aeaa9bb9976b43d424d2f84c7d8eba572 Mon Sep 17 00:00:00 2001 From: chicosilva Date: Tue, 26 Sep 2017 10:18:39 -0300 Subject: [PATCH 2/3] =?UTF-8?q?M=C3=A9todo=20para=20c=C3=A1lculo=20de=20pa?= =?UTF-8?q?rcelamento?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + moipy/moip.py | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/README.md b/README.md index 5f073de..529af9c 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,7 @@ Basta importar a classe do MoIP e sair brincando :-) moip.set_valor('12345') moip.set_data_vencimento('yyyy-mm-dd') moip.set_id_proprio('abc123') + moip.set_parcelamento('2', '6', '0') moip.envia() print moip.get_resposta() # {sucesso:'Sucesso','token':'KJHSDASKD392847293AHFJKDSAH'} diff --git a/moipy/moip.py b/moipy/moip.py index 4692846..e23ec24 100644 --- a/moipy/moip.py +++ b/moipy/moip.py @@ -77,6 +77,11 @@ def set_data_vencimento(self,data): return self + def set_parcelamento(self, minimo_parcelas, maximo_parcelas, juros): + + self._monta_xml(self.xml_node, unique=True, InstrucaoUnica=dict(Parcelamentos=dict(Parcelamento=dict(MinimoParcelas=minimo_parcelas, MaximoParcelas=maximo_parcelas, Juros=juros)))) + + return self def set_recebedor(self,login_moip,email,apelido): From 4c13fee06699ae63b5b7a95b72b2a295175c8ca4 Mon Sep 17 00:00:00 2001 From: chicosilva Date: Tue, 26 Sep 2017 10:25:30 -0300 Subject: [PATCH 3/3] =?UTF-8?q?Documenta=C3=A7=C3=A3o=20para=20aplica?= =?UTF-8?q?=C3=A7=C3=A3o=20do=20parcelamento?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 --- 1 file changed, 3 deletions(-) diff --git a/README.md b/README.md index 4d0a705..529af9c 100644 --- a/README.md +++ b/README.md @@ -46,10 +46,7 @@ Basta importar a classe do MoIP e sair brincando :-) moip.set_valor('12345') moip.set_data_vencimento('yyyy-mm-dd') moip.set_id_proprio('abc123') -<<<<<<< HEAD moip.set_parcelamento('2', '6', '0') -======= ->>>>>>> 53db558bcbea1fcc3e204eba7366e2e9df5ee3cf moip.envia() print moip.get_resposta() # {sucesso:'Sucesso','token':'KJHSDASKD392847293AHFJKDSAH'}