def sandbox_inicio_pagseguro(request): if request.method == "POST": print('sandboc-inicio-pagamento') pagseguro_api = PagSeguroApiTransparent() data = pagseguro_api.get_session_id() session_id = data['session_id'] return JsonResponse(session_id)
def get_session(request): pagseguro_api = PagSeguroApiTransparent() data = pagseguro_api.get_session_id() print data session_id = data['session_id'] if request.method == 'GET': return Response({ "message": "Generated Session!", "session_id": session_id }) return Response({ "message": "Generated Session!", "session_id": session_id })
def efetuar_pagamento(request, doacao_id): doacao = get_object_or_404(Doacao, id=doacao_id, usuario=request.user) usuario_form = UsuarioCompletoForm(request.POST or None, instance=request.user) pagseguro = None if usuario_form.is_valid(): request.user = usuario_form.save() pagseguro_api = PagSeguroApiTransparent() pagseguro_data = pagseguro_api.get_session_id() pagseguro = {'session_id': pagseguro_data.get('session_id')} return render(request, 'financeiro/efetuar_pagamento.html', { 'doacao': doacao, 'usuario_form': usuario_form, 'pagseguro': pagseguro })
class PagSeguroApiTransparentTest(TestCase): def setUp(self): self.item1 = PagSeguroItem(id='0001', description='Notebook Prata', amount='24300.00', quantity=1) self.pagseguro_api = PagSeguroApiTransparent() def test_set_sender_hash(self): hsh = 'abc123' self.pagseguro_api.set_sender_hash(hsh) self.assertEqual(self.pagseguro_api.params['senderHash'], hsh) def test_set_receiver_email(self): email = '*****@*****.**' self.pagseguro_api.set_receiver_email(email) self.assertEqual(self.pagseguro_api.params['receiverEmail'], email) def test_set_payment_method(self): method = 'boleto' self.pagseguro_api.set_payment_method(method) self.assertEqual(self.pagseguro_api.params['paymentMethod'], method) def test_set_extra_amount(self): amount = 1.0 self.pagseguro_api.set_extra_amount(amount) self.assertEqual(self.pagseguro_api.params['extraAmount'], amount) def test_set_notification_url(self): url = 'https://sualoja.com.br/notifica.html' self.pagseguro_api.set_notification_url(url) self.assertEqual(self.pagseguro_api.params['notificationURL'], url) def test_set_bank_name(self): name = 'bradesco' self.pagseguro_api.set_bank_name(name) self.assertEqual(self.pagseguro_api.params['bankName'], name) def test_set_sender(self): sender = { 'name': 'Jose Comprador', 'area_code': 11, 'phone': 56273440, 'email': '*****@*****.**', 'cpf': '22111944785', } self.pagseguro_api.set_sender(**sender) params = self.pagseguro_api.params self.assertEqual(params['senderName'], sender['name']) self.assertEqual(params['senderAreaCode'], sender['area_code']) self.assertEqual(params['senderPhone'], sender['phone']) self.assertEqual(params['senderEmail'], sender['email']) self.assertEqual(params['senderCPF'], sender['cpf']) def test_set_shipping(self): shipping = { 'street': "Av. Brigadeiro Faria Lima", 'number': 1384, 'complement': '5o andar', 'district': 'Jardim Paulistano', 'postal_code': '01452002', 'city': 'Sao Paulo', 'state': 'SP', 'country': 'BRA', } self.pagseguro_api.set_shipping(**shipping) params = self.pagseguro_api.params self.assertEqual(params['shippingAddressStreet'], shipping['street']) self.assertEqual(params['shippingAddressNumber'], shipping['number']) self.assertEqual(params['shippingAddressComplement'], shipping['complement']) self.assertEqual(params['shippingAddressDistrict'], shipping['district']) self.assertEqual(params['shippingAddressPostalCode'], shipping['postal_code']) self.assertEqual(params['shippingAddressCity'], shipping['city']) self.assertEqual(params['shippingAddressState'], shipping['state']) self.assertEqual(params['shippingAddressCountry'], shipping['country']) def test_set_creditcard_data(self): data = { 'quantity': 5, 'value': 125.22, 'name': 'Jose Comprador', 'birth_date': '27/10/1987', 'cpf': '22111944785', 'area_code': 11, 'phone': 56273440, } self.pagseguro_api.set_creditcard_data(**data) params = self.pagseguro_api.params self.assertEqual(params['installmentQuantity'], data['quantity']) self.assertEqual(params['installmentValue'], data['value']) self.assertEqual(params['creditCardHolderName'], data['name']) self.assertEqual(params['creditCardHolderBirthDate'], data['birth_date']) self.assertEqual(params['creditCardHolderCPF'], data['cpf']) self.assertEqual(params['creditCardHolderAreaCode'], data['area_code']) self.assertEqual(params['creditCardHolderPhone'], data['phone']) def test_set_creditcard_billing_address(self): data = { 'street': 'Av. Brig. Faria Lima', 'number': 1384, 'district': 'Jardim Paulistano', 'postal_code': '01452002', 'city': 'Sao Paulo', 'state': 'SP', 'country': 'BRA', } self.pagseguro_api.set_creditcard_billing_address(**data) params = self.pagseguro_api.params self.assertEqual(params['billingAddressStreet'], data['street']) self.assertEqual(params['billingAddressNumber'], data['number']) self.assertEqual(params['billingAddressDistrict'], data['district']) self.assertEqual(params['billingAddressPostalCode'], data['postal_code']) self.assertEqual(params['billingAddressCity'], data['city']) self.assertEqual(params['billingAddressState'], data['state']) self.assertEqual(params['billingAddressCountry'], data['country']) def test_set_creditcard_token(self): token = '4as56d4a56d456as456dsa' self.pagseguro_api.set_creditcard_token(token) self.assertEqual(self.pagseguro_api.params['creditCardToken'], token) @httpretty.activate def test_valid_get_session_id(self): # mock requests httpretty.register_uri( httpretty.POST, SESSION_URL, body=session_response_xml, status=200, ) data = self.pagseguro_api.get_session_id() self.assertEqual(data['status_code'], 200) self.assertEqual(data['session_id'], '620f99e348c24f07877c927b353e49d3') @httpretty.activate def test_invalid_get_session_id(self): # mock requests httpretty.register_uri( httpretty.POST, SESSION_URL, body='Unauthorized', status=401, ) data = self.pagseguro_api.get_session_id() self.assertEqual(data['status_code'], 401) self.assertEqual(data['message'], 'Unauthorized') @httpretty.activate def test_valid_checkout(self): sender = { 'name': 'Jose Comprador', 'area_code': 11, 'phone': 56273440, 'email': '*****@*****.**', 'cpf': '22111944785', } shipping = { 'street': "Av. Brigadeiro Faria Lima", 'number': 1384, 'complement': '5o andar', 'district': 'Jardim Paulistano', 'postal_code': '01452002', 'city': 'Sao Paulo', 'state': 'SP', 'country': 'BRA', } self.pagseguro_api.add_item(self.item1) self.pagseguro_api.set_sender_hash('abc123') self.pagseguro_api.set_receiver_email('*****@*****.**') self.pagseguro_api.set_payment_method('boleto') self.pagseguro_api.set_extra_amount(1.0) self.pagseguro_api.set_notification_url( 'https://sualoja.com.br/notifica.html') self.pagseguro_api.set_sender(**sender) self.pagseguro_api.set_shipping(**shipping) # mock requests httpretty.register_uri( httpretty.POST, TRANSACTION_URL, body=transparent_response_xml, status=200, ) data = self.pagseguro_api.checkout() self.assertEqual(data['success'], True) self.assertEqual(data['status_code'], 200) self.assertEqual(data['code'], '9E884542-81B3-4419-9A75-BCC6FB495EF1') self.assertEqual(data['date'], parse('2011-02-05T15:46:12.000-02:00')) @httpretty.activate def test_invalid_checkout(self): # mock requests httpretty.register_uri( httpretty.POST, TRANSACTION_URL, body='Unauthorized', status=401, ) data = self.pagseguro_api.checkout() self.assertEqual(data['success'], False) self.assertEqual(data['message'], 'Unauthorized') self.assertEqual(data['status_code'], 401)
def sandbox(request): print("sandbox") c = request.GET.get('pk', None) qnt = request.GET.get('qnt_passagens', None) carrinho = get_object_or_404(Carrinho, pk=c) car = carrinho.total car = '{:.2f}'.format(car) pagseguro_api = PagSeguroApiTransparent() data = pagseguro_api.get_session_id() session_id = data['session_id'] if request.method == 'POST': print("post") bandeira = request.POST.get('brand') nome = request.POST.get('username') token = request.POST.get('token') hashPagseguro = request.POST.get('hash') api = PagSeguroApiTransparent() descricao = carrinho.viagem.destino item1 = PagSeguroItem(id=c, description=descricao, amount=car, quantity=1) api.add_item(item1) sender = { 'name': 'Jose Comprador', 'area_code': 92, 'phone': 56273440, 'email': '*****@*****.**', 'cpf': '22111944785', } api.set_sender(**sender) shipping = { 'street': "Av. Brigadeiro Faria Lima", 'number': 1384, 'complement': '5o andar', 'district': 'Jardim Paulistano', 'postal_code': '01452002', 'city': 'Sao Paulo', 'state': 'SP', 'country': 'BRA', } api.set_shipping(**shipping) api.set_payment_method('creditcard') data = { 'quantity': 1, 'value': car, 'name': 'Jose Comprador', 'birth_date': '27/10/1987', 'cpf': '22111944785', 'area_code': 11, 'phone': 56273440, 'no_interest_quantity': 5 } api.set_creditcard_data(**data) billing_address = { 'street': 'Av. Brig. Faria Lima', 'number': 1384, 'district': 'Jardim Paulistano', 'postal_code': '01452002', 'city': 'Sao Paulo', 'state': 'SP', 'country': 'BRA', } api.set_creditcard_billing_address(**billing_address) api.set_creditcard_token(token) api.set_sender_hash(hashPagseguro) data = api.checkout() print("data", data) if data['success'] is False: raise CheckoutException(data['message']) print(notificacao_recebida.connect(load_signal)) return HttpResponse(notificacao_recebida.connect(load_signal)) context = { 'session': session_id, 'cart': carrinho, 'qnt': qnt, } return render(request, 'dev_radioanori/sandbox-pagamento.html', context)
class PagSeguroApiTransparentTest(TestCase): def setUp(self): self.item1 = PagSeguroItem( id='0001', description='Notebook Prata', amount='24300.00', quantity=1 ) self.pagseguro_api = PagSeguroApiTransparent() def test_set_sender_hash(self): hsh = 'abc123' self.pagseguro_api.set_sender_hash(hsh) self.assertEqual(self.pagseguro_api.params['senderHash'], hsh) def test_set_receiver_email(self): email = '*****@*****.**' self.pagseguro_api.set_receiver_email(email) self.assertEqual(self.pagseguro_api.params['receiverEmail'], email) def test_set_payment_method(self): method = 'boleto' self.pagseguro_api.set_payment_method(method) self.assertEqual(self.pagseguro_api.params['paymentMethod'], method) def test_set_extra_amount(self): amount = 1.0 self.pagseguro_api.set_extra_amount(amount) self.assertEqual(self.pagseguro_api.params['extraAmount'], amount) def test_set_notification_url(self): url = 'https://sualoja.com.br/notifica.html' self.pagseguro_api.set_notification_url(url) self.assertEqual(self.pagseguro_api.params['notificationURL'], url) def test_set_bank_name(self): name = 'bradesco' self.pagseguro_api.set_bank_name(name) self.assertEqual(self.pagseguro_api.params['bankName'], name) def test_set_sender(self): sender = { 'name': 'Jose Comprador', 'area_code': 11, 'phone': 56273440, 'email': '*****@*****.**', 'cpf': '22111944785', } self.pagseguro_api.set_sender(**sender) params = self.pagseguro_api.params self.assertEqual(params['senderName'], sender['name']) self.assertEqual(params['senderAreaCode'], sender['area_code']) self.assertEqual(params['senderPhone'], sender['phone']) self.assertEqual(params['senderEmail'], sender['email']) self.assertEqual(params['senderCPF'], sender['cpf']) def test_set_shipping(self): shipping = { 'street': "Av. Brigadeiro Faria Lima", 'number': 1384, 'complement': '5o andar', 'district': 'Jardim Paulistano', 'postal_code': '01452002', 'city': 'Sao Paulo', 'state': 'SP', 'country': 'BRA', } self.pagseguro_api.set_shipping(**shipping) params = self.pagseguro_api.params self.assertEqual(params['shippingAddressStreet'], shipping['street']) self.assertEqual(params['shippingAddressNumber'], shipping['number']) self.assertEqual(params['shippingAddressComplement'], shipping['complement']) self.assertEqual(params['shippingAddressDistrict'], shipping['district']) self.assertEqual(params['shippingAddressPostalCode'], shipping['postal_code']) self.assertEqual(params['shippingAddressCity'], shipping['city']) self.assertEqual(params['shippingAddressState'], shipping['state']) self.assertEqual(params['shippingAddressCountry'], shipping['country']) def test_set_creditcard_data(self): data = { 'quantity': 5, 'value': 125.22, 'name': 'Jose Comprador', 'birth_date': '27/10/1987', 'cpf': '22111944785', 'area_code': 11, 'phone': 56273440, } self.pagseguro_api.set_creditcard_data(**data) params = self.pagseguro_api.params self.assertEqual(params['installmentQuantity'], data['quantity']) self.assertEqual(params['installmentValue'], data['value']) self.assertEqual(params['creditCardHolderName'], data['name']) self.assertEqual(params['creditCardHolderBirthDate'], data['birth_date']) self.assertEqual(params['creditCardHolderCPF'], data['cpf']) self.assertEqual(params['creditCardHolderAreaCode'], data['area_code']) self.assertEqual(params['creditCardHolderPhone'], data['phone']) def test_set_creditcard_billing_address(self): data = { 'street': 'Av. Brig. Faria Lima', 'number': 1384, 'district': 'Jardim Paulistano', 'postal_code': '01452002', 'city': 'Sao Paulo', 'state': 'SP', 'country': 'BRA', } self.pagseguro_api.set_creditcard_billing_address(**data) params = self.pagseguro_api.params self.assertEqual(params['billingAddressStreet'], data['street']) self.assertEqual(params['billingAddressNumber'], data['number']) self.assertEqual(params['billingAddressDistrict'], data['district']) self.assertEqual(params['billingAddressPostalCode'], data['postal_code']) self.assertEqual(params['billingAddressCity'], data['city']) self.assertEqual(params['billingAddressState'], data['state']) self.assertEqual(params['billingAddressCountry'], data['country']) def test_set_creditcard_token(self): token = '4as56d4a56d456as456dsa' self.pagseguro_api.set_creditcard_token(token) self.assertEqual(self.pagseguro_api.params['creditCardToken'], token) @httpretty.activate def test_valid_get_session_id(self): # mock requests httpretty.register_uri( httpretty.POST, SESSION_URL, body=session_response_xml, status=200, ) data = self.pagseguro_api.get_session_id() self.assertEqual(data['status_code'], 200) self.assertEqual( data['session_id'], '620f99e348c24f07877c927b353e49d3' ) @httpretty.activate def test_invalid_get_session_id(self): # mock requests httpretty.register_uri( httpretty.POST, SESSION_URL, body='Unauthorized', status=401, ) data = self.pagseguro_api.get_session_id() self.assertEqual(data['status_code'], 401) self.assertEqual(data['message'], 'Unauthorized') @httpretty.activate def test_valid_checkout(self): sender = { 'name': 'Jose Comprador', 'area_code': 11, 'phone': 56273440, 'email': '*****@*****.**', 'cpf': '22111944785', } shipping = { 'street': "Av. Brigadeiro Faria Lima", 'number': 1384, 'complement': '5o andar', 'district': 'Jardim Paulistano', 'postal_code': '01452002', 'city': 'Sao Paulo', 'state': 'SP', 'country': 'BRA', } self.pagseguro_api.add_item(self.item1) self.pagseguro_api.set_sender_hash('abc123') self.pagseguro_api.set_receiver_email('*****@*****.**') self.pagseguro_api.set_payment_method('boleto') self.pagseguro_api.set_extra_amount(1.0) self.pagseguro_api.set_notification_url('https://sualoja.com.br/notifica.html') self.pagseguro_api.set_sender(**sender) self.pagseguro_api.set_shipping(**shipping) # mock requests httpretty.register_uri( httpretty.POST, TRANSACTION_URL, body=transparent_response_xml, status=200, ) data = self.pagseguro_api.checkout() self.assertEqual(data['success'], True) self.assertEqual(data['status_code'], 200) self.assertEqual(data['code'], '9E884542-81B3-4419-9A75-BCC6FB495EF1') self.assertEqual(data['date'], parse('2011-02-05T15:46:12.000-02:00')) @httpretty.activate def test_invalid_checkout(self): # mock requests httpretty.register_uri( httpretty.POST, TRANSACTION_URL, body='Unauthorized', status=401, ) data = self.pagseguro_api.checkout() self.assertEqual(data['success'], False) self.assertEqual(data['message'], 'Unauthorized') self.assertEqual(data['status_code'], 401)
def pagseguro_retornar_sessao_id(self, request): api = PagSeguroApiTransparent() data = api.get_session_id() data = {"session_id": data['session_id']} return Response(data)