Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[14.0][RFC] payment_pagseguro: refactor charge to orders endpoint #2309

Draft
wants to merge 1 commit into
base: 14.0
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 73 additions & 22 deletions payment_pagseguro/models/payment_transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

_logger = logging.getLogger(__name__)

try:
from erpbrasil.base.misc import punctuation_rm
except ImportError:
_logger.error("Biblioteca erpbrasil.base não instalada")


class PaymentTransactionPagseguro(models.Model):
_inherit = "payment.transaction"
Expand All @@ -33,7 +38,7 @@ def _create_pagseguro_charge(self):

Uses encrypted credit card.
"""
api_url_charge = "https://%s/charges" % (
api_url_charge = "https://%s/orders" % (
self.acquirer_id._get_pagseguro_api_url()
)

Expand Down Expand Up @@ -83,7 +88,12 @@ def pagseguro_s2s_capture_transaction(self):
r = requests.post(
self.pagseguro_s2s_capture_link,
headers=self.acquirer_id._get_pagseguro_api_headers(),
json=self._get_pagseguro_charge_params(),
json={
"charge_id": self.id,
"amount": {
"value": int(self.amount * 100),
},
},
)
res = r.json()
_logger.info(
Expand Down Expand Up @@ -166,13 +176,16 @@ def _pagseguro_s2s_validate_tree(self, tree):
)
return True

if tree.get("payment_response"):
code = tree.get("payment_response", {}).get("code")
if tree.get("error_messages"):
return False

if tree.get("charges", {})[0].get("payment_response"):
code = tree.get("charges", {})[0].get("payment_response").get("code")
if code == "20000":
self.log_transaction(reference=tree.get("id"), message="")

# store capture and void links for future manual operations
for method in tree.get("links"):
for method in tree.get("charges", {})[0].get("links"):
if "rel" in method and "href" in method:
if method.get("rel") == "SELF":
self.pagseguro_s2s_check_link = method.get("href")
Expand Down Expand Up @@ -223,24 +236,63 @@ def _get_pagseguro_charge_params(self):
# if currency != "BRL":
# raise UserError(_("Only BRL currency is allowed."))
CHARGE_PARAMS = {
"reference_id": str(self.payment_token_id.acquirer_id.id),
"description": self.display_name[:13],
"amount": {
# Charge is in BRL cents -> Multiply by 100
"value": int(self.amount * 100),
"currency": "BRL",
"reference_id": self.sale_order_ids[0].name,
"customer": {
"name": self.partner_name,
"email": self.partner_email,
"tax_id": punctuation_rm(self.partner_id.vat)
or punctuation_rm(self.partner_id.cnpj_cpf),
"phones": [
{
"country": "55",
"area": "11",
"number": "999999999",
"type": "MOBILE",
}
],
},
"payment_method": {
"soft_descriptor": self.acquirer_id.company_id.name,
"type": "CREDIT_CARD",
"installments": 1,
"capture": False,
"card": {
"encrypted": self.payment_token_id.pagseguro_card_token,
},
"items": [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eu colocaria algo assim para listar as sale_order_line:
items = []
for line in self.sale_order_ids[0].order_line:
item = {
"reference_id": line.product_id.default_code,
"name": line.product_id.name,
"quantity": line.product_uom_qty,
"unit_amount": int(line.price_unit * 100),
}
items.append(item)

Copy link
Member Author

@marcelsavegnago marcelsavegnago Jan 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@augustodinizl se possível, pega a branch desta PR e submete uma PR para o fork da https://github.com/escodoo/l10n-brazil fazendo favor.

{
"reference_id": self.sale_order_ids[0].name,
"name": "Produto Vendido",
"quantity": 1,
"unit_amount": int(self.amount * 100),
}
],
"qr_code": {"amount": {"value": int(self.amount * 100)}},
"shipping": {
"address": {
"street": self.partner_id.street,
"number": self.partner_id.street_number or "S/N",
"complement": self.partner_id.street2 or "N/A",
"locality": self.partner_id.district,
"city": self.partner_id.district,
"region_code": self.partner_id.state_id.code,
"country": "BRA",
"postal_code": punctuation_rm(self.partner_zip),
}
},
"charges": [
{
"reference_id": str(self.payment_token_id.acquirer_id.id),
"description": self.display_name[:13],
"amount": {
# Charge is in BRL cents -> Multiply by 100
"value": int(self.amount * 100),
"currency": "BRL",
},
"payment_method": {
"soft_descriptor": self.acquirer_id.company_id.name,
"type": "CREDIT_CARD",
"installments": 1,
"capture": False,
"card": {
"encrypted": self.payment_token_id.pagseguro_card_token,
},
},
}
],
}

return CHARGE_PARAMS

def log_transaction(self, reference, message):
Expand All @@ -257,9 +309,8 @@ def log_transaction(self, reference, message):
def pprint_filtered_response(response):
# Returns response removing payment's sensitive information
output_response = response.copy()
output_response.pop("charges", None)
output_response.pop("links", None)
output_response.pop("metadata", None)
output_response.pop("notification_urls", None)
output_response.pop("payment_method", None)

return pprint.pformat(output_response)