def pagseguro_do_checkout(self, request, show_id): api = PagSeguroApiTransparent() ingresso = self.get_ingresso_disponvel(show_id) item = PagSeguroItem(id=ingresso.codigo, description='Ingresso do show {}'.format(ingresso.show.nome), amount=ingresso.show.valor, quantity=1) api.add_item(item) api.set_payment_method('creditcard') # usando cartão de crédito # dados do comprador. Deve vir no post ou do banco. Pra facilitar o teste, coloquei fixo. comprador = {'name': 'Tio Patinhas', 'area_code': 41, 'phone': 987654321, 'email': '*****@*****.**', 'cpf': '22111944785', } api.set_sender(**comprador) # dados do pedido e do comprador. Deve vir no post ou do banco. Pra facilitar o teste, coloquei fixo. pedido = {'quantity': 1, 'value': ingresso.show.valor, 'name': 'Tio Patinhas', 'birth_date': '27/10/1987', 'cpf': '22111944785', 'area_code': 11, 'phone': 56273440, 'no_interest_quantity': 5} api.set_creditcard_data(**pedido) # dados do endereço do comprador. Deve vir no post ou do banco. Pra facilitar o teste, coloquei fixo. endereco = {'street': 'Av. Sete de setembro', 'number': 123, 'district': 'Centro', 'postal_code': '01234500', 'city': 'Curitiba', 'state': 'PR', 'country': 'BRA', } api.set_creditcard_billing_address(**endereco) 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) # cartão adicionado via token gerado pela aplicação frontend api.set_creditcard_token(request.POST.get('card_token')) # adicionando o hash do usuario que vem via post, da aplicação frontend api.set_sender_hash(request.POST.get('sender_hash')) data = api.checkout() checkout = Checkout.objects.filter(code=data['transaction']['code']).get() # a id do usuario logado vem via POST, na aplicação frontend id_usuario = request.POST.get('id_usuario') usuario = User.objects.filter(id=id_usuario).get() self.salvar_venda(ingresso, checkout, usuario) # atualizando ingresso caso vendido == False if not ingresso.vendido: ingresso.vendido = True ingresso.save() return Response(data)
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 payment_card(request): api = PagSeguroApiTransparent() print '---- api' print api data = request.POST print '-----data' print data token_card = data['token'] hash_sender = data['hash'] # for product in data['items']: # item = PagSeguroItem(id=product['id'], description=product['description'], amount=product['amount'], # quantity=product['quantity']) item = PagSeguroItem(id='0001', description='Notebook Prata', amount='24300.00', quantity=1) api.add_item(item) sender = { 'name': 'Jose Comprador', 'area_code': 11, '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_card = { 'quantity': 5, 'value': 125.22, 'name': 'Jose Comprador', 'birth_date': '27/10/1987', 'cpf': '22111944785', 'area_code': 11, 'phone': 56273440, } api.set_creditcard_data(**data_card) 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_card) api.set_sender_hash(hash_sender) data_checkout = api.checkout() print data_checkout return Response({"message": "payment", "data": data_checkout})
def post(self, request, *args, **kwargs): print(request.POST) cart = Cart(request.session) products = [ OrderProduct( product=item.product, price=item.price, quantity=item.quantity ) for item in cart.items ] new_order = Order(owner=request.user) ### PagSeguro Begin api = PagSeguroApiTransparent() api.set_sender_hash(request.POST['sender-hash']) sender = { 'name': 'Joãozinho do CITi', 'area_code': 81, 'phone': 21268430, 'email': settings.PAGSEGURO_TEST_BUYER_EMAIL, 'cpf': '10683924443' } api.set_sender(**sender) shipping = { 'street': 'Av. Jornalista Aníbal Fernandes', 'number': 1, 'complement': 'Centro de Informática', 'district': 'Cidade Universitária', 'postal_code': '01452002', 'city': 'Recife', 'state': 'PE', 'country': 'BRA', 'cost': Decimal('0.00') } api.set_shipping(**shipping) if 'bank-slip' in request.POST: # Boleto bancário api.set_payment_method('boleto') elif 'credit-card' in request.POST: # Cartão de crédito api.set_payment_method('creditcard') installments = 1 installment_amount = cart.total # Deve ser o valor da parcela obtida no browser data = { 'quantity': installments, 'value': installment_amount, 'name': 'Joãozinho do CITi', 'birth_date': '10/02/1995', 'cpf': '10683924443', 'area_code': 81, 'phone': 21268430 } api.set_creditcard_data(**data) billing = { 'street': 'Av. Jornalista Aníbal Fernandes', 'number': 1, 'complement': 'Centro de Informática', 'district': 'Cidade Universitária', 'postal_code': '01452002', 'city': 'Recife', 'state': 'PE', 'country': 'BRA', } api.set_creditcard_billing_address(**billing) api.set_creditcard_token(request.POST['card-token']) for p in products: api.add_item(p.to_pagseguro()) checkout = api.checkout() if checkout['success']: print(checkout) ### PagSeguro End new_order.save() for p in products: p.save() new_order.products.add(p); cart.clear() return HttpResponseRedirect(reverse_lazy('core:index'))