def pagseguro_form_generate_values(self, values):
     pagseguro_tx_values = dict(values)
     pagseguro_tx_values.update({
         'pagseguro_email': self.pagseguro_email_account,
         'pagseguro_token': self.pagseguro_token,
         'amount': format(values['amount'], '.2f')
     })
     if self.environment == 'prod':
         config = {'sandbox': False}
     else:
         config = {'sandbox': True}
     pg = PagSeguro(email=self.pagseguro_email_account,
                    token=self.pagseguro_token,
                    config=config)
     pg.reference_prefix = None
     pg.reference = pagseguro_tx_values['reference']
     pg.items = [
         {
             "id": "0001",
             "description": pagseguro_tx_values['reference'],
             "amount": pagseguro_tx_values['amount'],
             "quantity": 1
         },
     ]
     base_url = self.env['ir.config_parameter'].sudo().get_param(
         'web.base.url')
     pg.redirect_url = base_url + pagseguro_tx_values['return_url']
     _logger.info("Redirect Url: %s" % (pg.redirect_url))
     pg.notification_url = base_url + "/payment/pagseguro/feedback"
     response = pg.checkout()
     _logger.info("Response Errors: %s" % (response.errors))
     pagseguro_tx_values.update({'payment_url': response.payment_url})
     # _logger.info(values['reference'])
     _logger.info("Transaction Values: %s" % (pagseguro_tx_values))
     return pagseguro_tx_values
def checkout_pg(sender, shipping, cart):
    pagseguro = PagSeguro(email=app.config['EMAIL'], token=app.config['TOKEN'])
    pagseguro.sender = sender
    shipping['type'] = pagseguro.SEDEX
    pagseguro.shipping = shipping
    pagseguro.extra_amount = "%.2f" % float(app.config['EXTRA_AMOUNT'])
    pagseguro.redirect_url = app.config['REDIRECT_URL']
    pagseguro.notification_url = app.config['NOTIFICATION_URL']
    pagseguro.items = cart.items
    for item in cart.items:
        item['amount'] = "%.2f" % float(app.config['EXTRA_AMOUNT'])
    return pagseguro
def checkout_pg(sender, shipping, cart):
    pg = PagSeguro(email=app.config['EMAIL'], token=app.config['TOKEN'])
    pg.sender = sender
    shipping['type'] = pg.SEDEX
    pg.shipping = shipping
    pg.extra_amount = "%.2f" % float(app.config['EXTRA_AMOUNT'])
    pg.redirect_url = app.config['REDIRECT_URL']
    pg.notification_url = app.config['NOTIFICATION_URL']
    pg.items = cart.items
    for item in cart.items:
        item['amount'] = "%.2f" % float(app.config['EXTRA_AMOUNT'])
    return pg
Example #4
0
def make_pg():
    pg = PagSeguro(email="*****@*****.**", token="ABCDEFGHIJKLMNO")
    pg.sender = {
        "name": "Bruno Rocha",
        "area_code": 11,
        "phone": 981001213,
        "email": "*****@*****.**",
    }
    pg.shipping = {
        "type": pg.SEDEX,
        "street": "Av Brig Faria Lima",
        "number": 1234,
        "complement": "5 andar",
        "district": "Jardim Paulistano",
        "postal_code": "06650030",
        "city": "Sao Paulo",
        "state": "SP",
        "country": "BRA"
    }
    pg.extra_amount = 12.70
    pg.redirect_url = "http://meusite.com/obrigado"
    pg.notification_url = "http://meusite.com/notification"
    return pg
