def pagseguroAPI_transparente(request, pk): print("pagseguroAPI_transparente") session = request.GET.get('session') id = request.GET.get('pk') description = request.GET.get('pk') api = PagSeguroApiTransparent() item1 = PagSeguroItem(id=id, description='passagem', amount='200.00', quantity=1) api.add_item(item1) api.set_payment_method('eft') api.set_bank_name('itau') if request.method == "GET": context = {'session': session, 'item': item1} return render(request, 'dev_radioanori/pagseguro_transparente.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 sandbox_debito(request): if request.method == "POST": print("post/sandbox-debito") pk = request.POST.get('pk', None) carrinho = get_object_or_404(Carrinho, pk=pk) amount = carrinho.total amount = '{:.2f}'.format(amount) nome = request.POST.get('username') cpf = request.POST.get('cpf') brand = request.POST.get('brand_radio') hashPagseguro = request.POST.get('hash') qnt = carrinho.qnt_inteira + carrinho.qnt_meia descricao = "beiraonline " + carrinho.viagem.origem + "-" + carrinho.viagem.destino print('brand:', brand) print('nome:', nome) print('cpf', cpf) item1 = PagSeguroItem(id=pk, description=descricao, amount=amount, quantity=qnt) api = PagSeguroApiTransparent() api.add_item(item1) sender = { 'name': nome, '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('eft') api.set_bank_name('itau') api.set_sender_hash(hashPagseguro) data = api.checkout() #print("data", data) if data['success'] is False: raise CheckoutException(data['message']) payment_url = data['transaction']['paymentLink'] return redirect(payment_url) return HttpResponse("deu certo")
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)