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 setUp(self): self.item1 = PagSeguroItem( id='0001', description='Notebook Prata', amount='24300.00', quantity=1 ) self.pagseguro_api = PagSeguroApiTransparent()
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 })
def sandbox_pagamento(request): print('sandbox_pagamento') if request.method == "POST": print("post") pagseguro_api = PagSeguroApiTransparent() #json = serializers.serialize('json', session_id) return HttpResponse(json, content_type='application/json') return render(request, 'dev_radioanori/sandbox.html')
def teste_pagseguro(request): pagseguro_api = PagSeguroApi( reference='id-unico-de-referencia-do-seu-sistema') item1 = PagSeguroItem(id='0001', description='Notebook Prata', amount='24300.00', quantity=1) item2 = PagSeguroItem(id='0002', description='Meu item 0002', amount='150.00', quantity=1, shipping_cost='25.00', weight=500) pagseguro_api.add_item(item1) pagseguro_api.add_item(item2) api = PagSeguroApiTransparent() api.add_item(item1) 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')
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)
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")
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 render_to_response(self, context): pagseguro_session = PagSeguroApiTransparent().get_session_id() response = super(CartView, self).render_to_response(context) response.set_cookie('pagseguro_session', value=pagseguro_session['session_id']) return response
def completar_pagamento(request, doacao_id): if not request.method == 'POST': return redirect( reverse('financeiro:doacao_pagamento:index', kwargs={'doacao_id': doacao_id})) doacao = get_object_or_404(Doacao, id=doacao_id, usuario=request.user) payment_method = request.POST.get('payment_method') pagseguro_item = PagSeguroItem(id=str(doacao.aniversario.id), description=str(doacao), amount='%.2f' % float(doacao.pagamento.valor), quantity=1) if payment_method == 'pagseguro': pagseguro_api = PagSeguroApi(reference=str(doacao.id)) pagseguro_api.add_item(pagseguro_item) pagseguro_data = pagseguro_api.checkout() doacao.pagamento.checkout = Checkout.objects.get( code=pagseguro_data.get('code')) doacao.pagamento.save(update_fields=['checkout']) return redirect(pagseguro_data.get('redirect_url')) sender = { 'name': request.user.nome, 'area_code': request.user.telefone_ddd, 'phone': request.user.telefone_numero, 'email': request.user.email, 'cpf': request.user.cleaned_cpf } endereco = None if request.POST.get('endereco') == 'novo': endereco = Endereco.objects.create( usuario=request.user, lagradouro=request.POST.get('endereco-lagradouro'), numero=int(request.POST.get('endereco-numero')), complemento=request.POST.get('endereco-complemento'), bairro=request.POST.get('endereco-bairro'), cep=request.POST.get('endereco-cep'), cidade=request.POST.get('endereco-cidade'), estado=request.POST.get('endereco-estado')) else: endereco = Endereco.objects.get(usuario=request.user, id=int(request.POST.get('endereco'))) if endereco: pagseguro_api = PagSeguroApiTransparent(reference=str(doacao.id)) pagseguro_api.add_item(pagseguro_item) pagseguro_api.set_sender(**sender) pagseguro_api.set_shipping(**endereco.pagseguro_serialize()) pagseguro_api.set_payment_method(payment_method) pagseguro_api.set_sender_hash(request.POST.get('sender_hash')) if payment_method == 'boleto': pagseguro_data = pagseguro_api.checkout() doacao.pagamento.boleto_link = pagseguro_data.get( 'transaction').get('paymentLink') doacao.pagamento.save(update_fields=['boleto_link']) return redirect( reverse('financeiro:doacao_pagamento:gerar_boleto', kwargs={'doacao_id': doacao.id})) if payment_method == 'creditcard': pagseguro_api.set_creditcard_token(request.POST.get('card_token')) pagseguro_api.set_creditcard_data( quantity=1, value='%.2f' % doacao.pagamento.valor, name=request.POST.get('nome-no-cartao'), birth_date=request.POST.get('data-nascimento-cartao'), cpf=request.POST.get('cpf-cartao'), area_code=request.POST.get('telefone-ddd'), phone=request.POST.get('telefone')) endereco_cartao = None endereco_cartao_opt = request.POST.get('endereco-cartao') if endereco_cartao_opt == 'mesmo': endereco_cartao = endereco elif endereco_cartao_opt == 'novo': endereco_cartao = Endereco.objects.create( usuario=request.user, lagradouro=request.POST.get('endereco-cartao-lagradouro'), numero=int(request.POST.get('endereco-cartao-numero')), complemento=request.POST.get( 'endereco-cartao-complemento'), bairro=request.POST.get('endereco-cartao-bairro'), cep=request.POST.get('endereco-cartao-cep'), cidade=request.POST.get('endereco-cartao-cidade'), estado=request.POST.get('endereco-cartao-estado')) else: endereco_cartao = Endereco.objects.get( usuario=request.user, id=int(request.POST.get('endereco-cartao'))) pagseguro_api.set_creditcard_billing_address( **endereco_cartao.pagseguro_serialize()) pagseguro_data = pagseguro_api.checkout() doacao.pagamento.cartao = Checkout.objects.get( code=pagseguro_data.get('code')) doacao.pagamento.save(update_fields=['cartao']) return redirect( reverse('aniversario:doacao_realizada', kwargs={ 'slug_usuario': doacao.aniversario.usuario.slug, 'slug_missao': doacao.aniversario.missao.slug })) raise Http404()
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)
def pagseguro_retornar_sessao_id(self, request): api = PagSeguroApiTransparent() data = api.get_session_id() data = {"session_id": data['session_id']} return Response(data)
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'))
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})