Example #5
0
    def post(self, request, format=None):
        conferencia_pk = request.data.get("conferencia")
        inscricao_pk = request.data.get("inscricao")

        inscricao = Inscricao.objects.get(pk=inscricao_pk,
                                          conferencia_id=conferencia_pk)
        conferencia = inscricao.conferencia

        pg = PagSeguro(
            email=settings.PAGSEGURO_EMAIL,
            token=settings.PAGSEGURO_TOKEN,
        )

        pg.sender = {
            "name": inscricao.nome,
            "area_code": inscricao.ddd,
            "phone": inscricao.cleanned_telefone(),
            "email": inscricao.email,
        }

        pg.shipping = {
            "type": pg.NONE,
            "street": inscricao.endereco,
            "number": inscricao.numero,
            "complement": inscricao.complemento,
            "district": inscricao.bairro,
            "postal_code": inscricao.cep,
            "city": inscricao.cidade,
            "state": inscricao.uf,
            "country": "BRA"
        }

        pg.reference_prefix = "REFID_"
        pg.reference = inscricao.pk

        pg.items = [
            {
                "id": "0001",
                "description": conferencia.titulo,
                "amount": inscricao.valor_total,
                "quantity": 1,
            },
        ]

        url_base = reverse_lazy(
            'home', kwargs={"conferencia": conferencia.titulo_slug})

        redirect_url = "{}{}".format(settings.BASE_URL, url_base)

        pg.redirect_url = redirect_url
        pg.notification_url = settings.NOTIFICATION_URL

        response = pg.checkout()

        inscricao.payment_reference = pg.reference
        inscricao.status = 1
        inscricao.save()

        return Response({
            "code": response.code,
            "transaction": response.transaction,
            "date": response.date,
            "payment_url": response.payment_url,
            "payment_link": response.payment_link,
            "errors": response.errors,
            "pre_ref": pg.reference_prefix,
            "reference": pg.reference,
        })
Example #6
0
def payment_post():
    if request.method == 'POST':

        config = {
            'sandbox': True,
            'CURRENCY': 'BRL',
            'USE_SHIPPING': 'false',
        }

        pg = PagSeguro(email="*****@*****.**",
                       token="6B1E3EF957F84EF4913EFDA3B76D6D84",
                       config=config)

        pg.sender = {
            "name": current_user.name + ' ' + current_user.surname,
            "area_code": current_user.phone[:2],
            "phone": current_user.phone[2:],
            "email": current_user.email,
        }

        orders = session['orders']
        street = session['street']
        number = session['number']
        district = session['district']
        city = session['city']
        cep = session['cep']
        complement = session['complement']

        pg.shipping = {
            "type": pg.SEDEX,
            "street": street,
            "number": number,
            "complement": complement,
            "district": district,
            "postal_code": cep,
            "city": city,
            "state": "CE",
            "country": "BRA"
        }

        new_order = Order(client_id=current_user.id,
                          street=street,
                          address_number=number,
                          district=district,
                          city=city,
                          cep=cep,
                          complement=complement,
                          price=session['subtotal'])
        db.session.add(new_order)
        db.session.flush()

        pg.reference = new_order.id
        pg.extra_amount = 0

        itens = []
        # É possivel adicionar um record ao db usando uma lista ao invés de instanciar um objeto como feito anteriormente
        for product, quantity in orders.items(
        ):  # cria uma lista de dicionários, dicionários esses que representam um objeto da tabela
            item = {}

            item['quantity'] = quantity
            item['product_id'] = Product.query.filter_by(
                title=product).first().id
            item['order_id'] = new_order.id

            itens.append(item)

        for item in itens:  #Passa pela lista de dicionarios e adiciona cada um ao banco de dados
            new_order_item = ItemOrder(**item)
            db.session.add(new_order_item)

            product = Product.query.filter_by(id=item['product_id']).first()
            pg.items.append(
                {
                    "id": product.id,
                    "description": product.desc,
                    "amount": "{:.2f}".format(product.price),
                    "quantity": item['quantity'],
                    "weight": None
                }, )
        pg.notification_url = request.url_root + "/notificacao"
        pg.redirect_url = request.url_root
        response = pg.checkout()
        new_response = PagSeguroResponse(code=response.code,
                                         date=response.date,
                                         payment_url=response.payment_url,
                                         errors=response.errors,
                                         order_id=new_order.id)
        db.session.add(new_response)

        db.session.commit()

        return redirect(response.payment_url)